Skip to content

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

feat: create separate `TilingWindow` and `NonTilingWindow`; add `TilingContainer` trait

Lars Bergerf8d79ad4ada8
diff --git a/packages/wm/src/containers/container_ref.rs b/packages/wm/src/containers/container_ref.rs
index 0c450c3b..c087aed9 100644
--- a/packages/wm/src/containers/container_ref.rs
+++ b/packages/wm/src/containers/container_ref.rs
@@ -1,10 +1,15 @@
use crate::{
- monitors::MonitorRef, windows::WindowRef, workspaces::WorkspaceRef,
+ monitors::MonitorRef,
+ windows::{NonTilingWindowRef, TilingWindowRef},
+ workspaces::WorkspaceRef,
};
-use super::{CommonContainer, RootContainerRef, SplitContainerRef};
+use super::{
+ traits::{CommonContainer, TilingContainer},
+ RootContainerRef, SplitContainerRef,
+};
-/// A reference to a `Container`.
+/// A reference to a container.
///
/// Internally, this uses reference counting for lifetime tracking and
/// `std::cell::RefCell` for interior mutability.
@@ -16,11 +21,23 @@ pub enum ContainerRef {
Monitor(MonitorRef),
Workspace(WorkspaceRef),
SplitContainer(SplitContainerRef),
- Window(WindowRef),
+ TilingWindow(TilingWindowRef),
+ NonTilingWindow(NonTilingWindowRef),
}
impl ContainerRef {
- pub fn common(&self) -> &dyn CommonContainer {
+ pub fn as_common(&self) -> &dyn CommonContainer {
+ match self {
+ ContainerRef::RootContainer(c) => c,
+ ContainerRef::Monitor(c) => c,
+ // ContainerRef::Workspace(c) => c,
+ // ContainerRef::SplitContainer(c) => c,
+ // ContainerRef::Window(c) => c,
+ _ => todo!(),
+ }
+ }
+
+ pub fn as_tiling(&self) -> &dyn TilingContainer {
match self {
ContainerRef::RootContainer(c) => c,
ContainerRef::Monitor(c) => c,
@@ -31,3 +48,12 @@ impl ContainerRef {
}
}
}
+
+#[derive(Clone, Debug)]
+pub enum TilingContainerRef {
+ RootContainer(RootContainerRef),
+ Monitor(MonitorRef),
+ Workspace(WorkspaceRef),
+ SplitContainer(SplitContainerRef),
+ TilingWindow(TilingWindowRef),
+}
diff --git a/packages/wm/src/containers/mod.rs b/packages/wm/src/containers/mod.rs
index 2b2773d8..f7236a85 100644
--- a/packages/wm/src/containers/mod.rs
+++ b/packages/wm/src/containers/mod.rs
@@ -1,11 +1,10 @@
pub mod commands;
-mod common_container;
mod container_ref;
mod container_type;
mod root_container;
mod split_container;
+pub mod traits;
-pub use common_container::*;
pub use container_ref::*;
pub use container_type::*;
pub use root_container::*;
diff --git a/packages/wm/src/containers/root_container.rs b/packages/wm/src/containers/root_container.rs
index 46333c8e..92526b5d 100644
--- a/packages/wm/src/containers/root_container.rs
+++ b/packages/wm/src/containers/root_container.rs
@@ -3,13 +3,19 @@ use std::{
rc::Rc,
};
-use super::{CommonContainer, ContainerRef};
+use uuid::Uuid;
+
+use super::{
+ traits::{CommonContainer, TilingContainer},
+ ContainerRef, ContainerType,
+};
#[derive(Clone, Debug)]
pub struct RootContainerRef(Rc<RefCell<RootContainer>>);
#[derive(Debug)]
pub struct RootContainer {
+ id: Uuid,
pub parent: Option<ContainerRef>,
pub children: Vec<ContainerRef>,
}
@@ -17,6 +23,7 @@ pub struct RootContainer {
impl RootContainerRef {
pub fn new() -> Self {
let root = RootContainer {
+ id: Uuid::new_v4(),
parent: None,
children: Vec::new(),
};
@@ -26,6 +33,16 @@ impl RootContainerRef {
}
impl CommonContainer for RootContainerRef {
+ fn id(&self) -> Uuid {
+ self.0.borrow().id
+ }
+
+ fn r#type(&self) -> ContainerType {
+ ContainerType::RootContainer
+ }
+}
+
+impl TilingContainer for RootContainerRef {
fn borrow_parent(&self) -> Ref<'_, Option<ContainerRef>> {
Ref::map(self.0.borrow(), |c| &c.parent)
}
diff --git a/packages/wm/src/containers/traits/common_container.rs b/packages/wm/src/containers/traits/common_container.rs
new file mode 100644
index 00000000..4a39841a
--- /dev/null
+++ b/packages/wm/src/containers/traits/common_container.rs
@@ -0,0 +1,9 @@
+use uuid::Uuid;
+
+use crate::containers::ContainerType;
+
+pub trait CommonContainer {
+ fn id(&self) -> Uuid;
+
+ fn r#type(&self) -> ContainerType;
+}
diff --git a/packages/wm/src/containers/traits/mod.rs b/packages/wm/src/containers/traits/mod.rs
new file mode 100644
index 00000000..58999a0c
--- /dev/null
+++ b/packages/wm/src/containers/traits/mod.rs
@@ -0,0 +1,5 @@
+mod common_container;
+mod tiling_container;
+
+pub use common_container::*;
+pub use tiling_container::*;
diff --git a/packages/wm/src/containers/common_container.rs b/packages/wm/src/containers/traits/tiling_container.rs
similarity index 77%
rename from packages/wm/src/containers/common_container.rs
rename to packages/wm/src/containers/traits/tiling_container.rs
index 4ac5b5df..b48505d8 100644
--- a/packages/wm/src/containers/common_container.rs
+++ b/packages/wm/src/containers/traits/tiling_container.rs
@@ -1,9 +1,10 @@
use std::cell::{Ref, RefMut};
-use super::ContainerRef;
+use crate::containers::ContainerRef;
-// TODO: Consider renaming to `TilingContainer`.
-pub trait CommonContainer {
+use super::CommonContainer;
+
+pub trait TilingContainer: CommonContainer {
fn borrow_parent(&self) -> Ref<'_, Option<ContainerRef>>;
fn borrow_parent_mut(&self) -> RefMut<'_, Option<ContainerRef>>;
fn borrow_children(&self) -> Ref<'_, Vec<ContainerRef>>;
@@ -24,10 +25,10 @@ pub trait CommonContainer {
.borrow_children_mut()
.insert(target_index, child.clone());
- *child.common().borrow_parent_mut() = Some(child.clone());
+ *child.as_tiling().borrow_parent_mut() = Some(child.clone());
}
fn grandparent(&self) -> Option<ContainerRef> {
- self.parent()?.common().parent()
+ self.parent()?.as_tiling().parent()
}
}
diff --git a/packages/wm/src/monitors/monitor.rs b/packages/wm/src/monitors/monitor.rs
index ef287ee6..19b936d3 100644
--- a/packages/wm/src/monitors/monitor.rs
+++ b/packages/wm/src/monitors/monitor.rs
@@ -3,9 +3,14 @@ use std::{
rc::Rc,
};
+use uuid::Uuid;
+
use crate::{
common::platform::NativeMonitor,
- containers::{CommonContainer, ContainerRef},
+ containers::{
+ traits::{CommonContainer, TilingContainer},
+ ContainerRef, ContainerType,
+ },
};
#[derive(Clone, Debug)]
@@ -13,6 +18,7 @@ pub struct MonitorRef(Rc<RefCell<Monitor>>);
#[derive(Debug)]
pub struct Monitor {
+ id: Uuid,
pub parent: Option<ContainerRef>,
pub children: Vec<ContainerRef>,
pub device_name: String,
@@ -25,6 +31,7 @@ pub struct Monitor {
impl MonitorRef {
pub fn new(native_monitor: NativeMonitor) -> Self {
let monitor = Monitor {
+ id: Uuid::new_v4(),
parent: None,
children: Vec::new(),
device_name: native_monitor.device_name,
@@ -39,6 +46,16 @@ impl MonitorRef {
}
impl CommonContainer for MonitorRef {
+ fn id(&self) -> Uuid {
+ self.0.borrow().id
+ }
+
+ fn r#type(&self) -> ContainerType {
+ ContainerType::Monitor
+ }
+}
+
+impl TilingContainer for MonitorRef {
fn borrow_parent(&self) -> Ref<'_, Option<ContainerRef>> {
Ref::map(self.0.borrow(), |c| &c.parent)
}
diff --git a/packages/wm/src/windows/mod.rs b/packages/wm/src/windows/mod.rs
index 71df2d5f..68624db0 100644
--- a/packages/wm/src/windows/mod.rs
+++ b/packages/wm/src/windows/mod.rs
@@ -1,5 +1,8 @@
-mod window;
+mod non_tiling_window;
+mod tiling_window;
+pub mod traits;
mod window_state;
-pub use window::*;
+pub use non_tiling_window::*;
+pub use tiling_window::*;
pub use window_state::*;
diff --git a/packages/wm/src/windows/window.rs b/packages/wm/src/windows/non_tiling_window.rs
similarity index 59%
rename from packages/wm/src/windows/window.rs
rename to packages/wm/src/windows/non_tiling_window.rs
index 4f112078..11a0c080 100644
--- a/packages/wm/src/windows/window.rs
+++ b/packages/wm/src/windows/non_tiling_window.rs
@@ -1,18 +1,17 @@
use std::{cell::RefCell, rc::Rc};
#[derive(Clone, Debug)]
-pub struct WindowRef(Rc<RefCell<Window>>);
+pub struct NonTilingWindowRef(Rc<RefCell<NonTilingWindow>>);
-// TODO: Consider renaming to `TilingWindow` and splitting out `NonTilingWindow`.
#[derive(Debug)]
-pub struct Window {
+pub struct NonTilingWindow {
width: u32,
height: u32,
x: u32,
y: u32,
}
-impl Window {
+impl NonTilingWindow {
pub fn new() -> Self {
Self {
width: 0,
diff --git a/packages/wm/src/windows/tiling_window.rs b/packages/wm/src/windows/tiling_window.rs
new file mode 100644
index 00000000..7b4d5914
--- /dev/null
+++ b/packages/wm/src/windows/tiling_window.rs
@@ -0,0 +1,23 @@
+use std::{cell::RefCell, rc::Rc};
+
+#[derive(Clone, Debug)]
+pub struct TilingWindowRef(Rc<RefCell<TilingWindow>>);
+
+#[derive(Debug)]
+pub struct TilingWindow {
+ width: u32,
+ height: u32,
+ x: u32,
+ y: u32,
+}
+
+impl TilingWindow {
+ pub fn new() -> Self {
+ Self {
+ width: 0,
+ height: 0,
+ x: 0,
+ y: 0,
+ }
+ }
+}
diff --git a/packages/wm/src/windows/traits/common_window.rs b/packages/wm/src/windows/traits/common_window.rs
new file mode 100644
index 00000000..8b137891
--- /dev/null
+++ b/packages/wm/src/windows/traits/common_window.rs
@@ -0,0 +1 @@
+
diff --git a/packages/wm/src/windows/traits/mod.rs b/packages/wm/src/windows/traits/mod.rs
new file mode 100644
index 00000000..bebbb070
--- /dev/null
+++ b/packages/wm/src/windows/traits/mod.rs
@@ -0,0 +1,3 @@
+mod common_window;
+
+pub use common_window::*;