Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions native/external-media-integration/src/sys_media/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Comment thread
apoint123 marked this conversation as resolved.
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,
);
}
Comment thread
apoint123 marked this conversation as resolved.
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();
Expand Down Expand Up @@ -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(),
Expand Down