GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
wip
Lars Berger00f57571da2c
diff --git a/packages/wm/src/containers/container.rs b/packages/wm/src/containers/container.rsindex 12f17ce9..131e8469 100644--- a/packages/wm/src/containers/container.rs+++ b/packages/wm/src/containers/container.rs@@ -1,13 +1,46 @@-use std::sync::Arc;+use std::{+ cell::RefCell,+ collections::VecDeque,+ rc::{Rc, Weak},+ sync::Arc,+};use uuid::Uuid;-use crate::{monitors::Monitor, windows::Window, workspaces::Workspace};--use super::{- container_type::ContainerType, RootContainer, SplitContainer,+use crate::{+ monitors::Monitor,+ windows::{Window, WindowState},+ workspaces::Workspace,};+use super::{ContainerType, RootContainer, SplitContainer};++#[derive(Debug)]+pub struct InnerContainer {+ pub id: Uuid,+ container_type: ContainerType,+ pub parent: Option<Weak<RefCell<Container>>>,+ pub children: VecDeque<Rc<RefCell<Container>>>,+ // TODO: Use vec of ids instead and a getter to get the actual containers.+ pub child_focus_order: VecDeque<Rc<RefCell<Container>>>,+}++impl InnerContainer {+ pub fn new(+ parent: Option<Arc<Container>>,+ children: Vec<Arc<Container>>,+ ) -> Self {+ Self {+ id: Uuid::new_v4(),+ // TODO+ container_type: ContainerType::RootContainer,+ parent,+ children,+ child_focus_order: children,+ }+ }+}+#[derive(Debug)]pub enum Container {RootContainer(RootContainer),@@ -39,24 +72,56 @@ impl Container {}/// Height of the container. Implementation varies by container type.- fn height(&self) -> u32 {- match self {- Self::RootContainer(c) => c.height(),- Self::Monitor(c) => c.height(),- Self::Workspace(c) => c.height(),+ pub fn height(&self) -> u32 {+ match self.value {+ Self::Monitor(c) => c.height,+ Self::Workspace(c) => c.height,Self::SplitContainer(c) => c.height(),Self::Window(c) => c.height(),+ _ => 0,}}/// Width of the container. Implementation varies by container type.- fn width(&self) -> u32;+ pub fn width(&self) -> u32 {+ match self.value {+ Self::Monitor(c) => c.width,+ Self::Workspace(c) => c.width,+ Self::SplitContainer(c) => c.width(),+ Self::Window(c) => c.width(),+ _ => 0,+ }+ }/// X-coordinate of the container. Implementation varies by container type.- fn x(&self) -> u32;+ pub fn x(&self) -> u32 {+ match self.value {+ Self::Monitor(c) => c.x,+ Self::Workspace(c) => c.x,+ Self::SplitContainer(c) => c.x(),+ Self::Window(c) => c.x(),+ _ => 0,+ }+ }/// Y-coordinate of the container. Implementation varies by container type.- fn y(&self) -> u32;+ pub fn y(&self) -> u32 {+ match self.value {+ Self::Monitor(c) => c.y,+ Self::Workspace(c) => c.y,+ Self::SplitContainer(c) => c.y(),+ Self::Window(c) => c.y(),+ _ => 0,+ }+ }++ /// Whether the container can be tiled.+ pub fn can_tile(&self) -> bool {+ match self.value {+ Self::Window(c) => c.state == WindowState::Tiling,+ _ => true,+ }+ }/// Unique identifier for the container.fn id(&self) -> Uuid {@@ -110,396 +175,3 @@ impl Container {}}}--#[derive(Debug)]-pub struct InnerContainer {- pub id: Uuid,- pub parent: Option<Arc<Container>>,- pub children: Vec<Arc<Container>>,- pub child_focus_order: Vec<Arc<Container>>,-}--impl InnerContainer {- pub fn new(- parent: Option<Arc<Container>>,- children: Vec<Arc<Container>>,- ) -> Self {- Self {- id: Uuid::new_v4(),- parent,- children,- child_focus_order: children,- }- }-}--pub trait ContainerVariant {- /// Derived container type (eg. `ContainerType::Monitor`).- fn r#type(&self) -> ContainerType;-- /// Height of the container. Implementation varies by container type.- fn height(&self) -> u32;-- /// Width of the container. Implementation varies by container type.- fn width(&self) -> u32;-- /// X-coordinate of the container. Implementation varies by container type.- fn x(&self) -> u32;-- /// Y-coordinate of the container. Implementation varies by container type.- fn y(&self) -> u32;-- /// A unique identifier for the container.- fn inner(&self) -> InnerContainer;-- /// Unique identifier for the container.- fn id(&self) -> Uuid {- self.inner().id- }-- fn parent(&self) -> Option<Arc<Container>> {- self.inner().parent- }-- fn set_parent(&mut self, parent: Option<Arc<Container>>) {- self.inner().parent = parent;- }-- fn children(&self) -> Vec<Arc<Container>> {- self.inner().children- }-- /// Order of which child containers last had focus.- fn child_focus_order(&self) -> Vec<Arc<Container>> {- self.inner().child_focus_order- }-- fn set_child_focus_order(- &mut self,- child_focus_order: Vec<Arc<Container>>,- ) {- self.inner().child_focus_order = child_focus_order;- }-- /// Child container that last had focus.- fn last_focused_child(&self) -> Option<Arc<Container>> {- self.child_focus_order().get(0).cloned()- }-- /// 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;-- // public IEnumerable<Container> Siblings =>- // this is RootContainer- // ? Array.Empty<Container>()- // : Parent.Children.Where(children => children != this);-- // /// Index of this container amongst its siblings.- // public int Index => this is RootContainer ? 0 : Parent.Children.IndexOf(this);-- // /// Get the last focused descendant by traversing downwards.- // public Container LastFocusedDescendant- // {- // get- // {- // var lastFocusedDescendant = LastFocusedChild;-- // while (lastFocusedDescendant?.LastFocusedChild != null)- // lastFocusedDescendant = lastFocusedDescendant.LastFocusedChild;-- // return lastFocusedDescendant;- // }- // }-- // /// The sibling at the next index to this container.- // public Container NextSibling => SelfAndSiblings.ElementAtOrDefault(Index + 1);-- // /// The sibling at the previous index to this container.- // public Container PreviousSibling => SelfAndSiblings.ElementAtOrDefault(Index - 1);-- // // TODO: Rename to SelfAndDescendants and change to getter.- // public IEnumerable<Container> Flatten()- // {- // return new[] { this }.Concat(Children.SelectMany(children => children.Flatten()));- // }-- // public IEnumerable<Container> SelfAndAncestors => new[] { this }.Concat(Ancestors);-- // public IEnumerable<Container> Ancestors- // {- // get- // {- // var parent = Parent;-- // while (parent != null)- // {- // yield return parent;- // parent = parent.Parent;- // }- // }- // }-- // public IEnumerable<Container> SelfAndDescendants => new[] { this }.Concat(Descendants);-- // /// Breadth-first downward traversal from a single container.- // public IEnumerable<Container> Descendants- // {- // get- // {- // var queue = new Queue<Container>();-- // foreach (var child in Children)- // queue.Enqueue(child);-- // while (queue.Count > 0)- // {- // var current = queue.Dequeue();- // yield return current;- // foreach (var child in current.Children)- // queue.Enqueue(child);- // }- // }- // }-- // /// Leaf nodes (ie. windows and workspaces) in order of last focus.- // public IEnumerable<Container> DescendantFocusOrder- // {- // get- // {- // var stack = new Stack<Container>();- // stack.Push(this);-- // // Do a depth-first search using child focus order.- // while (stack.Count > 0)- // {- // var current = stack.Pop();-- // // Get containers that have no children. Descendant also cannot be the container itself.- // if (current != this && !current.HasChildren())- // yield return current;-- // // Reverse the child focus order so that the first element is pushed last/popped first.- // foreach (var focusChild in current.ChildFocusOrder.AsEnumerable().Reverse())- // stack.Push(focusChild);- // }- // }- // }-- // public void InsertChild(int targetIndex, Container child)- // {- // Children.Insert(targetIndex, child);- // ChildFocusOrder.Add(child);- // child.Parent = this;- // }-- // public void RemoveChild(Container child)- // {- // child.Parent = null;- // Children.Remove(child);- // ChildFocusOrder.Remove(child);- // }-- // public bool IsDetached()- // {- // return Parent is null || Index == -1;- // }-- // public bool HasChildren()- // {- // return Children.Count > 0;- // }-- // public bool HasSiblings()- // {- // return Siblings.Any();- // }-- // public Rect ToRect()- // {- // return new Rect()- // {- // Left = X,- // Right = X + Width,- // Top = Y,- // Bottom = Y + Height,- // };- // }-- // public IEnumerable<Container> ChildrenOfType<T>()- // {- // return Children.Where(container => typeof(T).IsAssignableFrom(container.GetType()));- // }-- // public IEnumerable<Container> SiblingsOfType<T>()- // {- // return Siblings.Where(container => typeof(T).IsAssignableFrom(container.GetType()));- // }-- // public IEnumerable<Container> SelfAndSiblingsOfType<T>()- // {- // return SelfAndSiblings.Where(container => typeof(T).IsAssignableFrom(container.GetType()));- // }-- // public Container NextSiblingOfType<T>()- // {- // return SelfAndSiblings- // .Skip(Index + 1)- // .FirstOrDefault(container => typeof(T).IsAssignableFrom(container.GetType()));- // }-- // public Container PreviousSiblingOfType<T>()- // {- // return SelfAndSiblings- // .Take(Index)- // .Reverse()- // .FirstOrDefault(container => typeof(T).IsAssignableFrom(container.GetType()));- // }-- // /// Get the last focused child that matches the given type.- // public Container LastFocusedChildOfType<T>()- // {- // return ChildFocusOrder.Find(- // container => typeof(T).IsAssignableFrom(container.GetType())- // );- // }-- // /// Get the last focused descendant that matches the given type.- // public Container LastFocusedDescendantOfType<T>()- // {- // return DescendantFocusOrder.FirstOrDefault(- // container => typeof(T).IsAssignableFrom(container.GetType())- // );- // }-}--/// Aproach 1:-#[derive(Debug)]-struct Container {- parent: Weak<RefCell<Node>>,- children: VecDeque<Rc<RefCell<Node>>>,- value: ContainerValue,-}--impl Container {- /// Height of the container. Implementation varies by container type.- pub fn height(&self) -> u32 {- match self.value {- Self::Monitor(c) => c.height,- Self::Workspace(c) => c.height,- Self::Split(c) => c.height(),- Self::Window(c) => c.height(),- _ => 0,- }- }-- /// Width of the container. Implementation varies by container type.- pub fn width(&self) -> u32 {- match self.value {- Self::Monitor(c) => c.width,- Self::Workspace(c) => c.width,- Self::Split(c) => c.width(),- Self::Window(c) => c.width(),- _ => 0,- }- }-- /// X-coordinate of the container. Implementation varies by container type.- pub fn x(&self) -> u32 {- match self.value {- Self::Monitor(c) => c.x,- Self::Workspace(c) => c.x,- Self::Split(c) => c.x(),- Self::Window(c) => c.x(),- _ => 0,- }- }-- /// Y-coordinate of the container. Implementation varies by container type.- pub fn y(&self) -> u32 {- match self.value {- Self::Monitor(c) => c.y,- Self::Workspace(c) => c.y,- Self::Split(c) => c.y(),- Self::Window(c) => c.y(),- _ => 0,- }- }-- /// Whether the container can be tiled.- pub fn can_tile(&self) -> bool {- match self.value {- Self::Window(c) => c.state == WindowState::Tiling,- _ => true,- }- }-}--enum ContainerValue {- Root,- Monitor(MonitorValue),- Workspace(WorkspaceValue),- Split(SplitValue),- Window(WindowValue),-}--/// Approach 2 ****:-#[derive(Debug)]-pub enum Container {- RootContainer(RootContainer),- Monitor(Monitor),- Workspace(Workspace),- SplitContainer(SplitContainer),- Window(Window),-}--pub struct InnerContainer {- parent: Weak<RefCell<Node>>,- children: VecDeque<Rc<RefCell<Node>>>,-}--impl Container {- pub fn inner(&self) -> &InnerContainer {- match self {- Self::RootContainer(c) => &c.inner,- Self::Monitor(c) => &c.inner,- Self::Workspace(c) => &c.inner,- Self::SplitContainer(c) => &c.inner,- Self::Window(c) => &c.inner,- }- }-}--#[derive(Debug)]-pub struct RootContainer {- pub inner: InnerContainer,- width: u32,- height: u32,- x: u32,- y: u32,-}--impl RootContainer {- pub fn new() -> Self {- Self {- inner: InnerContainer::new(None, vec![]),- width: 0,- height: 0,- x: 0,- y: 0,- }- }-}diff --git a/packages/wm/src/containers/root_container.rs b/packages/wm/src/containers/root_container.rsindex 9e3a72bc..69f80578 100644--- a/packages/wm/src/containers/root_container.rs+++ b/packages/wm/src/containers/root_container.rs@@ -1,6 +1,4 @@-use crate::containers::ContainerType;--use super::{ContainerVariant, InnerContainer};+use super::InnerContainer;#[derive(Debug)]pub struct RootContainer {@@ -22,29 +20,3 @@ impl RootContainer {}}}--impl ContainerVariant for RootContainer {- fn inner(&self) -> InnerContainer {- self.inner- }-- fn r#type(&self) -> ContainerType {- ContainerType::RootContainer- }-- fn height(&self) -> u32 {- self.height- }-- fn width(&self) -> u32 {- self.width- }-- fn x(&self) -> u32 {- self.x- }-- fn y(&self) -> u32 {- self.y- }-}diff --git a/packages/wm/src/windows/mod.rs b/packages/wm/src/windows/mod.rsindex dfee58e1..71df2d5f 100644--- a/packages/wm/src/windows/mod.rs+++ b/packages/wm/src/windows/mod.rs@@ -1,3 +1,5 @@mod window;+mod window_state;pub use window::*;+pub use window_state::*;diff --git a/packages/wm/src/windows/window.rs b/packages/wm/src/windows/window.rsindex cab515aa..9a7a87be 100644--- a/packages/wm/src/windows/window.rs+++ b/packages/wm/src/windows/window.rs@@ -1,4 +1,4 @@-use crate::containers::{ContainerType, ContainerVariant, InnerContainer};+use crate::containers::InnerContainer;#[derive(Debug)]pub struct Window {@@ -20,29 +20,3 @@ impl Window {}}}--impl ContainerVariant for Window {- fn inner(&self) -> InnerContainer {- self.inner- }-- fn r#type(&self) -> ContainerType {- ContainerType::Window- }-- fn height(&self) -> u32 {- self.height- }-- fn width(&self) -> u32 {- self.width- }-- fn x(&self) -> u32 {- self.x- }-- fn y(&self) -> u32 {- self.y- }-}diff --git a/packages/wm/src/windows/window_state.rs b/packages/wm/src/windows/window_state.rsnew file mode 100644index 00000000..322c4615--- /dev/null+++ b/packages/wm/src/windows/window_state.rs@@ -0,0 +1,7 @@+pub enum WindowState {+ Floating,+ Fullscreen,+ Maximized,+ Minimized,+ Tiling,+}