New subcommand switch-workspace-or-window

timeout_old
Tassilo Horn 4 years ago
parent d7be7cecbb
commit a17271a918
  1. 1
      .gitignore
  2. 5
      src/bin/swayr.rs
  3. 22
      src/client.rs
  4. 59
      src/con.rs

1
.gitignore vendored

@ -1 +1,2 @@
/target /target
Cargo.lock

@ -24,6 +24,8 @@ enum SwayrCommand {
QuitWindow, QuitWindow,
/// Switch workspace with LRU display order /// Switch workspace with LRU display order
SwitchWorkspace, SwitchWorkspace,
/// Switch workspace or window with LRU display order
SwitchWorkspaceOrWindow,
/// Select and execute a swaymsg command /// Select and execute a swaymsg command
ExecuteSwaymsgCommand, ExecuteSwaymsgCommand,
} }
@ -34,6 +36,9 @@ fn main() {
SwayrCommand::SwitchWindow => client::switch_window(), SwayrCommand::SwitchWindow => client::switch_window(),
SwayrCommand::QuitWindow => client::quit_window(), SwayrCommand::QuitWindow => client::quit_window(),
SwayrCommand::SwitchWorkspace => client::switch_workspace(), SwayrCommand::SwitchWorkspace => client::switch_workspace(),
SwayrCommand::SwitchWorkspaceOrWindow => {
client::switch_workspace_or_window()
}
SwayrCommand::ExecuteSwaymsgCommand => client::exec_swaymsg_command(), SwayrCommand::ExecuteSwaymsgCommand => client::exec_swaymsg_command(),
} }
} }

@ -26,6 +26,28 @@ pub fn switch_workspace() {
} }
} }
pub fn switch_workspace_or_window() {
let root = con::get_tree();
let workspaces = con::get_workspaces(&root, false);
let ws_or_wins = con::WsOrWin::from_workspaces(&workspaces);
if let Some(ws_or_win) = con::select_workspace_or_window(
"Select workspace or window",
&ws_or_wins,
) {
match ws_or_win {
con::WsOrWin::Ws { ws } => {
util::swaymsg(&["workspace", "number", ws.get_name()]);
}
con::WsOrWin::Win { win } => {
util::swaymsg(&[
format!("[con_id={}]", win.get_id()).as_str(),
"focus",
]);
}
}
}
}
pub fn quit_window() { pub fn quit_window() {
let root = con::get_tree(); let root = con::get_tree();
let mut windows = con::get_windows(&root); let mut windows = con::get_windows(&root);

@ -140,18 +140,20 @@ fn build_workspaces(
) -> Vec<Workspace> { ) -> Vec<Workspace> {
let mut v = vec![]; let mut v = vec![];
for workspace in root.workspaces() { for workspace in root.workspaces() {
let mut wins: Vec<Window> = workspace
.windows()
.iter()
.map(|w| Window {
node: &w,
con_props: con_props.remove(&w.id),
workspace: &workspace,
})
.collect();
wins.sort();
v.push(Workspace { v.push(Workspace {
node: &workspace, node: &workspace,
con_props: con_props.remove(&workspace.id), con_props: con_props.remove(&workspace.id),
windows: workspace windows: wins,
.windows()
.iter()
.map(|w| Window {
node: &w,
con_props: con_props.remove(&w.id),
workspace: &workspace,
})
.collect(),
}) })
} }
v v
@ -246,6 +248,45 @@ pub fn select_workspace<'a>(
util::wofi_select(prompt, workspaces) util::wofi_select(prompt, workspaces)
} }
pub enum WsOrWin<'a> {
Ws { ws: &'a Workspace<'a> },
Win { win: &'a Window<'a> },
}
impl<'a> fmt::Display for WsOrWin<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match self {
WsOrWin::Ws { ws } => ws.fmt(f),
WsOrWin::Win { win } => match f.write_str("\t") {
Ok(()) => win.fmt(f),
Err(e) => Err(e),
},
}
}
}
impl WsOrWin<'_> {
pub fn from_workspaces<'a>(
workspaces: &'a [Workspace],
) -> Vec<WsOrWin<'a>> {
let mut v = vec![];
for ws in workspaces {
v.push(WsOrWin::Ws { ws });
for win in &ws.windows {
v.push(WsOrWin::Win { win: &win });
}
}
v
}
}
pub fn select_workspace_or_window<'a>(
prompt: &'a str,
ws_or_wins: &'a [WsOrWin<'a>],
) -> Option<&'a WsOrWin<'a>> {
util::wofi_select(prompt, ws_or_wins)
}
pub struct Workspace<'a> { pub struct Workspace<'a> {
node: &'a ipc::Node, node: &'a ipc::Node,
con_props: Option<ipc::ConProps>, con_props: Option<ipc::ConProps>,

Loading…
Cancel
Save