From 8c939328d120713db7a7412f01fb2929d107ee37 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 30 Jul 2021 12:06:29 -0700 Subject: [PATCH] feat(wm): ensure workspace count Allow the number of workspaces for a given monitor to be pre-created, so that configuration options can be sent (name, padding, layout) before the workspace has ever been activated. --- komorebi-core/src/lib.rs | 3 ++- komorebi/src/monitor.rs | 7 +++++++ komorebi/src/process_command.rs | 5 ++++- komorebi/src/window_manager.rs | 15 +++++++++++++++ komorebic/src/main.rs | 25 ++++++++++++++++++++----- 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/komorebi-core/src/lib.rs b/komorebi-core/src/lib.rs index 5019603a..0b83b28c 100644 --- a/komorebi-core/src/lib.rs +++ b/komorebi-core/src/lib.rs @@ -37,6 +37,7 @@ pub enum SocketMessage { ChangeLayout(Layout), FlipLayout(LayoutFlip), // Monitor and Workspace Commands + EnsureWorkspaces(usize, usize), Stop, TogglePause, Retile, @@ -45,7 +46,7 @@ pub enum SocketMessage { ContainerPadding(usize, usize, i32), WorkspacePadding(usize, usize, i32), WorkspaceName(usize, usize, String), - SetLayout(usize, usize, Layout), + WorkspaceLayout(usize, usize, Layout), // Configuration FloatClass(String), FloatExe(String), diff --git a/komorebi/src/monitor.rs b/komorebi/src/monitor.rs index b26f844b..c501f321 100644 --- a/komorebi/src/monitor.rs +++ b/komorebi/src/monitor.rs @@ -53,6 +53,13 @@ impl Monitor { Ok(()) } + pub fn ensure_workspace_count(&mut self, ensure_count: usize) { + if self.workspaces().len() < ensure_count { + self.workspaces_mut() + .resize(ensure_count, Workspace::default()); + } + } + pub fn move_container_to_workspace( &mut self, target_workspace_idx: usize, diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 660d6c1e..4959e9e4 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -125,7 +125,7 @@ impl WindowManager { } SocketMessage::FlipLayout(layout_flip) => self.flip_layout(layout_flip)?, SocketMessage::ChangeLayout(layout) => self.change_workspace_layout(layout)?, - SocketMessage::SetLayout(monitor_idx, workspace_idx, layout) => { + SocketMessage::WorkspaceLayout(monitor_idx, workspace_idx, layout) => { self.set_workspace_layout(monitor_idx, workspace_idx, layout)?; } SocketMessage::FocusWorkspaceNumber(workspace_idx) => { @@ -136,6 +136,9 @@ impl WindowManager { self.restore_all_windows(); std::process::exit(0) } + SocketMessage::EnsureWorkspaces(monitor_idx, workspace_count) => { + self.ensure_workspaces_for_monitor(monitor_idx, workspace_count)?; + } SocketMessage::WorkspaceName(monitor_idx, workspace_idx, name) => { self.set_workspace_name(monitor_idx, workspace_idx, name)?; } diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 8cbe423e..573f54c4 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -387,6 +387,21 @@ impl WindowManager { } } + pub fn ensure_workspaces_for_monitor( + &mut self, + monitor_idx: usize, + workspace_count: usize, + ) -> Result<()> { + let monitor = self + .monitors_mut() + .get_mut(monitor_idx) + .context("there is no monitor")?; + + monitor.ensure_workspace_count(workspace_count); + + Ok(()) + } + pub fn set_workspace_padding( &mut self, monitor_idx: usize, diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 95d73f6a..07f4c9c2 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -30,9 +30,11 @@ enum SubCommand { FocusMonitor(Target), FocusWorkspace(Target), Promote, + EnsureWorkspaces(WorkspaceCountForMonitor), Retile, ContainerPadding(SizeForMonitorWorkspace), WorkspacePadding(SizeForMonitorWorkspace), + WorkspaceLayout(LayoutForMonitorWorkspace), WorkspaceName(NameForMonitorWorkspace), ToggleFloat, TogglePause, @@ -45,7 +47,12 @@ enum SubCommand { AdjustContainerPadding(SizingAdjustment), AdjustWorkspacePadding(SizingAdjustment), FlipLayout(LayoutFlip), - Layout(LayoutForMonitorWorkspace), +} + +#[derive(Clap)] +struct WorkspaceCountForMonitor { + monitor: usize, + workspace_count: usize, } #[derive(Clap)] @@ -174,10 +181,11 @@ fn main() -> Result<()> { let bytes = SocketMessage::ToggleMonocle.as_bytes()?; send_message(&*bytes); } - SubCommand::Layout(layout) => { - let bytes = SocketMessage::SetLayout(layout.monitor, layout.workspace, layout.layout) - .as_bytes() - .unwrap(); + SubCommand::WorkspaceLayout(layout) => { + let bytes = + SocketMessage::WorkspaceLayout(layout.monitor, layout.workspace, layout.layout) + .as_bytes() + .unwrap(); send_message(&*bytes); } SubCommand::Start => { @@ -241,6 +249,13 @@ fn main() -> Result<()> { .unwrap(); send_message(&*bytes); } + SubCommand::EnsureWorkspaces(workspaces) => { + let bytes = + SocketMessage::EnsureWorkspaces(workspaces.monitor, workspaces.workspace_count) + .as_bytes() + .unwrap(); + send_message(&*bytes); + } } Ok(())