GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
various fixes; add some wm state fns
Lars Berger0f6998e09293
diff --git a/packages/wm/src/common/platform/platform.rs b/packages/wm/src/common/platform/platform.rsindex a671a406..056d59e0 100644--- a/packages/wm/src/common/platform/platform.rs+++ b/packages/wm/src/common/platform/platform.rs@@ -22,7 +22,7 @@ impl Platform {native_monitor::available_monitors()}- pub fn nearest_monitor(window: NativeWindow) -> Option<NativeMonitor> {+ pub fn nearest_monitor(window: &NativeWindow) -> Option<NativeMonitor> {native_monitor::available_monitors().ok().and_then(|m| m.first().cloned())diff --git a/packages/wm/src/common/rect.rs b/packages/wm/src/common/rect.rsindex f81a9714..49eae43a 100644--- a/packages/wm/src/common/rect.rs+++ b/packages/wm/src/common/rect.rs@@ -58,8 +58,8 @@ impl Rect {pub fn translate_to_center(&mut self, outer_rect: Rect) -> Self {Self::translate_to_coordinates(self,- outer_rect.x + (outer_rect.width() / 2) - (self.width() / 2),- outer_rect.y + (outer_rect.height() / 2) - (self.height() / 2),+ outer_rect.left + (outer_rect.width() / 2) - (self.width() / 2),+ outer_rect.top + (outer_rect.height() / 2) - (self.height() / 2),)}diff --git a/packages/wm/src/common/rect_delta.rs b/packages/wm/src/common/rect_delta.rsindex 87cd4cd4..ef503ce4 100644--- a/packages/wm/src/common/rect_delta.rs+++ b/packages/wm/src/common/rect_delta.rs@@ -6,16 +6,16 @@ use super::LengthValue;#[derive(Debug, Clone)]pub struct RectDelta {/// The delta in x-coordinates on the left of the rectangle.- left: LengthValue,+ pub left: LengthValue,/// The delta in y-coordinates on the top of the rectangle.- top: LengthValue,+ pub top: LengthValue,/// The delta in x-coordinates on the right of the rectangle.- right: LengthValue,+ pub right: LengthValue,/// The delta in y-coordinates on the bottom of the rectangle.- bottom: LengthValue,+ pub bottom: LengthValue,}impl RectDelta {diff --git a/packages/wm/src/containers/container.rs b/packages/wm/src/containers/container.rsindex d3020491..2d642d45 100644--- a/packages/wm/src/containers/container.rs+++ b/packages/wm/src/containers/container.rs@@ -59,3 +59,19 @@ impl TryFrom<Container> for TilingContainer {}}}++impl TilingContainer {+ pub fn as_monitor(&self) -> Option<Monitor> {+ match self {+ TilingContainer::Monitor(c) => Some(c.clone()),+ _ => None,+ }+ }++ pub fn as_workspace(&self) -> Option<Workspace> {+ match self {+ TilingContainer::Workspace(c) => Some(c.clone()),+ _ => None,+ }+ }+}diff --git a/packages/wm/src/monitors/monitor.rs b/packages/wm/src/monitors/monitor.rsindex 70099e9e..06b68e03 100644--- a/packages/wm/src/monitors/monitor.rs+++ b/packages/wm/src/monitors/monitor.rs@@ -38,6 +38,10 @@ impl Monitor {Self(Rc::new(RefCell::new(monitor)))}++ pub fn native(&self) -> NativeMonitor {+ todo!()+ }}impl_common_behavior!(Monitor, ContainerType::Monitor);diff --git a/packages/wm/src/windows/commands/manage_window_command.rs b/packages/wm/src/windows/commands/manage_window_command.rsnew file mode 100644index 00000000..64489d3a--- /dev/null+++ b/packages/wm/src/windows/commands/manage_window_command.rs@@ -0,0 +1,10 @@+use crate::wm_state::WmState;++pub struct ManageWindowCommand;++pub fn manage_window_command(+ command: ManageWindowCommand,+ state: &mut WmState,+) -> anyhow::Result<()> {+ todo!()+}diff --git a/packages/wm/src/windows/commands/mod.rs b/packages/wm/src/windows/commands/mod.rsnew file mode 100644index 00000000..05311c23--- /dev/null+++ b/packages/wm/src/windows/commands/mod.rs@@ -0,0 +1,3 @@+mod manage_window_command;++pub use manage_window_command::*;diff --git a/packages/wm/src/wm.rs b/packages/wm/src/wm.rsindex 69efe765..3518fefe 100644--- a/packages/wm/src/wm.rs+++ b/packages/wm/src/wm.rs@@ -32,15 +32,6 @@ impl WindowManager {})}- // pub fn init() -> Result<()> {- // let foreground_window = Platform::foreground_window();-- // let monitors = Platform::monitors()?;- // let manageable_windows = Platform::manageable_windows()?;-- // Ok(())- // }-pub async fn process_event(&mut self, event: PlatformEvent) {todo!()}diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rsindex e4b364ea..59b1e06a 100644--- a/packages/wm/src/wm_state.rs+++ b/packages/wm/src/wm_state.rs@@ -1,27 +1,17 @@-use std::{- borrow::BorrowMut,- cell::RefCell,- rc::{Rc, Weak},- sync::Arc,-};+use std::{rc::Weak, sync::Arc};-use anyhow::Result;use tokio::sync::{mpsc::UnboundedSender, Mutex};-use tracing::warn;-use uuid::Uuid;use crate::{- common::{- platform::{NativeMonitor, Platform},- FocusMode,- },+ common::platform::{NativeMonitor, NativeWindow, Platform},containers::{- traits::{CommonBehavior, TilingBehavior},- Container, ContainerType, RootContainer, TilingContainer,+ traits::TilingBehavior, Container, RootContainer, TilingContainer,},monitors::Monitor,user_config::{BindingModeConfig, UserConfig},+ windows::TilingWindow,wm_event::WmEvent,+ workspaces::Workspace,};pub struct WmState {@@ -64,7 +54,10 @@ impl WmState {}}+ /// Populates the initial WM state by creating containers for all+ /// existing windows and monitors.pub fn populate(&mut self) -> anyhow::Result<()> {+ // Get the originally focused window when the WM is started.let foreground_window = Platform::foreground_window();let native_monitors = Platform::monitors()?;@@ -77,38 +70,55 @@ impl WmState {}for native_window in Platform::manageable_windows()? {- // let window = ContainerRef::Window(native_window);- // self.root_container.insert_tiling_child(0, window);+ let nearest_monitor = Platform::nearest_monitor(&native_window)+ .and_then(|native| self.monitor_from_native(&native))+ .or(self.monitors().first().cloned());++ if let Some(monitor) = nearest_monitor {+ // TODO: This should actually add to the monitor's displayed workspace.+ let window = TilingWindow::new(native_window);+ monitor+ .insert_tiling_child(0, TilingContainer::TilingWindow(window));+ }}Ok(())}- pub fn nearest_monitor(&self) -> Option<Monitor> {- todo!()+ pub fn monitors(&self) -> Vec<Monitor> {+ self+ .root_container+ .tiling_children()+ .iter()+ .map(|c| c.as_monitor().unwrap())+ .collect()}- pub fn add_monitor(&mut self) {- let monitor = Monitor::new(NativeMonitor::new(- windows::Win32::Graphics::Gdi::HMONITOR(1),- String::from("aaa"),- 0,- 0,- 0,- 0,- ));-+ pub fn workspaces(&self) -> Vec<Workspace> {self- .root_container- .insert_tiling_child(0, TilingContainer::Monitor(monitor))+ .monitors()+ .iter()+ .flat_map(|c| c.tiling_children())+ .map(|c| c.as_workspace().unwrap())+ .collect()}- pub fn emit_event(&self, event: WmEvent) -> Result<()> {- if let Err(err) = self.event_tx.send(event) {- warn!("Failed to send event: {}", err);- }+ pub fn monitor_from_native(+ &self,+ native_monitor: &NativeMonitor,+ ) -> Option<Monitor> {+ self+ .monitors()+ .iter()+ .find(|&m| m.native() == *native_monitor)+ .cloned()+ }- Ok(())+ pub fn window_from_native(+ &self,+ native_window: NativeWindow,+ ) -> Option<Container> {+ todo!()}// Get the currently focused container. This can either be a `Window` ordiff --git a/packages/wm/src/workspaces/workspace.rs b/packages/wm/src/workspaces/workspace.rsindex 83a8b0ea..b503ce2d 100644--- a/packages/wm/src/workspaces/workspace.rs+++ b/packages/wm/src/workspaces/workspace.rs@@ -45,6 +45,10 @@ impl Workspace {Self(Rc::new(RefCell::new(workspace)))}++ pub fn outer_gaps(&self) -> &RectDelta {+ &self.0.borrow().outer_gaps+ }}impl_common_behavior!(Workspace, ContainerType::Workspace);@@ -53,21 +57,21 @@ impl_tiling_behavior!(Workspace);impl PositionBehavior for Workspace {fn width(&self) -> i32 {self.parent().unwrap().width()- - self.outer_gaps().left- - self.outer_gaps().right+ - self.outer_gaps().left.amount+ - self.outer_gaps().right.amount}fn height(&self) -> i32 {self.parent().unwrap().height()- - self.outer_gaps().top- - self.outer_gaps().bottom+ - self.outer_gaps().top.amount+ - self.outer_gaps().bottom.amount}fn x(&self) -> i32 {- self.parent().unwrap().x() + self.outer_gaps().left+ self.parent().unwrap().x() + self.outer_gaps().left.amount}fn y(&self) -> i32 {- self.parent().unwrap().y() + self.outer_gaps().top+ self.parent().unwrap().y() + self.outer_gaps().top.amount}}