diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index 35890123..023be973 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -109,6 +109,30 @@ impl Placement { } } +#[derive( + Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, Display, EnumString, ValueEnum, +)] +pub enum MoveBehaviour { + /// Swap the window container with the window container at the edge of the adjacent monitor + #[default] + Swap, + /// Insert the window container into the focused workspace on the adjacent monitor + Insert, + /// Do nothing if trying to move a window container in the direction of an adjacent monitor + NoOp, +} + +#[derive( + Clone, Copy, Debug, Default, Serialize, Deserialize, Display, EnumString, ValueEnum, PartialEq, +)] +pub enum CrossBoundaryBehaviour { + /// Attempt to perform actions across a workspace boundary + Workspace, + /// Attempt to perform actions across a monitor boundary + #[default] + Monitor, +} + #[serde_with::serde_as] #[derive(Clone, Debug, Serialize, Deserialize, Display)] #[serde(tag = "type", content = "content")] @@ -122,10 +146,20 @@ pub enum SocketMessage { ChangeLayout(DefaultLayout), CycleLayout(CycleDirection), FlipLayout(Axis), - TogglePause, - ToggleMonocle, + ToggleWorkspaceWindowContainerBehaviour, + ToggleWorkspaceFloatOverride, + ToggleLock, ToggleFloat, + ToggleMonocle, + ToggleWindowContainerBehaviour, + ToggleFloatOverride, + ToggleCrossMonitorMoveBehaviour, + ToggleTiling, + ToggleWindowBasedWorkAreaOffset, + TogglePause, ToggleWorkspaceLayer, + // TODO: look into maximized windows + // ToggleMaximize, FocusWorkspaceNumber(usize), MoveContainerToWorkspaceNumber(usize), SendContainerToWorkspaceNumber(usize), diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 0c9e69e3..c9edaf9f 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -1,4 +1,6 @@ +use crate::core::MoveBehaviour; use crate::core::SocketMessage; +use crate::core::WindowContainerBehaviour; use crate::core::arrangement::Axis; use crate::core::operation_direction::OperationDirection; use crate::core::rect::Rect; @@ -167,7 +169,7 @@ impl WindowManager { for container in workspace.containers() { if let Some(_window) = container.focused_window() { - // todo: figure out z order + // TODO: figure out z order // window.lower()?; } } @@ -175,7 +177,7 @@ impl WindowManager { if let Some(monocle) = &workspace.monocle_container && let Some(_window) = monocle.focused_window() { - // todo: figure out z order + // TODO: figure out z order // window.lower()?; } } @@ -220,7 +222,7 @@ impl WindowManager { }); for window in window_idx_pairs { - // todo: figure out z order + // TODO: figure out z order window.hide()?; // window.lower()?; } @@ -297,6 +299,73 @@ impl WindowManager { } SocketMessage::Retile => self.retile_all(false)?, SocketMessage::RetileWithResizeDimensions => self.retile_all(true)?, + SocketMessage::ToggleWorkspaceWindowContainerBehaviour => { + let current_global_behaviour = self.window_management_behaviour.current_behaviour; + if let Some(behaviour) = + &mut self.focused_workspace_mut()?.window_container_behaviour + { + match behaviour { + WindowContainerBehaviour::Create => { + *behaviour = WindowContainerBehaviour::Append + } + WindowContainerBehaviour::Append => { + *behaviour = WindowContainerBehaviour::Create + } + } + } else { + self.focused_workspace_mut()?.window_container_behaviour = + Some(match current_global_behaviour { + WindowContainerBehaviour::Create => WindowContainerBehaviour::Append, + WindowContainerBehaviour::Append => WindowContainerBehaviour::Create, + }); + }; + } + SocketMessage::ToggleWorkspaceFloatOverride => { + let current_global_override = self.window_management_behaviour.float_override; + if let Some(float_override) = &mut self.focused_workspace_mut()?.float_override { + *float_override = !*float_override; + } else { + self.focused_workspace_mut()?.float_override = Some(!current_global_override); + }; + } + SocketMessage::ToggleLock => self.toggle_lock()?, + SocketMessage::ToggleWindowContainerBehaviour => { + match self.window_management_behaviour.current_behaviour { + WindowContainerBehaviour::Create => { + self.window_management_behaviour.current_behaviour = + WindowContainerBehaviour::Append; + } + WindowContainerBehaviour::Append => { + self.window_management_behaviour.current_behaviour = + WindowContainerBehaviour::Create; + } + } + } + SocketMessage::ToggleFloatOverride => { + self.window_management_behaviour.float_override = + !self.window_management_behaviour.float_override; + } + SocketMessage::ToggleWindowBasedWorkAreaOffset => { + let workspace = self.focused_workspace_mut()?; + workspace.apply_window_based_work_area_offset = + !workspace.apply_window_based_work_area_offset; + + self.retile_all(true)?; + } + SocketMessage::ToggleCrossMonitorMoveBehaviour => { + match self.cross_monitor_move_behaviour { + MoveBehaviour::Swap => { + self.cross_monitor_move_behaviour = MoveBehaviour::Insert; + } + MoveBehaviour::Insert => { + self.cross_monitor_move_behaviour = MoveBehaviour::Swap; + } + _ => {} + } + } + SocketMessage::ToggleTiling => { + self.toggle_tiling()?; + } } Ok(()) diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index 9543f750..61e615dd 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -51,7 +51,7 @@ impl WindowManager { let application = self.application(process_id)?; if let Some(window_id) = application.main_window_id() { - // todo: figure out if this applies on macOS too + // 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 @@ -108,7 +108,7 @@ impl WindowManager { self.update_focused_workspace(false, false)?; } } - // todo: upate this to work with floating applications / rules + // TODO: upate this to work with floating applications / rules WindowManagerEvent::Show(_, process_id) => { let mut window_id = None; let mut window_element = None; diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 3c9bac4e..7e7981ed 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -4,6 +4,8 @@ use crate::LibraryError; use crate::accessibility::AccessibilityApi; use crate::application::Application; use crate::container::Container; +use crate::core::CrossBoundaryBehaviour; +use crate::core::MoveBehaviour; use crate::core::Placement; use crate::core::Sizing; use crate::core::WindowManagementBehaviour; @@ -14,6 +16,7 @@ use crate::core::default_layout::DefaultLayout; use crate::core::layout::Layout; use crate::core::operation_direction::OperationDirection; use crate::core::rect::Rect; +use crate::lockable_sequence::Lockable; use crate::macos_api::MacosApi; use crate::monitor::Monitor; use crate::ring::Ring; @@ -42,6 +45,8 @@ pub struct WindowManager { pub is_paused: bool, pub resize_delta: i32, pub window_management_behaviour: WindowManagementBehaviour, + pub cross_monitor_move_behaviour: MoveBehaviour, + pub cross_boundary_behaviour: CrossBoundaryBehaviour, pub mouse_follows_focus: bool, pub work_area_offset: Option, pub incoming_events: Receiver, @@ -71,7 +76,7 @@ impl WindowManager { let listener = UnixListener::bind(&socket)?; - // todo: undo this when we get config + // TODO: undo this when we get config let mut behaviour = WindowManagementBehaviour::default(); behaviour.toggle_float_placement = Placement::CenterAndResize; @@ -83,6 +88,8 @@ impl WindowManager { is_paused: false, resize_delta: 50, window_management_behaviour: behaviour, + cross_monitor_move_behaviour: Default::default(), + cross_boundary_behaviour: Default::default(), mouse_follows_focus: true, work_area_offset: None, incoming_events: incoming, @@ -244,125 +251,126 @@ impl WindowManager { return Ok(()); } - // let mut cross_monitor_monocle_or_max = false; + let mut cross_monitor_monocle_or_max = false; - // let workspace_idx = self.focused_workspace_idx()?; + let workspace_idx = self.focused_workspace_idx()?; // this is for when we are scrolling across workspaces like PaperWM - // if matches!( - // self.cross_boundary_behaviour, - // CrossBoundaryBehaviour::Workspace - // ) && matches!( - // direction, - // OperationDirection::Left | OperationDirection::Right - // ) { - // let workspace_count = if let Some(monitor) = self.focused_monitor() { - // monitor.workspaces().len() - // } else { - // 1 - // }; - // - // let next_idx = match direction { - // OperationDirection::Left => match workspace_idx { - // 0 => workspace_count - 1, - // n => n - 1, - // }, - // OperationDirection::Right => match workspace_idx { - // n if n == workspace_count - 1 => 0, - // n => n + 1, - // }, - // _ => workspace_idx, - // }; - // - // self.focus_workspace(next_idx)?; - // - // if let Ok(focused_workspace) = self.focused_workspace_mut() { - // if focused_workspace.monocle_container.is_none() { - // match direction { - // OperationDirection::Left => match focused_workspace.layout { - // Layout::Default(layout) => { - // let target_index = - // layout.rightmost_index(focused_workspace.containers().len()); - // focused_workspace.focus_container(target_index); - // } - // }, - // OperationDirection::Right => match focused_workspace.layout { - // Layout::Default(layout) => { - // let target_index = - // layout.leftmost_index(focused_workspace.containers().len()); - // focused_workspace.focus_container(target_index); - // } - // }, - // _ => {} - // }; - // } - // } - // - // return Ok(()); - // } + if matches!( + self.cross_boundary_behaviour, + CrossBoundaryBehaviour::Workspace + ) && matches!( + direction, + OperationDirection::Left | OperationDirection::Right + ) { + let workspace_count = if let Some(monitor) = self.focused_monitor() { + monitor.workspaces().len() + } else { + 1 + }; + + let next_idx = match direction { + OperationDirection::Left => match workspace_idx { + 0 => workspace_count - 1, + n => n - 1, + }, + OperationDirection::Right => match workspace_idx { + n if n == workspace_count - 1 => 0, + n => n + 1, + }, + _ => workspace_idx, + }; + + self.focus_workspace(next_idx)?; + + if let Ok(focused_workspace) = self.focused_workspace_mut() + && focused_workspace.monocle_container.is_none() + { + match direction { + OperationDirection::Left => match focused_workspace.layout { + Layout::Default(layout) => { + let target_index = + layout.rightmost_index(focused_workspace.containers().len()); + focused_workspace.focus_container(target_index); + } + }, + OperationDirection::Right => match focused_workspace.layout { + Layout::Default(layout) => { + let target_index = + layout.leftmost_index(focused_workspace.containers().len()); + focused_workspace.focus_container(target_index); + } + }, + _ => {} + }; + } + + return Ok(()); + } // if there is no floating_window in that direction for this workspace - // let monitor_idx = self - // .monitor_idx_in_direction(direction) - // .ok_or_else(|| eyre!("there is no container or monitor in this direction"))?; - // - // self.focus_monitor(monitor_idx)?; - // let mouse_follows_focus = self.mouse_follows_focus; - // - // if let Ok(focused_workspace) = self.focused_workspace_mut() { - // // if let Some(window) = focused_workspace.maximized_window() { - // // window.focus(mouse_follows_focus)?; - // // cross_monitor_monocle_or_max = true; - // // } else - // if let Some(monocle) = focused_workspace.monocle_container { - // if let Some(window) = monocle.focused_window() { - // window.focus(mouse_follows_focus)?; - // cross_monitor_monocle_or_max = true; - // } - // } else if focused_workspace.layer == WorkspaceLayer::Tiling { - // match direction { - // OperationDirection::Left => match focused_workspace.layout { - // Layout::Default(layout) => { - // let target_index = - // layout.rightmost_index(focused_workspace.containers().len()); - // focused_workspace.focus_container(target_index); - // } - // }, - // OperationDirection::Right => match focused_workspace.layout { - // Layout::Default(layout) => { - // let target_index = - // layout.leftmost_index(focused_workspace.containers().len()); - // focused_workspace.focus_container(target_index); - // } - // }, - // _ => {} - // }; - // } - // } - // - // if !cross_monitor_monocle_or_max { - // let ws = self.focused_workspace_mut()?; - // if ws.is_empty() { - // // This is to remove focus from the previous monitor - // let desktop_window = Window::from(MacosApi::desktop_window()?); - // - // match MacosApi::raise_and_focus_window(desktop_window.id) { - // Ok(()) => {} - // Err(error) => { - // tracing::warn!("{} {}:{}", error, file!(), line!()); - // } - // } - // } else if ws.layer == WorkspaceLayer::Floating && !ws.floating_windows().is_empty() { - // if let Some(window) = ws.focused_floating_window() { - // window.focus(self.mouse_follows_focus)?; - // } - // } else { - // ws.layer = WorkspaceLayer::Tiling; - // if let Ok(focused_window) = self.focused_window() { - // focused_window.focus(self.mouse_follows_focus)?; - // } - // } - // } + let monitor_idx = self + .monitor_idx_in_direction(direction) + .ok_or_else(|| eyre!("there is no container or monitor in this direction"))?; + + self.focus_monitor(monitor_idx)?; + let mouse_follows_focus = self.mouse_follows_focus; + + if let Ok(focused_workspace) = self.focused_workspace_mut() { + // if let Some(window) = focused_workspace.maximized_window() { + // window.focus(mouse_follows_focus)?; + // cross_monitor_monocle_or_max = true; + // } else + if let Some(monocle) = &focused_workspace.monocle_container { + if let Some(window) = monocle.focused_window() { + window.focus(mouse_follows_focus)?; + cross_monitor_monocle_or_max = true; + } + } else if focused_workspace.layer == WorkspaceLayer::Tiling { + match direction { + OperationDirection::Left => match focused_workspace.layout { + Layout::Default(layout) => { + let target_index = + layout.rightmost_index(focused_workspace.containers().len()); + focused_workspace.focus_container(target_index); + } + }, + OperationDirection::Right => match focused_workspace.layout { + Layout::Default(layout) => { + let target_index = + layout.leftmost_index(focused_workspace.containers().len()); + focused_workspace.focus_container(target_index); + } + }, + _ => {} + }; + } + } + + if !cross_monitor_monocle_or_max { + let ws = self.focused_workspace_mut()?; + if ws.is_empty() { + // TODO: figure out if we need to do this on macos + // This is to remove focus from the previous monitor + // let desktop_window = Window::from(MacosApi::desktop_window()?); + // + // match MacosApi::raise_and_focus_window(desktop_window.id) { + // Ok(()) => {} + // Err(error) => { + // tracing::warn!("{} {}:{}", error, file!(), line!()); + // } + // } + } else if ws.layer == WorkspaceLayer::Floating && !ws.floating_windows().is_empty() { + if let Some(window) = ws.focused_floating_window() { + window.focus(mouse_follows_focus)?; + } + } else { + ws.layer = WorkspaceLayer::Tiling; + if let Ok(focused_window) = self.focused_window() { + focused_window.focus(mouse_follows_focus)?; + } + } + } Ok(()) } @@ -1233,4 +1241,66 @@ impl WindowManager { self.update_focused_workspace(self.mouse_follows_focus, false) } + + #[tracing::instrument(skip(self))] + pub fn toggle_lock(&mut self) -> eyre::Result<()> { + let workspace = self.focused_workspace_mut()?; + if let Some(container) = workspace.focused_container_mut() { + // Toggle the locked flag + container.set_locked(!container.locked()); + } + Ok(()) + } + + #[tracing::instrument(skip(self))] + pub fn toggle_tiling(&mut self) -> eyre::Result<()> { + let workspace = self.focused_workspace_mut()?; + workspace.tile = !workspace.tile; + self.update_focused_workspace(false, false) + } + + #[tracing::instrument(skip(self))] + pub fn focus_monitor(&mut self, idx: usize) -> eyre::Result<()> { + tracing::info!("focusing monitor"); + + if self.monitors().get(idx).is_some() { + self.monitors.focus(idx); + } else { + return Err(eyre!("this is not a valid monitor index")); + } + + Ok(()) + } + + // TODO: see if this logic is the same in macos when I have more than one monitor + pub fn monitor_idx_in_direction(&self, direction: OperationDirection) -> Option { + let current_monitor_size = self.focused_monitor_size().ok()?; + + for (idx, monitor) in self.monitors.elements().iter().enumerate() { + match direction { + OperationDirection::Left => { + if monitor.size.left + monitor.size.right == current_monitor_size.left { + return Option::from(idx); + } + } + OperationDirection::Right => { + if current_monitor_size.right + current_monitor_size.left == monitor.size.left { + return Option::from(idx); + } + } + OperationDirection::Up => { + if monitor.size.top + monitor.size.bottom == current_monitor_size.top { + return Option::from(idx); + } + } + OperationDirection::Down => { + if current_monitor_size.top + current_monitor_size.bottom == monitor.size.top { + return Option::from(idx); + } + } + } + } + + None + } } diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index 0be5b6fb..099eecd8 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -301,6 +301,13 @@ impl Workspace { self.focus_container(focused_idx.saturating_sub(1)); } + pub fn is_empty(&self) -> bool { + self.containers().is_empty() + && self.maximized_window.is_none() + && self.monocle_container.is_none() + && self.floating_windows().is_empty() + } + pub fn contains_window(&self, window_id: u32) -> bool { for container in self.containers() { if container.contains_window(window_id) { diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index cc5d8a4e..5dc65108 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -163,6 +163,26 @@ enum SubCommand { SendToWorkspace(SendToWorkspace), /// Force the retiling of all managed windows Retile, + /// Toggle application of the window-based work area offset for the focused workspace + ToggleWindowBasedWorkAreaOffset, + /// Toggle the behaviour for new windows (stacking or dynamic tiling) + ToggleWindowContainerBehaviour, + /// Enable or disable float override, which makes it so every new window opens in floating mode + ToggleFloatOverride, + /// Toggle the behaviour for new windows (stacking or dynamic tiling) for currently focused + /// workspace. If there was no behaviour set for the workspace previously it takes the opposite + /// of the global value. + ToggleWorkspaceWindowContainerBehaviour, + /// Enable or disable float override, which makes it so every new window opens in floating + /// mode, for the currently focused workspace. If there was no override value set for the + /// workspace previously it takes the opposite of the global value. + ToggleWorkspaceFloatOverride, + /// Toggle window tiling on the focused workspace + ToggleTiling, + /// Toggle a lock for the focused container, ensuring it will not be displaced by any new windows + ToggleLock, + /// Toggle the behaviour when moving windows across monitor boundaries + ToggleCrossMonitorMoveBehaviour, } fn main() -> eyre::Result<()> { @@ -232,6 +252,30 @@ fn main() -> eyre::Result<()> { SubCommand::PromoteWindow(arg) => { send_message(&SocketMessage::PromoteWindow(arg.operation_direction))?; } + SubCommand::ToggleWindowBasedWorkAreaOffset => { + send_message(&SocketMessage::ToggleWindowBasedWorkAreaOffset)?; + } + SubCommand::ToggleWindowContainerBehaviour => { + send_message(&SocketMessage::ToggleWindowContainerBehaviour)?; + } + SubCommand::ToggleFloatOverride => { + send_message(&SocketMessage::ToggleFloatOverride)?; + } + SubCommand::ToggleWorkspaceWindowContainerBehaviour => { + send_message(&SocketMessage::ToggleWorkspaceWindowContainerBehaviour)?; + } + SubCommand::ToggleWorkspaceFloatOverride => { + send_message(&SocketMessage::ToggleWorkspaceFloatOverride)?; + } + SubCommand::ToggleTiling => { + send_message(&SocketMessage::ToggleTiling)?; + } + SubCommand::ToggleLock => { + send_message(&SocketMessage::ToggleLock)?; + } + SubCommand::ToggleCrossMonitorMoveBehaviour => { + send_message(&SocketMessage::ToggleCrossMonitorMoveBehaviour)?; + } } Ok(())