feat(cli): implement manage and unmanage cmds

In implementing these two I'm diverging from how komorebi for Windows
handles these a little by tracking the window IDs of manually unmanaged
applications in a global vec UNMANAGED_WINDOW_IDS.

This is due to other changes in how Show and Destroy are handled in
komorebi for macOS which means we can't just piggy-back on those
handlers like we used to.
This commit is contained in:
LGUG2Z committed 2025-09-25 12:34:07 -07:00
1 parent 20d6121568
commit c59e882a2d
8 files changed
+144 -10

No files matched your search

+2 -2
View File
@@ -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),
+1
View File
@@ -124,6 +124,7 @@ lazy_static! {
]));
static ref DO_NOT_OBSERVE_APPLICATIONS: Arc<Mutex<Vec<String>>> =
Arc::new(Mutex::new(vec!["Spotlight".to_string(),]));
static ref UNMANAGED_WINDOW_IDS: Arc<Mutex<Vec<u32>>> = Arc::new(Mutex::new(vec![]));
}
shadow_rs::shadow!(build);
+6
View File
@@ -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,
+38 -4
View File
@@ -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)?;
+21
View File
@@ -731,6 +731,20 @@ impl Window {
pub struct AdhocWindow;
impl AdhocWindow {
pub fn process_id(element: &CFRetained<AXUIElement>) -> Option<i32> {
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<AXUIElement>) -> Option<String> {
let parent =
AccessibilityApi::copy_attribute_value::<AXUIElement>(element, kAXParentAttribute)?;
@@ -837,6 +851,13 @@ impl AdhocWindow {
Ok(())
}
pub fn raise(element: &CFRetained<AXUIElement>) -> 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 {
+43
View File
@@ -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(())
}
}
+23
View File
@@ -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<u32> {
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),
}
}
}
+10 -4
View File
@@ -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)?;
}