Skip to content

Commit

Permalink
Merge pull request #2 from jhzn/master
Browse files Browse the repository at this point in the history
Fixed window spliting axis bug and restructured code to show intent more clearly
  • Loading branch information
ammgws authored Dec 11, 2020
2 parents 40c8e2c + aece755 commit c0ec1f8
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
use swayipc::reply::{Event, Node, NodeLayout, NodeType, WindowChange};
use swayipc::reply::{Event, NodeLayout, NodeType, WindowChange};
use swayipc::{Connection, EventType};

fn switch_splitting(conn: &mut Connection, focused_node: Node) -> Result<(), String> {
// get info from parent node which unfortunately requires us to call get_tree
fn switch_splitting(conn: &mut Connection) -> Result<(), String> {
// get info from focused node and parent node which unfortunately requires us to call get_tree
let tree = conn.get_tree().unwrap();
let focused_node = tree
.find_focused_as_ref(|n| n.focused)
.ok_or("Could not find the focused node")?;

{
// get info from the focused child node
let is_stacked = focused_node.layout == NodeLayout::Stacked;
let is_tabbed = focused_node.layout == NodeLayout::Tabbed;
let is_floating = focused_node.node_type == NodeType::FloatingCon;
let is_full_screen = focused_node.percent.unwrap_or(1.0) > 1.0;
if is_floating || is_full_screen || is_stacked || is_tabbed {
return Ok(());
}
}

let new_layout = if focused_node.rect.height > focused_node.rect.width {
NodeLayout::SplitV
} else {
NodeLayout::SplitH
};
let parent = tree
.find_focused_as_ref(|n| n.nodes.iter().any(|n| n.focused))
.ok_or("No parent")?;
let is_stacked = parent.layout == NodeLayout::Stacked;
let is_tabbed = parent.layout == NodeLayout::Tabbed;

// get info from the focused child node
let is_floating = focused_node.node_type == NodeType::FloatingCon;
let is_full_screen = focused_node.percent.unwrap_or(1.0) > 1.0;

if !is_floating && !is_full_screen && !is_stacked && !is_tabbed {
let new_layout = if focused_node.rect.height > focused_node.rect.width {
NodeLayout::SplitV
} else {
NodeLayout::SplitH
};

if new_layout != parent.layout {
let cmd = match new_layout {
NodeLayout::SplitV => "splitv",
NodeLayout::SplitH => "splith",
_ => "nop",
};
conn.run_command(cmd).unwrap();
};
if new_layout == parent.layout {
return Ok(());
}
let cmd = match new_layout {
NodeLayout::SplitV => "splitv",
NodeLayout::SplitH => "splith",
_ => "nop",
};

conn.run_command(cmd).unwrap();
Ok(())
}

Expand All @@ -44,7 +49,12 @@ fn main() -> Result<(), std::io::Error> {
match event.unwrap() {
Event::Window(e) => match e.change {
WindowChange::Focus => {
if let Err(err) = switch_splitting(&mut conn, e.container) {
//we can not use the e.container because the data is stale
//if we compare that node data with the node given from get_tree() after we
//delete a node we find that the e.container.rect.height and e.container.rect.width are stale
//and therefore we make the wrong decision on which layout our next window
//should be
if let Err(err) = switch_splitting(&mut conn) {
println!("err: {}", err);
}
}
Expand Down

0 comments on commit c0ec1f8

Please sign in to comment.