From 22fa8591c7ddeeaf61b8dd27239ebc6d2bd4aed5 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Thu, 28 Oct 2021 18:34:56 +0200 Subject: [PATCH] Some simplifications --- src/cmds.rs | 54 +++++++++++++++++++++++---------------------------- src/con.rs | 26 +++++++++++++------------ src/layout.rs | 2 +- 3 files changed, 39 insertions(+), 43 deletions(-) diff --git a/src/cmds.rs b/src/cmds.rs index f4c66be..5f3409a 100644 --- a/src/cmds.rs +++ b/src/cmds.rs @@ -196,27 +196,25 @@ pub fn exec_swayr_cmd(args: ExecSwayrCmdArgs) { match args.cmd { SwayrCommand::SwitchToUrgentOrLRUWindow => { - switch_to_urgent_or_lru_window(Some(&*props.read().unwrap())) - } - SwayrCommand::SwitchWindow => { - switch_window(Some(&*props.read().unwrap())) + switch_to_urgent_or_lru_window(&*props.read().unwrap()) } + SwayrCommand::SwitchWindow => switch_window(&*props.read().unwrap()), SwayrCommand::NextWindow { windows } => focus_window_in_direction( Direction::Forward, windows, - Some(&*props.read().unwrap()), + &*props.read().unwrap(), Box::new(always_true), ), SwayrCommand::PrevWindow { windows } => focus_window_in_direction( Direction::Backward, windows, - Some(&*props.read().unwrap()), + &*props.read().unwrap(), Box::new(always_true), ), SwayrCommand::NextTiledWindow { windows } => focus_window_in_direction( Direction::Forward, windows, - Some(&*props.read().unwrap()), + &*props.read().unwrap(), Box::new(|w: &con::Window| { !w.is_floating() && w.is_child_of_tiled_container() }), @@ -224,7 +222,7 @@ pub fn exec_swayr_cmd(args: ExecSwayrCmdArgs) { SwayrCommand::PrevTiledWindow { windows } => focus_window_in_direction( Direction::Backward, windows, - Some(&*props.read().unwrap()), + &*props.read().unwrap(), Box::new(|w: &con::Window| { !w.is_floating() && w.is_child_of_tiled_container() }), @@ -233,7 +231,7 @@ pub fn exec_swayr_cmd(args: ExecSwayrCmdArgs) { focus_window_in_direction( Direction::Forward, windows, - Some(&*props.read().unwrap()), + &*props.read().unwrap(), Box::new(|w: &con::Window| { !w.is_floating() && w.is_child_of_tabbed_or_stacked_container() @@ -244,7 +242,7 @@ pub fn exec_swayr_cmd(args: ExecSwayrCmdArgs) { focus_window_in_direction( Direction::Backward, windows, - Some(&*props.read().unwrap()), + &*props.read().unwrap(), Box::new(|w: &con::Window| { !w.is_floating() && w.is_child_of_tabbed_or_stacked_container() @@ -255,7 +253,7 @@ pub fn exec_swayr_cmd(args: ExecSwayrCmdArgs) { focus_window_in_direction( Direction::Forward, windows, - Some(&*props.read().unwrap()), + &*props.read().unwrap(), Box::new(|w: &con::Window| w.is_floating()), ) } @@ -263,7 +261,7 @@ pub fn exec_swayr_cmd(args: ExecSwayrCmdArgs) { focus_window_in_direction( Direction::Backward, windows, - Some(&*props.read().unwrap()), + &*props.read().unwrap(), Box::new(|w: &con::Window| w.is_floating()), ) } @@ -271,25 +269,25 @@ pub fn exec_swayr_cmd(args: ExecSwayrCmdArgs) { focus_similar_window_in_direction( Direction::Forward, windows, - Some(&*props.read().unwrap()), + &*props.read().unwrap(), ) } SwayrCommand::PrevSimilarWindow { windows } => { focus_similar_window_in_direction( Direction::Backward, windows, - Some(&*props.read().unwrap()), + &*props.read().unwrap(), ) } - SwayrCommand::QuitWindow => quit_window(Some(&*props.read().unwrap())), + SwayrCommand::QuitWindow => quit_window(&*props.read().unwrap()), SwayrCommand::SwitchWorkspace => { - switch_workspace(Some(&*props.read().unwrap())) + switch_workspace(&*props.read().unwrap()) } SwayrCommand::SwitchWorkspaceOrWindow => { - switch_workspace_or_window(Some(&*props.read().unwrap())) + switch_workspace_or_window(&*props.read().unwrap()) } SwayrCommand::QuitWorkspaceOrWindow => { - quit_workspace_or_window(Some(&*props.read().unwrap())) + quit_workspace_or_window(&*props.read().unwrap()) } SwayrCommand::TileWorkspace { floating } => { tile_current_workspace(floating, false) @@ -381,7 +379,7 @@ pub fn get_tree() -> s::Node { } pub fn switch_to_urgent_or_lru_window( - extra_props: Option<&HashMap>, + extra_props: &HashMap, ) { let root = get_tree(); let windows = con::get_windows(&root, false, extra_props); @@ -397,7 +395,7 @@ pub fn switch_to_urgent_or_lru_window( } } -pub fn switch_window(extra_props: Option<&HashMap>) { +pub fn switch_window(extra_props: &HashMap) { let root = get_tree(); let windows = con::get_windows(&root, true, extra_props); @@ -414,7 +412,7 @@ pub enum Direction { pub fn focus_window_in_direction( dir: Direction, consider_wins: &ConsiderWindows, - extra_props: Option<&HashMap>, + extra_props: &HashMap, pred: Box bool>, ) { let root = get_tree(); @@ -471,7 +469,7 @@ pub fn focus_window_in_direction( pub fn focus_similar_window_in_direction( dir: Direction, consider_wins: &ConsiderWindows, - extra_props: Option<&HashMap>, + extra_props: &HashMap, ) { let root = get_tree(); let windows = con::get_windows(&root, false, extra_props); @@ -507,7 +505,7 @@ pub fn focus_similar_window_in_direction( } } -pub fn switch_workspace(extra_props: Option<&HashMap>) { +pub fn switch_workspace(extra_props: &HashMap) { let root = get_tree(); let workspaces = con::get_workspaces(&root, false, extra_props); @@ -518,9 +516,7 @@ pub fn switch_workspace(extra_props: Option<&HashMap>) { } } -pub fn switch_workspace_or_window( - extra_props: Option<&HashMap>, -) { +pub fn switch_workspace_or_window(extra_props: &HashMap) { let root = get_tree(); let workspaces = con::get_workspaces(&root, true, extra_props); let ws_or_wins = con::WsOrWin::from_workspaces(&workspaces); @@ -537,7 +533,7 @@ pub fn switch_workspace_or_window( } } -pub fn quit_window(extra_props: Option<&HashMap>) { +pub fn quit_window(extra_props: &HashMap) { let root = get_tree(); let windows = con::get_windows(&root, true, extra_props); @@ -546,9 +542,7 @@ pub fn quit_window(extra_props: Option<&HashMap>) { } } -pub fn quit_workspace_or_window( - extra_props: Option<&HashMap>, -) { +pub fn quit_workspace_or_window(extra_props: &HashMap) { let root = get_tree(); let workspaces = con::get_workspaces(&root, true, extra_props); let ws_or_wins = con::WsOrWin::from_workspaces(&workspaces); diff --git a/src/con.rs b/src/con.rs index ca5b6cb..742adb5 100644 --- a/src/con.rs +++ b/src/con.rs @@ -316,7 +316,7 @@ impl<'a> DisplayFormat for Window<'a> { fn build_windows<'a>( root: &'a s::Node, include_scratchpad_windows: bool, - extra_props: Option<&HashMap>, + extra_props: &HashMap, ) -> Vec> { let mut v = vec![]; for workspace in root.workspaces() { @@ -327,7 +327,7 @@ fn build_windows<'a>( for n in workspace.windows() { v.push(Window { node: n, - extra_props: extra_props.and_then(|m| m.get(&n.id).cloned()), + extra_props: extra_props.get(&n.id).cloned(), workspace, }) } @@ -338,7 +338,7 @@ fn build_windows<'a>( fn build_workspaces<'a>( root: &'a s::Node, include_scratchpad: bool, - extra_props: Option<&HashMap>, + extra_props: &HashMap, ) -> Vec> { let mut v = vec![]; for workspace in root.workspaces() { @@ -353,19 +353,22 @@ fn build_workspaces<'a>( .iter() .map(|w| Window { node: w, - extra_props: extra_props.and_then(|m| m.get(&w.id).cloned()), + extra_props: extra_props.get(&w.id).cloned(), workspace, }) .collect(); - wins.sort(); + if !extra_props.is_empty() { + wins.sort(); + } v.push(Workspace { node: workspace, - extra_props: extra_props - .and_then(|m| m.get(&workspace.id).cloned()), + extra_props: extra_props.get(&workspace.id).cloned(), windows: wins, }) } - v.sort(); + if !extra_props.is_empty() { + v.sort(); + } v } @@ -373,11 +376,10 @@ fn build_workspaces<'a>( pub fn get_windows<'a>( root: &'a s::Node, include_scratchpad_windows: bool, - extra_props: Option<&HashMap>, + extra_props: &HashMap, ) -> Vec> { - let extra_props_given = extra_props.is_some(); let mut wins = build_windows(root, include_scratchpad_windows, extra_props); - if extra_props_given { + if !extra_props.is_empty() { wins.sort(); } wins @@ -387,7 +389,7 @@ pub fn get_windows<'a>( pub fn get_workspaces<'a>( root: &'a s::Node, include_scratchpad: bool, - extra_props: Option<&HashMap>, + extra_props: &HashMap, ) -> Vec> { let mut workspaces = build_workspaces(root, include_scratchpad, extra_props); diff --git a/src/layout.rs b/src/layout.rs index 62cafdc..18c3757 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -134,7 +134,7 @@ pub fn relayout_current_workspace( >, ) -> s::Fallible<()> { let root = cmds::get_tree(); - let workspaces = con::get_workspaces(&root, false, None); + let workspaces = con::get_workspaces(&root, false, &HashMap::new()); if let Some(cur_ws) = workspaces.iter().find(|ws| ws.is_current()) { if let Ok(mut con) = s::Connection::new() { let mut moved_wins: Vec<&con::Window> = vec![];