Skip to content

Commit

Permalink
Add swap functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Nukesor committed Dec 30, 2023
1 parent 5d013f7 commit c369220
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased] - ReleaseDate

## [1.5.0] - unreleased
- Add the `--swap` flag to swap the currently active window with the selected window.

## [1.4.0] - 2023-01-22
- Modernize all dependencies
- Fix border offset issue resulting from newer versions of i3
Expand Down
4 changes: 4 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ pub struct AppConfig {
/// List of keys to exit application, sequences separator is space, key separator is '+', eg Control_L+g Shift_L+f
#[arg(short, long, value_parser(parse_exit_keys))]
pub exit_keys: Vec<utils::Sequence>,

/// If this flag is set, the currently active window will swap with the selected window.
#[arg(short, long)]
pub swap: bool,
}

pub fn parse_args() -> AppConfig {
Expand Down
15 changes: 14 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,20 @@ fn main() -> Result<()> {
if app_config.print_only {
println!("0x{:x}", rw.desktop_window.x_window_id.unwrap_or(0));
} else {

Check failure on line 299 in src/main.rs

View workflow job for this annotation

GitHub Actions / CI with nightly

this `else { if .. }` block can be collapsed
wm::focus_window(rw.desktop_window).context("Couldn't focus window")?;
if app_config.swap {
let Some(active_window) =
desktop_windows.iter().find(|window| window.is_focused)
else {
warn!("There's on active window.");
closed = true;
continue;
};
wm::swap_windows(active_window, rw.desktop_window)
.context("Couldn't swap windows")?;
} else {
wm::focus_window(rw.desktop_window)
.context("Couldn't focus window")?;
}
}
closed = true;
} else if !pressed_keys.is_empty()
Expand Down
14 changes: 14 additions & 0 deletions src/wm_i3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,17 @@ pub fn focus_window(window: &DesktopWindow) -> Result<()> {
info!("Sending to i3: {:?}", command);
Ok(())
}

/// Focus a specific `window`.
pub fn swap_windows(active_window: &DesktopWindow, window: &DesktopWindow) -> Result<()> {
let mut connection = I3Connection::connect().context("Couldn't acquire i3 connection")?;
let command_str = format!(
"[con_id=\"{}\"] swap with container con_id {}",
active_window.id, window.id
);
let command = connection
.run_command(&command_str)
.context("Couldn't communicate with i3")?;
info!("Sending to i3: {:?}", command);
Ok(())
}

0 comments on commit c369220

Please sign in to comment.