Skip to content

Commit

Permalink
feat: focus next/prev workspace by config order; add `—next-active-wo…
Browse files Browse the repository at this point in the history
…rkspace` and `—prev-active-workspace` (#783)
  • Loading branch information
DreamMaoMao authored Oct 14, 2024
1 parent cc4952e commit 8eb71b8
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 14 deletions.
38 changes: 38 additions & 0 deletions packages/wm/src/app_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ impl InvokeCommand {
focus_monitor(*monitor_index, state, config)?;
}

if args.next_active_workspace {
focus_workspace(WorkspaceTarget::NextActive, state, config)?;
}

if args.prev_active_workspace {
focus_workspace(WorkspaceTarget::PreviousActive, state, config)?;
}

if args.next_workspace {
focus_workspace(WorkspaceTarget::Next, state, config)?;
}
Expand Down Expand Up @@ -358,6 +366,24 @@ impl InvokeCommand {
)?;
}

if args.next_active_workspace {
move_window_to_workspace(
window.clone(),
WorkspaceTarget::NextActive,
state,
config,
)?;
}

if args.prev_active_workspace {
move_window_to_workspace(
window.clone(),
WorkspaceTarget::PreviousActive,
state,
config,
)?;
}

if args.next_workspace {
move_window_to_workspace(
window.clone(),
Expand Down Expand Up @@ -723,6 +749,12 @@ pub struct InvokeFocusCommand {
#[clap(long)]
monitor: Option<usize>,

#[clap(long)]
next_active_workspace: bool,

#[clap(long)]
prev_active_workspace: bool,

#[clap(long)]
next_workspace: bool,

Expand All @@ -744,6 +776,12 @@ pub struct InvokeMoveCommand {
#[clap(long)]
workspace: Option<String>,

#[clap(long)]
next_active_workspace: bool,

#[clap(long)]
prev_active_workspace: bool,

#[clap(long)]
next_workspace: bool,

Expand Down
65 changes: 51 additions & 14 deletions packages/wm/src/wm_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,38 +302,75 @@ impl WmState {
.as_ref()
.and_then(|name| self.workspace_by_name(&name)),
),
WorkspaceTarget::Next => {
let workspaces = self.sorted_workspaces(config);
let origin_index = workspaces
WorkspaceTarget::NextActive => {
let active_workspaces = self.sorted_workspaces(config);
let origin_index = active_workspaces
.iter()
.position(|workspace| workspace.id() == origin_workspace.id())
.context("Failed to get index of given workspace.")?;

let next_workspace = workspaces
let next_active_workspace = active_workspaces
.get(origin_index + 1)
.or_else(|| workspaces.first());
.or_else(|| active_workspaces.first());

(
next_workspace.map(|workspace| workspace.config().name),
next_workspace.cloned(),
next_active_workspace.map(|workspace| workspace.config().name),
next_active_workspace.cloned(),
)
}
WorkspaceTarget::Previous => {
let workspaces = self.sorted_workspaces(config);
let origin_index = workspaces
WorkspaceTarget::PreviousActive => {
let active_workspaces = self.sorted_workspaces(config);
let origin_index = active_workspaces
.iter()
.position(|workspace| workspace.id() == origin_workspace.id())
.context("Failed to get index of given workspace.")?;

let prev_workspace = workspaces.get(
origin_index.checked_sub(1).unwrap_or(workspaces.len() - 1),
let prev_active_workspace = active_workspaces.get(
origin_index.checked_sub(1).unwrap_or(active_workspaces.len() - 1),
);

(
prev_workspace.map(|workspace| workspace.config().name),
prev_workspace.cloned(),
prev_active_workspace.map(|workspace| workspace.config().name),
prev_active_workspace.cloned(),
)
}
WorkspaceTarget::Next => {
let workspaces = &config.value.workspaces;
let origin_name = origin_workspace.config().name.clone();
let origin_index = workspaces.iter()
.position(|workspace| workspace.name == origin_name)
.context("Failed to get index of given workspace.")?;

let next_workspace_config = workspaces
.get(origin_index + 1)
.or_else(|| workspaces.first());

let next_workspace_name = next_workspace_config.map(|config| config.name.clone());
let next_workspace = next_workspace_name
.as_ref()
.and_then(|name| self.workspace_by_name(name));

(next_workspace_name, next_workspace)
}
WorkspaceTarget::Previous => {
let workspaces = &config.value.workspaces;
let origin_name = origin_workspace.config().name.clone();
let origin_index = workspaces.iter()
.position(|workspace| workspace.name == origin_name)
.context("Failed to get index of given workspace.")?;

let previous_workspace_config = workspaces.get(
origin_index.checked_sub(1).unwrap_or(workspaces.len() - 1),
);

let previous_workspace_name = previous_workspace_config.map(|config| config.name.clone());
let previous_workspace = previous_workspace_name
.as_ref()
.and_then(|name| self.workspace_by_name(name));

(previous_workspace_name, previous_workspace)
}

WorkspaceTarget::Direction(direction) => {
let origin_monitor =
origin_workspace.monitor().context("No focused monitor.")?;
Expand Down
2 changes: 2 additions & 0 deletions packages/wm/src/workspaces/workspace_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::common::Direction;
pub enum WorkspaceTarget {
Name(String),
Recent,
NextActive,
PreviousActive,
Next,
Previous,
Direction(Direction),
Expand Down

0 comments on commit 8eb71b8

Please sign in to comment.