From 20dbac018a51b7feaef2f96865a69b5f6e4f47fa Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 2 Apr 2025 13:15:25 +0200 Subject: [PATCH] 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() {