Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat: implement `ancestors`, `siblings`, and `descendants` iterators

Lars Berger2441830dc425
diff --git a/packages/wm/src/containers/traits/common_behavior.rs b/packages/wm/src/containers/traits/common_behavior.rs
index 0bdf7c4e..b025d039 100644
--- a/packages/wm/src/containers/traits/common_behavior.rs
+++ b/packages/wm/src/containers/traits/common_behavior.rs
@@ -21,10 +21,16 @@ pub trait CommonBehavior {
/// Derived container type (eg. `ContainerType::Monitor`).
fn r#type(&self) -> ContainerType;
+ fn self_as_container(&self) -> Container;
+
fn borrow_parent(&self) -> Ref<'_, Option<TilingContainer>>;
fn borrow_parent_mut(&self) -> RefMut<'_, Option<TilingContainer>>;
+ fn borrow_children(&self) -> Ref<'_, Vec<Container>>;
+
+ fn borrow_children_mut(&self) -> RefMut<'_, Vec<Container>>;
+
/// Returns a reference to the parent container, unless this container is
/// the root.
///
@@ -34,6 +40,99 @@ pub trait CommonBehavior {
fn parent(&self) -> Option<TilingContainer> {
self.borrow_parent().clone()
}
+
+ fn children(&self) -> Vec<Container> {
+ self.borrow_children().clone()
+ }
+
+ fn descendants(&self) -> Descendants {
+ Descendants {
+ stack: self.children(),
+ }
+ }
+
+ fn siblings(&self) -> Siblings {
+ Siblings(Some(self.self_as_container()))
+ }
+
+ fn ancestors(&self) -> Ancestors {
+ Ancestors(self.parent())
+ }
+}
+
+/// Helper macro for implementing iterators that return `Option<Container>`.
+#[macro_export]
+macro_rules! impl_container_iterator {
+ ($name: ident, $next: expr) => {
+ impl Iterator for $name {
+ type Item = Container;
+
+ fn next(&mut self) -> Option<Container> {
+ match self.0.take() {
+ Some(container) => {
+ self.0 = $next(&container);
+ Some(container)
+ }
+ None => None,
+ }
+ }
+ }
+ };
+}
+
+/// Helper macro for implementing iterators that return `Option<TilingContainer>`.
+#[macro_export]
+macro_rules! impl_tiling_container_iterator {
+ ($name: ident, $next: expr) => {
+ impl Iterator for $name {
+ type Item = TilingContainer;
+
+ fn next(&mut self) -> Option<TilingContainer> {
+ match self.0.take() {
+ Some(container) => {
+ self.0 = $next(&container);
+ Some(container)
+ }
+ None => None,
+ }
+ }
+ }
+ };
+}
+
+/// An iterator over ancestors of a given container.
+pub struct Ancestors(Option<TilingContainer>);
+impl_tiling_container_iterator!(
+ Ancestors,
+ |container: &TilingContainer| { container.parent() }
+);
+
+/// An iterator over siblings of a given container.
+pub struct Siblings(Option<Container>);
+impl_container_iterator!(Siblings, |container: &Container| {
+ container.parent().and_then(|parent| {
+ parent
+ .children()
+ .into_iter()
+ .find(|child| child != container)
+ })
+});
+
+/// An iterator over descendants of a given container.
+pub struct Descendants {
+ stack: Vec<Container>,
+}
+
+impl Iterator for Descendants {
+ type Item = Container;
+
+ fn next(&mut self) -> Option<Container> {
+ while let Some(container) = self.stack.pop() {
+ self.stack.extend(container.children());
+ return Some(container);
+ }
+ None
+ }
}
/// Implements the `CommonBehavior` trait for a given struct.
@@ -52,6 +151,10 @@ macro_rules! impl_common_behavior {
$container_type
}
+ fn self_as_container(&self) -> Container {
+ self.clone().into()
+ }
+
fn borrow_parent(&self) -> Ref<'_, Option<TilingContainer>> {
Ref::map(self.0.borrow(), |c| &c.parent)
}
@@ -59,6 +162,14 @@ macro_rules! impl_common_behavior {
fn borrow_parent_mut(&self) -> RefMut<'_, Option<TilingContainer>> {
RefMut::map(self.0.borrow_mut(), |c| &mut c.parent)
}
+
+ fn borrow_children(&self) -> Ref<'_, Vec<Container>> {
+ Ref::map(self.0.borrow(), |c| &c.children)
+ }
+
+ fn borrow_children_mut(&self) -> RefMut<'_, Vec<Container>> {
+ RefMut::map(self.0.borrow_mut(), |c| &mut c.children)
+ }
}
};
}
diff --git a/packages/wm/src/containers/traits/tiling_behavior.rs b/packages/wm/src/containers/traits/tiling_behavior.rs
index 14faa211..cac706a5 100644
--- a/packages/wm/src/containers/traits/tiling_behavior.rs
+++ b/packages/wm/src/containers/traits/tiling_behavior.rs
@@ -1,5 +1,3 @@
-use std::cell::{Ref, RefMut};
-
use enum_dispatch::enum_dispatch;
use crate::containers::{Container, TilingContainer};
@@ -8,22 +6,14 @@ use super::CommonBehavior;
#[enum_dispatch]
pub trait TilingBehavior: CommonBehavior {
- fn borrow_children(&self) -> Ref<'_, Vec<Container>>;
-
- fn borrow_children_mut(&self) -> RefMut<'_, Vec<Container>>;
-
- fn self_as_tiling(&self) -> TilingContainer;
-
- fn children(&self) -> Vec<Container> {
- self.borrow_children().clone()
- }
+ fn self_as_tiling_container(&self) -> TilingContainer;
fn insert_child(&self, target_index: usize, child: Container) {
self
.borrow_children_mut()
.insert(target_index, child.clone());
- *child.borrow_parent_mut() = Some(self.self_as_tiling());
+ *child.borrow_parent_mut() = Some(self.self_as_tiling_container());
}
}
@@ -35,15 +25,7 @@ pub trait TilingBehavior: CommonBehavior {
macro_rules! impl_tiling_behavior {
($struct_name:ident) => {
impl TilingBehavior for $struct_name {
- fn borrow_children(&self) -> Ref<'_, Vec<Container>> {
- Ref::map(self.0.borrow(), |c| &c.children)
- }
-
- fn borrow_children_mut(&self) -> RefMut<'_, Vec<Container>> {
- RefMut::map(self.0.borrow_mut(), |c| &mut c.children)
- }
-
- fn self_as_tiling(&self) -> TilingContainer {
+ fn self_as_tiling_container(&self) -> TilingContainer {
self.clone().into()
}
}
diff --git a/packages/wm/src/windows/non_tiling_window.rs b/packages/wm/src/windows/non_tiling_window.rs
index b6c0aa22..9837578b 100644
--- a/packages/wm/src/windows/non_tiling_window.rs
+++ b/packages/wm/src/windows/non_tiling_window.rs
@@ -9,7 +9,7 @@ use crate::{
common::{platform::NativeWindow, Rect},
containers::{
traits::{CommonBehavior, PositionBehavior},
- ContainerType, TilingContainer,
+ Container, ContainerType, TilingContainer,
},
impl_common_behavior,
};
@@ -21,6 +21,7 @@ pub struct NonTilingWindow(Rc<RefCell<NonTilingWindowInner>>);
struct NonTilingWindowInner {
id: Uuid,
parent: Option<TilingContainer>,
+ children: Vec<Container>,
native: NativeWindow,
position: Rect,
}
@@ -30,6 +31,7 @@ impl NonTilingWindow {
let window = NonTilingWindowInner {
id: Uuid::new_v4(),
parent: None,
+ children: Vec::new(),
native: native_window,
position: Rect::from_xy(0, 0, 0, 0),
};
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index f0b90da5..21c1a01a 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -3,9 +3,12 @@ use tokio::sync::mpsc::UnboundedSender;
use crate::{
common::platform::{NativeMonitor, NativeWindow, Platform},
- containers::{traits::TilingBehavior, Container, RootContainer},
+ containers::{
+ traits::{CommonBehavior, TilingBehavior},
+ Container, RootContainer,
+ },
monitors::Monitor,
- user_config::{BindingModeConfig, ParsedConfig, UserConfig},
+ user_config::{BindingModeConfig, UserConfig},
windows::TilingWindow,
wm_event::WmEvent,
workspaces::Workspace,