diff --git a/docs/dev/macos-handcheck.md b/docs/dev/macos-handcheck.md index b86aae80..23406b7c 100644 --- a/docs/dev/macos-handcheck.md +++ b/docs/dev/macos-handcheck.md @@ -98,6 +98,7 @@ timeout. Until then H5 documents the hang. | 11(iii) Monitor: `refactor(mac): the daemon's Monitor is komorebi_core::Monitor` (see `jj log -r wip-u-kfm2`) | The monitor is the shared generic type; the corner (`window_hiding_position`) is the platform's flattened `extra` (written last, as before), the constructors, `update_*_globals`, `load_focused_workspace`, `move_container_to_workspace(1)`, `add_container_with_direction`, `hide_unfocused_workspaces` and `restore_focused_workspace` are `MonitorExt`; `focus_workspace`, `add_container`, `remove_workspace*`, `ensure_workspace_count` and the name and layout lookups are the shared ones. The monitor gives its workspaces the corner through `workspace.state` when it updates their globals. | `cargo test` (monitor tests, `state_pin`). By hand: `komorebic state | jq '.monitors.elements[0] | keys_unsorted'` is the same list as before with `window_hiding_position` last; switch workspaces on each monitor (hide and restore of the whole workspace), focus-workspace by index and by name, move a container to another workspace and to another monitor, unplug and replug a display (the monitor and its workspaces are rebuilt through `Monitor::new` and `update_all_workspace_globals`), a config with `window_hiding_position` per monitor. | | `wip-u-kfm3` registry fixes (see `jj log -r wip-u-kfm3`) | The registry keeps inert application copies (no blocking Accessibility call when an entry is dropped), a window is registered when its observer is attached, a clone being dropped no longer deregisters its window, `forget_pid` runs ahead of the pause and other-space returns, and the application reaper forgets what it reaps. Nothing reads the registry except the unused `MacosBackend`. | H3, H4, H6, H7 above, and the usual walk-through: no change in what the daemon does is expected. | | deep dedup 1: `refactor(mac): the daemon uses the shared Container API with the hide spot` (see `jj log -r wip-u-mac-deep`) | `ContainerExt` (`add_window_at`, `load_focused_window_at`, `hide_on`, `restore_focused_window`) is gone: every call is the shared `Container::{add_window, load_focused_window, hide, restore}` with `workspace.hide_spot()`. `monocle_on` and `cycle_monocle` took the corner from the monitor (`monitor.extra`) and now take it from the workspace (`workspace.state`), which the monitor writes with the globals; `restore()` now skips the Accessibility call under `cfg!(test)`. | `cargo test`; then H1 (hide spot) and: toggle monocle on a workspace with two containers on a monitor with `window_hiding_position: BottomRight` (every other container parks in that corner), cycle the monocle container, stack with `komorebic stack`/`stack-all`, `cycle-stack`, `add-window-to-container`, switch workspaces. Change the monitor's `window_hiding_position` in the config, reload, and toggle monocle at once: the corner must be the new one. | +| deep dedup 2: `refactor(mac): five router arms are methods with the KDE names` | `ToggleWorkspaceLayer`, `ResizeWindowAxis`, `SessionFloatRule`, `IgnoreRule` and `ReplaceConfiguration` call `toggle_workspace_layer`, `resize_window_axis`, `add_session_float_rule`, `add_ignore_rule(identifier, id: &str)` and `replace_configuration(&PathBuf)`, with the bodies the arms had (a pure move; the router arms are now the KDE text). `id` of the ignore rule is a `&str`, compared with `==`. | `cargo test`; by hand: `komorebic toggle-workspace-layer` twice (tiling to floating and back: a window of the layer is focused, the others hide at the corner and come back), `resize-axis horizontal|vertical increase|decrease`, `komorebic session-float-rule` on a window, `komorebic ignore-rule exe ` and `ignore-rule title/class` (the matching windows leave the workspaces), `komorebic replace-configuration ` (the daemon restarts its windows under the new config; with a broken file it is left alone). | Revisions in section B change no behaviour on purpose (rename, move, route); they still need the sections C walk-through, because a mechanical mistake would show there. diff --git a/komorebi-for-mac/komorebi/src/process_command.rs b/komorebi-for-mac/komorebi/src/process_command.rs index da3e5181..773d1290 100644 --- a/komorebi-for-mac/komorebi/src/process_command.rs +++ b/komorebi-for-mac/komorebi/src/process_command.rs @@ -21,6 +21,7 @@ use crate::core::MonocleFocusBehaviour; use crate::core::MoveBehaviour; use crate::core::OperationDirection; use crate::core::Rect; +use crate::core::Sizing; use crate::core::SocketMessage; use crate::core::StateQuery; use crate::core::WindowContainerBehaviour; @@ -645,117 +646,7 @@ impl WindowManager { } SocketMessage::ToggleMonocle => self.toggle_monocle()?, SocketMessage::ToggleFloat => self.toggle_float(false)?, - SocketMessage::ToggleWorkspaceLayer => { - let mouse_follows_focus = self.mouse_follows_focus; - let hiding_position = self - .focused_monitor() - .ok_or_eyre("there is no monitor")? - .extra - .window_hiding_position; - let workspace = self.focused_workspace_mut()?; - - let mut to_focus = None; - match workspace.layer { - WorkspaceLayer::Tiling => { - workspace.layer = WorkspaceLayer::Floating; - tracing::info!("WorkspaceLayer is now Floating"); - - let focused_idx = workspace.focused_floating_window_idx(); - let mut window_idx_pairs = workspace - .floating_windows_mut() - .make_contiguous() - .iter_mut() - .enumerate() - .collect::>(); - - // Sort by window area - window_idx_pairs.sort_by_key(|(_, w)| { - let rect = Rect::from(w.window_rect().unwrap_or_default()); - rect.right * rect.bottom - }); - window_idx_pairs.reverse(); - - for (i, window) in window_idx_pairs { - if i == focused_idx { - to_focus = Some(window.clone()); - } else { - window.restore()?; - window.raise()?; - } - } - - if let Some(focused_window) = &mut to_focus { - // The focused window should be the last one raised to make sure it is - // on top - focused_window.restore()?; - focused_window.raise()?; - } - - for container in workspace.containers() { - if let Some(_window) = container.focused_window() { - // TODO: figure out z order - // window.lower()?; - } - } - - if let Some(monocle) = &workspace.monocle_container - && let Some(_window) = monocle.focused_window() - { - // TODO: figure out z order - // window.lower()?; - } - } - WorkspaceLayer::Floating => { - workspace.layer = WorkspaceLayer::Tiling; - tracing::info!("WorkspaceLayer is now Tiling"); - - if let Some(monocle) = &workspace.monocle_container { - if let Some(window) = monocle.focused_window() { - to_focus = Some(window.clone()); - window.raise()?; - } - - for window in workspace.floating_windows_mut() { - window.hide(hiding_position)?; - } - } else { - let focused_container_idx = workspace.focused_container_idx(); - for (i, container) in workspace.containers_mut().iter_mut().enumerate() - { - if let Some(window) = container.focused_window() { - if i == focused_container_idx { - to_focus = Some(window.clone()); - } - - window.raise()?; - } - } - - let mut window_idx_pairs = workspace - .floating_windows_mut() - .make_contiguous() - .iter_mut() - .collect::>(); - - // Sort by window area - window_idx_pairs.sort_by_key(|w| { - let rect = Rect::from(w.window_rect().unwrap_or_default()); - rect.right * rect.bottom - }); - - for window in window_idx_pairs { - // TODO: figure out z order - window.hide(hiding_position)?; - // window.lower()?; - } - } - } - }; - - if let Some(window) = to_focus { - window.focus(mouse_follows_focus)?; - } - } + SocketMessage::ToggleWorkspaceLayer => self.toggle_workspace_layer()?, SocketMessage::ResizeWindowEdge(direction, sizing) => { // resizing is done by the key, over and over, and every press is laid out where it // goes and not by an animation that the next press would have to catch up with @@ -763,65 +654,7 @@ impl WindowManager { self.resize_window(direction, sizing, self.resize_delta, true)?; } SocketMessage::ResizeWindowAxis(axis, sizing) => { - let _suspended_animations = animation::suspend(); - match axis { - Axis::Horizontal => { - self.resize_window( - OperationDirection::Left, - sizing, - self.resize_delta, - false, - )?; - self.resize_window( - OperationDirection::Right, - sizing, - self.resize_delta, - false, - )?; - } - Axis::Vertical => { - self.resize_window( - OperationDirection::Up, - sizing, - self.resize_delta, - false, - )?; - self.resize_window( - OperationDirection::Down, - sizing, - self.resize_delta, - false, - )?; - } - Axis::HorizontalAndVertical => { - self.resize_window( - OperationDirection::Left, - sizing, - self.resize_delta, - false, - )?; - self.resize_window( - OperationDirection::Right, - sizing, - self.resize_delta, - false, - )?; - self.resize_window( - OperationDirection::Up, - sizing, - self.resize_delta, - false, - )?; - self.resize_window( - OperationDirection::Down, - sizing, - self.resize_delta, - false, - )?; - } - } - - self.update_focused_workspace(false, false)?; + self.resize_window_axis(axis, sizing)? } SocketMessage::Retile => self.retile_all(false)?, SocketMessage::RetileWithResizeDimensions => self.retile_all(true)?, @@ -1012,42 +845,7 @@ impl WindowManager { reply.write_all(monitors_state.as_bytes())?; } SocketMessage::Query(query) => self.answer_query(query, &mut reply)?, - SocketMessage::SessionFloatRule => { - let foreground_window = - MacosApi::foreground_window().ok_or_eyre("there is no foreground window")?; - let (exe, title, role) = ( - AdhocWindow::exe(&foreground_window), - AdhocWindow::title(&foreground_window), - AdhocWindow::role(&foreground_window), - ); - - if let (Some(exe), Some(title), Some(role)) = (exe, title, role) { - let rule = MatchingRule::Composite(vec![ - IdWithIdentifier { - kind: ApplicationIdentifier::Exe, - id: exe, - matching_strategy: Option::from(MatchingStrategy::Equals), - }, - IdWithIdentifier { - kind: ApplicationIdentifier::Title, - id: title, - matching_strategy: Option::from(MatchingStrategy::Equals), - }, - IdWithIdentifier { - kind: ApplicationIdentifier::Class, - id: role, - matching_strategy: Option::from(MatchingStrategy::Equals), - }, - ]); - - let mut floating_applications = FLOATING_APPLICATIONS.lock(); - floating_applications.push(rule.clone()); - let mut session_floating_applications = SESSION_FLOATING_APPLICATIONS.lock(); - session_floating_applications.push(rule.clone()); - - self.toggle_float(true)?; - } - } + SocketMessage::SessionFloatRule => self.add_session_float_rule()?, SocketMessage::SessionFloatRules => { let session_floating_applications = SESSION_FLOATING_APPLICATIONS.lock(); let rules = serde_json::to_string_pretty(&*session_floating_applications) @@ -1130,78 +928,7 @@ impl WindowManager { self.enforce_workspace_rules()?; } SocketMessage::IgnoreRule(identifier, ref id) => { - let mut ignore_identifiers = IGNORE_IDENTIFIERS.lock(); - - let mut should_push = true; - for i in &*ignore_identifiers { - if let MatchingRule::Simple(i) = i - && i.id.eq(id) - { - should_push = false; - } - } - - if should_push { - ignore_identifiers.push(MatchingRule::Simple(IdWithIdentifier { - kind: identifier, - id: id.clone(), - matching_strategy: Option::from(MatchingStrategy::Legacy), - })); - } - - let offset = self.work_area_offset; - - let mut window_ids_to_purge = vec![]; - for (i, monitor) in self.monitors().iter().enumerate() { - for container in monitor - .focused_workspace() - .ok_or_eyre("there is no workspace")? - .containers() - { - for window in container.windows() { - match identifier { - ApplicationIdentifier::Path => { - if window.path().unwrap_or_default().to_string_lossy() == *id { - window_ids_to_purge.push((i, window.id)); - } - } - ApplicationIdentifier::Exe => { - if window.exe().unwrap_or_default() == *id { - window_ids_to_purge.push((i, window.id)); - } - } - ApplicationIdentifier::Class => { - if window.role().unwrap_or_default() == *id { - window_ids_to_purge.push((i, window.id)); - } - - if window.subrole().unwrap_or_default() == *id { - window_ids_to_purge.push((i, window.id)); - } - } - ApplicationIdentifier::Title => { - if window.title().unwrap_or_default() == *id { - window_ids_to_purge.push((i, window.id)); - } - } - } - } - } - } - - for (monitor_idx, id) in window_ids_to_purge { - let monitor = self - .monitors_mut() - .get_mut(monitor_idx) - .ok_or_eyre("there is no monitor")?; - - monitor - .focused_workspace_mut() - .ok_or_eyre("there is no focused workspace")? - .remove_window(id)?; - - monitor.update_focused_workspace(offset)?; - } + self.add_ignore_rule(identifier, id)? } SocketMessage::ManageRule(identifier, ref id) => { let mut manage_identifiers = MANAGE_IDENTIFIERS.lock(); @@ -1562,34 +1289,7 @@ impl WindowManager { }); } SocketMessage::ReplaceConfiguration(ref config) => { - // Check that this is a valid static config file first - if StaticConfig::read(config).is_ok() { - // Clear workspace rules; these will need to be replaced - WORKSPACE_MATCHING_RULES.lock().clear(); - // Pause so that restored windows come to the foreground from all workspaces - self.is_paused = true; - // Bring all windows to the foreground - self.restore_all_windows(false)?; - - // Create a new wm from the config path - let mut wm = StaticConfig::preload( - config, - window_manager_event_listener::event_rx(), - self.command_listener.try_clone().ok(), - &self.run_loop.0, - )?; - - // Initialize the new wm - wm.init()?; - - wm.restore_all_windows(true)?; - - // This is equivalent to StaticConfig::postload for this use case - StaticConfig::reload(config, &mut wm)?; - - // Set self to the new wm instance - *self = wm; - } + self.replace_configuration(config)? } SocketMessage::Border(enable) => { border_manager::BORDER_ENABLED.store(enable, Ordering::SeqCst); @@ -1727,6 +1427,298 @@ pub fn read_commands_uds(wm: &Arc>, stream: UnixStream) -> komorebi_transport::read_commands::(wm, stream, &NoPolicy) } +impl WindowManager { + /// Switches the focused workspace between its tiling and floating layers, and focuses a window of the layer it switched to + pub fn toggle_workspace_layer(&mut self) -> eyre::Result<()> { + let mouse_follows_focus = self.mouse_follows_focus; + let hiding_position = self + .focused_monitor() + .ok_or_eyre("there is no monitor")? + .extra + .window_hiding_position; + let workspace = self.focused_workspace_mut()?; + + let mut to_focus = None; + match workspace.layer { + WorkspaceLayer::Tiling => { + workspace.layer = WorkspaceLayer::Floating; + tracing::info!("WorkspaceLayer is now Floating"); + + let focused_idx = workspace.focused_floating_window_idx(); + let mut window_idx_pairs = workspace + .floating_windows_mut() + .make_contiguous() + .iter_mut() + .enumerate() + .collect::>(); + + // Sort by window area + window_idx_pairs.sort_by_key(|(_, w)| { + let rect = Rect::from(w.window_rect().unwrap_or_default()); + rect.right * rect.bottom + }); + window_idx_pairs.reverse(); + + for (i, window) in window_idx_pairs { + if i == focused_idx { + to_focus = Some(window.clone()); + } else { + window.restore()?; + window.raise()?; + } + } + + if let Some(focused_window) = &mut to_focus { + // The focused window should be the last one raised to make sure it is + // on top + focused_window.restore()?; + focused_window.raise()?; + } + + for container in workspace.containers() { + if let Some(_window) = container.focused_window() { + // TODO: figure out z order + // window.lower()?; + } + } + + if let Some(monocle) = &workspace.monocle_container + && let Some(_window) = monocle.focused_window() + { + // TODO: figure out z order + // window.lower()?; + } + } + WorkspaceLayer::Floating => { + workspace.layer = WorkspaceLayer::Tiling; + tracing::info!("WorkspaceLayer is now Tiling"); + + if let Some(monocle) = &workspace.monocle_container { + if let Some(window) = monocle.focused_window() { + to_focus = Some(window.clone()); + window.raise()?; + } + + for window in workspace.floating_windows_mut() { + window.hide(hiding_position)?; + } + } else { + let focused_container_idx = workspace.focused_container_idx(); + for (i, container) in workspace.containers_mut().iter_mut().enumerate() { + if let Some(window) = container.focused_window() { + if i == focused_container_idx { + to_focus = Some(window.clone()); + } + + window.raise()?; + } + } + + let mut window_idx_pairs = workspace + .floating_windows_mut() + .make_contiguous() + .iter_mut() + .collect::>(); + + // Sort by window area + window_idx_pairs.sort_by_key(|w| { + let rect = Rect::from(w.window_rect().unwrap_or_default()); + rect.right * rect.bottom + }); + + for window in window_idx_pairs { + // TODO: figure out z order + window.hide(hiding_position)?; + // window.lower()?; + } + } + } + }; + + if let Some(window) = to_focus { + window.focus(mouse_follows_focus)?; + } + Ok(()) + } + + /// Resizes the focused window along an axis, by the edge that the axis says + pub fn resize_window_axis(&mut self, axis: Axis, sizing: Sizing) -> eyre::Result<()> { + let _suspended_animations = animation::suspend(); + match axis { + Axis::Horizontal => { + self.resize_window(OperationDirection::Left, sizing, self.resize_delta, false)?; + self.resize_window(OperationDirection::Right, sizing, self.resize_delta, false)?; + } + Axis::Vertical => { + self.resize_window(OperationDirection::Up, sizing, self.resize_delta, false)?; + self.resize_window(OperationDirection::Down, sizing, self.resize_delta, false)?; + } + Axis::HorizontalAndVertical => { + self.resize_window(OperationDirection::Left, sizing, self.resize_delta, false)?; + self.resize_window(OperationDirection::Right, sizing, self.resize_delta, false)?; + self.resize_window(OperationDirection::Up, sizing, self.resize_delta, false)?; + self.resize_window(OperationDirection::Down, sizing, self.resize_delta, false)?; + } + } + + self.update_focused_workspace(false, false)?; + Ok(()) + } + + /// Adds a float rule for the focused window (its executable, title and role), until the daemon stops + pub fn add_session_float_rule(&mut self) -> eyre::Result<()> { + let foreground_window = + MacosApi::foreground_window().ok_or_eyre("there is no foreground window")?; + let (exe, title, role) = ( + AdhocWindow::exe(&foreground_window), + AdhocWindow::title(&foreground_window), + AdhocWindow::role(&foreground_window), + ); + + if let (Some(exe), Some(title), Some(role)) = (exe, title, role) { + let rule = MatchingRule::Composite(vec![ + IdWithIdentifier { + kind: ApplicationIdentifier::Exe, + id: exe, + matching_strategy: Option::from(MatchingStrategy::Equals), + }, + IdWithIdentifier { + kind: ApplicationIdentifier::Title, + id: title, + matching_strategy: Option::from(MatchingStrategy::Equals), + }, + IdWithIdentifier { + kind: ApplicationIdentifier::Class, + id: role, + matching_strategy: Option::from(MatchingStrategy::Equals), + }, + ]); + + let mut floating_applications = FLOATING_APPLICATIONS.lock(); + floating_applications.push(rule.clone()); + let mut session_floating_applications = SESSION_FLOATING_APPLICATIONS.lock(); + session_floating_applications.push(rule.clone()); + + self.toggle_float(true)?; + } + Ok(()) + } + + /// Adds a rule that ignores the windows matching `id`, and takes the ones it matches out of the workspaces + pub fn add_ignore_rule( + &mut self, + identifier: ApplicationIdentifier, + id: &str, + ) -> eyre::Result<()> { + let mut ignore_identifiers = IGNORE_IDENTIFIERS.lock(); + + let mut should_push = true; + for i in &*ignore_identifiers { + if let MatchingRule::Simple(i) = i + && i.id == id + { + should_push = false; + } + } + + if should_push { + ignore_identifiers.push(MatchingRule::Simple(IdWithIdentifier { + kind: identifier, + id: id.to_string(), + matching_strategy: Option::from(MatchingStrategy::Legacy), + })); + } + + let offset = self.work_area_offset; + + let mut window_ids_to_purge = vec![]; + for (i, monitor) in self.monitors().iter().enumerate() { + for container in monitor + .focused_workspace() + .ok_or_eyre("there is no workspace")? + .containers() + { + for window in container.windows() { + match identifier { + ApplicationIdentifier::Path => { + if window.path().unwrap_or_default().to_string_lossy() == *id { + window_ids_to_purge.push((i, window.id)); + } + } + ApplicationIdentifier::Exe => { + if window.exe().unwrap_or_default() == *id { + window_ids_to_purge.push((i, window.id)); + } + } + ApplicationIdentifier::Class => { + if window.role().unwrap_or_default() == *id { + window_ids_to_purge.push((i, window.id)); + } + + if window.subrole().unwrap_or_default() == *id { + window_ids_to_purge.push((i, window.id)); + } + } + ApplicationIdentifier::Title => { + if window.title().unwrap_or_default() == *id { + window_ids_to_purge.push((i, window.id)); + } + } + } + } + } + } + + for (monitor_idx, id) in window_ids_to_purge { + let monitor = self + .monitors_mut() + .get_mut(monitor_idx) + .ok_or_eyre("there is no monitor")?; + + monitor + .focused_workspace_mut() + .ok_or_eyre("there is no focused workspace")? + .remove_window(id)?; + + monitor.update_focused_workspace(offset)?; + } + Ok(()) + } + + /// Replaces the running configuration with the one in the file + pub fn replace_configuration(&mut self, config: &std::path::Path) -> eyre::Result<()> { + // Check that this is a valid static config file first + if StaticConfig::read(config).is_ok() { + // Clear workspace rules; these will need to be replaced + WORKSPACE_MATCHING_RULES.lock().clear(); + // Pause so that restored windows come to the foreground from all workspaces + self.is_paused = true; + // Bring all windows to the foreground + self.restore_all_windows(false)?; + + // Create a new wm from the config path + let mut wm = StaticConfig::preload( + config, + window_manager_event_listener::event_rx(), + self.command_listener.try_clone().ok(), + &self.run_loop.0, + )?; + + // Initialize the new wm + wm.init()?; + + wm.restore_all_windows(true)?; + + // This is equivalent to StaticConfig::postload for this use case + StaticConfig::reload(config, &mut wm)?; + + // Set self to the new wm instance + *self = wm; + } + Ok(()) + } +} + /// The window manager as the shared commands of `komorebi_core::command` see it impl komorebi_core::CommandHost for WindowManager { type P = crate::workspace::MacosPlatform;