Skip to content

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

feat: working container tree implementation (`ContainerRef` enum with derived ref types for each container type)

Lars Berger4483cf3a347c
diff --git a/packages/wm/src/containers/container.rs b/packages/wm/src/containers/container.rs
index 3b6d8a03..ce6ef386 100644
--- a/packages/wm/src/containers/container.rs
+++ b/packages/wm/src/containers/container.rs
@@ -7,7 +7,17 @@ use std::{
use uuid::Uuid;
-use super::{ContainerType, RootContainer, SplitContainer};
+// use super::{ContainerType, SplitContainer};
+
+#[derive(Clone)]
+pub struct RootContainerRef(Rc<RefCell<RootContainer>>);
+
+#[derive(Debug)]
+pub struct RootContainer {
+ pub parent: Option<ContainerRef>,
+ pub children: Vec<ContainerRef>,
+ pub val: i32,
+}
/// A reference to a `Container`.
///
@@ -15,81 +25,76 @@ use super::{ContainerType, RootContainer, SplitContainer};
/// `std::cell::RefCell` for interior mutability.
///
/// **Note:** Cloning a `ContainerRef` only increments a reference count.
-pub struct ContainerRef(Rc<RefCell<Container>>);
-
-struct Container {
- parent: Option<Weak<RefCell<Container>>>,
- children: Vec<Rc<RefCell<Container>>>,
- data: ContainerData,
+#[derive(Clone, Debug)]
+pub enum ContainerRef {
+ RootContainer(RootContainerRef),
+ // Monitor(MonitorRef),
+ // Workspace(WorkspaceRef),
+ // SplitContainer(SplitContainerRef),
+ // Window(WindowRef),
}
-#[derive(Debug)]
-pub enum ContainerData {
- RootContainer,
- Monitor,
- Workspace,
- SplitContainer,
- Window,
- // RootContainer(RootContainer),
- // Monitor(Monitor),
- // Workspace(Workspace),
- // SplitContainer(SplitContainer),
- // Window(Window),
+pub trait CommonContainer {
+ /// Return a reference to the parent node, unless this node is the root
+ /// of the tree.
+ ///
+ /// # Panics
+ ///
+ /// Panics if the node is currently mutability borrowed.
+ fn parent(&self) -> Option<ContainerRef>;
+ fn set_parent(&self, parent: ContainerRef);
+ fn grandparent(&self) -> Option<ContainerRef>;
+
+ fn insert_child(&self, child: ContainerRef);
}
-/// Cloning a `ContainerRef` only increments a reference count. It does not
-/// copy the data.
-impl Clone for ContainerRef {
- fn clone(&self) -> ContainerRef {
- ContainerRef(self.0.clone())
+impl RootContainerRef {
+ pub fn new(val: i32) -> Self {
+ let root = RootContainer {
+ parent: None,
+ children: Vec::new(),
+ val,
+ };
+
+ Self(Rc::new(RefCell::new(root)))
}
-}
-impl fmt::Debug for ContainerRef {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- fmt::Debug::fmt(&*self.borrow(), f)
+ pub fn set_val(&self, val: i32) {
+ self.0.borrow_mut().val = val
}
}
-impl ContainerRef {
- /// Create a new node from its associated data.
- pub fn new(data: ContainerData) -> ContainerRef {
- ContainerRef(Rc::new(RefCell::new(Container {
- parent: None,
- children: Vec::new(),
- data,
- })))
+impl CommonContainer for RootContainerRef {
+ fn grandparent(&self) -> Option<ContainerRef> {
+ self.parent()?.common().parent()
}
- /// Return a reference to the parent node, unless this node is the root
- /// of the tree.
- ///
- /// # Panics
- ///
- /// Panics if the node is currently mutability borrowed.
- pub fn parent(&self) -> Option<ContainerRef> {
- Some(ContainerRef(self.0.borrow().parent.as_ref()?.upgrade()?))
+ fn parent(&self) -> Option<ContainerRef> {
+ self.0.borrow().parent.clone()
}
- /// Return a shared reference to this node’s data
- ///
- /// # Panics
- ///
- /// Panics if the node is currently mutability borrowed.
- pub fn borrow(&self) -> Ref<ContainerData> {
- Ref {
- _ref: self.0.borrow(),
- }
+ fn insert_child(&self, child: ContainerRef) {
+ self.0.borrow_mut().children.push(child.clone());
+ child
+ .common()
+ .set_parent(ContainerRef::RootContainer(self.clone()))
}
- /// Return a unique/mutable reference to this node’s data
- ///
- /// # Panics
- ///
- /// Panics if the node is currently borrowed.
- pub fn borrow_mut(&self) -> RefMut<ContainerData> {
- RefMut {
- _ref: self.0.borrow_mut(),
+ fn set_parent(&self, parent: ContainerRef) {
+ self.0.borrow_mut().parent = Some(parent)
+ }
+}
+
+impl ContainerRef {
+ fn common(&self) -> &dyn CommonContainer {
+ match self {
+ ContainerRef::RootContainer(c) => c,
}
}
}
+
+impl fmt::Debug for RootContainerRef {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Debug::fmt(&self.0.borrow().val, f)
+ }
+}
diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rs
index 395916e8..6f384c40 100644
--- a/packages/wm/src/main.rs
+++ b/packages/wm/src/main.rs
@@ -78,6 +78,22 @@ async fn start_wm(config_path: Option<String>) -> Result<()> {
wm_state.lock().await.add_monitor();
wm_state.lock().await.add_monitor();
wm_state.lock().await.add_monitor();
+
+ let root = RootContainerRef::new(1);
+ let leaf = RootContainerRef::new(2);
+ root.insert_child(ContainerRef::RootContainer(leaf.clone()));
+
+ let leaf_deep = RootContainerRef::new(3);
+ leaf.insert_child(ContainerRef::RootContainer(leaf_deep.clone()));
+
+ match leaf_deep.grandparent().unwrap() {
+ ContainerRef::RootContainer(val) => val.set_val(999),
+ }
+ println!("aaaa {:?}", root);
+ println!("aaaa {:?}", leaf);
+ println!("aaaa {:?}", leaf_deep);
+ println!("aaaa {:?}", leaf_deep.grandparent());
+
wm.process_event(event).await
},
Some(wm_event) = wm.event_rx.recv() => {