From 985ba5d2c7c554a499d7303cc184886e7ef803cd Mon Sep 17 00:00:00 2001 From: edde746 <86283021+edde746@users.noreply.github.com> Date: Thu, 5 Mar 2026 22:41:37 +0100 Subject: [PATCH] fix: toggle PiP button closes PiP when already active --- lib/services/pip_service.dart | 5 +++++ lib/services/video_pip_manager.dart | 6 ++++++ macos/Runner/MpvPlayer/MpvPlayerPlugin.swift | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/lib/services/pip_service.dart b/lib/services/pip_service.dart index fa06b23f..125d3faf 100644 --- a/lib/services/pip_service.dart +++ b/lib/services/pip_service.dart @@ -53,6 +53,11 @@ class PipService { }); } + static Future exit() async { + if (!_isAvailable) return; + await _channel.invokeMethod('exit'); + } + static Future<(bool success, String? error)> enter({int? width, int? height}) async { if (!_isAvailable) return (false, null); final result = await _channel.invokeMethod('enter', {'width': width, 'height': height}); diff --git a/lib/services/video_pip_manager.dart b/lib/services/video_pip_manager.dart index 5b5f3122..a38532e1 100644 --- a/lib/services/video_pip_manager.dart +++ b/lib/services/video_pip_manager.dart @@ -61,6 +61,12 @@ class VideoPIPManager { final supported = await PipService.isSupported(); if (!supported) return (false, 'PiP not supported on this device'); + // If PiP is already active, exit it + if (isPipActive.value) { + await PipService.exit(); + return (true, null); + } + // Reset video filter to contain mode BEFORE entering PiP (Android only — // iOS switches VO entirely so the filter is irrelevant) if (Platform.isAndroid) { diff --git a/macos/Runner/MpvPlayer/MpvPlayerPlugin.swift b/macos/Runner/MpvPlayer/MpvPlayerPlugin.swift index 2abc42bc..5a8ff2e5 100644 --- a/macos/Runner/MpvPlayer/MpvPlayerPlugin.swift +++ b/macos/Runner/MpvPlayer/MpvPlayerPlugin.swift @@ -117,6 +117,9 @@ class MpvPlayerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, MpvPlayerD result(MpvPipController.isSupported) case "enter": enterPip(manual: true, result: result) + case "exit": + pipController?.stopPip() + result(nil) case "setAutoPipReady": if let args = call.arguments as? [String: Any], let ready = args["ready"] as? Bool { autoPipEnabled = ready @@ -171,6 +174,7 @@ class MpvPlayerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, MpvPlayerD enteredPipViaAuto = !manual playerCore.isPipActive = true + pip.startPip(metalLayer: metalLayer, window: window, aspectRatio: aspectRatio) pipChannel?.invokeMethod("onPipChanged", arguments: true) result?(["success": true])