From 972a6fe0a074cc1e1e9e6f4502cc35b995933ac8 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 1 Apr 2025 16:27:14 +0200 Subject: [PATCH 1/2] fix(app): send ipc win-state-changed on paint --- src/stremio_app/app.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/stremio_app/app.rs b/src/stremio_app/app.rs index a9b3576..f98b0c9 100644 --- a/src/stremio_app/app.rs +++ b/src/stremio_app/app.rs @@ -46,10 +46,22 @@ pub struct MainWindow { #[nwg_resource(source_embed: Some(&data.embed), source_embed_str: Some("MAINICON"))] pub window_icon: nwg::Icon, #[nwg_control(icon: Some(&data.window_icon), title: APP_NAME, flags: "MAIN_WINDOW")] - #[nwg_events( OnWindowClose: [Self::on_quit(SELF, EVT_DATA)], OnInit: [Self::on_init], OnPaint: [Self::on_paint], OnMinMaxInfo: [Self::on_min_max(SELF, EVT_DATA)], OnWindowMinimize: [Self::transmit_window_state_change], OnWindowMaximize: [Self::transmit_window_state_change] )] + #[nwg_events( + OnWindowClose: [Self::on_quit(SELF, EVT_DATA)], + OnInit: [Self::on_init], + OnPaint: [Self::on_paint], + OnMinMaxInfo: [Self::on_min_max(SELF, EVT_DATA)], + OnWindowMinimize: [Self::transmit_window_state_change], + OnWindowMaximize: [Self::transmit_window_state_change], + )] pub window: nwg::Window, #[nwg_partial(parent: window)] - #[nwg_events((tray, MousePressLeftUp): [Self::on_show], (tray_exit, OnMenuItemSelected): [nwg::stop_thread_dispatch()], (tray_show_hide, OnMenuItemSelected): [Self::on_show_hide], (tray_topmost, OnMenuItemSelected): [Self::on_toggle_topmost]) ] + #[nwg_events( + (tray, MousePressLeftUp): [Self::on_show], + (tray_exit, OnMenuItemSelected): [nwg::stop_thread_dispatch()], + (tray_show_hide, OnMenuItemSelected): [Self::on_show_hide], + (tray_topmost, OnMenuItemSelected): [Self::on_toggle_topmost], + )] pub tray: SystemTray, #[nwg_partial(parent: window)] pub splash_screen: SplashImage, @@ -341,6 +353,7 @@ impl MainWindow { if !self.splash_screen.visible() { self.webview.fit_to_window(self.window.handle.hwnd()); } + self.transmit_window_state_change(); } fn on_toggle_fullscreen_notice(&self) { if let Some(hwnd) = self.window.handle.hwnd() { From 20dbac018a51b7feaef2f96865a69b5f6e4f47fa Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 2 Apr 2025 13:15:25 +0200 Subject: [PATCH 2/2] refactor(app): use WM_SETFOCUS event instead to send ipc win-state-change --- src/stremio_app/app.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/stremio_app/app.rs b/src/stremio_app/app.rs index f98b0c9..b9019a1 100644 --- a/src/stremio_app/app.rs +++ b/src/stremio_app/app.rs @@ -13,7 +13,10 @@ use std::{ thread, time, }; use url::Url; -use winapi::um::{winbase::CREATE_BREAKAWAY_FROM_JOB, winuser::WS_EX_TOPMOST}; +use winapi::um::{ + winbase::CREATE_BREAKAWAY_FROM_JOB, + winuser::{WM_SETFOCUS, WS_EX_TOPMOST}, +}; use crate::stremio_app::{ constants::{APP_NAME, UPDATE_ENDPOINT, UPDATE_INTERVAL, WINDOW_MIN_HEIGHT, WINDOW_MIN_WIDTH}, @@ -135,6 +138,8 @@ impl MainWindow { self.window.set_visible(!self.start_hidden); self.tray.tray_show_hide.set_checked(!self.start_hidden); + self.handle_on_win_focus(); + let player_channel = self.player.channel.borrow(); let (player_tx, player_rx) = player_channel .as_ref() @@ -345,6 +350,32 @@ impl MainWindow { } // recv }); // thread } + fn handle_on_win_focus(&self) { + if let (Some(hwnd), Ok(web_channel), Ok(style)) = ( + self.window.handle.hwnd(), + self.webview.channel.try_borrow(), + self.saved_window_style.try_borrow(), + ) { + let state = style.clone().get_window_state(hwnd); + drop(style); + let (web_tx, _) = web_channel + .as_ref() + .expect("Cannont obtain communication channel for the Web UI"); + let web_tx_app = web_tx.clone(); + + let handler_id = 0x10001; + let handle = self.window.handle; + nwg::bind_raw_event_handler(&handle, handler_id, move |_hwnd, msg, _w, _l| { + if msg == WM_SETFOCUS { + web_tx_app.send(RPCResponse::state_change(state)).ok(); + } + None + }) + .ok(); + } else { + eprintln!("Cannot obtain window handle or communication channel"); + } + } fn on_min_max(&self, data: &nwg::EventData) { let data = data.on_min_max(); data.set_min_size(WINDOW_MIN_WIDTH, WINDOW_MIN_HEIGHT); @@ -353,7 +384,6 @@ impl MainWindow { if !self.splash_screen.visible() { self.webview.fit_to_window(self.window.handle.hwnd()); } - self.transmit_window_state_change(); } fn on_toggle_fullscreen_notice(&self) { if let Some(hwnd) = self.window.handle.hwnd() {