From 1e1ac9d9b0fb5272284d504ffa023909defd636c Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Sun, 14 Nov 2021 20:49:03 +0100 Subject: [PATCH] New swap-focused-with command --- NEWS.md | 4 +++- src/cmds.rs | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 328e75b..ead5e18 100644 --- a/NEWS.md +++ b/NEWS.md @@ -16,7 +16,9 @@ swayr v0.10.0 selected one. - New command: `quit-workspace-container-or-window` shows workspaces, containers, and their windows in the menu program and quits all windows of - the selected workspace/container, or the selected window. + the selected workspace/container or the selected window. +- New command: `swap-focused-with` swaps the currently focused window or + container with the one selected from the menu program. diff --git a/src/cmds.rs b/src/cmds.rs index 6db0047..c978a6a 100644 --- a/src/cmds.rs +++ b/src/cmds.rs @@ -119,6 +119,9 @@ pub enum SwayrCommand { /// Move the currently focused window or container to the selected /// workspace. MoveFocusedToWorkspace, + /// Swap the currently focused window or container with the selected + /// container or window. + SwapFocusedWith, /// Tab or shuffle-and-tile the windows on the current workspace, including /// or excluding floating windows. ToggleTabShuffleTileWorkspace { @@ -318,6 +321,10 @@ pub fn exec_swayr_cmd(args: ExecSwayrCmdArgs) { SwayrCommand::MoveFocusedToWorkspace => { move_focused_to_workspace(&*props.read().unwrap()) } + SwayrCommand::SwapFocusedWith => { + swap_focused_with(&*props.read().unwrap()) + } + SwayrCommand::TileWorkspace { floating } => { tile_current_workspace(floating, false) } @@ -597,7 +604,11 @@ fn select_and_move_focused_to(prompt: &str, choices: &[t::DisplayNode]) { match util::select_from_menu(prompt, choices) { Ok(tn) => match tn.node.get_type() { t::Type::Workspace => { - move_focused_to_workspace_1(tn.node.get_name()) + if tn.node.is_scratchpad() { + run_sway_command(&["move", "container", "to", "scratchpad"]) + } else { + move_focused_to_workspace_1(tn.node.get_name()) + } } t::Type::Container => move_focused_to_container_1(tn.node.id), t => eprintln!("Cannot move focused to {:?}", t), @@ -618,6 +629,32 @@ pub fn move_focused_to_workspace(extra_props: &HashMap) { ); } +pub fn swap_focused_with(extra_props: &HashMap) { + let root = get_tree(true); + let tree = t::get_tree(&root, extra_props); + match util::select_from_menu( + "Swap focused with", + &tree.get_workspaces_containers_and_windows(), + ) { + Ok(tn) => match tn.node.get_type() { + t::Type::Workspace | t::Type::Container | t::Type::Window => { + run_sway_command(&[ + "swap", + "container", + "with", + "con_id", + &format!("{}", tn.node.id), + ]) + } + t => eprintln!("Cannot move focused to {:?}", t), + }, + Err(input) => { + let ws_name = chop_workspace_shortcut(&input); + move_focused_to_workspace_1(ws_name); + } + } +} + pub enum Direction { Backward, Forward,