diff --git a/komorebi-for-kde/komorebi/src/process_command.rs b/komorebi-for-kde/komorebi/src/process_command.rs index a61b56ee..acb00d94 100644 --- a/komorebi-for-kde/komorebi/src/process_command.rs +++ b/komorebi-for-kde/komorebi/src/process_command.rs @@ -375,6 +375,174 @@ fn is_layout_command(message: &SocketMessage) -> bool { } impl WindowManager { + /// Looks for a window of the executable `exe` on every monitor and workspace, and focuses it + pub fn eager_focus(&mut self, exe: &str) -> eyre::Result<()> { + let focused_monitor_idx = self.focused_monitor_idx(); + let mouse_follows_focus = self.mouse_follows_focus; + + let mut window_location = None; + let mut monitor_to_focus = None; + let mut needs_workspace_loading = false; + + 'search: for (monitor_idx, monitor) in self.monitors_mut().iter_mut().enumerate() { + for (workspace_idx, workspace) in monitor.workspaces().iter().enumerate() { + if let Some(location) = workspace.location_from_exe(exe) { + window_location = Some(location); + + if monitor_idx != focused_monitor_idx { + monitor_to_focus = Some(monitor_idx); + } + + // Focus workspace if it is not already the focused one, without + // loading it so that we don't give focus to the wrong window, we will + // load it later after focusing the wanted window + let focused_ws_idx = monitor.focused_workspace_idx(); + if focused_ws_idx != workspace_idx { + monitor.last_focused_workspace = Option::from(focused_ws_idx); + monitor.focus_workspace(workspace_idx)?; + needs_workspace_loading = true; + } + + break 'search; + } + } + } + + if let Some(monitor_idx) = monitor_to_focus { + self.focus_monitor(monitor_idx)?; + } + + if let Some(location) = window_location { + match location { + WorkspaceWindowLocation::Monocle(window_idx) => { + self.focus_container_window(window_idx)?; + } + WorkspaceWindowLocation::Maximized => { + if let Some(window) = &mut self.focused_workspace_mut()?.maximized_window { + window.focus(mouse_follows_focus)?; + } + } + WorkspaceWindowLocation::Container(container_idx, window_idx) => { + let focused_container_idx = self.focused_container_idx()?; + if container_idx != focused_container_idx { + self.focused_workspace_mut()?.focus_container(container_idx); + } + + self.focus_container_window(window_idx)?; + } + WorkspaceWindowLocation::Floating(window_idx) => { + if let Some(window) = self + .focused_workspace_mut()? + .floating_windows_mut() + .get_mut(window_idx) + { + window.focus(mouse_follows_focus)?; + } + } + } + + if needs_workspace_loading { + let mouse_follows_focus = self.mouse_follows_focus; + if let Some(monitor) = self.focused_monitor_mut() { + monitor.load_focused_workspace(mouse_follows_focus)?; + } + } + } + Ok(()) + } + + /// Brings the other layer of the focused workspace, the floating windows or the tiled ones, in front, and focuses a window of it + pub fn toggle_workspace_layer(&mut self) -> eyre::Result<()> { + let mouse_follows_focus = self.mouse_follows_focus; + 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 = w.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); + } 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()?; + } + } + 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); + window.raise()?; + } + + for window in workspace.floating_windows_mut() { + window.hide(); + } + } 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); + } + + 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 = w.rect().unwrap_or_default(); + rect.right * rect.bottom + }); + + for window in window_idx_pairs { + window.hide(); + } + } + } + }; + + if let Some(window) = to_focus { + window.focus(mouse_follows_focus)?; + } + Ok(()) + } + /// Focuses the next or previous workspace of the monitor that is focused, or of the monitor the pointer is on when its workspace is empty pub fn cycle_focus_workspace(&mut self, direction: CycleDirection) -> eyre::Result<()> { // This is to ensure that even on an empty workspace on a secondary monitor, the @@ -595,81 +763,7 @@ impl WindowManager { self.focus_container_in_direction(direction)?; self.promote_container_to_front()? } - SocketMessage::EagerFocus(ref exe) => { - let focused_monitor_idx = self.focused_monitor_idx(); - let mouse_follows_focus = self.mouse_follows_focus; - - let mut window_location = None; - let mut monitor_to_focus = None; - let mut needs_workspace_loading = false; - - 'search: for (monitor_idx, monitor) in self.monitors_mut().iter_mut().enumerate() { - for (workspace_idx, workspace) in monitor.workspaces().iter().enumerate() { - if let Some(location) = workspace.location_from_exe(exe) { - window_location = Some(location); - - if monitor_idx != focused_monitor_idx { - monitor_to_focus = Some(monitor_idx); - } - - // Focus workspace if it is not already the focused one, without - // loading it so that we don't give focus to the wrong window, we will - // load it later after focusing the wanted window - let focused_ws_idx = monitor.focused_workspace_idx(); - if focused_ws_idx != workspace_idx { - monitor.last_focused_workspace = Option::from(focused_ws_idx); - monitor.focus_workspace(workspace_idx)?; - needs_workspace_loading = true; - } - - break 'search; - } - } - } - - if let Some(monitor_idx) = monitor_to_focus { - self.focus_monitor(monitor_idx)?; - } - - if let Some(location) = window_location { - match location { - WorkspaceWindowLocation::Monocle(window_idx) => { - self.focus_container_window(window_idx)?; - } - WorkspaceWindowLocation::Maximized => { - if let Some(window) = - &mut self.focused_workspace_mut()?.maximized_window - { - window.focus(mouse_follows_focus)?; - } - } - WorkspaceWindowLocation::Container(container_idx, window_idx) => { - let focused_container_idx = self.focused_container_idx()?; - if container_idx != focused_container_idx { - self.focused_workspace_mut()?.focus_container(container_idx); - } - - self.focus_container_window(window_idx)?; - } - WorkspaceWindowLocation::Floating(window_idx) => { - if let Some(window) = self - .focused_workspace_mut()? - .floating_windows_mut() - .get_mut(window_idx) - { - window.focus(mouse_follows_focus)?; - } - } - } - - if needs_workspace_loading { - let mouse_follows_focus = self.mouse_follows_focus; - if let Some(monitor) = self.focused_monitor_mut() { - monitor.load_focused_workspace(mouse_follows_focus)?; - } - } - } - } + SocketMessage::EagerFocus(ref exe) => self.eager_focus(exe)?, SocketMessage::FocusWindow(direction) => { let focused_workspace = self.focused_workspace()?; match focused_workspace.layer { @@ -1095,96 +1189,7 @@ impl WindowManager { SocketMessage::ToggleMonocle => self.toggle_monocle()?, SocketMessage::ToggleMaximize => self.toggle_maximize()?, SocketMessage::ToggleFloat => self.toggle_float(false)?, - SocketMessage::ToggleWorkspaceLayer => { - let mouse_follows_focus = self.mouse_follows_focus; - 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 = w.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); - } 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()?; - } - } - 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); - window.raise()?; - } - - for window in workspace.floating_windows_mut() { - window.hide(); - } - } 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); - } - - 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 = w.rect().unwrap_or_default(); - rect.right * rect.bottom - }); - - for window in window_idx_pairs { - window.hide(); - } - } - } - }; - - if let Some(window) = to_focus { - window.focus(mouse_follows_focus)?; - } - } + SocketMessage::ToggleWorkspaceLayer => self.toggle_workspace_layer()?, SocketMessage::ResizeWindowEdge(direction, sizing) => { self.resize_window(direction, sizing, self.resize_delta, true)?; }