From 2fe4cf56581821b0f9b7f77e11d9b1e6e3e9ba1b Mon Sep 17 00:00:00 2001 From: apoint123 <108002475+apoint123@users.noreply.github.com> Date: Wed, 11 Feb 2026 16:32:45 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=20=E4=B8=BA=20macOS=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20toggle=20=E5=91=BD=E4=BB=A4=E5=A4=84?= =?UTF-8?q?=E7=90=86=E4=BB=A5=E5=B0=9D=E8=AF=95=E8=A7=A3=E5=86=B3=E5=9C=A8?= =?UTF-8?q?=20macOS=2026=20=E4=B8=8A=E7=9A=84=E6=92=AD=E6=94=BE=E6=9A=82?= =?UTF-8?q?=E5=81=9C=E6=8C=89=E9=92=AE=E6=97=A0=E6=95=88=E7=9A=84=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/sys_media/macos.rs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/native/external-media-integration/src/sys_media/macos.rs b/native/external-media-integration/src/sys_media/macos.rs index 301f1592f..de641cb2d 100644 --- a/native/external-media-integration/src/sys_media/macos.rs +++ b/native/external-media-integration/src/sys_media/macos.rs @@ -94,6 +94,40 @@ impl MacosImpl { } } + fn add_toggle_handler(&self) { + let command = unsafe { self.cmd_ctr.togglePlayPauseCommand() }; + let handler_arc = self.event_handler.clone(); + let info_ctr = self.np_info_ctr.clone(); + + let block = RcBlock::new(move |_| -> MPRemoteCommandHandlerStatus { + let current_state = unsafe { info_ctr.playbackState() }; + + let event_type = if current_state == MPNowPlayingPlaybackState::Playing { + SystemMediaEventType::Pause + } else { + SystemMediaEventType::Play + }; + + if let Ok(guard) = handler_arc.lock() + && let Some(tsfn) = guard.as_ref() + { + debug!(?event_type, "MPRemoteCommand Toggle 触发"); + let evt = SystemMediaEvent::new(event_type); + tsfn.call( + evt, + napi::threadsafe_function::ThreadsafeFunctionCallMode::NonBlocking, + ); + } + MPRemoteCommandHandlerStatus::Success + }); + + unsafe { + command.setEnabled(true); + let token = command.addTargetWithHandler(&block); + self.store_token(&command, token); + } + } + fn add_seek_handler(&self) { let command = unsafe { self.cmd_ctr.changePlaybackPositionCommand() }; let handler_arc = self.event_handler.clone(); @@ -217,6 +251,9 @@ impl MacosImpl { // 暂停 self.add_command_handler(&self.cmd_ctr.pauseCommand(), SystemMediaEventType::Pause); + // 播放暂停 + self.add_toggle_handler(); + // 上一首 self.add_command_handler( &self.cmd_ctr.previousTrackCommand(),