Skip to content

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

feat: add `enum_dispatch`; rename `ContainerRef` -> `Container`

Lars Berger76b83d54458e
diff --git a/Cargo.lock b/Cargo.lock
index 25cfdf38..ad910c3d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -234,6 +234,18 @@ dependencies = [
"crypto-common",
]
+[[package]]
+name = "enum_dispatch"
+version = "0.3.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f33313078bb8d4d05a2733a94ac4c2d8a0df9a2b84424ebf4f33bfc224a890e"
+dependencies = [
+ "once_cell",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
[[package]]
name = "equivalent"
version = "1.0.1"
@@ -602,12 +614,6 @@ dependencies = [
"getrandom",
]
-[[package]]
-name = "rctree"
-version = "0.6.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e03e7866abec1101869ffa8e2c8355c4c2419d0214ece0cc3e428e5b94dea6e9"
-
[[package]]
name = "redox_syscall"
version = "0.4.1"
@@ -699,17 +705,6 @@ dependencies = [
"syn",
]
-[[package]]
-name = "serde_json"
-version = "1.0.114"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0"
-dependencies = [
- "itoa",
- "ryu",
- "serde",
-]
-
[[package]]
name = "serde_yaml"
version = "0.9.32"
@@ -1256,13 +1251,12 @@ version = "0.0.0"
dependencies = [
"anyhow",
"clap",
+ "enum_dispatch",
"futures-util",
"home",
"ipc-client",
- "rctree",
"regex",
"serde",
- "serde_json",
"serde_yaml",
"tokio",
"tokio-tungstenite",
diff --git a/packages/wm/Cargo.toml b/packages/wm/Cargo.toml
index b81de681..4ea3a255 100644
--- a/packages/wm/Cargo.toml
+++ b/packages/wm/Cargo.toml
@@ -12,12 +12,11 @@ watcher = { path = "../watcher", artifact = "bin" }
anyhow = { workspace = true }
clap = { version = "4", features = ["derive"] }
-rctree = "0.6"
+enum_dispatch = "0.3"
futures-util = "0.3"
home = "0.5"
uuid = { version = "1", features = ["v4"] }
serde = { version = "1", features = ["derive"] }
-serde_json = "1"
serde_yaml = "0.9"
regex = { workspace = true }
tokio = { workspace = true }
diff --git a/packages/wm/src/containers/container_ref.rs b/packages/wm/src/containers/container_ref.rs
index c087aed9..461ac081 100644
--- a/packages/wm/src/containers/container_ref.rs
+++ b/packages/wm/src/containers/container_ref.rs
@@ -1,13 +1,12 @@
+use enum_dispatch::enum_dispatch;
+
use crate::{
- monitors::MonitorRef,
- windows::{NonTilingWindowRef, TilingWindowRef},
- workspaces::WorkspaceRef,
+ monitors::Monitor,
+ windows::{NonTilingWindow, TilingWindow},
+ workspaces::Workspace,
};
-use super::{
- traits::{CommonContainer, TilingContainer},
- RootContainerRef, SplitContainerRef,
-};
+use super::{traits::TilingContainer, RootContainer, SplitContainer};
/// A reference to a container.
///
@@ -16,31 +15,21 @@ use super::{
///
/// **Note:** Cloning a `ContainerRef` only increments a reference count.
#[derive(Clone, Debug)]
-pub enum ContainerRef {
- RootContainer(RootContainerRef),
- Monitor(MonitorRef),
- Workspace(WorkspaceRef),
- SplitContainer(SplitContainerRef),
- TilingWindow(TilingWindowRef),
- NonTilingWindow(NonTilingWindowRef),
+#[enum_dispatch(CommonContainer)]
+pub enum Container {
+ Root(RootContainer),
+ Monitor(Monitor),
+ Workspace(Workspace),
+ Split(SplitContainer),
+ TilingWindow(TilingWindow),
+ NonTilingWindow(NonTilingWindow),
}
-impl ContainerRef {
- 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!(),
- }
- }
-
+impl Container {
pub fn as_tiling(&self) -> &dyn TilingContainer {
match self {
- ContainerRef::RootContainer(c) => c,
- ContainerRef::Monitor(c) => c,
+ Container::Root(c) => c,
+ Container::Monitor(c) => c,
// ContainerRef::Workspace(c) => c,
// ContainerRef::SplitContainer(c) => c,
// ContainerRef::Window(c) => c,
@@ -49,11 +38,24 @@ impl ContainerRef {
}
}
-#[derive(Clone, Debug)]
-pub enum TilingContainerRef {
- RootContainer(RootContainerRef),
- Monitor(MonitorRef),
- Workspace(WorkspaceRef),
- SplitContainer(SplitContainerRef),
- TilingWindow(TilingWindowRef),
-}
+// impl CommonContainer for ContainerRef {
+// fn id(&self) -> Uuid {
+// self.as_common().id()
+// }
+
+// fn r#type(&self) -> ContainerType {
+// self.as_common().r#type()
+// }
+
+// fn borrow_parent(&self) -> Ref<'_, Option<ContainerRef>> {
+// self.as_common().borrow_parent()
+// }
+
+// fn borrow_parent_mut(&self) -> RefMut<'_, Option<ContainerRef>> {
+// self.as_common().borrow_parent_mut()
+// }
+
+// fn parent(&self) -> Option<ContainerRef> {
+// self.as_common().parent()
+// }
+// }
diff --git a/packages/wm/src/containers/container_type.rs b/packages/wm/src/containers/container_type.rs
index e37696bd..487cfc94 100644
--- a/packages/wm/src/containers/container_type.rs
+++ b/packages/wm/src/containers/container_type.rs
@@ -3,9 +3,9 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Clone, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContainerType {
- RootContainer,
+ Root,
Monitor,
Workspace,
- SplitContainer,
+ Split,
Window,
}
diff --git a/packages/wm/src/containers/root_container.rs b/packages/wm/src/containers/root_container.rs
index 92526b5d..db4698c1 100644
--- a/packages/wm/src/containers/root_container.rs
+++ b/packages/wm/src/containers/root_container.rs
@@ -5,24 +5,26 @@ use std::{
use uuid::Uuid;
+use crate::impl_common_container;
+
use super::{
traits::{CommonContainer, TilingContainer},
- ContainerRef, ContainerType,
+ Container, ContainerType,
};
#[derive(Clone, Debug)]
-pub struct RootContainerRef(Rc<RefCell<RootContainer>>);
+pub struct RootContainer(Rc<RefCell<RootContainerInner>>);
#[derive(Debug)]
-pub struct RootContainer {
+struct RootContainerInner {
id: Uuid,
- pub parent: Option<ContainerRef>,
- pub children: Vec<ContainerRef>,
+ parent: Option<Container>,
+ children: Vec<Container>,
}
-impl RootContainerRef {
+impl RootContainer {
pub fn new() -> Self {
- let root = RootContainer {
+ let root = RootContainerInner {
id: Uuid::new_v4(),
parent: None,
children: Vec::new(),
@@ -32,30 +34,14 @@ 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)
- }
-
- fn borrow_parent_mut(&self) -> RefMut<'_, Option<ContainerRef>> {
- RefMut::map(self.0.borrow_mut(), |c| &mut c.parent)
- }
+impl_common_container!(RootContainer, ContainerType::Root);
- fn borrow_children(&self) -> Ref<'_, Vec<ContainerRef>> {
+impl TilingContainer for RootContainer {
+ fn borrow_tiling_children(&self) -> Ref<'_, Vec<Container>> {
Ref::map(self.0.borrow(), |c| &c.children)
}
- fn borrow_children_mut(&self) -> RefMut<'_, Vec<ContainerRef>> {
+ fn borrow_tiling_children_mut(&self) -> RefMut<'_, Vec<Container>> {
RefMut::map(self.0.borrow_mut(), |c| &mut c.children)
}
}
diff --git a/packages/wm/src/containers/split_container.rs b/packages/wm/src/containers/split_container.rs
index 1f0cd839..d2ce60d8 100644
--- a/packages/wm/src/containers/split_container.rs
+++ b/packages/wm/src/containers/split_container.rs
@@ -1,17 +1,49 @@
-use std::{cell::RefCell, rc::Rc};
+use std::{
+ cell::{Ref, RefCell, RefMut},
+ rc::Rc,
+};
-use crate::common::TilingDirection;
+use uuid::Uuid;
+
+use crate::{common::TilingDirection, impl_common_container};
+
+use super::{
+ traits::{CommonContainer, TilingContainer},
+ Container, ContainerType,
+};
#[derive(Clone, Debug)]
-pub struct SplitContainerRef(Rc<RefCell<SplitContainer>>);
+pub struct SplitContainer(Rc<RefCell<SplitContainerInner>>);
#[derive(Debug)]
-pub struct SplitContainer {
+pub struct SplitContainerInner {
+ id: Uuid,
+ parent: Option<Container>,
+ children: Vec<Container>,
tiling_direction: TilingDirection,
}
impl SplitContainer {
pub fn new(tiling_direction: TilingDirection) -> Self {
- Self { tiling_direction }
+ let split = SplitContainerInner {
+ id: Uuid::new_v4(),
+ parent: None,
+ children: Vec::new(),
+ tiling_direction,
+ };
+
+ Self(Rc::new(RefCell::new(split)))
+ }
+}
+
+impl_common_container!(SplitContainer, ContainerType::Split);
+
+impl TilingContainer for SplitContainer {
+ fn borrow_tiling_children(&self) -> Ref<'_, Vec<Container>> {
+ Ref::map(self.0.borrow(), |c| &c.children)
+ }
+
+ fn borrow_tiling_children_mut(&self) -> RefMut<'_, Vec<Container>> {
+ RefMut::map(self.0.borrow_mut(), |c| &mut c.children)
}
}
diff --git a/packages/wm/src/containers/traits/common_container.rs b/packages/wm/src/containers/traits/common_container.rs
index 4a39841a..19e12fcd 100644
--- a/packages/wm/src/containers/traits/common_container.rs
+++ b/packages/wm/src/containers/traits/common_container.rs
@@ -1,9 +1,61 @@
+use std::cell::{Ref, RefMut};
+
+use enum_dispatch::enum_dispatch;
use uuid::Uuid;
-use crate::containers::ContainerType;
+use crate::{
+ containers::{Container, ContainerType, RootContainer, SplitContainer},
+ monitors::Monitor,
+ windows::{NonTilingWindow, TilingWindow},
+ workspaces::Workspace,
+};
+#[enum_dispatch(ContainerRef)]
pub trait CommonContainer {
+ /// A unique identifier for the container.
fn id(&self) -> Uuid;
+ /// Derived container type (eg. `ContainerType::Monitor`).
fn r#type(&self) -> ContainerType;
+
+ fn borrow_parent(&self) -> Ref<'_, Option<Container>>;
+
+ fn borrow_parent_mut(&self) -> RefMut<'_, Option<Container>>;
+
+ /// Returns a reference to the parent container, unless this container is
+ /// the root container.
+ ///
+ /// # Panics
+ ///
+ /// Panics if the container is currently mutably borrowed.
+ fn parent(&self) -> Option<Container> {
+ self.borrow_parent().clone()
+ }
+}
+
+/// Implements the `CommonContainer` trait for a given struct.
+///
+/// Expects that the struct has a wrapping `RefCell` containing a struct
+/// with an `id` and a `parent` field.
+#[macro_export]
+macro_rules! impl_common_container {
+ ($struct_name:ident, $container_type:expr) => {
+ impl CommonContainer for $struct_name {
+ fn id(&self) -> Uuid {
+ self.0.borrow().id
+ }
+
+ fn r#type(&self) -> ContainerType {
+ $container_type
+ }
+
+ fn borrow_parent(&self) -> Ref<'_, Option<Container>> {
+ Ref::map(self.0.borrow(), |c| &c.parent)
+ }
+
+ fn borrow_parent_mut(&self) -> RefMut<'_, Option<Container>> {
+ RefMut::map(self.0.borrow_mut(), |c| &mut c.parent)
+ }
+ }
+ };
}
diff --git a/packages/wm/src/containers/traits/tiling_container.rs b/packages/wm/src/containers/traits/tiling_container.rs
index b48505d8..82107731 100644
--- a/packages/wm/src/containers/traits/tiling_container.rs
+++ b/packages/wm/src/containers/traits/tiling_container.rs
@@ -1,34 +1,23 @@
use std::cell::{Ref, RefMut};
-use crate::containers::ContainerRef;
+use crate::containers::Container;
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>>;
- fn borrow_children_mut(&self) -> RefMut<'_, Vec<ContainerRef>>;
+ fn borrow_tiling_children(&self) -> Ref<'_, Vec<Container>>;
- /// Returns 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> {
- self.borrow_parent().clone()
+ fn borrow_tiling_children_mut(&self) -> RefMut<'_, Vec<Container>>;
+
+ fn tiling_children(&self) -> Vec<Container> {
+ self.borrow_tiling_children().clone()
}
- fn insert_child(&self, target_index: usize, child: ContainerRef) {
+ fn insert_tiling_child(&self, target_index: usize, child: Container) {
self
- .borrow_children_mut()
+ .borrow_tiling_children_mut()
.insert(target_index, child.clone());
*child.as_tiling().borrow_parent_mut() = Some(child.clone());
}
-
- fn grandparent(&self) -> Option<ContainerRef> {
- self.parent()?.as_tiling().parent()
- }
}
diff --git a/packages/wm/src/monitors/monitor.rs b/packages/wm/src/monitors/monitor.rs
index 19b936d3..ba51f777 100644
--- a/packages/wm/src/monitors/monitor.rs
+++ b/packages/wm/src/monitors/monitor.rs
@@ -9,66 +9,43 @@ use crate::{
common::platform::NativeMonitor,
containers::{
traits::{CommonContainer, TilingContainer},
- ContainerRef, ContainerType,
+ Container, ContainerType,
},
+ impl_common_container,
};
#[derive(Clone, Debug)]
-pub struct MonitorRef(Rc<RefCell<Monitor>>);
+pub struct Monitor(Rc<RefCell<MonitorInner>>);
#[derive(Debug)]
-pub struct Monitor {
+struct MonitorInner {
id: Uuid,
- pub parent: Option<ContainerRef>,
- pub children: Vec<ContainerRef>,
- pub device_name: String,
- pub width: i32,
- pub height: i32,
- pub x: i32,
- pub y: i32,
+ parent: Option<Container>,
+ children: Vec<Container>,
+ native: NativeMonitor,
}
-impl MonitorRef {
+impl Monitor {
pub fn new(native_monitor: NativeMonitor) -> Self {
- let monitor = Monitor {
+ let monitor = MonitorInner {
id: Uuid::new_v4(),
parent: None,
children: Vec::new(),
- device_name: native_monitor.device_name,
- width: native_monitor.width,
- height: native_monitor.height,
- x: native_monitor.x,
- y: native_monitor.y,
+ native: native_monitor,
};
Self(Rc::new(RefCell::new(monitor)))
}
}
-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)
- }
-
- fn borrow_parent_mut(&self) -> RefMut<'_, Option<ContainerRef>> {
- RefMut::map(self.0.borrow_mut(), |c| &mut c.parent)
- }
+impl_common_container!(Monitor, ContainerType::Monitor);
- fn borrow_children(&self) -> Ref<'_, Vec<ContainerRef>> {
+impl TilingContainer for Monitor {
+ fn borrow_tiling_children(&self) -> Ref<'_, Vec<Container>> {
Ref::map(self.0.borrow(), |c| &c.children)
}
- fn borrow_children_mut(&self) -> RefMut<'_, Vec<ContainerRef>> {
+ fn borrow_tiling_children_mut(&self) -> RefMut<'_, Vec<Container>> {
RefMut::map(self.0.borrow_mut(), |c| &mut c.children)
}
}
diff --git a/packages/wm/src/windows/mod.rs b/packages/wm/src/windows/mod.rs
index 57f33996..92b60012 100644
--- a/packages/wm/src/windows/mod.rs
+++ b/packages/wm/src/windows/mod.rs
@@ -1,4 +1,4 @@
-pub mod commands;
+// pub mod commands;
mod non_tiling_window;
mod tiling_window;
pub mod traits;
diff --git a/packages/wm/src/windows/non_tiling_window.rs b/packages/wm/src/windows/non_tiling_window.rs
index 30958eea..27f3f820 100644
--- a/packages/wm/src/windows/non_tiling_window.rs
+++ b/packages/wm/src/windows/non_tiling_window.rs
@@ -1,27 +1,36 @@
-use std::{cell::RefCell, rc::Rc};
+use std::{
+ cell::{Ref, RefCell, RefMut},
+ rc::Rc,
+};
-use crate::workspaces::WorkspaceRef;
+use uuid::Uuid;
+
+use crate::{
+ common::platform::NativeWindow,
+ containers::{traits::CommonContainer, Container, ContainerType},
+ impl_common_container,
+};
#[derive(Clone, Debug)]
-pub struct NonTilingWindowRef(Rc<RefCell<NonTilingWindow>>);
+pub struct NonTilingWindow(Rc<RefCell<NonTilingWindowInner>>);
#[derive(Debug)]
-pub struct NonTilingWindow {
- parent: Option<WorkspaceRef>,
- width: u32,
- height: u32,
- x: u32,
- y: u32,
+struct NonTilingWindowInner {
+ id: Uuid,
+ parent: Option<Container>,
+ native: NativeWindow,
}
impl NonTilingWindow {
- pub fn new() -> Self {
- Self {
+ pub fn new(native_window: NativeWindow) -> Self {
+ let window = NonTilingWindowInner {
+ id: Uuid::new_v4(),
parent: None,
- width: 0,
- height: 0,
- x: 0,
- y: 0,
- }
+ native: native_window,
+ };
+
+ Self(Rc::new(RefCell::new(window)))
}
}
+
+impl_common_container!(NonTilingWindow, ContainerType::Window);
diff --git a/packages/wm/src/windows/tiling_window.rs b/packages/wm/src/windows/tiling_window.rs
index 7b4d5914..90ac00aa 100644
--- a/packages/wm/src/windows/tiling_window.rs
+++ b/packages/wm/src/windows/tiling_window.rs
@@ -1,23 +1,51 @@
-use std::{cell::RefCell, rc::Rc};
+use std::{
+ cell::{Ref, RefCell, RefMut},
+ rc::Rc,
+};
+
+use uuid::Uuid;
+
+use crate::{
+ common::platform::NativeWindow,
+ containers::{
+ traits::{CommonContainer, TilingContainer},
+ Container, ContainerType,
+ },
+ impl_common_container,
+};
#[derive(Clone, Debug)]
-pub struct TilingWindowRef(Rc<RefCell<TilingWindow>>);
+pub struct TilingWindow(Rc<RefCell<TilingWindowInner>>);
#[derive(Debug)]
-pub struct TilingWindow {
- width: u32,
- height: u32,
- x: u32,
- y: u32,
+struct TilingWindowInner {
+ id: Uuid,
+ parent: Option<Container>,
+ children: Vec<Container>,
+ native: NativeWindow,
}
impl TilingWindow {
- pub fn new() -> Self {
- Self {
- width: 0,
- height: 0,
- x: 0,
- y: 0,
- }
+ pub fn new(native_window: NativeWindow) -> Self {
+ let window = TilingWindowInner {
+ id: Uuid::new_v4(),
+ parent: None,
+ children: Vec::new(),
+ native: native_window,
+ };
+
+ Self(Rc::new(RefCell::new(window)))
+ }
+}
+
+impl_common_container!(TilingWindow, ContainerType::Window);
+
+impl TilingContainer for TilingWindow {
+ fn borrow_tiling_children(&self) -> Ref<'_, Vec<Container>> {
+ Ref::map(self.0.borrow(), |c| &c.children)
+ }
+
+ fn borrow_tiling_children_mut(&self) -> RefMut<'_, Vec<Container>> {
+ RefMut::map(self.0.borrow_mut(), |c| &mut c.children)
}
}
diff --git a/packages/wm/src/wm_event.rs b/packages/wm/src/wm_event.rs
index b97094e3..84967d5d 100644
--- a/packages/wm/src/wm_event.rs
+++ b/packages/wm/src/wm_event.rs
@@ -2,7 +2,7 @@ use uuid::Uuid;
use crate::{
common::{platform::WindowHandle, TilingDirection},
- containers::ContainerRef,
+ containers::Container,
};
#[derive(Debug)]
@@ -12,17 +12,17 @@ pub struct BindingModeChangedEvent {
#[derive(Debug)]
pub struct FocusChangedEvent {
- pub focused_container: ContainerRef,
+ pub focused_container: Container,
}
#[derive(Debug)]
pub struct FocusedContainerMovedEvent {
- pub focused_container: ContainerRef,
+ pub focused_container: Container,
}
#[derive(Debug)]
pub struct MonitorAddedEvent {
- pub added_monitor: ContainerRef,
+ pub added_monitor: Container,
}
#[derive(Debug)]
@@ -33,7 +33,7 @@ pub struct MonitorRemovedEvent {
#[derive(Debug)]
pub struct NativeFocusSyncedEvent {
- pub focused_container: ContainerRef,
+ pub focused_container: Container,
}
#[derive(Debug)]
@@ -43,7 +43,7 @@ pub struct TilingDirectionChangedEvent {
#[derive(Debug)]
pub struct WindowManagedEvent {
- pub managed_window: ContainerRef,
+ pub managed_window: Container,
}
#[derive(Debug)]
@@ -54,7 +54,7 @@ pub struct WindowUnmanagedEvent {
#[derive(Debug)]
pub struct WorkspaceActivatedEvent {
- pub activated_workspace: ContainerRef,
+ pub activated_workspace: Container,
}
#[derive(Debug)]
@@ -65,7 +65,7 @@ pub struct WorkspaceDeactivatedEvent {
#[derive(Debug)]
pub struct WorkingAreaResizedEvent {
- pub affected_monitor: ContainerRef,
+ pub affected_monitor: Container,
}
#[derive(Debug)]
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index 4e3c3ca1..60557285 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -16,10 +16,10 @@ use crate::{
FocusMode,
},
containers::{
- traits::TilingContainer, ContainerRef, ContainerType, RootContainer,
- RootContainerRef,
+ traits::{CommonContainer, TilingContainer},
+ Container, ContainerType, RootContainer,
},
- monitors::{Monitor, MonitorRef},
+ monitors::Monitor,
user_config::{BindingModeConfig, UserConfig},
wm_event::WmEvent,
};
@@ -27,10 +27,10 @@ use crate::{
pub struct WmState {
/// Root node of the container tree. Monitors are the children of the
/// root node, followed by workspaces, then split containers/windows.
- root_container: RootContainerRef,
+ root_container: RootContainer,
/// Containers (and their descendants) that have a pending redraw.
- containers_to_redraw: Vec<Weak<ContainerRef>>,
+ containers_to_redraw: Vec<Weak<Container>>,
/// Whether native focus needs to be reassigned to the WM's focused
/// container.
@@ -54,7 +54,7 @@ impl WmState {
event_tx: UnboundedSender<WmEvent>,
) -> Self {
Self {
- root_container: RootContainerRef::new(),
+ root_container: RootContainer::new(),
containers_to_redraw: Vec::new(),
has_pending_focus_sync: false,
binding_modes: Vec::new(),
@@ -69,27 +69,27 @@ impl WmState {
let native_monitors = Platform::monitors()?;
for native_monitor in native_monitors {
- let monitor = MonitorRef::new(native_monitor);
+ let monitor = Monitor::new(native_monitor);
self
.root_container
- .insert_child(0, ContainerRef::Monitor(monitor));
+ .insert_tiling_child(0, Container::Monitor(monitor));
}
for native_window in Platform::manageable_windows()? {
// let window = ContainerRef::Window(native_window);
- // self.root_container.insert_child(0, window);
+ // self.root_container.insert_tiling_child(0, window);
}
Ok(())
}
- pub fn nearest_monitor(&self) -> Option<MonitorRef> {
+ pub fn nearest_monitor(&self) -> Option<Monitor> {
todo!()
}
pub fn add_monitor(&mut self) {
- let monitor = MonitorRef::new(NativeMonitor::new(
+ let monitor = Monitor::new(NativeMonitor::new(
windows::Win32::Graphics::Gdi::HMONITOR(1),
String::from("aaa"),
0,
@@ -98,9 +98,10 @@ impl WmState {
0,
));
+ let xx = self.root_container.parent().unwrap().parent();
self
.root_container
- .insert_child(0, ContainerRef::Monitor(monitor))
+ .insert_tiling_child(0, Container::Monitor(monitor))
}
pub fn emit_event(&self, event: WmEvent) -> Result<()> {
diff --git a/packages/wm/src/workspaces/workspace.rs b/packages/wm/src/workspaces/workspace.rs
index 72d0b73a..c8fb3424 100644
--- a/packages/wm/src/workspaces/workspace.rs
+++ b/packages/wm/src/workspaces/workspace.rs
@@ -1,34 +1,56 @@
-use std::{cell::RefCell, rc::Rc};
+use std::{
+ cell::{Ref, RefCell, RefMut},
+ rc::Rc,
+};
-use crate::{common::RectDelta, containers::ContainerRef};
+use uuid::Uuid;
+
+use crate::{
+ common::RectDelta,
+ containers::{
+ traits::{CommonContainer, TilingContainer},
+ Container, ContainerType,
+ },
+ impl_common_container,
+ user_config::WorkspaceConfig,
+};
#[derive(Clone, Debug)]
-pub struct WorkspaceRef(Rc<RefCell<Workspace>>);
+pub struct Workspace(Rc<RefCell<WorkspaceInner>>);
#[derive(Debug)]
-pub struct Workspace {
- name: String,
- display_name: String,
- keep_alive: bool,
+struct WorkspaceInner {
+ id: Uuid,
+ parent: Option<Container>,
+ tiling_children: Vec<Container>,
+ non_tiling_children: Vec<Container>,
+ config: WorkspaceConfig,
outer_gaps: RectDelta,
- tiling_children: Vec<ContainerRef>,
- non_tiling_children: Vec<ContainerRef>,
}
impl Workspace {
- pub fn new(
- name: String,
- display_name: String,
- keep_alive: bool,
- outer_gaps: RectDelta,
- ) -> Self {
- Self {
- name,
- display_name,
- keep_alive,
- outer_gaps,
+ pub fn new(config: WorkspaceConfig, outer_gaps: RectDelta) -> Self {
+ let workspace = WorkspaceInner {
+ id: Uuid::new_v4(),
+ parent: None,
tiling_children: Vec::new(),
non_tiling_children: Vec::new(),
- }
+ config,
+ outer_gaps,
+ };
+
+ Self(Rc::new(RefCell::new(workspace)))
+ }
+}
+
+impl_common_container!(Workspace, ContainerType::Workspace);
+
+impl TilingContainer for Workspace {
+ fn borrow_tiling_children(&self) -> Ref<'_, Vec<Container>> {
+ Ref::map(self.0.borrow(), |c| &c.tiling_children)
+ }
+
+ fn borrow_tiling_children_mut(&self) -> RefMut<'_, Vec<Container>> {
+ RefMut::map(self.0.borrow_mut(), |c| &mut c.tiling_children)
}
}