Skip to content

Commit

Permalink
feat: Focus command to focus on specific monitor by index
Browse files Browse the repository at this point in the history
  • Loading branch information
hiszd committed Aug 20, 2024
1 parent 05116c9 commit 7a0e35e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/wm/src/app_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
traits::CommonGetters,
Container,
},
monitors::commands::focus_monitor,
user_config::{FloatingStateConfig, FullscreenStateConfig, UserConfig},
windows::{
commands::{
Expand Down Expand Up @@ -302,6 +303,10 @@ impl InvokeCommand {
)?;
}

if let Some(monitor) = &args.monitor {
focus_monitor(monitor, state, config)?;
}

if args.next_workspace {
focus_workspace(WorkspaceTarget::Next, state, config)?;
}
Expand Down Expand Up @@ -646,6 +651,9 @@ pub struct InvokeFocusCommand {
#[clap(long)]
workspace: Option<String>,

#[clap(long)]
monitor: Option<usize>,

#[clap(long)]
next_workspace: bool,

Expand Down
39 changes: 39 additions & 0 deletions packages/wm/src/monitors/commands/focus_monitor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::{
containers::traits::CommonGetters,
user_config::UserConfig,
wm_state::WmState,
workspaces::{commands::focus_workspace, WorkspaceTarget},
};

/// Focuses a monitor by a given monitor index.
pub fn focus_monitor(
target: &usize,
state: &mut WmState,
config: &UserConfig,
) -> anyhow::Result<()> {
let monitors = state.monitors();

let target_monitor = monitors.get(target.clone());

// if there are fewer monitors than the index provided error out and bail
// early from function
if target_monitor.is_none() {
anyhow::bail!("target index greater than number of monitors");
}

let target_monitor = target_monitor.unwrap();

if target_monitor.has_focus(None) {
return Ok(());
}

let displayed_workspace = target_monitor.displayed_workspace().unwrap();

focus_workspace(
WorkspaceTarget::Name(displayed_workspace.config().name),
state,
config,
)?;

Ok(())
}
2 changes: 2 additions & 0 deletions packages/wm/src/monitors/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

0 comments on commit 7a0e35e

Please sign in to comment.