diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs index aacba1ea..4ed2fc35 100644 --- a/packages/wm/src/app_command.rs +++ b/packages/wm/src/app_command.rs @@ -19,6 +19,7 @@ use crate::{ traits::CommonGetters, Container, }, + monitors::commands::focus_monitor, user_config::{FloatingStateConfig, FullscreenStateConfig, UserConfig}, windows::{ commands::{ @@ -302,6 +303,10 @@ impl InvokeCommand { )?; } + if let Some(monitor_index) = &args.monitor { + focus_monitor(*monitor_index, state, config)?; + } + if args.next_workspace { focus_workspace(WorkspaceTarget::Next, state, config)?; } @@ -646,6 +651,9 @@ pub struct InvokeFocusCommand { #[clap(long)] workspace: Option, + #[clap(long)] + monitor: Option, + #[clap(long)] next_workspace: bool, diff --git a/packages/wm/src/monitors/commands/focus_monitor.rs b/packages/wm/src/monitors/commands/focus_monitor.rs new file mode 100644 index 00000000..53838603 --- /dev/null +++ b/packages/wm/src/monitors/commands/focus_monitor.rs @@ -0,0 +1,27 @@ +use anyhow::Context; + +use crate::{ + user_config::UserConfig, + wm_state::WmState, + workspaces::{commands::focus_workspace, WorkspaceTarget}, +}; + +/// Focuses a monitor by a given monitor index. +pub fn focus_monitor( + monitor_index: usize, + state: &mut WmState, + config: &UserConfig, +) -> anyhow::Result<()> { + let monitors = state.monitors(); + + let target_monitor = monitors.get(monitor_index).with_context(|| { + format!("Monitor at index {} was not found.", monitor_index) + })?; + + let workspace_name = target_monitor + .displayed_workspace() + .map(|workspace| workspace.config().name) + .context("Failed to get target workspace name.")?; + + focus_workspace(WorkspaceTarget::Name(workspace_name), state, config) +} diff --git a/packages/wm/src/monitors/commands/mod.rs b/packages/wm/src/monitors/commands/mod.rs index 63b053f1..9a01ac6a 100644 --- a/packages/wm/src/monitors/commands/mod.rs +++ b/packages/wm/src/monitors/commands/mod.rs @@ -2,8 +2,10 @@ mod add_monitor; mod remove_monitor; mod sort_monitors; mod update_monitor; +mod focus_monitor; pub use add_monitor::*; pub use remove_monitor::*; pub use sort_monitors::*; pub use update_monitor::*; +pub use focus_monitor::*;