From 8c56db05d0d278f8430618ec350868d1ec4ab8c3 Mon Sep 17 00:00:00 2001 From: DreamMaoMao <2523610504@qq.com> Date: Thu, 10 Oct 2024 10:46:07 +0800 Subject: [PATCH 1/5] feat: support switch workspace in the order specified in the configuration --- packages/wm/src/app_command.rs | 38 +++++++++++++++++ packages/wm/src/wm_state.rs | 41 +++++++++++++++++++ .../wm/src/workspaces/workspace_target.rs | 2 + 3 files changed, 81 insertions(+) diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs index 3a93dde6c..af33f7424 100644 --- a/packages/wm/src/app_command.rs +++ b/packages/wm/src/app_command.rs @@ -325,6 +325,14 @@ impl InvokeCommand { focus_workspace(WorkspaceTarget::Previous, state, config)?; } + if args.next_order_workspace { + focus_workspace(WorkspaceTarget::NextOrder, state, config)?; + } + + if args.prev_order_workspace { + focus_workspace(WorkspaceTarget::PreviousOrder, state, config)?; + } + if args.recent_workspace { focus_workspace(WorkspaceTarget::Recent, state, config)?; } @@ -376,6 +384,24 @@ impl InvokeCommand { )?; } + if args.next_order_workspace { + move_window_to_workspace( + window.clone(), + WorkspaceTarget::NextOrder, + state, + config, + )?; + } + + if args.prev_order_workspace { + move_window_to_workspace( + window.clone(), + WorkspaceTarget::PreviousOrder, + state, + config, + )?; + } + if args.recent_workspace { move_window_to_workspace( window, @@ -729,6 +755,12 @@ pub struct InvokeFocusCommand { #[clap(long)] prev_workspace: bool, + #[clap(long)] + next_order_workspace: bool, + + #[clap(long)] + prev_order_workspace: bool, + #[clap(long)] recent_workspace: bool, } @@ -750,6 +782,12 @@ pub struct InvokeMoveCommand { #[clap(long)] prev_workspace: bool, + #[clap(long)] + next_order_workspace: bool, + + #[clap(long)] + prev_order_workspace: bool, + #[clap(long)] recent_workspace: bool, } diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs index 16e828591..0da98abab 100644 --- a/packages/wm/src/wm_state.rs +++ b/packages/wm/src/wm_state.rs @@ -334,6 +334,47 @@ impl WmState { prev_workspace.cloned(), ) } + WorkspaceTarget::PreviousOrder => { + let workspaces = &config.value.workspaces; + let origin_name = origin_workspace.config().name.clone(); + let origin_index = workspaces.iter() + .position(|workspace| workspace.name == origin_name) + .unwrap_or_else(|| workspaces.len()); + + let previous_workspace_config = if origin_index > 0 { + workspaces.get(origin_index - 1) + } else { + workspaces.last() + }; + + 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::NextOrder => { + let workspaces = &config.value.workspaces; + let origin_name = origin_workspace.config().name.clone(); + let origin_index = workspaces.iter() + .position(|workspace| workspace.name == origin_name) + .unwrap_or_else(|| workspaces.len()); + + let next_workspace_config = if origin_index < workspaces.len() - 1 { + workspaces.get(origin_index + 1) + } 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::Direction(direction) => { let origin_monitor = origin_workspace.monitor().context("No focused monitor.")?; diff --git a/packages/wm/src/workspaces/workspace_target.rs b/packages/wm/src/workspaces/workspace_target.rs index abda82ca9..ce2644de2 100644 --- a/packages/wm/src/workspaces/workspace_target.rs +++ b/packages/wm/src/workspaces/workspace_target.rs @@ -5,5 +5,7 @@ pub enum WorkspaceTarget { Recent, Next, Previous, + NextOrder, + PreviousOrder, Direction(Direction), } From 3c2e4e391da31d047738b846a4ac9c5f8738ce84 Mon Sep 17 00:00:00 2001 From: DreamMaoMao <2523610504@qq.com> Date: Mon, 14 Oct 2024 17:18:36 +0800 Subject: [PATCH 2/5] optimize: optimize variable naming and code ordering --- packages/wm/src/app_command.rs | 48 +++++++++---------- packages/wm/src/wm_state.rs | 46 +++++++++--------- .../wm/src/workspaces/workspace_target.rs | 4 +- 3 files changed, 49 insertions(+), 49 deletions(-) diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs index af33f7424..f124c9208 100644 --- a/packages/wm/src/app_command.rs +++ b/packages/wm/src/app_command.rs @@ -317,20 +317,20 @@ impl InvokeCommand { focus_monitor(*monitor_index, state, config)?; } - if args.next_workspace { - focus_workspace(WorkspaceTarget::Next, state, config)?; + if args.next_active_workspace { + focus_workspace(WorkspaceTarget::NextActive, state, config)?; } - if args.prev_workspace { - focus_workspace(WorkspaceTarget::Previous, state, config)?; + if args.prev_active_workspace { + focus_workspace(WorkspaceTarget::PreviousActive, state, config)?; } - if args.next_order_workspace { - focus_workspace(WorkspaceTarget::NextOrder, state, config)?; + if args.next_workspace { + focus_workspace(WorkspaceTarget::Next, state, config)?; } - if args.prev_order_workspace { - focus_workspace(WorkspaceTarget::PreviousOrder, state, config)?; + if args.prev_workspace { + focus_workspace(WorkspaceTarget::Previous, state, config)?; } if args.recent_workspace { @@ -366,37 +366,37 @@ impl InvokeCommand { )?; } - if args.next_workspace { + if args.next_active_workspace { move_window_to_workspace( window.clone(), - WorkspaceTarget::Next, + WorkspaceTarget::NextActive, state, config, )?; } - if args.prev_workspace { + if args.prev_active_workspace { move_window_to_workspace( window.clone(), - WorkspaceTarget::Previous, + WorkspaceTarget::PreviousActive, state, config, )?; } - if args.next_order_workspace { + if args.next_workspace { move_window_to_workspace( window.clone(), - WorkspaceTarget::NextOrder, + WorkspaceTarget::Next, state, config, )?; } - if args.prev_order_workspace { + if args.prev_workspace { move_window_to_workspace( window.clone(), - WorkspaceTarget::PreviousOrder, + WorkspaceTarget::Previous, state, config, )?; @@ -750,16 +750,16 @@ pub struct InvokeFocusCommand { monitor: Option, #[clap(long)] - next_workspace: bool, + next_active_workspace: bool, #[clap(long)] - prev_workspace: bool, + prev_active_workspace: bool, #[clap(long)] - next_order_workspace: bool, + next_workspace: bool, #[clap(long)] - prev_order_workspace: bool, + prev_workspace: bool, #[clap(long)] recent_workspace: bool, @@ -777,16 +777,16 @@ pub struct InvokeMoveCommand { workspace: Option, #[clap(long)] - next_workspace: bool, + next_active_workspace: bool, #[clap(long)] - prev_workspace: bool, + prev_active_workspace: bool, #[clap(long)] - next_order_workspace: bool, + next_workspace: bool, #[clap(long)] - prev_order_workspace: bool, + prev_workspace: bool, #[clap(long)] recent_workspace: bool, diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs index 0da98abab..903b58ea9 100644 --- a/packages/wm/src/wm_state.rs +++ b/packages/wm/src/wm_state.rs @@ -302,7 +302,7 @@ impl WmState { .as_ref() .and_then(|name| self.workspace_by_name(&name)), ), - WorkspaceTarget::Next => { + WorkspaceTarget::NextActive => { let workspaces = self.sorted_workspaces(config); let origin_index = workspaces .iter() @@ -318,7 +318,7 @@ impl WmState { next_workspace.cloned(), ) } - WorkspaceTarget::Previous => { + WorkspaceTarget::PreviousActive => { let workspaces = self.sorted_workspaces(config); let origin_index = workspaces .iter() @@ -334,7 +334,27 @@ impl WmState { prev_workspace.cloned(), ) } - WorkspaceTarget::PreviousOrder => { + 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) + .unwrap_or_else(|| workspaces.len()); + + let next_workspace_config = if origin_index < workspaces.len() - 1 { + workspaces.get(origin_index + 1) + } 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() @@ -355,26 +375,6 @@ impl WmState { (previous_workspace_name, previous_workspace) } - WorkspaceTarget::NextOrder => { - let workspaces = &config.value.workspaces; - let origin_name = origin_workspace.config().name.clone(); - let origin_index = workspaces.iter() - .position(|workspace| workspace.name == origin_name) - .unwrap_or_else(|| workspaces.len()); - - let next_workspace_config = if origin_index < workspaces.len() - 1 { - workspaces.get(origin_index + 1) - } 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::Direction(direction) => { let origin_monitor = origin_workspace.monitor().context("No focused monitor.")?; diff --git a/packages/wm/src/workspaces/workspace_target.rs b/packages/wm/src/workspaces/workspace_target.rs index ce2644de2..41d138a9f 100644 --- a/packages/wm/src/workspaces/workspace_target.rs +++ b/packages/wm/src/workspaces/workspace_target.rs @@ -3,9 +3,9 @@ use crate::common::Direction; pub enum WorkspaceTarget { Name(String), Recent, + NextActive, + PreviousActive, Next, Previous, - NextOrder, - PreviousOrder, Direction(Direction), } From b56a6041811a7abddc566ec58e9d297211711014 Mon Sep 17 00:00:00 2001 From: DreamMaoMao <2523610504@qq.com> Date: Mon, 14 Oct 2024 17:31:23 +0800 Subject: [PATCH 3/5] optimize: use unwrap_or_else to return err --- packages/wm/src/wm_state.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs index 903b58ea9..0b302c51f 100644 --- a/packages/wm/src/wm_state.rs +++ b/packages/wm/src/wm_state.rs @@ -339,7 +339,7 @@ impl WmState { let origin_name = origin_workspace.config().name.clone(); let origin_index = workspaces.iter() .position(|workspace| workspace.name == origin_name) - .unwrap_or_else(|| workspaces.len()); + .context("Failed to get index of given workspace.")?; let next_workspace_config = if origin_index < workspaces.len() - 1 { workspaces.get(origin_index + 1) @@ -359,7 +359,7 @@ impl WmState { let origin_name = origin_workspace.config().name.clone(); let origin_index = workspaces.iter() .position(|workspace| workspace.name == origin_name) - .unwrap_or_else(|| workspaces.len()); + .context("Failed to get index of given workspace.")?; let previous_workspace_config = if origin_index > 0 { workspaces.get(origin_index - 1) From 375f30dfabe9f5145c6eb3dc448c6fc96cfdae93 Mon Sep 17 00:00:00 2001 From: DreamMaoMao <2523610504@qq.com> Date: Mon, 14 Oct 2024 17:39:43 +0800 Subject: [PATCH 4/5] optimize: simplify the logic of finding the next/previous workspace --- packages/wm/src/wm_state.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs index 0b302c51f..af3171903 100644 --- a/packages/wm/src/wm_state.rs +++ b/packages/wm/src/wm_state.rs @@ -341,11 +341,9 @@ impl WmState { .position(|workspace| workspace.name == origin_name) .context("Failed to get index of given workspace.")?; - let next_workspace_config = if origin_index < workspaces.len() - 1 { - workspaces.get(origin_index + 1) - } else { - workspaces.first() - }; + 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 @@ -361,11 +359,9 @@ impl WmState { .position(|workspace| workspace.name == origin_name) .context("Failed to get index of given workspace.")?; - let previous_workspace_config = if origin_index > 0 { - workspaces.get(origin_index - 1) - } else { - workspaces.last() - }; + 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 From 6b9e1d81f61a71368446ea62b704294051e9ecea Mon Sep 17 00:00:00 2001 From: DreamMaoMao <2523610504@qq.com> Date: Mon, 14 Oct 2024 17:43:41 +0800 Subject: [PATCH 5/5] optimize: optimize variable naming --- packages/wm/src/wm_state.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs index af3171903..88599a312 100644 --- a/packages/wm/src/wm_state.rs +++ b/packages/wm/src/wm_state.rs @@ -303,35 +303,35 @@ impl WmState { .and_then(|name| self.workspace_by_name(&name)), ), WorkspaceTarget::NextActive => { - let workspaces = self.sorted_workspaces(config); - let origin_index = workspaces + 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::PreviousActive => { - let workspaces = self.sorted_workspaces(config); - let origin_index = workspaces + 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 => {