From c496275101de066ef0a14a3712fb3cab39125641 Mon Sep 17 00:00:00 2001 From: Yuriy Panov Date: Sun, 18 Oct 2026 01:06:41 +0600 Subject: [PATCH] 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 --- crates/musyx/src/app.rs | 11 +++++++++++ crates/musyx/src/media.rs | 38 +++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/crates/musyx/src/app.rs b/crates/musyx/src/app.rs index 2761837..8d70f3d 100644 --- a/crates/musyx/src/app.rs +++ b/crates/musyx/src/app.rs @@ -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), 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::(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(); diff --git a/crates/musyx/src/media.rs b/crates/musyx/src/media.rs index 605f700..4251f5e 100644 --- a/crates/musyx/src/media.rs +++ b/crates/musyx/src/media.rs @@ -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 { + 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 { Subscription::run(commands) }