Skip to content

Commit

Permalink
resize feature added, untested
Browse files Browse the repository at this point in the history
  • Loading branch information
nowakf committed Oct 16, 2021
1 parent a844a7e commit 48627ff
Show file tree
Hide file tree
Showing 2 changed files with 964 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
use i3ipc::{reply::NodeLayout, I3Connection, I3EventListener, Subscription, event::Event, event::inner::WindowChange};
use i3ipc::{reply::NodeLayout, I3Connection, I3EventListener, Subscription, event::{Event, WindowEventInfo}, event::inner::WindowChange};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut connection = I3Connection::connect()?;
let mut listener = I3EventListener::connect()?;
let subs = [Subscription::Window];
let subs = [Subscription::Window, Subscription::Binding];
listener.subscribe(&subs)?;
for event in listener.listen() {
if let Event::WindowEvent(info) = event? {
match info.container.layout {
NodeLayout::SplitH | NodeLayout::SplitV if info.change == WindowChange::New => {
let (_, _, w, h) = info.container.window_rect;
if w > h {
connection.run_command("split h")?;
} else {
connection.run_command("split v")?;
}
},
_ => (),
}
let relevant_event = |i : &WindowEventInfo|
i.container.layout == NodeLayout::SplitH
|| i.container.layout == NodeLayout::SplitV
&& i.change == WindowChange::Focus;

let (_, _, w, h) = match event? {
Event::WindowEvent(info) if relevant_event(&info) => {
info.container.window_rect
},
Event::BindingEvent(binding) if binding.binding.command.starts_with("resize") => {
let mut node = &connection.get_tree()?;
while !node.focused {
node = node.nodes.iter().find(|n| node.focus.first().map(|&id| id == n.id).unwrap_or(false)).expect("no focused node");
}
node.window_rect
},
_ => continue,
};

if w > h {
connection.run_command("split h")?;
} else {
connection.run_command("split v")?;
}
}
Ok(())
Expand Down
Loading

0 comments on commit 48627ff

Please sign in to comment.