From 2702a69925da82aff2acc563a882a62a0f3bd3ce Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Fri, 4 Mar 2022 20:46:46 +0100 Subject: [PATCH] New command switch-to-app-or-urgent-or-lru-window --- NEWS.md | 8 ++++++++ src/cmds.rs | 30 +++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 250d659..ea2d129 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,11 @@ +swayr v0.15.0 +============= + +- There's a new command `switch-to-app-or-urgent-or-lru-window` which given an + application ID or window class switches to a matching window unless that's + already the current window. In that case, it acts just like + `switch-to-urgent-or-lru-window`. + swayr v0.14.0 ============= diff --git a/src/cmds.rs b/src/cmds.rs index b83b137..8d01017 100644 --- a/src/cmds.rs +++ b/src/cmds.rs @@ -67,6 +67,10 @@ pub enum ConsiderWindows { pub enum SwayrCommand { /// Switch to next urgent window (if any) or to last recently used window. SwitchToUrgentOrLRUWindow, + /// Switch to the given app (given by app_id or window class) if that's not + /// currently focused. If it is, switch to the next urgent window (if any) + /// or to last recently used window. + SwitchToAppOrUrgentOrLRUWindow { name: String }, /// Focus the selected window. SwitchWindow, /// Switch to the selected workspace. @@ -249,6 +253,12 @@ pub fn exec_swayr_cmd(args: ExecSwayrCmdArgs) { SwayrCommand::SwitchToUrgentOrLRUWindow => { switch_to_urgent_or_lru_window(&*props.read().unwrap()) } + SwayrCommand::SwitchToAppOrUrgentOrLRUWindow { name } => { + switch_to_app_or_urgent_or_lru_window( + Some(name), + &*props.read().unwrap(), + ) + } SwayrCommand::SwitchWindow => switch_window(&*props.read().unwrap()), SwayrCommand::SwitchWorkspace => { switch_workspace(&*props.read().unwrap()) @@ -476,13 +486,27 @@ pub fn get_outputs() -> Vec { pub fn switch_to_urgent_or_lru_window( extra_props: &HashMap, +) { + switch_to_app_or_urgent_or_lru_window(None, extra_props) +} + +pub fn switch_to_app_or_urgent_or_lru_window( + name: Option<&str>, + extra_props: &HashMap, ) { let root = get_tree(false); let tree = t::get_tree(&root, extra_props); - if let Some(win) = tree.get_windows().get(0) { - focus_window_by_id(win.node.id) + let wins = tree.get_windows(); + let app_win = + name.and_then(|n| wins.iter().find(|w| w.node.get_app_name() == n)); + if app_win.is_none() || app_win.unwrap().node.is_current() { + if let Some(win) = wins.get(0) { + focus_window_by_id(win.node.id) + } else { + log::debug!("No window to switch to.") + } } else { - log::debug!("No window to switch to.") + focus_window_by_id(app_win.unwrap().node.id) } }