Skip to content

Commit

Permalink
feat(cli): add close-workspace cmd
Browse files Browse the repository at this point in the history
This commit introduces a new komorebic command, close-workspace. This
command will remove the focused workspace from the window manager state
if the following conditions are met:

1. The number of workspaces on the focused monitor are >1
2. The workspace is empty
3. The workspace is unnamed

The third condition is to ensure that we are not removing workspaces
which have been declared in the static configuration file.
  • Loading branch information
LGUG2Z committed Nov 4, 2024
1 parent dc1eb8f commit 0f022d4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions komorebi/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub enum SocketMessage {
CycleFocusWorkspace(CycleDirection),
FocusMonitorNumber(usize),
FocusLastWorkspace,
CloseWorkspace,
FocusWorkspaceNumber(usize),
FocusWorkspaceNumbers(usize),
FocusMonitorWorkspaceNumber(usize, usize),
Expand Down
38 changes: 38 additions & 0 deletions komorebi/src/process_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,44 @@ impl WindowManager {

self.focus_workspace(workspace_idx)?;
}
SocketMessage::CloseWorkspace => {
// This is to ensure that even on an empty workspace on a secondary monitor, the
// secondary monitor where the cursor is focused will be used as the target for
// the workspace switch op
if let Some(monitor_idx) = self.monitor_idx_from_current_pos() {
self.focus_monitor(monitor_idx)?;
}

let mut can_close = false;

if let Some(monitor) = self.focused_monitor_mut() {
let focused_workspace_idx = monitor.focused_workspace_idx();
let last_focused_workspace = monitor
.last_focused_workspace()
.unwrap_or(focused_workspace_idx.saturating_sub(1));

if let Some(workspace) = monitor.focused_workspace() {
if monitor.workspaces().len() > 1
&& workspace.containers().is_empty()
&& workspace.floating_windows().is_empty()
&& workspace.monocle_container().is_none()
&& workspace.maximized_window().is_none()
&& workspace.name().is_none()
{
can_close = true;
}
}

if can_close
&& monitor
.workspaces_mut()
.remove(focused_workspace_idx)
.is_some()
{
self.focus_workspace(last_focused_workspace)?;
}
}
}
SocketMessage::FocusLastWorkspace => {
// This is to ensure that even on an empty workspace on a secondary monitor, the
// secondary monitor where the cursor is focused will be used as the target for
Expand Down
5 changes: 5 additions & 0 deletions komorebic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,8 @@ enum SubCommand {
/// Focus the specified workspace
#[clap(arg_required_else_help = true)]
FocusNamedWorkspace(FocusNamedWorkspace),
/// Close the focused workspace (must be empty and unnamed)
CloseWorkspace,
/// Focus the monitor in the given cycle direction
#[clap(arg_required_else_help = true)]
CycleMonitor(CycleMonitor),
Expand Down Expand Up @@ -2338,6 +2340,9 @@ Stop-Process -Name:komorebi -ErrorAction SilentlyContinue
SubCommand::FocusNamedWorkspace(arg) => {
send_message(&SocketMessage::FocusNamedWorkspace(arg.workspace))?;
}
SubCommand::CloseWorkspace => {
send_message(&SocketMessage::CloseWorkspace)?;
}
SubCommand::CycleMonitor(arg) => {
send_message(&SocketMessage::CycleFocusMonitor(arg.cycle_direction))?;
}
Expand Down

0 comments on commit 0f022d4

Please sign in to comment.