Skip to content

Commit

Permalink
layouts: resize windows if the window node's window is changed
Browse files Browse the repository at this point in the history
Closes #23
  • Loading branch information
Antikyth committed Nov 19, 2023
1 parent a1e718f commit 3679cb8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
5 changes: 5 additions & 0 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ pub struct GroupNode<Window> {
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct WindowNode<Window> {
window: Window,
/// Whether the `window` was changed in the latest [`add_window`] or [`remove_window`] call.
///
/// [`add_window`]: TilingLayoutManager::add_window
/// [`remove_window`]: TilingLayoutManager::remove_window
window_changed: bool,

width: u32,
height: u32,
Expand Down
26 changes: 18 additions & 8 deletions src/layout/implementations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use std::{
borrow::{Borrow, BorrowMut},
mem,
ops::{Deref, DerefMut, Index, IndexMut},
};

Expand Down Expand Up @@ -184,7 +185,12 @@ impl<Window> WindowNode<Window> {
/// Creates a window node of the given `window` and dimensions.
#[inline]
pub(crate) const fn with_dimensions(window: Window, width: u32, height: u32) -> Self {
Self { window, width, height }
Self {
window,
window_changed: false,
width,
height,
}
}

/// Returns a reference to the window node's window.
Expand All @@ -193,18 +199,22 @@ impl<Window> WindowNode<Window> {
&self.window
}

/// Returns a mutable reference to the window node's window.
#[inline(always)]
pub fn window_mut(&mut self) -> &mut Window {
&mut self.window
}

/// Sets the window node's window to the given `window`.
#[inline(always)]
#[inline]
pub fn set_window(&mut self, window: Window) {
self.window_changed = true;

self.window = window;
}

/// Replaces the window node's window with the given `window`, returning the previous one.
#[inline]
pub fn replace_window(&mut self, window: Window) -> Window {
self.window_changed = true;

mem::replace(&mut self.window, window)
}

/// Returns the window node's window.
#[inline(always)]
pub fn unwrap(self) -> Window {
Expand Down
32 changes: 24 additions & 8 deletions src/layout/implementations/node_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,14 +467,21 @@ impl<Window> GroupNode<Window> {
{
// If no changes have been made to this group, apply all the child groups' changes and return.
if !self.changes_made() {
let groups = self.children.iter_mut().filter_map(|node| match node {
Node::Group(group) => Some(group),
for node in self {
match node {
Node::Group(group) => group.apply_resizes(resize_window.clone())?,

Node::Window(_) => None,
});

for group in groups {
group.apply_resizes(resize_window.clone())?;
Node::Window(WindowNode {
window,
window_changed,
width,
height,
}) => {
if mem::take(window_changed) {
resize_window(window, *width, *height)?
}
},
}
}

return Ok(());
Expand Down Expand Up @@ -539,7 +546,16 @@ impl<Window> GroupNode<Window> {

match node {
Node::Group(group) => group.apply_resizes(resize_window.clone()),
Node::Window(WindowNode { window, .. }) => resize_window(window, primary, secondary),

Node::Window(WindowNode {
window,
window_changed,
width,
height,
}) => {
*window_changed = false;
resize_window(window, *width, *height)
},
}
};

Expand Down
10 changes: 5 additions & 5 deletions src/layout/managers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ where
for (i, node) in window_nodes {
// If the window matches, remove it and return.
if node.window() == window {
if stack.len() == 1 {
// If it is the last window in the stack, remove the whole stack.
self.layout.remove(1);
} else {
// Otherwise, if it is not the last window in the stack, simply remove that window node.
if stack.len() > 1 {
// If it is not the last window in the stack, remove the node.
stack.remove(i);
} else {
// Otherwise, if it is the last window in the stack, remove the stack.
self.layout.remove(1);
}

return;
Expand Down

0 comments on commit 3679cb8

Please sign in to comment.