mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-16 13:51:19 +01:00
feat(wm): add basic workspace switching support
This commit migrates the code needed from komorebi to implement basic workspace support. The FocusWorkspaceNumber, MoveContainerToWorkspaceNumber and SendContainerToWorkspaceNumber SocketMessage variants have been implemented in process_command, and the commands to emit those SocketMessage variants have been added to komorebic. I took a stab at improving the code for hiding the windows by moving them as off-screen as possible by taking inspiration of Aerospace's approach of trying to leave only 1px of them visible. Right now this always looks to put the hidden windows off-screen on the left side of the screen.
This commit is contained in:
1 parent
92701d902d
commit
061d7e3e73
11 files changed
+382
-14
No files matched your search
@@ -6,7 +6,7 @@ use color_eyre::eyre;
|
||||
|
||||
impl_ring_elements!(Container, Window);
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct Container {
|
||||
pub windows: Ring<Window>,
|
||||
pub locked: bool,
|
||||
@@ -91,4 +91,31 @@ impl Container {
|
||||
self.focus_window(idx.saturating_sub(1));
|
||||
window
|
||||
}
|
||||
|
||||
pub fn hide(&mut self, omit: Option<u32>) -> eyre::Result<()> {
|
||||
for window in self.windows_mut().iter_mut().rev() {
|
||||
let mut should_hide = omit.is_none();
|
||||
|
||||
if !should_hide
|
||||
&& let Some(omit) = omit
|
||||
&& omit != window.id
|
||||
{
|
||||
should_hide = true
|
||||
}
|
||||
|
||||
if should_hide {
|
||||
window.hide()?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn restore(&mut self) -> eyre::Result<()> {
|
||||
if let Some(window) = self.focused_window_mut() {
|
||||
window.restore()?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,9 @@ pub enum SocketMessage {
|
||||
UnstackWindow,
|
||||
ChangeLayout(DefaultLayout),
|
||||
TogglePause,
|
||||
FocusWorkspaceNumber(usize),
|
||||
MoveContainerToWorkspaceNumber(usize),
|
||||
SendContainerToWorkspaceNumber(usize),
|
||||
}
|
||||
|
||||
impl SocketMessage {
|
||||
|
||||
@@ -5,6 +5,7 @@ use objc2_core_foundation::CGFloat;
|
||||
use objc2_core_foundation::CGPoint;
|
||||
use objc2_core_foundation::CGRect;
|
||||
use objc2_core_graphics::CGDisplayBounds;
|
||||
use objc2_core_graphics::CGGetDisplaysWithPoint;
|
||||
use objc2_core_graphics::CGGetOnlineDisplayList;
|
||||
use objc2_core_graphics::CGRectIntersectsRect;
|
||||
use objc2_core_graphics::CGWarpMouseCursorPosition;
|
||||
@@ -56,6 +57,32 @@ impl CoreGraphicsApi {
|
||||
unsafe { CGDisplayBounds(display_id) }
|
||||
}
|
||||
|
||||
pub fn display_bounds_for_window_rect(window_rect: CGRect) -> Option<CGRect> {
|
||||
let mut displays: Vec<u32> = Vec::with_capacity(1);
|
||||
let mut display_count = 0;
|
||||
|
||||
unsafe {
|
||||
match CoreGraphicsError::from(CGGetDisplaysWithPoint(
|
||||
window_rect.origin,
|
||||
displays.capacity() as u32,
|
||||
displays.as_mut_ptr(),
|
||||
&mut display_count,
|
||||
)) {
|
||||
CoreGraphicsError::Success => {
|
||||
displays.set_len(display_count as usize);
|
||||
}
|
||||
error => {
|
||||
tracing::error!("failed to find display for point: {error}");
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
displays
|
||||
.first()
|
||||
.map(|display| CoreGraphicsApi::display_bounds(*display))
|
||||
}
|
||||
|
||||
pub fn window_list_info() -> Option<CFRetained<CFArray>> {
|
||||
unsafe {
|
||||
CGWindowListCopyWindowInfo(
|
||||
|
||||
@@ -11,6 +11,9 @@ use objc2_core_foundation::CFDictionary;
|
||||
use objc2_core_foundation::CFRetained;
|
||||
use objc2_core_foundation::CFRunLoop;
|
||||
use objc2_core_foundation::CFString;
|
||||
use objc2_core_foundation::CGPoint;
|
||||
use objc2_core_foundation::CGRect;
|
||||
use objc2_core_foundation::CGSize;
|
||||
use std::ops::Deref;
|
||||
use std::path::PathBuf;
|
||||
use std::ptr::NonNull;
|
||||
@@ -113,3 +116,11 @@ pub enum LibraryError {
|
||||
#[error(transparent)]
|
||||
CoreGraphics(#[from] CoreGraphicsError),
|
||||
}
|
||||
|
||||
pub fn hidden_frame_bottom_left(screen_frame: CGRect, window_size: CGSize) -> CGRect {
|
||||
let visible_sliver: f64 = 1.0;
|
||||
let origin_x = screen_frame.origin.x - (window_size.width - visible_sliver);
|
||||
let origin_y = screen_frame.origin.y - (window_size.height - visible_sliver);
|
||||
|
||||
CGRect::new(CGPoint::new(origin_x, origin_y), window_size)
|
||||
}
|
||||
@@ -125,7 +125,7 @@ impl MacosApi {
|
||||
)?)
|
||||
}
|
||||
|
||||
pub fn window_rect(element: &AXUIElement) -> Result<Rect, AccessibilityError> {
|
||||
pub fn window_rect(element: &AXUIElement) -> Result<CGRect, AccessibilityError> {
|
||||
let mut position_receiver = std::ptr::null();
|
||||
let mut size_receiver = std::ptr::null();
|
||||
|
||||
@@ -168,7 +168,7 @@ impl MacosApi {
|
||||
NonNull::from_mut(&mut rect.size).cast::<c_void>(),
|
||||
);
|
||||
|
||||
Ok(Rect::from(rect))
|
||||
Ok(rect)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
use crate::DEFAULT_CONTAINER_PADDING;
|
||||
use crate::DEFAULT_WORKSPACE_PADDING;
|
||||
use crate::container::Container;
|
||||
use crate::core::default_layout::DefaultLayout;
|
||||
use crate::core::layout::Layout;
|
||||
use crate::core::operation_direction::OperationDirection;
|
||||
use crate::core::rect::Rect;
|
||||
use crate::ring::Ring;
|
||||
use crate::workspace::Workspace;
|
||||
@@ -21,6 +25,7 @@ pub struct Monitor {
|
||||
pub window_based_work_area_offset_limit: isize,
|
||||
pub container_padding: Option<i32>,
|
||||
pub workspace_padding: Option<i32>,
|
||||
pub last_focused_workspace: Option<usize>,
|
||||
}
|
||||
|
||||
impl Monitor {
|
||||
@@ -39,6 +44,7 @@ impl Monitor {
|
||||
window_based_work_area_offset_limit: 0,
|
||||
container_padding: None,
|
||||
workspace_padding: None,
|
||||
last_focused_workspace: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,4 +91,147 @@ impl Monitor {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn focus_workspace(&mut self, idx: usize) -> eyre::Result<()> {
|
||||
tracing::info!("focusing workspace");
|
||||
|
||||
{
|
||||
let workspaces = self.workspaces_mut();
|
||||
|
||||
if workspaces.get(idx).is_none() {
|
||||
workspaces.resize(idx + 1, Workspace::default());
|
||||
}
|
||||
self.last_focused_workspace = Some(self.workspaces.focused_idx());
|
||||
self.workspaces.focus(idx);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load_focused_workspace(&mut self, mouse_follows_focus: bool) -> eyre::Result<()> {
|
||||
let focused_idx = self.focused_workspace_idx();
|
||||
for (i, workspace) in self.workspaces_mut().iter_mut().enumerate() {
|
||||
if i == focused_idx {
|
||||
workspace.restore(mouse_follows_focus)?;
|
||||
} else {
|
||||
workspace.hide(None)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn move_container_to_workspace(
|
||||
&mut self,
|
||||
target_workspace_idx: usize,
|
||||
follow: bool,
|
||||
direction: Option<OperationDirection>,
|
||||
) -> eyre::Result<()> {
|
||||
let workspace = self
|
||||
.focused_workspace_mut()
|
||||
.ok_or_else(|| eyre!("there is no workspace"))?;
|
||||
|
||||
let container = workspace
|
||||
.remove_focused_container()
|
||||
.ok_or_else(|| eyre!("there is no container"))?;
|
||||
|
||||
let workspaces = self.workspaces_mut();
|
||||
|
||||
#[allow(clippy::option_if_let_else)]
|
||||
let target_workspace = match workspaces.get_mut(target_workspace_idx) {
|
||||
None => {
|
||||
workspaces.resize(target_workspace_idx + 1, Workspace::default());
|
||||
workspaces.get_mut(target_workspace_idx).unwrap()
|
||||
}
|
||||
Some(workspace) => workspace,
|
||||
};
|
||||
|
||||
if let Some(direction) = direction {
|
||||
self.add_container_with_direction(container, Some(target_workspace_idx), direction)?;
|
||||
} else {
|
||||
target_workspace.add_container_to_back(container);
|
||||
}
|
||||
|
||||
if follow {
|
||||
self.focus_workspace(target_workspace_idx)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Adds a container to this `Monitor` using the move direction to calculate if the container
|
||||
/// should be added in front of all containers, in the back or in place of the focused
|
||||
/// container, moving the rest along. The move direction should be from the origin monitor
|
||||
/// towards the target monitor or from the origin workspace towards the target workspace.
|
||||
pub fn add_container_with_direction(
|
||||
&mut self,
|
||||
container: Container,
|
||||
workspace_idx: Option<usize>,
|
||||
direction: OperationDirection,
|
||||
) -> eyre::Result<()> {
|
||||
let workspace = if let Some(idx) = workspace_idx {
|
||||
self.workspaces_mut()
|
||||
.get_mut(idx)
|
||||
.ok_or_else(|| eyre!("there is no workspace at index {}", idx))?
|
||||
} else {
|
||||
self.focused_workspace_mut()
|
||||
.ok_or_else(|| eyre!("there is no workspace"))?
|
||||
};
|
||||
|
||||
match direction {
|
||||
OperationDirection::Left => {
|
||||
// insert the container into the workspace on the monitor at the back (or rightmost position)
|
||||
// if we are moving across a boundary to the left (back = right side of the target)
|
||||
match workspace.layout {
|
||||
Layout::Default(layout) => match layout {
|
||||
DefaultLayout::RightMainVerticalStack => {
|
||||
workspace.add_container_to_front(container);
|
||||
}
|
||||
DefaultLayout::UltrawideVerticalStack => {
|
||||
if workspace.containers().len() == 1 {
|
||||
workspace.insert_container_at_idx(0, container);
|
||||
} else {
|
||||
workspace.add_container_to_back(container);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
workspace.add_container_to_back(container);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
OperationDirection::Right => {
|
||||
// insert the container into the workspace on the monitor at the front (or leftmost position)
|
||||
// if we are moving across a boundary to the right (front = left side of the target)
|
||||
match workspace.layout {
|
||||
Layout::Default(layout) => {
|
||||
let target_index = layout.leftmost_index(workspace.containers().len());
|
||||
|
||||
match layout {
|
||||
DefaultLayout::RightMainVerticalStack
|
||||
| DefaultLayout::UltrawideVerticalStack => {
|
||||
if workspace.containers().len() == 1 {
|
||||
workspace.add_container_to_back(container);
|
||||
} else {
|
||||
workspace.insert_container_at_idx(target_index, container);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
workspace.insert_container_at_idx(target_index, container);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
OperationDirection::Up | OperationDirection::Down => {
|
||||
// insert the container into the workspace on the monitor at the position
|
||||
// where the currently focused container on that workspace is
|
||||
workspace.insert_container_at_idx(workspace.focused_container_idx(), container);
|
||||
}
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -79,6 +79,17 @@ impl WindowManager {
|
||||
|
||||
self.is_paused = !self.is_paused;
|
||||
}
|
||||
SocketMessage::FocusWorkspaceNumber(workspace_idx) => {
|
||||
if self.focused_workspace_idx().unwrap_or_default() != workspace_idx {
|
||||
self.focus_workspace(workspace_idx)?;
|
||||
}
|
||||
}
|
||||
SocketMessage::MoveContainerToWorkspaceNumber(workspace_idx) => {
|
||||
self.move_container_to_workspace(workspace_idx, true, None)?;
|
||||
}
|
||||
SocketMessage::SendContainerToWorkspaceNumber(workspace_idx) => {
|
||||
self.move_container_to_workspace(workspace_idx, false, None)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
+16
-10
@@ -19,6 +19,8 @@ use crate::application::Application;
|
||||
use crate::ax_event_listener::event_tx;
|
||||
use crate::cf_dictionary_value;
|
||||
use crate::core::rect::Rect;
|
||||
use crate::core_graphics::CoreGraphicsApi;
|
||||
use crate::hidden_frame_bottom_left;
|
||||
use crate::macos_api::MacosApi;
|
||||
use crate::window_manager_event::WindowManagerEvent;
|
||||
use objc2_app_kit::NSApplicationActivationOptions;
|
||||
@@ -206,20 +208,24 @@ impl Window {
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
pub fn hide(&mut self) -> Result<(), AccessibilityError> {
|
||||
// I don't love this, but it's basically what Aerospace does in lieu of an actual "Hide" API
|
||||
if self.restore_point.is_none() {
|
||||
let rect = MacosApi::window_rect(&self.element)?;
|
||||
self.restore_point = Some((rect.left as f32, rect.top as f32));
|
||||
if let Some(monitor_size) = CoreGraphicsApi::display_bounds_for_window_rect(rect) {
|
||||
self.restore_point = Some((rect.origin.x as f32, rect.origin.y as f32));
|
||||
let hidden_rect = hidden_frame_bottom_left(monitor_size, rect.size);
|
||||
|
||||
tracing::debug!(
|
||||
"hiding {} and setting restore point to {},{}",
|
||||
self.title()
|
||||
.unwrap_or_else(|| String::from("<NO TITLE FOUND>")),
|
||||
rect.left,
|
||||
rect.top,
|
||||
);
|
||||
tracing::debug!(
|
||||
"hiding {} and setting restore point to {},{}",
|
||||
self.title()
|
||||
.unwrap_or_else(|| String::from("<NO TITLE FOUND>")),
|
||||
rect.origin.x,
|
||||
rect.origin.y,
|
||||
);
|
||||
|
||||
// todo: this doesn't really do what I want
|
||||
self.set_point(CGPoint::new(-5000.0 as CGFloat, rect.top as CGFloat))?;
|
||||
self.set_point(hidden_rect.origin)?;
|
||||
self.set_size(hidden_rect.size)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -418,4 +418,40 @@ impl WindowManager {
|
||||
|
||||
self.update_focused_workspace(mouse_follows_focus, true)
|
||||
}
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn focus_workspace(&mut self, idx: usize) -> eyre::Result<()> {
|
||||
tracing::info!("focusing workspace");
|
||||
|
||||
let mouse_follows_focus = self.mouse_follows_focus;
|
||||
let monitor = self
|
||||
.focused_monitor_mut()
|
||||
.ok_or_else(|| eyre!("there is no workspace"))?;
|
||||
|
||||
monitor.focus_workspace(idx)?;
|
||||
monitor.load_focused_workspace(mouse_follows_focus)?;
|
||||
|
||||
self.update_focused_workspace(false, true)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn move_container_to_workspace(
|
||||
&mut self,
|
||||
idx: usize,
|
||||
follow: bool,
|
||||
direction: Option<OperationDirection>,
|
||||
) -> eyre::Result<()> {
|
||||
tracing::info!("moving container");
|
||||
|
||||
let mouse_follows_focus = self.mouse_follows_focus;
|
||||
let monitor = self
|
||||
.focused_monitor_mut()
|
||||
.ok_or_else(|| eyre!("there is no monitor"))?;
|
||||
|
||||
monitor.move_container_to_workspace(idx, follow, direction)?;
|
||||
monitor.load_focused_workspace(mouse_follows_focus)?;
|
||||
|
||||
self.update_focused_workspace(mouse_follows_focus, true)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ use std::num::NonZeroUsize;
|
||||
|
||||
impl_ring_elements!(Workspace, Container);
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Workspace {
|
||||
pub containers: Ring<Container>,
|
||||
pub workspace_padding: Option<i32>,
|
||||
@@ -376,4 +376,65 @@ impl Workspace {
|
||||
container.add_window(&window)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn hide(&mut self, omit: Option<u32>) -> eyre::Result<()> {
|
||||
for container in self.containers_mut() {
|
||||
container.hide(omit)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn restore(&mut self, mouse_follows_focus: bool) -> eyre::Result<()> {
|
||||
let idx = self.focused_container_idx();
|
||||
let mut to_focus = None;
|
||||
|
||||
for (i, container) in self.containers().iter().enumerate() {
|
||||
if let Some(window) = container.focused_window()
|
||||
&& idx == i
|
||||
{
|
||||
to_focus = Option::from(window.clone());
|
||||
}
|
||||
}
|
||||
|
||||
for container in self.containers_mut() {
|
||||
container.restore()?;
|
||||
}
|
||||
|
||||
if let Some(container) = self.focused_container_mut() {
|
||||
container.focus_window(container.focused_window_idx());
|
||||
}
|
||||
|
||||
if let Some(window) = to_focus {
|
||||
window.focus(mouse_follows_focus)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove_focused_container(&mut self) -> Option<Container> {
|
||||
let focused_idx = self.focused_container_idx();
|
||||
let container = self.remove_container_by_idx(focused_idx);
|
||||
self.focus_previous_container();
|
||||
|
||||
container
|
||||
}
|
||||
|
||||
pub fn add_container_to_back(&mut self, container: Container) {
|
||||
self.containers_mut().push_back(container);
|
||||
self.focus_last_container();
|
||||
}
|
||||
|
||||
pub fn add_container_to_front(&mut self, container: Container) {
|
||||
self.containers_mut().push_front(container);
|
||||
self.focus_first_container();
|
||||
}
|
||||
|
||||
fn focus_last_container(&mut self) {
|
||||
self.focus_container(self.containers().len().saturating_sub(1));
|
||||
}
|
||||
|
||||
fn focus_first_container(&mut self) {
|
||||
self.focus_container(0);
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,25 @@ gen_enum_subcommand_args! {
|
||||
CycleStack: CycleDirection,
|
||||
}
|
||||
|
||||
macro_rules! gen_target_subcommand_args {
|
||||
// SubCommand Pattern
|
||||
( $( $name:ident ),+ $(,)? ) => {
|
||||
$(
|
||||
#[derive(clap::Parser)]
|
||||
pub struct $name {
|
||||
/// Target index (zero-indexed)
|
||||
target: usize,
|
||||
}
|
||||
)+
|
||||
};
|
||||
}
|
||||
|
||||
gen_target_subcommand_args! {
|
||||
FocusWorkspace,
|
||||
MoveToWorkspace,
|
||||
SendToWorkspace
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
#[clap(author, about, version)]
|
||||
struct Opts {
|
||||
@@ -87,6 +106,15 @@ enum SubCommand {
|
||||
/// Set the layout on the focused workspace
|
||||
#[clap(arg_required_else_help = true)]
|
||||
ChangeLayout(ChangeLayout),
|
||||
/// Focus the specified workspace on the focused monitor
|
||||
#[clap(arg_required_else_help = true)]
|
||||
FocusWorkspace(FocusWorkspace),
|
||||
/// Move the focused window to the specified workspace
|
||||
#[clap(arg_required_else_help = true)]
|
||||
MoveToWorkspace(MoveToWorkspace),
|
||||
/// Send the focused window to the specified workspace
|
||||
#[clap(arg_required_else_help = true)]
|
||||
SendToWorkspace(SendToWorkspace),
|
||||
}
|
||||
|
||||
fn main() -> eyre::Result<()> {
|
||||
@@ -114,6 +142,15 @@ fn main() -> eyre::Result<()> {
|
||||
SubCommand::CycleStack(arg) => {
|
||||
send_message(&SocketMessage::CycleStack(arg.cycle_direction))?;
|
||||
}
|
||||
SubCommand::FocusWorkspace(arg) => {
|
||||
send_message(&SocketMessage::FocusWorkspaceNumber(arg.target))?;
|
||||
}
|
||||
SubCommand::MoveToWorkspace(arg) => {
|
||||
send_message(&SocketMessage::MoveContainerToWorkspaceNumber(arg.target))?;
|
||||
}
|
||||
SubCommand::SendToWorkspace(arg) => {
|
||||
send_message(&SocketMessage::SendContainerToWorkspaceNumber(arg.target))?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in new issue
Block a user