GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
fix: more type error fixes
Lars Berger0a429ee02612
diff --git a/packages/ipc-client/src/ipc_message.rs b/packages/ipc-client/src/ipc_message.rsindex 6841e599..e2b99eff 100644--- a/packages/ipc-client/src/ipc_message.rs+++ b/packages/ipc-client/src/ipc_message.rs@@ -1,9 +1,9 @@-pub enum IpcMessage {- QueryMonitors,- QueryWorkspaces,- QueryWindows,- QueryFocusedContainer,- QueryBindingMode,- InvokeCommand,- SubscribeEvent,-}+// pub enum IpcMessage {+// QueryMonitors,+// QueryWorkspaces,+// QueryWindows,+// QueryFocusedContainer,+// QueryBindingMode,+// InvokeCommand,+// SubscribeEvent,+// }diff --git a/packages/wm/src/common/direction.rs b/packages/wm/src/common/direction.rsindex be0dbb26..968668f4 100644--- a/packages/wm/src/common/direction.rs+++ b/packages/wm/src/common/direction.rs@@ -1,5 +1,6 @@use anyhow::{bail, Result};+#[derive(Debug)]pub enum Direction {Left,Right,diff --git a/packages/wm/src/common/focus_mode.rs b/packages/wm/src/common/focus_mode.rsindex edb1a8f2..fb146b2a 100644--- a/packages/wm/src/common/focus_mode.rs+++ b/packages/wm/src/common/focus_mode.rs@@ -1,3 +1,4 @@+#[derive(Debug)]pub enum FocusMode {Tiling,Floating,diff --git a/packages/wm/src/common/platform/event_listener.rs b/packages/wm/src/common/platform/event_listener.rsindex 3516b771..b4e7a90b 100644--- a/packages/wm/src/common/platform/event_listener.rs+++ b/packages/wm/src/common/platform/event_listener.rs@@ -1,4 +1,5 @@-use tokio::sync::mpsc::{self, Receiver, Sender};+use anyhow::Result;+use tokio::sync::mpsc::{self, UnboundedReceiver};use wineventhook::{EventFilter, WindowEvent, WindowEventHook};pub enum PlatformEvent {@@ -16,28 +17,24 @@ pub enum PlatformEvent {}pub struct EventListener {- event_tx: Sender<WindowEvent>,- pub event_rx: Receiver<WindowEvent>,+ pub event_rx: UnboundedReceiver<WindowEvent>,hook: WindowEventHook,}impl EventListener {/// Start listening for platform events.- pub async fn start() -> Self {- let (event_tx, mut event_rx) = mpsc::unbounded_channel();+ pub async fn start() -> Result<Self> {+ let (event_tx, event_rx) = mpsc::unbounded_channel();let hook =WindowEventHook::hook(EventFilter::default(), event_tx).await?;- Self {- event_tx,- event_rx,- hook,- }+ Ok(Self { event_rx, hook })}}impl Drop for EventListener {fn drop(&mut self) {- self.hook.unhook().unwrap();+ // TODO+ // self.hook.unhook().unwrap();}}diff --git a/packages/wm/src/common/rect_delta.rs b/packages/wm/src/common/rect_delta.rsindex 4887185f..6ab4ea2b 100644--- a/packages/wm/src/common/rect_delta.rs+++ b/packages/wm/src/common/rect_delta.rs@@ -2,6 +2,7 @@ use anyhow::{bail, Result};use super::LengthValue;+#[derive(Debug)]pub struct RectDelta {/// The delta in x-coordinates on the left of the rectangle.left: LengthValue,@@ -32,39 +33,29 @@ impl RectDelta {}pub fn from_str(unparsed: &str) -> Result<Self> {- let parts: Vec<_> = unparsed.split_whitespace();+ let mut parts = unparsed.split_whitespace();- match parts.len() {+ match parts.count() {1 => {- let value = LengthValue::from_str(&parts[0])?;- Ok(Self::new(- value.clone(),- value.clone(),- value.clone(),- value,- ))+ let value = LengthValue::from_str(parts.next().unwrap())?;+ Ok(Self::new(value, value, value, value))}2 => {- let top_bottom = LengthValue::from_str(&parts[0])?;- let left_right = LengthValue::from_str(&parts[1])?;- Ok(Self::new(- left_right.clone(),- top_bottom.clone(),- left_right,- top_bottom,- ))+ let top_bottom = LengthValue::from_str(parts.next().unwrap())?;+ let left_right = LengthValue::from_str(parts.next().unwrap())?;+ Ok(Self::new(left_right, top_bottom, left_right, top_bottom))}3 => {- let top = LengthValue::from_str(&parts[0])?;- let left_right = LengthValue::from_str(&parts[1])?;- let bottom = LengthValue::from_str(&parts[2])?;- Ok(Self::new(left_right.clone(), top, left_right, bottom))+ let top = LengthValue::from_str(parts.next().unwrap())?;+ let left_right = LengthValue::from_str(parts.next().unwrap())?;+ let bottom = LengthValue::from_str(parts.next().unwrap())?;+ Ok(Self::new(left_right, top, left_right, bottom))}4 => {- let top = LengthValue::from_str(&parts[0])?;- let right = LengthValue::from_str(&parts[1])?;- let bottom = LengthValue::from_str(&parts[2])?;- let left = LengthValue::from_str(&parts[3])?;+ let top = LengthValue::from_str(parts.next().unwrap())?;+ let right = LengthValue::from_str(parts.next().unwrap())?;+ let bottom = LengthValue::from_str(parts.next().unwrap())?;+ let left = LengthValue::from_str(parts.next().unwrap())?;Ok(Self::new(left, top, right, bottom))}_ => bail!("Invalid shorthand."),diff --git a/packages/wm/src/common/tiling_direction.rs b/packages/wm/src/common/tiling_direction.rsindex 62556eec..dee10cdb 100644--- a/packages/wm/src/common/tiling_direction.rs+++ b/packages/wm/src/common/tiling_direction.rs@@ -2,6 +2,7 @@ use anyhow::{bail, Result};use super::Direction;+#[derive(Debug)]pub enum TilingDirection {Vertical,Horizontal,diff --git a/packages/wm/src/common/units.rs b/packages/wm/src/common/units.rsindex 5cbc701b..1152b934 100644--- a/packages/wm/src/common/units.rs+++ b/packages/wm/src/common/units.rs@@ -1,13 +1,15 @@use std::str::FromStr;-use anyhow::{bail, Result};+use anyhow::{bail, Context, Result};use regex::Regex;+#[derive(Debug)]pub struct LengthValue {pub amount: f32,pub unit: LengthUnit,}+#[derive(Debug)]pub enum LengthUnit {Pixel,Percentage,@@ -23,7 +25,10 @@ impl LengthValue {pub fn from_str(unparsed: &str) -> Result<LengthValue> {let units_regex = Regex::new(r"(\d+)(%|ppt|px)?")?;- let captures = units_regex.captures(unparsed)?;+ let captures = units_regex+ .captures(unparsed)+ .context("Invalid length value.")?;+let amount = f32::from_str(&captures[1])?;let unit_str = captures.get(2).map_or("", |m| m.as_str());diff --git a/packages/wm/src/containers/container.rs b/packages/wm/src/containers/container.rsindex 19807696..f733cbc8 100644--- a/packages/wm/src/containers/container.rs+++ b/packages/wm/src/containers/container.rs@@ -3,7 +3,11 @@ use std::sync::Arc;use serde::{Deserialize, Serialize};use uuid::Uuid;-use super::container_type::ContainerType;+use crate::{monitors::Monitor, windows::Window, workspaces::Workspace};++use super::{+ container_type::ContainerType, RootContainer, SplitContainer,+};#[derive(Debug, Deserialize, Clone, Serialize)]pub struct InnerContainer {@@ -27,7 +31,7 @@ impl InnerContainer {}}-#[derive(Debug, Deserialize, Clone, Serialize)]+#[derive(Debug)]pub enum Container {RootContainer(RootContainer),Monitor(Monitor),@@ -36,7 +40,7 @@ pub enum Container {Window(Window),}-pub trait Containeraa {+pub trait ContainerVariant {/// Derived container type (eg. `ContainerType::Monitor`).fn r#type(&self) -> ContainerType;diff --git a/packages/wm/src/containers/mod.rs b/packages/wm/src/containers/mod.rsindex 76a008c8..c3e2e023 100644--- a/packages/wm/src/containers/mod.rs+++ b/packages/wm/src/containers/mod.rs@@ -1,7 +1,9 @@mod container;mod container_type;mod root_container;+mod split_container;pub use container::*;pub use container_type::*;pub use root_container::*;+pub use split_container::*;diff --git a/packages/wm/src/containers/root_container.rs b/packages/wm/src/containers/root_container.rsindex a5d77bb4..24e80f1f 100644--- a/packages/wm/src/containers/root_container.rs+++ b/packages/wm/src/containers/root_container.rs@@ -1,7 +1,8 @@-use crate::containers::{Container, ContainerType};+use crate::containers::ContainerType;-use super::InnerContainer;+use super::{ContainerVariant, InnerContainer};+#[derive(Debug)]pub struct RootContainer {inner: InnerContainer,width: u32,@@ -22,7 +23,7 @@ impl RootContainer {}}-impl Container for RootContainer {+impl ContainerVariant for RootContainer {fn inner(&self) -> InnerContainer {self.inner}diff --git a/packages/wm/src/containers/split_container.rs b/packages/wm/src/containers/split_container.rsnew file mode 100644index 00000000..8f109613--- /dev/null+++ b/packages/wm/src/containers/split_container.rs@@ -0,0 +1,50 @@+use crate::containers::ContainerType;++use super::{ContainerVariant, InnerContainer};++#[derive(Debug)]+pub struct SplitContainer {+ inner: InnerContainer,+ width: u32,+ height: u32,+ x: u32,+ y: u32,+}++impl SplitContainer {+ pub fn new() -> Self {+ Self {+ inner: InnerContainer::new(None, vec![]),+ width: 0,+ height: 0,+ x: 0,+ y: 0,+ }+ }+}++impl ContainerVariant for SplitContainer {+ fn inner(&self) -> InnerContainer {+ self.inner+ }++ fn r#type(&self) -> ContainerType {+ ContainerType::SplitContainer+ }++ 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/main.rs b/packages/wm/src/main.rsindex 26361192..44ba045b 100644--- a/packages/wm/src/main.rs+++ b/packages/wm/src/main.rs@@ -3,7 +3,7 @@ use std::env;use anyhow::{Context, Result};use clap::Parser;use common::platform::EventListener;-use ipc_client::{self, IpcClient};+use ipc_client::IpcClient;use ipc_server::IpcServer;use tokio::process::Command;@@ -19,7 +19,7 @@ mod containers;mod ipc_server;mod monitors;mod user_config;-// mod windows;+mod windows;mod wm;mod wm_command;mod wm_event;diff --git a/packages/wm/src/monitors/monitor.rs b/packages/wm/src/monitors/monitor.rsindex def35e9f..0057e3b8 100644--- a/packages/wm/src/monitors/monitor.rs+++ b/packages/wm/src/monitors/monitor.rs@@ -1,17 +1,8 @@-use std::sync::Arc;--use uuid::Uuid;--use crate::{- containers::{Container, ContainerType, RootContainer},- workspaces::Workspace,-};+use crate::containers::{ContainerType, ContainerVariant, InnerContainer};+#[derive(Debug)]pub struct Monitor {- id: Uuid,- parent: Arc<RootContainer>,- children: Vec<Arc<Workspace>>,- child_focus_order: Vec<Arc<Workspace>>,+ inner: InnerContainer,device_name: String,width: u32,height: u32,@@ -21,7 +12,6 @@ pub struct Monitor {impl Monitor {pub fn new(- parent: RootContainer,device_name: String,width: u32,height: u32,@@ -29,10 +19,7 @@ impl Monitor {y: u32,) -> Self {Self {- id: Uuid::new_v4(),- parent: Arc::new(parent),- children: Vec::new(),- child_focus_order: Vec::new(),+ inner: InnerContainer::new(None, vec![]),device_name,width,height,@@ -42,9 +29,9 @@ impl Monitor {}}-impl Container for Monitor {- fn id(&self) -> Uuid {- self.id+impl ContainerVariant for Monitor {+ fn inner(&self) -> InnerContainer {+ self.inner}fn r#type(&self) -> ContainerType {@@ -66,31 +53,4 @@ impl Container for Monitor {fn y(&self) -> u32 {self.y}-- fn parent(&self) -> Arc<dyn Container> {- self.parent- }-- 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/user_config.rs b/packages/wm/src/user_config.rsindex 58d0fa48..fc6df728 100644--- a/packages/wm/src/user_config.rs+++ b/packages/wm/src/user_config.rs@@ -3,7 +3,28 @@ use std::path::PathBuf;use serde::{Deserialize, Deserializer};use tokio::fs;-use crate::common::{LengthValue, RectDelta};+use crate::{+ common::{LengthValue, RectDelta},+ wm_command::WmCommand,+};++#[derive(Debug, Deserialize)]+pub struct KeybindingConfig {+ /// Keyboard shortcut to trigger the keybinding.+ pub bindings: Vec<String>,++ /// WM commands to run when the keybinding is triggered.+ pub commands: Vec<WmCommand>,+}++#[derive(Debug, Deserialize)]+pub struct BindingModeConfig {+ /// Name of the binding mode.+ pub name: String,++ /// Keybindings that will be active when the binding mode is active.+ pub keybindings: Vec<KeybindingConfig>,+}#[derive(Debug, Deserialize)]pub struct GapsConfig {@@ -16,6 +37,39 @@ pub struct GapsConfig {pub outer_gap: RectDelta,}+#[derive(Debug, Deserialize)]+pub struct GeneralConfig {+ /// Whether to show floating windows as always on top.+ pub show_floating_on_top: bool,++ /// Center the cursor in the middle of a newly focused window.+ pub cursor_follows_focus: bool,++ /// Focus the window directly under the cursor at all times.+ pub focus_follows_cursor: bool,++ /// Amount by which to move floating windows+ #[serde(deserialize_with = "to_length_value")]+ pub floating_window_move_amount: LengthValue,++ /// If activated, by switching to the current workspace the previous+ /// focused workspace is activated.+ pub toggle_workspace_on_refocus: bool,++ /// Whether to enable window transition animations (on minimize, close).+ pub window_animations: WindowAnimations,++ /// Whether to center new floating windows+ pub center_new_floating_windows: bool,+}++#[derive(Debug, Deserialize)]+pub enum WindowAnimations {+ Enabled,+ Disabled,+ Unchanged,+}+#[derive(Debug, Deserialize)]pub struct FocusBorder {/// Whether the default transparent border be used.@@ -35,12 +89,19 @@ pub struct FocusBordersConfig {}#[derive(Debug, Deserialize)]-pub struct BindingModeConfig {- /// Name of the binding mode.- pub name: String,+pub struct WindowRuleConfig {+ pub match_process_name: Option<String>,+ pub match_class_name: Option<String>,+ pub match_title: Option<String>,+ pub commands: Vec<WmCommand>,+}- /// Keybindings that will be active when the binding mode is active.- pub keybindings: Vec<KeybindingConfig>,+#[derive(Debug, Deserialize)]+pub struct WorkspaceConfig {+ pub name: String,+ pub display_name: Option<String>,+ pub bind_to_monitor: Option<String>,+ pub keep_alive: bool,}#[derive(Debug, Deserialize)]diff --git a/packages/wm/src/windows/mod.rs b/packages/wm/src/windows/mod.rsnew file mode 100644index 00000000..dfee58e1--- /dev/null+++ b/packages/wm/src/windows/mod.rs@@ -0,0 +1,3 @@+mod window;++pub use window::*;diff --git a/packages/wm/src/windows/window.rs b/packages/wm/src/windows/window.rsindex 8b137891..a75b507f 100644--- a/packages/wm/src/windows/window.rs+++ b/packages/wm/src/windows/window.rs@@ -1 +1,48 @@+use crate::containers::{ContainerType, ContainerVariant, InnerContainer};+#[derive(Debug)]+pub struct Window {+ inner: InnerContainer,+ width: u32,+ height: u32,+ x: u32,+ y: u32,+}++impl Window {+ pub fn new() -> Self {+ Self {+ inner: InnerContainer::new(None, vec![]),+ width: 0,+ height: 0,+ x: 0,+ y: 0,+ }+ }+}++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/wm_command.rs b/packages/wm/src/wm_command.rsindex b7d04bab..885aec61 100644--- a/packages/wm/src/wm_command.rs+++ b/packages/wm/src/wm_command.rs@@ -1,96 +1,78 @@-use crate::{- common::{Direction, TilingDirection},- containers::Container,-};+use std::string;+use anyhow::{bail, Result};+use serde::{Deserialize, Deserializer};++use crate::common::{Direction, TilingDirection};++#[derive(Debug)]pub enum WmCommand {- CloseWindow(Container),+ CloseWindow,ExitWm,- FocusInDirection(Container, Direction),+ FocusInDirection(Direction),FocusRecentWorkspace,FocusWorkspaceInSequence,- FocusWorkspace(Container, String),- IgnoreWindow(Container),- MoveWindow(Container, Direction),- MoveWorkspace(Container, Direction),+ FocusWorkspace(String),+ IgnoreWindow,+ MoveWindow(Direction),+ MoveWindowToWorkspace(String),+ MoveWorkspace(Direction),Noop,Redraw,ReloadConfig,- SetTilingDirection(Container, TilingDirection),- ToggleTilingDirection(Container),+ SetTilingDirection(TilingDirection),+ ToggleTilingDirection,+ ToggleFocusMode,}impl WmCommand {- pub fn from_str(- unparsed: &str,- subject_container: Container,- ) -> Result<Self> {+ pub fn from_str(unparsed: &str) -> Result<Self> {let parts: Vec<&str> = unparsed.split_whitespace().collect();let command = match parts.as_slice() {- ["close_window"] => WmCommand::CloseWindow(subject_container),+ ["close_window"] => WmCommand::CloseWindow,["exit_wm"] => WmCommand::ExitWm,- ["focus", "left"] => {- WmCommand::FocusInDirection(subject_container, Direction::Left)- }- ["focus", "right"] => {- WmCommand::Focus(subject_container, Direction::Right)- }- ["focus", "up"] => {- WmCommand::Focus(subject_container, Direction::Up)- }- ["focus", "down"] => {- WmCommand::Focus(subject_container, Direction::Down)+ ["focus", direction] => {+ WmCommand::FocusInDirection(Direction::from_str(direction)?)}["focus_workspace", name] => {- WmCommand::FocusWorkspace(subject_container, name)- }- ["ignore_window"] => WmCommand::IgnoreWindow(subject_container),- ["move_window", "left"] => {- WmCommand::MoveWindow(subject_container, Direction::Left)- }- ["move_window", "right"] => {- WmCommand::MoveWindow(subject_container, Direction::Right)+ WmCommand::FocusWorkspace(name.to_string())}- ["move_window", "up"] => {- WmCommand::MoveWindow(subject_container, Direction::Up)+ ["ignore_window"] => WmCommand::IgnoreWindow,+ ["move_window", direction] => {+ WmCommand::MoveWindow(Direction::from_str(direction)?)}- ["move_window", "down"] => {- WmCommand::MoveWindow(subject_container, Direction::Down)+ ["move_window_to_workspace", name] => {+ WmCommand::MoveWindowToWorkspace(name.to_string())}- ["move_workspace", "left"] => {- WmCommand::MoveWorkspace(subject_container, Direction::Left)- }- ["move_workspace", "right"] => {- WmCommand::MoveWorkspace(subject_container, Direction::Right)- }- ["move_workspace", "up"] => {- WmCommand::MoveWorkspace(subject_container, Direction::Up)- }- ["move_workspace", "down"] => {- WmCommand::MoveWorkspace(subject_container, Direction::Down)+ ["move_workspace", direction] => {+ WmCommand::MoveWorkspace(Direction::from_str(direction)?)}["noop"] => WmCommand::Noop,["redraw"] => WmCommand::Redraw,["reload_config"] => WmCommand::ReloadConfig,- ["set_tiling_direction", "horizontal"] => {- WmCommand::SetTilingDirection(- subject_container,- TilingDirection::Horizontal,- )- }- ["set_tiling_direction", "vertical"] => {- WmCommand::SetTilingDirection(- subject_container,- TilingDirection::Vertical,- )+ ["set_tiling_direction", tiling_direction] => {+ WmCommand::SetTilingDirection(TilingDirection::from_str(+ tiling_direction,+ )?)}["toggle_tiling_direction", "vertical"] => {- WmCommand::ToggleTilingDirection(subject_container)+ WmCommand::ToggleTilingDirection}- _ => Err("Not a known command."),+ ["toggle_focus_mode"] => WmCommand::ToggleFocusMode,+ _ => bail!("Not a known command."),};Ok(command)}}++impl<'de> Deserialize<'de> for WmCommand {+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>+ where+ D: Deserializer<'de>,+ {+ let str = String::deserialize(deserializer)?;+ Self::from_str(&str).map_err(serde::de::Error::custom)+ }+}diff --git a/packages/wm/src/workspaces/workspace.rs b/packages/wm/src/workspaces/workspace.rsindex 608059d1..cce1b515 100644--- a/packages/wm/src/workspaces/workspace.rs+++ b/packages/wm/src/workspaces/workspace.rs@@ -1,8 +1,9 @@use crate::{common::RectDelta,- containers::{Container, ContainerType, InnerContainer},+ containers::{ContainerType, ContainerVariant, InnerContainer},};+#[derive(Debug)]pub struct Workspace {inner: InnerContainer,name: String,@@ -28,7 +29,7 @@ impl Workspace {}}-impl Container for Workspace {+impl ContainerVariant for Workspace {fn inner(&self) -> InnerContainer {self.inner}