From b7ebd3fe633eab74b490c6e865798c5fbb9f6801 Mon Sep 17 00:00:00 2001 From: alex-ds13 <145657253+alex-ds13@users.noreply.github.com> Date: Sat, 8 Feb 2025 03:40:16 +0000 Subject: [PATCH] fix(bar): check monitor connection on all notifications --- komorebi-bar/src/bar.rs | 80 ++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/komorebi-bar/src/bar.rs b/komorebi-bar/src/bar.rs index 1ed0d365..8ff68d31 100644 --- a/komorebi-bar/src/bar.rs +++ b/komorebi-bar/src/bar.rs @@ -673,50 +673,50 @@ impl eframe::App for Komobar { } }, Ok(KomorebiEvent::Notification(notification)) => { + let state = ¬ification.state; + let usr_monitor_index = match &self.config.monitor { + MonitorConfigOrIndex::MonitorConfig(monitor_config) => monitor_config.index, + MonitorConfigOrIndex::Index(idx) => *idx, + }; + let monitor_index = state + .monitor_usr_idx_map + .get(&usr_monitor_index) + .map_or(usr_monitor_index, |i| *i); + self.monitor_index = monitor_index; + + if self.monitor_index >= state.monitors.elements().len() { + // Monitor for this bar got disconnected lets disable the bar until it + // reconnects + self.disabled = true; + tracing::warn!( + "This bar's monitor got disconnected. The bar will be disabled until it reconnects..." + ); + return; + } else { + if self.disabled { + tracing::info!( + "This bar's monitor reconnected. The bar will be enabled again!" + ); + + // Restore the bar in case it has been minimized when the monitor + // disconnected + if let Some(hwnd) = self.hwnd { + let window = komorebi_client::Window::from(hwnd); + if window.is_miminized() { + komorebi_client::WindowsApi::restore_window(hwnd); + } + } + + // Reposition the Bar + self.position_bar(); + } + self.disabled = false; + } + let should_apply_config = if matches!( notification.event, NotificationEvent::Monitor(MonitorNotification::DisplayConnectionChange) ) { - let state = ¬ification.state; - let usr_monitor_index = match &self.config.monitor { - MonitorConfigOrIndex::MonitorConfig(monitor_config) => monitor_config.index, - MonitorConfigOrIndex::Index(idx) => *idx, - }; - let monitor_index = state - .monitor_usr_idx_map - .get(&usr_monitor_index) - .map_or(usr_monitor_index, |i| *i); - self.monitor_index = monitor_index; - - if self.monitor_index >= state.monitors.elements().len() { - // Monitor for this bar got disconnected lets disable the bar until it - // reconnects - self.disabled = true; - tracing::warn!( - "This bar's monitor got disconnected. The bar will be disabled until it reconnects..." - ); - return; - } else { - if self.disabled { - tracing::info!( - "This bar's monitor reconnected. The bar will be enabled again!" - ); - - // Restore the bar in case it has been minimized when the monitor - // disconnected - if let Some(hwnd) = self.hwnd { - let window = komorebi_client::Window::from(hwnd); - if window.is_miminized() { - komorebi_client::WindowsApi::restore_window(hwnd); - } - } - - // Reposition the Bar - self.position_bar(); - } - self.disabled = false; - } - // Store the monitor coordinates in case they've changed MONITOR_RIGHT.store( state.monitors.elements()[self.monitor_index].size().right,