Skip to content

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

feat: add wm state

Lars Bergercd1a4c836973
diff --git a/packages/wm/src/common/focus_mode.rs b/packages/wm/src/common/focus_mode.rs
new file mode 100644
index 00000000..edb1a8f2
--- /dev/null
+++ b/packages/wm/src/common/focus_mode.rs
@@ -0,0 +1,4 @@
+pub enum FocusMode {
+ Tiling,
+ Floating,
+}
diff --git a/packages/wm/src/common/mod.rs b/packages/wm/src/common/mod.rs
index 3f5a86f4..7b20f3b4 100644
--- a/packages/wm/src/common/mod.rs
+++ b/packages/wm/src/common/mod.rs
@@ -1,5 +1,7 @@
+mod focus_mode;
mod rect_delta;
mod units;
+pub use focus_mode::*;
pub use rect_delta::*;
pub use units::*;
diff --git a/packages/wm/src/common/units.rs b/packages/wm/src/common/units.rs
index a350b3af..fca9b652 100644
--- a/packages/wm/src/common/units.rs
+++ b/packages/wm/src/common/units.rs
@@ -7,7 +7,7 @@ pub enum Unit {
None,
}
-/// Parses a string containing a number followed by a unit (px, %).
+/// Parses a string containing a number followed by a unit (`px`, `%`).
///
/// Returns a tuple where the first element is a float, and the second
/// element is the unit as an enum value.
diff --git a/packages/wm/src/containers/container.rs b/packages/wm/src/containers/container.rs
index 43d91828..b6be873b 100644
--- a/packages/wm/src/containers/container.rs
+++ b/packages/wm/src/containers/container.rs
@@ -4,17 +4,17 @@ use uuid::Uuid;
use super::container_type::ContainerType;
-pub struct ContainerInner {
+pub struct InnerContainer {
id: Uuid,
- parent: Option<Box<ContainerInner>>,
- children: Vec<Box<ContainerInner>>,
- child_focus_order: Vec<Box<ContainerInner>>,
+ parent: Option<Arc<dyn Container>>,
+ children: Vec<Arc<dyn Container>>,
+ child_focus_order: Vec<Arc<dyn Container>>,
}
-impl ContainerInner {
- fn new(
- parent: Option<Box<ContainerInner>>,
- children: Vec<Box<ContainerInner>>,
+impl InnerContainer {
+ pub fn new(
+ parent: Option<Arc<dyn Container>>,
+ children: Vec<Arc<dyn Container>>,
) -> Self {
Self {
id: Uuid::new_v4(),
@@ -24,18 +24,23 @@ impl ContainerInner {
}
}
- fn set_parent(&mut self, parent: ContainerInner) -> () {
- self.parent = Some(Box::new(parent))
+ pub fn set_parent(&mut self, parent: Arc<dyn Container>) {
+ self.parent = Some(parent);
}
-}
-pub trait Container {
- /// A unique identifier for the container.
- fn inner(&self) -> ContainerInner;
+ pub fn set_children(&mut self, children: Vec<Arc<dyn Container>>) {
+ self.children = children;
+ }
- /// A unique identifier for the container.
- fn id(&self) -> Uuid;
+ pub fn set_child_focus_order(
+ &mut self,
+ child_focus_order: Vec<Arc<dyn Container>>,
+ ) {
+ self.child_focus_order = child_focus_order;
+ }
+}
+pub trait Container {
/// Derived container type (eg. `ContainerType::Monitor`).
fn r#type(&self) -> ContainerType;
@@ -44,32 +49,56 @@ pub trait Container {
fn x(&self) -> u32;
fn y(&self) -> u32;
- fn parent(&self) -> Option<Box<dyn Container>>;
+ /// A unique identifier for the container.
+ fn inner(&self) -> InnerContainer;
+
+ /// A unique identifier for the container.
+ fn id(&self) -> Uuid {
+ self.inner().id
+ }
- fn set_parent(&mut self, parent: &dyn Container) -> () {
- self.inner().set_parent(parent.inner())
+ fn parent(&self) -> Option<Arc<dyn Container>> {
+ self.inner().parent
}
- fn children(&self) -> Vec<Box<dyn Container>> {
- // ??????
+ fn set_parent(&mut self, parent: Arc<dyn Container>) {
+ self.inner().set_parent(parent)
}
- fn set_children(&self, children: Vec<Box<dyn Container>>) -> ();
+ fn children(&self) -> Vec<Arc<dyn Container>> {
+ self.inner().children
+ }
+
+ /// Order of which child containers last had focus.
+ fn child_focus_order(&self) -> Vec<Arc<dyn Container>> {
+ self.inner().child_focus_order
+ }
- /// The order of which child containers last had focus.
- fn child_focus_order(&self) -> Vec<Box<dyn Container>>;
fn set_child_focus_order(
&self,
- child_focus_order: Vec<Box<dyn Container>>,
- ) -> ();
+ child_focus_order: Vec<Arc<dyn Container>>,
+ ) {
+ self.inner().set_child_focus_order(child_focus_order)
+ }
- /// The child container that last had focus.
+ /// Child container that last had focus.
fn last_focused_child(&self) -> Option<Arc<dyn Container>> {
self.child_focus_order().get(0).cloned()
}
- // /// Index of this container in parent's child focus order.
- // public int FocusIndex => this is RootContainer ? 0 : Parent.ChildFocusOrder.IndexOf(this);
+ /// Index of this container in parent's child focus order.
+ fn focus_index(&self) -> u32 {
+ match self.inner().parent {
+ None => 0,
+ Some(p) => p
+ .child_focus_order()
+ .iter()
+ .position(|child| child.id() == self.id())
+ .unwrap()
+ .try_into()
+ .unwrap(),
+ }
+ }
// public List<Container> SelfAndSiblings =>
// this is RootContainer ? new List<Container>() { this } : Parent.Children;
diff --git a/packages/wm/src/containers/root_container.rs b/packages/wm/src/containers/root_container.rs
index 03cc6160..2e10cbf3 100644
--- a/packages/wm/src/containers/root_container.rs
+++ b/packages/wm/src/containers/root_container.rs
@@ -1,39 +1,32 @@
-use std::sync::Arc;
-
use uuid::Uuid;
-use crate::{
- containers::{Container, ContainerType},
- workspaces::Workspace,
-};
+use crate::containers::{Container, ContainerType};
+
+use super::InnerContainer;
pub struct RootContainer {
- id: Uuid,
- children: Vec<Arc<Workspace>>,
- child_focus_order: Vec<Arc<Workspace>>,
width: u32,
height: u32,
x: u32,
y: u32,
+ inner: InnerContainer,
}
impl RootContainer {
- pub fn new(width: u32, height: u32, x: u32, y: u32) -> Self {
+ pub fn new() -> Self {
Self {
- id: Uuid::new_v4(),
- children: Vec::new(),
- child_focus_order: Vec::new(),
- width,
- height,
- x,
- y,
+ inner: InnerContainer::new(None, vec![]),
+ width: 0,
+ height: 0,
+ x: 0,
+ y: 0,
}
}
}
impl Container for RootContainer {
- fn id(&self) -> Uuid {
- self.id
+ fn inner(&self) -> InnerContainer {
+ self.inner
}
fn r#type(&self) -> ContainerType {
@@ -55,31 +48,4 @@ impl Container for RootContainer {
fn y(&self) -> u32 {
self.y
}
-
- fn parent(&self) -> Arc<dyn Container> {
- todo!()
- }
-
- fn children(&self) -> Vec<Arc<dyn Container>> {
- self.children
- }
-
- fn child_focus_order(&self) -> Vec<Arc<dyn Container>> {
- self.child_focus_order
- }
-
- fn set_parent(&mut self, parent: Arc<dyn Container>) -> () {
- self.parent = parent
- }
-
- fn set_children(&self, children: Vec<Arc<dyn Container>>) -> () {
- self.children = children
- }
-
- fn set_child_focus_order(
- &self,
- child_focus_order: Vec<Arc<dyn Container>>,
- ) -> () {
- self.child_focus_order = child_focus_order
- }
}
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index e69de29b..18e55d84 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -0,0 +1,64 @@
+use std::sync::Arc;
+use uuid::Uuid;
+
+use crate::{
+ common::FocusMode,
+ containers::{Container, ContainerType, RootContainer},
+};
+
+pub struct WmState {
+ /// The root node of the container tree. Monitors are the children of the
+ /// root node, followed by workspaces, then split containers/windows.
+ root_container: RootContainer,
+
+ /// Containers (and their descendants) to redraw on the next invocation
+ /// of `RedrawContainersCommand`.
+ containers_to_redraw: Vec<Arc<dyn Container>>,
+
+ /// Whether native focus needs to be reassigned to the WM's focused
+ /// container.
+ has_pending_focus_sync: bool,
+
+ /// Name of the currently active binding mode (if one is active).
+ active_binding_mode: String,
+}
+
+impl WmState {
+ pub fn new() -> Self {
+ Self {
+ root_container: RootContainer::new(),
+ containers_to_redraw: Vec::new(),
+ has_pending_focus_sync: false,
+ active_binding_mode: String::new(),
+ }
+ }
+
+ /// The currently focused container. This can either be a `Window` or a
+ /// `Workspace` without any descendant windows.
+ pub fn focused_container(&self) -> Arc<dyn Container> {
+ self
+ .root_container
+ .last_focused_descendant()
+ .unwrap()
+ .clone()
+ }
+
+ /// Whether a tiling or floating container is currently focused.
+ pub fn focus_mode(&self) -> FocusMode {
+ match self.focused_container().r#type() {
+ ContainerType::FloatingWindow => FocusMode::Floating,
+ _ => FocusMode::Tiling,
+ }
+ }
+
+ pub fn get_container_by_id(
+ &self,
+ id: Uuid,
+ ) -> Option<Arc<dyn Container>> {
+ self
+ .root_container
+ .self_and_descendants()
+ .into_iter()
+ .find(|container| container.id() == id)
+ }
+}