fix: attach Windows media controls to the main window

souvlaki panics on Windows without an HWND, which crashed the app at
startup. Wait for the window to open and connect with its raw handle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
yuriy.pandClaude Opus 5.5 committed 2026-10-18 01:06:41 +06:00
1 parent 118fa3cf11
commit c496275101
2 files changed
+34 -15

No files matched your search

+11
View File
@@ -138,6 +138,9 @@ pub enum Message {
ClearCoverCache,
CoverCacheCleared(Result<(), String>),
Media(crate::media::Command),
/// A window opened; Windows media controls need its handle.
WindowOpened(iced::window::Id),
MediaWindow(u64),
LoadLikedIds,
LikedIdsLoaded(Result<LikedIds, Error>),
LikeTrack(Track, bool),
@@ -264,6 +267,9 @@ impl App {
let audio = player::engine::subscription().map(|e| Message::Player(player::Msg::Engine(e)));
let keys = crate::media::subscription().map(Message::Media);
let mut subs = vec![appearance, audio, keys];
if cfg!(windows) {
subs.push(iced::window::open_events().map(Message::WindowOpened));
}
if let Session::SignedOut(sign_in) = &self.session {
subs.push(sign_in.subscription().map(Message::SignIn));
}
@@ -521,6 +527,11 @@ impl App {
self.toast(trf("“{}” plays next", &[&track.full_title()]));
return self.update(Message::Player(player::Msg::PlayNext(track)));
}
Message::WindowOpened(id) => return iced::window::raw_id::<Message>(id).map(Message::MediaWindow),
Message::MediaWindow(raw_id) => {
self.media.attach_window(raw_id);
self.media.update(self.player.track(), self.player.status(), self.player.position(), true);
}
Message::Media(command) => {
use crate::media::Command as C;
let status = self.player.status();
+23 -15
View File
@@ -71,21 +71,16 @@ impl Media {
{
*inbox = Some(rx);
}
let controls = MediaControls::new(PlatformConfig { display_name: "Musyx", dbus_name: "musyx", hwnd: None })
.and_then(|mut c| {
c.attach(|event| {
if let Some(command) = Command::from_event(event) {
send(command);
}
})?;
Ok(c)
});
match controls {
Ok(c) => Self { controls: Some(c), shown: None },
Err(e) => {
eprintln!("media controls unavailable: {e:?}");
Self { controls: None, shown: None }
}
// Windows ties the controls to a window, so they wait for `attach_window`.
let controls = if cfg!(windows) { None } else { connect(None) };
Self { controls, shown: None }
}
/// Connects the controls to the main window (Windows needs its HWND, from `window::raw_id`).
pub fn attach_window(&mut self, raw_id: u64) {
if cfg!(windows) && self.controls.is_none() {
self.controls = connect(Some(raw_id as *mut std::ffi::c_void));
self.shown = None;
}
}
@@ -125,6 +120,19 @@ impl Media {
}
}
fn connect(hwnd: Option<*mut std::ffi::c_void>) -> Option<MediaControls> {
let controls = MediaControls::new(PlatformConfig { display_name: "Musyx", dbus_name: "musyx", hwnd })
.and_then(|mut c| {
c.attach(|event| {
if let Some(command) = Command::from_event(event) {
send(command);
}
})?;
Ok(c)
});
controls.map_err(|e| eprintln!("media controls unavailable: {e:?}")).ok()
}
pub fn subscription() -> Subscription<Command> {
Subscription::run(commands)
}