diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index 95c87194..1f573bfe 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -266,8 +266,8 @@ pub enum SocketMessage { CrossMonitorMoveBehaviour(MoveBehaviour), UnmanagedWindowOperationBehaviour(OperationBehaviour), // Current Workspace Commands - // ManageFocusedWindow, - // UnmanageFocusedWindow, + ManageFocusedWindow, + UnmanageFocusedWindow, AdjustContainerPadding(Sizing, i32), AdjustWorkspacePadding(Sizing, i32), ChangeLayout(DefaultLayout), diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index 830ea9e7..90a89b70 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -124,6 +124,7 @@ lazy_static! { ])); static ref DO_NOT_OBSERVE_APPLICATIONS: Arc>> = Arc::new(Mutex::new(vec!["Spotlight".to_string(),])); + static ref UNMANAGED_WINDOW_IDS: Arc>> = Arc::new(Mutex::new(vec![])); } shadow_rs::shadow!(build); diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index acfa41b1..bc8e3ab0 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -250,6 +250,12 @@ impl WindowManager { MacosApi::foreground_window().ok_or_eyre("there is no foreground window")?; AdhocWindow::close(&foreground_window)?; } + SocketMessage::ManageFocusedWindow => { + self.manage_focused_window()?; + } + SocketMessage::UnmanageFocusedWindow => { + self.unmanage_focused_window()?; + } SocketMessage::LockMonitorWorkspaceContainer( monitor_idx, workspace_idx, diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index a010ced3..bed3e37c 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -3,6 +3,7 @@ use crate::Notification; use crate::NotificationEvent; use crate::REGEX_IDENTIFIERS; use crate::TABBED_APPLICATIONS; +use crate::UNMANAGED_WINDOW_IDS; use crate::WORKSPACE_MATCHING_RULES; use crate::accessibility::AccessibilityApi; use crate::accessibility::error::AccessibilityApiError; @@ -69,9 +70,14 @@ impl WindowManager { if let Some(window_element) = application.main_window() && let Ok(window) = Window::new(window_element, application.clone()) { + let window_id = window.id; let print_window = window.clone(); should_manage = window.should_manage(Some(event))?; + if UNMANAGED_WINDOW_IDS.lock().contains(&window_id) { + should_manage = false; + } + if !should_manage { tracing::debug!( "ignoring event as window should not be managed: {print_window}" @@ -306,7 +312,8 @@ impl WindowManager { } } // TODO: update this to work with floating applications / rules - WindowManagerEvent::Show(_, process_id) => { + WindowManagerEvent::Show(_, process_id) + | WindowManagerEvent::Manage(_, process_id, _) => { let focused_monitor_idx = self.focused_monitor_idx(); let focused_workspace_idx = self.focused_workspace_idx_for_monitor_idx(focused_monitor_idx)?; @@ -432,9 +439,8 @@ impl WindowManager { } if behaviour.float_override - || behaviour.floating_layer_override - // TODO: look into if we want to have a Manage command here too - || (should_float /*&& !matches!(event, WindowManagerEvent::Manage(_))*/ ) + || behaviour.floating_layer_override + || (should_float && !matches!(event, WindowManagerEvent::Manage(_, _, _))) { let placement = if behaviour.floating_layer_override { // Floating layer override placement @@ -521,6 +527,34 @@ impl WindowManager { self.reap_invalid_windows_for_application(process_id)?; self.update_focused_workspace(false, false)?; } + WindowManagerEvent::Unmanage(_, _, window_id) => { + let behaviour = self.window_management_behaviour( + self.focused_monitor_idx(), + self.focused_workspace_idx()?, + ); + + let workspace = self.focused_workspace_mut()?; + let mut window = workspace.remove_window(window_id)?; + + // If we unmanaged a window, it shouldn't be immediately hidden behind managed windows + let placement = if behaviour.floating_layer_override { + // Floating layer override placement + behaviour.floating_layer_placement + } else if behaviour.float_override { + // Float override placement + behaviour.float_override_placement + } else { + // Float rule placement + behaviour.float_rule_placement + }; + + window.center(&workspace.globals.work_area, placement.should_resize())?; + + // we only want to add the window ID after the window has been removed + UNMANAGED_WINDOW_IDS.lock().push(window_id); + + self.update_focused_workspace(false, false)?; + } WindowManagerEvent::Minimize(_, _, window_id) => { self.extract_minimized_window(window_id)?; self.update_focused_workspace(false, false)?; diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index cfdc2c95..24693490 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -731,6 +731,20 @@ impl Window { pub struct AdhocWindow; impl AdhocWindow { + pub fn process_id(element: &CFRetained) -> Option { + let mut process_id = 0; + + unsafe { + element.pid(NonNull::from_mut(&mut process_id)); + } + + if process_id != 0 { + Some(process_id) + } else { + None + } + } + pub fn exe(element: &CFRetained) -> Option { let parent = AccessibilityApi::copy_attribute_value::(element, kAXParentAttribute)?; @@ -837,6 +851,13 @@ impl AdhocWindow { Ok(()) } + + pub fn raise(element: &CFRetained) -> Result<(), AccessibilityError> { + let cf_boolean = CFBoolean::new(true); + let value = &**cf_boolean; + AccessibilityApi::set_attribute_cf_value(element, kAXMainAttribute, value)?; + AccessibilityApi::set_attribute_cf_value(element, kAXFocusedAttribute, value) + } } impl From<&CFDictionary> for WindowInfo { diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 1dcba0e0..848abf59 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -3,6 +3,7 @@ use crate::DATA_DIR; use crate::LibraryError; use crate::REGEX_IDENTIFIERS; use crate::SUBSCRIPTION_SOCKETS; +use crate::UNMANAGED_WINDOW_IDS; use crate::WORKSPACE_MATCHING_RULES; use crate::accessibility::AccessibilityApi; use crate::application::Application; @@ -27,9 +28,13 @@ use crate::macos_api::MacosApi; use crate::monitor::Monitor; use crate::ring::Ring; use crate::static_config::StaticConfig; +use crate::window::AdhocWindow; use crate::window::Window; use crate::window::should_act_individual; +use crate::window_manager_event::ManualNotification; +use crate::window_manager_event::SystemNotification; use crate::window_manager_event::WindowManagerEvent; +use crate::window_manager_event_listener; use crate::workspace::Workspace; use crate::workspace::WorkspaceLayer; use color_eyre::eyre; @@ -2635,4 +2640,42 @@ impl WindowManager { } workspace } + + #[tracing::instrument(skip(self))] + pub fn manage_focused_window(&mut self) -> eyre::Result<()> { + let element = MacosApi::foreground_window().ok_or_eyre("there is no foreground window")?; + let window_id = MacosApi::foreground_window_id(); + if let Some(process_id) = AdhocWindow::process_id(&element) + && let Some(event) = WindowManagerEvent::from_system_notification( + SystemNotification::Manual(ManualNotification::Manage), + process_id, + window_id, + ) + { + UNMANAGED_WINDOW_IDS + .lock() + .retain(|id| *id != window_id.unwrap_or_default()); + + window_manager_event_listener::send_notification(event); + } + + Ok(()) + } + + #[tracing::instrument(skip(self))] + pub fn unmanage_focused_window(&mut self) -> eyre::Result<()> { + let element = MacosApi::foreground_window().ok_or_eyre("there is no foreground window")?; + let window_id = MacosApi::foreground_window_id(); + if let Some(process_id) = AdhocWindow::process_id(&element) + && let Some(event) = WindowManagerEvent::from_system_notification( + SystemNotification::Manual(ManualNotification::Unmanage), + process_id, + window_id, + ) + { + window_manager_event_listener::send_notification(event); + } + + Ok(()) + } } diff --git a/komorebi/src/window_manager_event.rs b/komorebi/src/window_manager_event.rs index 0e018465..9c089921 100644 --- a/komorebi/src/window_manager_event.rs +++ b/komorebi/src/window_manager_event.rs @@ -21,6 +21,8 @@ pub enum ManualNotification { ShowOnInputEvent, ShowOnFocusChangeFirstTabDestroyed, ShowOnFocusChangeWindowlessAppRestored, + Manage, + Unmanage, } #[derive(Debug, Copy, Clone, Serialize, Deserialize, Display)] @@ -32,6 +34,8 @@ pub enum WindowManagerEvent { Destroy(SystemNotification, i32), Minimize(SystemNotification, i32, u32), Restore(SystemNotification, i32, u32), + Manage(SystemNotification, i32, u32), + Unmanage(SystemNotification, i32, u32), } impl WindowManagerEvent { @@ -79,6 +83,10 @@ impl WindowManagerEvent { AccessibilityNotification::AXWindowDeminiaturized, ) => window_id .map(|window_id| WindowManagerEvent::Restore(notification, process_id, window_id)), + SystemNotification::Manual(ManualNotification::Manage) => window_id + .map(|window_id| WindowManagerEvent::Manage(notification, process_id, window_id)), + SystemNotification::Manual(ManualNotification::Unmanage) => window_id + .map(|window_id| WindowManagerEvent::Unmanage(notification, process_id, window_id)), // kAXWindowMovedNotification => {} // kAXWindowResizedNotification => {} // kAXTitleChangedNotification => {} @@ -93,6 +101,8 @@ impl WindowManagerEvent { | WindowManagerEvent::Show(_, process_id) | WindowManagerEvent::Destroy(_, process_id) | WindowManagerEvent::Minimize(_, process_id, _) + | WindowManagerEvent::Manage(_, process_id, _) + | WindowManagerEvent::Unmanage(_, process_id, _) | WindowManagerEvent::Restore(_, process_id, _) => *process_id, } } @@ -103,6 +113,8 @@ impl WindowManagerEvent { | WindowManagerEvent::Show(n, _) | WindowManagerEvent::Destroy(n, _) | WindowManagerEvent::Minimize(n, _, _) + | WindowManagerEvent::Manage(n, _, _) + | WindowManagerEvent::Unmanage(n, _, _) | WindowManagerEvent::Restore(n, _, _) => match n { SystemNotification::Accessibility(a) => a.to_string(), SystemNotification::AppKitWorkspace(a) => a.to_string(), @@ -110,4 +122,15 @@ impl WindowManagerEvent { }, } } + + pub fn window_id(&self) -> Option { + match self { + WindowManagerEvent::FocusChange(_, _, window_id) => *window_id, + WindowManagerEvent::Show(_, _) | WindowManagerEvent::Destroy(_, _) => None, + WindowManagerEvent::Minimize(_, _, window_id) + | WindowManagerEvent::Manage(_, _, window_id) + | WindowManagerEvent::Unmanage(_, _, window_id) + | WindowManagerEvent::Restore(_, _, window_id) => Some(*window_id), + } + } } diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 47842915..0eccc56f 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -929,10 +929,10 @@ enum SubCommand { ToggleLock, // /// Restore all hidden windows (debugging command) // RestoreWindows, - // /// Force komorebi to manage the focused window - // Manage, - // /// Unmanage a window that was forcibly managed - // Unmanage, + /// Force komorebi to manage the focused window + Manage, + /// Unmanage a window that was forcibly managed + Unmanage, // /// Replace the configuration of a running instance of komorebi from a static configuration file // #[clap(arg_required_else_help = true)] // ReplaceConfiguration(ReplaceConfiguration), @@ -1313,6 +1313,12 @@ fn main() -> eyre::Result<()> { SubCommand::Minimize => { send_message(&SocketMessage::Minimize)?; } + SubCommand::Manage => { + send_message(&SocketMessage::ManageFocusedWindow)?; + } + SubCommand::Unmanage => { + send_message(&SocketMessage::UnmanageFocusedWindow)?; + } SubCommand::Promote => { send_message(&SocketMessage::Promote)?; }