From b02711e86d43993afda158529661cbe1bb10d0f4 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Mon, 6 Oct 2025 15:00:38 -0700 Subject: [PATCH] fix(wm): improve video fullscreen handling This commit improves browser video fullscreen handling with borders. We are now emitting an event for AppKitWorkspaceNotification::NSWorkspaceActiveSpaceDidChangeNotification, even though it doesn't have a pid, by passing a dummy 0 pid. This is really stupid and I shouldn't do this. This lets us act with more granularity in process_event to destroy or redraw borders when entering or exiting browser video fullscreen. In addition, the reaper has been updated to not reap fullscreen windows so that they can be reintegrated into the layout without issue when fullscreen is exited. --- komorebi/src/notification_center_listener.rs | 16 +- komorebi/src/process_event.rs | 298 ++++++++++--------- komorebi/src/reaper.rs | 13 + komorebi/src/window_manager_event.rs | 7 +- 4 files changed, 187 insertions(+), 147 deletions(-) diff --git a/komorebi/src/notification_center_listener.rs b/komorebi/src/notification_center_listener.rs index 7f71b1c6..6ffbb4b0 100644 --- a/komorebi/src/notification_center_listener.rs +++ b/komorebi/src/notification_center_listener.rs @@ -60,10 +60,18 @@ define_class! { match process_id { None => { - tracing::debug!( - "notification: {}, skipping as there is no associated process id", - notif.name() - ); + if let Ok(notification) = + AppKitWorkspaceNotification::from_str(¬if.name().to_string()) { + if matches!(notification, AppKitWorkspaceNotification::NSWorkspaceActiveSpaceDidChangeNotification) { + // TODO: this is really, really stupid + window_manager_event_listener::send_notification(WindowManagerEvent::SpaceChange(SystemNotification::AppKitWorkspace(notification), 0)); + } + } else { + tracing::debug!( + "notification: {}, skipping as there is no associated process id", + notif.name() + ); + } } Some(process_id) => { tracing::debug!("notification: {}, process: {process_id}", notif.name()); diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index 0f2efb46..60c7b6d0 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -71,6 +71,19 @@ impl WindowManager { return Ok(()); } + if matches!(event, WindowManagerEvent::SpaceChange(_, _)) + && let Some(space_id) = &self.space_id + && let Some(current_space_id) = current_space_id() + { + if *space_id == current_space_id { + border_manager::send_notification(None, None); + } else { + border_manager::destroy_all_borders()?; + } + + return Ok(()); + } + if let Some(space_id) = &self.space_id && let Some(current_space_id) = current_space_id() && *space_id != current_space_id @@ -164,7 +177,7 @@ impl WindowManager { let mut tabbed_window = false; if let Some(window_id) = application.main_window_id() - && matches!( + && matches!( notification, SystemNotification::Accessibility( AccessibilityNotification::AXMainWindowChanged @@ -172,186 +185,186 @@ impl WindowManager { AppKitWorkspaceNotification::NSWorkspaceDidActivateApplicationNotification ) ) && let Some(element) = application.main_window() - { - let workspace = self.focused_workspace_mut()?; + { + let workspace = self.focused_workspace_mut()?; - let tabbed_applications = TABBED_APPLICATIONS.lock(); - if tabbed_applications.contains(&application_name) { - let mut first_tab_destroyed = false; + let tabbed_applications = TABBED_APPLICATIONS.lock(); + if tabbed_applications.contains(&application_name) { + let mut first_tab_destroyed = false; - for window in workspace.visible_windows() { - if let Some(window) = window - && window.application.name().unwrap_or_default() - == application_name - { - let tab_rect = MacosApi::window_rect(&element)?; - let main_rect = match MacosApi::window_rect(&window.element) { - Ok(rect) => rect, - Err(AccessibilityError::Api( - AccessibilityApiError::InvalidUIElement, - )) => { - // this means we have closed the 1st tab, so we need this window object to be reaped - // and for the new 1st tab to be the key element of the window struct - if let Some(event) = - WindowManagerEvent::from_system_notification( - SystemNotification::Manual( - ManualNotification::ShowOnFocusChangeFirstTabDestroyed, - ), - event.process_id(), - Some(window_id), - ) - { - window_manager_event_listener::send_notification( - event, - ); - } + for window in workspace.visible_windows() { + if let Some(window) = window + && window.application.name().unwrap_or_default() + == application_name + { + let tab_rect = MacosApi::window_rect(&element)?; + let main_rect = match MacosApi::window_rect(&window.element) { + Ok(rect) => rect, + Err(AccessibilityError::Api( + AccessibilityApiError::InvalidUIElement, + )) => { + // this means we have closed the 1st tab, so we need this window object to be reaped + // and for the new 1st tab to be the key element of the window struct + if let Some(event) = + WindowManagerEvent::from_system_notification( + SystemNotification::Manual( + ManualNotification::ShowOnFocusChangeFirstTabDestroyed, + ), + event.process_id(), + Some(window_id), + ) + { + window_manager_event_listener::send_notification( + event, + ); + } - first_tab_destroyed = true; + first_tab_destroyed = true; - tracing::debug!( + tracing::debug!( "first tab of a native tabbed app was destroyed; reaping window and sending a new show event" ); - tab_rect - } - Err(error) => return Err(error.into()), - }; + tab_rect + } + Err(error) => return Err(error.into()), + }; - if tab_rect == main_rect { - tracing::debug!("ignoring focus change for tabbed window"); - tabbed_window = true; + if tab_rect == main_rect { + tracing::debug!("ignoring focus change for tabbed window"); + tabbed_window = true; + } } } + + if first_tab_destroyed { + self.reap_invalid_windows_for_application(process_id)?; + } } - if first_tab_destroyed { - self.reap_invalid_windows_for_application(process_id)?; - } - } + drop(tabbed_applications); - drop(tabbed_applications); + if !tabbed_window { + let is_known = self.known_window_ids.get(&window_id).cloned(); + let mut is_on_current_workspace = false; - if !tabbed_window { - let is_known = self.known_window_ids.get(&window_id).cloned(); - let mut is_on_current_workspace = false; - - // TODO: figure out if this applies on macOS too - // don't want to trigger the full workspace updates when there are no managed - // containers - this makes floating windows on empty workspaces go into very - // annoying focus change loops which prevents users from interacting with them - if !matches!( + // TODO: figure out if this applies on macOS too + // don't want to trigger the full workspace updates when there are no managed + // containers - this makes floating windows on empty workspaces go into very + // annoying focus change loops which prevents users from interacting with them + if !matches!( self.focused_workspace()?.layout, Layout::Default(DefaultLayout::Scrolling) ) && !self.focused_workspace()?.containers().is_empty() { - self.update_focused_workspace(self.mouse_follows_focus, false)?; - } + self.update_focused_workspace(self.mouse_follows_focus, false)?; + } - let workspace = self.focused_workspace_mut()?; - if workspace.contains_window(window_id) { - is_on_current_workspace = true; - } + let workspace = self.focused_workspace_mut()?; + if workspace.contains_window(window_id) { + is_on_current_workspace = true; + } - let floating_window_idx = workspace - .floating_windows() - .iter() - .position(|w| w.id == window_id); + let floating_window_idx = workspace + .floating_windows() + .iter() + .position(|w| w.id == window_id); - match floating_window_idx { - None => { - // if let Some(w) = workspace.maximized_window() { - // if w.hwnd == window_id { - // return Ok(()); - // } - // } + match floating_window_idx { + None => { + // if let Some(w) = workspace.maximized_window() { + // if w.hwnd == window_id { + // return Ok(()); + // } + // } - if let Some(monocle) = &workspace.monocle_container { - if let Some(window) = monocle.focused_window() { - window.focus(false)?; - } - } else if !is_on_current_workspace && is_known.is_none() { - // thanks, I hate it - need to do this so that we don't mess up - // the workspace rules, but also don't miss events from dumb - // apps like notes, mail etc. - let mut has_matching_workspace_rule = false; - let workspace_rules = WORKSPACE_MATCHING_RULES.lock(); - for rule in &*workspace_rules { - match &rule.matching_rule { - MatchingRule::Simple(r) => { - if r.id.trim_end_matches(".exe") == application_name - { - has_matching_workspace_rule = true; - } - } - // TODO: this is pretty coarse - MatchingRule::Composite(rules) => { - for r in rules { - if r.id.trim_end_matches(".exe") - == application_name + if let Some(monocle) = &workspace.monocle_container { + if let Some(window) = monocle.focused_window() { + window.focus(false)?; + } + } else if !is_on_current_workspace && is_known.is_none() { + // thanks, I hate it - need to do this so that we don't mess up + // the workspace rules, but also don't miss events from dumb + // apps like notes, mail etc. + let mut has_matching_workspace_rule = false; + let workspace_rules = WORKSPACE_MATCHING_RULES.lock(); + for rule in &*workspace_rules { + match &rule.matching_rule { + MatchingRule::Simple(r) => { + if r.id.trim_end_matches(".exe") == application_name { has_matching_workspace_rule = true; } } + // TODO: this is pretty coarse + MatchingRule::Composite(rules) => { + for r in rules { + if r.id.trim_end_matches(".exe") + == application_name + { + has_matching_workspace_rule = true; + } + } + } } } - } - if !has_matching_workspace_rule - && workspace.focus_container_by_window(window_id).is_err() - { - // if this fails, the app was probably open but windowless when komorebi - // launched, so the window hasn't been registered - we should treat it - // as a "Show" event - if let Some(event) = - WindowManagerEvent::from_system_notification( - SystemNotification::Manual( - ManualNotification::ShowOnFocusChangeWindowlessAppRestored, - ), - event.process_id(), - Some(window_id), - ) + if !has_matching_workspace_rule + && workspace.focus_container_by_window(window_id).is_err() { - window_manager_event_listener::send_notification( - event, - ); - should_switch_workspace_layer_to_tiling = false; + // if this fails, the app was probably open but windowless when komorebi + // launched, so the window hasn't been registered - we should treat it + // as a "Show" event + if let Some(event) = + WindowManagerEvent::from_system_notification( + SystemNotification::Manual( + ManualNotification::ShowOnFocusChangeWindowlessAppRestored, + ), + event.process_id(), + Some(window_id), + ) + { + window_manager_event_listener::send_notification( + event, + ); + should_switch_workspace_layer_to_tiling = false; + } } + } else if is_on_current_workspace { + workspace.focus_container_by_window(window_id)?; } - } else if is_on_current_workspace { - workspace.focus_container_by_window(window_id)?; - } - if should_switch_workspace_layer_to_tiling { - workspace.layer = WorkspaceLayer::Tiling; - } + if should_switch_workspace_layer_to_tiling { + workspace.layer = WorkspaceLayer::Tiling; + } - if matches!(self.focused_workspace()?.layout, Layout::Default(DefaultLayout::Scrolling)) - && !self.focused_workspace()?.containers().is_empty() { - self.update_focused_workspace(self.mouse_follows_focus, false)?; + if matches!(self.focused_workspace()?.layout, Layout::Default(DefaultLayout::Scrolling)) + && !self.focused_workspace()?.containers().is_empty() { + self.update_focused_workspace(self.mouse_follows_focus, false)?; + } + } + Some(idx) => { + if let Some(_window) = workspace.floating_windows().get(idx) { + workspace.layer = WorkspaceLayer::Floating; + } } } - Some(idx) => { - if let Some(_window) = workspace.floating_windows().get(idx) { - workspace.layer = WorkspaceLayer::Floating; + + // maybe we don't need a separate reconciliator module?? this works pretty well! + if !is_on_current_workspace && let Some((m_idx, w_idx)) = is_known { + self.focus_monitor(m_idx)?; + self.focus_workspace(w_idx)?; + let event = WindowManagerEvent::from_system_notification( + SystemNotification::Manual(ManualNotification::FocusOnCmdTab), + process_id, + Some(window_id), + ); + + if let Some(manual_event) = event { + window_manager_event_listener::send_notification(manual_event); } } } - - // maybe we don't need a separate reconciliator module?? this works pretty well! - if !is_on_current_workspace && let Some((m_idx, w_idx)) = is_known { - self.focus_monitor(m_idx)?; - self.focus_workspace(w_idx)?; - let event = WindowManagerEvent::from_system_notification( - SystemNotification::Manual(ManualNotification::FocusOnCmdTab), - process_id, - Some(window_id) - ); - - if let Some(manual_event) = event { - window_manager_event_listener::send_notification(manual_event); - } - } } - } if matches!( notification, @@ -929,6 +942,7 @@ impl WindowManager { } } } + WindowManagerEvent::SpaceChange(_, _) => {} } self.update_known_window_ids(); diff --git a/komorebi/src/reaper.rs b/komorebi/src/reaper.rs index c8301331..8f0f9d96 100644 --- a/komorebi/src/reaper.rs +++ b/komorebi/src/reaper.rs @@ -1,10 +1,12 @@ use crate::TABBED_APPLICATIONS; +use crate::accessibility::AccessibilityApi; use crate::accessibility::error::AccessibilityError; use crate::border_manager; use crate::window::Window; use crate::window_manager::WindowManager; use crossbeam_channel::Receiver; use crossbeam_channel::Sender; +use objc2_core_foundation::CFBoolean; use parking_lot::Mutex; use std::sync::Arc; use std::sync::OnceLock; @@ -115,6 +117,17 @@ pub fn notify_on_error( should_reap = false; } + if let Some(is_fullscreen) = + AccessibilityApi::copy_attribute_value::(&window.element, "AXFullScreen") + && is_fullscreen.as_bool() + { + tracing::debug!( + "skipping reap for fullscreen window {} during position update failure", + window.id + ); + should_reap = false; + } + if should_reap { send_notification(ReaperNotification::InvalidWindow(window.id)); } diff --git a/komorebi/src/window_manager_event.rs b/komorebi/src/window_manager_event.rs index eb00cbfa..75fb60f1 100644 --- a/komorebi/src/window_manager_event.rs +++ b/komorebi/src/window_manager_event.rs @@ -42,6 +42,7 @@ pub enum WindowManagerEvent { MoveEnd(SystemNotification, i32, u32), ResizeStart(SystemNotification, i32, u32), ResizeEnd(SystemNotification, i32, u32), + SpaceChange(SystemNotification, i32), } impl WindowManagerEvent { @@ -141,6 +142,7 @@ impl WindowManagerEvent { | WindowManagerEvent::MoveEnd(_, process_id, _) | WindowManagerEvent::ResizeStart(_, process_id, _) | WindowManagerEvent::ResizeEnd(_, process_id, _) + | WindowManagerEvent::SpaceChange(_, process_id) | WindowManagerEvent::Restore(_, process_id, _) => *process_id, } } @@ -157,6 +159,7 @@ impl WindowManagerEvent { | WindowManagerEvent::ResizeStart(n, _, _) | WindowManagerEvent::ResizeEnd(n, _, _) | WindowManagerEvent::Unmanage(n, _, _) + | WindowManagerEvent::SpaceChange(n, _) | WindowManagerEvent::Restore(n, _, _) => match n { SystemNotification::Accessibility(a) => a.to_string(), SystemNotification::AppKitWorkspace(a) => a.to_string(), @@ -168,7 +171,9 @@ impl WindowManagerEvent { pub fn window_id(&self) -> Option { match self { WindowManagerEvent::FocusChange(_, _, window_id) => *window_id, - WindowManagerEvent::Show(_, _) | WindowManagerEvent::Destroy(_, _) => None, + WindowManagerEvent::Show(_, _) + | WindowManagerEvent::Destroy(_, _) + | WindowManagerEvent::SpaceChange(_, _) => None, WindowManagerEvent::Minimize(_, _, window_id) | WindowManagerEvent::Manage(_, _, window_id) | WindowManagerEvent::Unmanage(_, _, window_id)