Skip to content

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

feat: add getter for pending window rules to run

Lars Bergerbae9f8694e71
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs
index 9a697737..05cc0872 100644
--- a/packages/wm/src/app_command.rs
+++ b/packages/wm/src/app_command.rs
@@ -168,7 +168,7 @@ pub enum SubscribableEvent {
WorkspaceMoved,
}
-#[derive(Clone, Debug, Parser, Serialize)]
+#[derive(Clone, Debug, Parser, PartialEq, Serialize)]
pub enum InvokeCommand {
AdjustBorders(InvokeAdjustBordersCommand),
Close,
@@ -579,7 +579,7 @@ impl<'de> Deserialize<'de> for InvokeCommand {
}
}
-#[derive(Args, Clone, Debug, Serialize)]
+#[derive(Args, Clone, Debug, PartialEq, Serialize)]
#[group(required = true, multiple = true)]
pub struct InvokeAdjustBordersCommand {
#[clap(long, allow_hyphen_values = true)]
@@ -595,7 +595,7 @@ pub struct InvokeAdjustBordersCommand {
left: Option<LengthValue>,
}
-#[derive(Args, Clone, Debug, Serialize)]
+#[derive(Args, Clone, Debug, PartialEq, Serialize)]
#[group(required = true, multiple = false)]
pub struct InvokeFocusCommand {
#[clap(long)]
@@ -614,7 +614,7 @@ pub struct InvokeFocusCommand {
recent_workspace: bool,
}
-#[derive(Args, Clone, Debug, Serialize)]
+#[derive(Args, Clone, Debug, PartialEq, Serialize)]
#[group(required = true, multiple = false)]
pub struct InvokeMoveCommand {
/// Direction to move the window.
@@ -635,7 +635,7 @@ pub struct InvokeMoveCommand {
recent_workspace: bool,
}
-#[derive(Args, Clone, Debug, Serialize)]
+#[derive(Args, Clone, Debug, PartialEq, Serialize)]
#[group(required = true, multiple = true)]
pub struct InvokeResizeCommand {
#[clap(long, allow_hyphen_values = true)]
diff --git a/packages/wm/src/common/length_value.rs b/packages/wm/src/common/length_value.rs
index 0c8b6337..e67bd5a8 100644
--- a/packages/wm/src/common/length_value.rs
+++ b/packages/wm/src/common/length_value.rs
@@ -4,13 +4,13 @@ use anyhow::{bail, Context};
use regex::Regex;
use serde::{Deserialize, Deserializer, Serialize};
-#[derive(Debug, Clone, Serialize)]
+#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct LengthValue {
pub amount: f32,
pub unit: LengthUnit,
}
-#[derive(Debug, Deserialize, Clone, Serialize)]
+#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum LengthUnit {
Percentage,
diff --git a/packages/wm/src/user_config.rs b/packages/wm/src/user_config.rs
index 99098d20..4ecc35ea 100644
--- a/packages/wm/src/user_config.rs
+++ b/packages/wm/src/user_config.rs
@@ -7,7 +7,9 @@ use tokio::fs;
use crate::{
app_command::InvokeCommand,
common::{Color, LengthValue, RectDelta},
+ containers::WindowContainer,
monitors::Monitor,
+ windows::traits::WindowGetters,
workspaces::Workspace,
};
@@ -213,6 +215,57 @@ impl UserConfig {
window_rules_by_event
}
+ /// Window rules that should be applied to the window when the given
+ /// event occurs.
+ pub fn pending_window_rules(
+ &self,
+ window: &WindowContainer,
+ event: &WindowRuleEvent,
+ ) -> anyhow::Result<Vec<WindowRuleConfig>> {
+ let window_title = window.native().title()?;
+ let window_class = window.native().class_name()?;
+ let window_process = window.native().process_name()?;
+
+ let pending_window_rules = self
+ .window_rules_by_event
+ .get(&event)
+ .unwrap_or(&Vec::new())
+ .iter()
+ .filter(|rule| {
+ // Skip if window has already ran the rule.
+ if window.done_window_rules().contains(&rule) {
+ return false;
+ }
+
+ // Check if the window matches the rule.
+ rule.match_window.iter().all(|match_config| {
+ let is_process_match = match_config
+ .window_process
+ .as_ref()
+ .map(|match_type| match_type.is_match(&window_process))
+ .unwrap_or(true);
+
+ let is_class_match = match_config
+ .window_class
+ .as_ref()
+ .map(|match_type| match_type.is_match(&window_class))
+ .unwrap_or(true);
+
+ let is_title_match = match_config
+ .window_title
+ .as_ref()
+ .map(|match_type| match_type.is_match(&window_title))
+ .unwrap_or(true);
+
+ is_process_match && is_class_match && is_title_match
+ })
+ })
+ .cloned()
+ .collect::<Vec<_>>();
+
+ Ok(pending_window_rules)
+ }
+
pub fn inactive_workspace_configs(
&self,
active_workspaces: &Vec<Workspace>,
@@ -431,7 +484,7 @@ pub struct BorderEffectConfig {
pub color: Color,
}
-#[derive(Clone, Debug, Deserialize, Serialize)]
+#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all(serialize = "camelCase"))]
pub struct WindowRuleConfig {
pub commands: Vec<InvokeCommand>,
@@ -446,7 +499,7 @@ pub struct WindowRuleConfig {
pub run_once: bool,
}
-#[derive(Clone, Debug, Default, Deserialize, Serialize)]
+#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
#[serde(rename_all(serialize = "camelCase"))]
pub struct WindowMatchConfig {
#[serde(default)]
@@ -460,8 +513,9 @@ pub struct WindowMatchConfig {
}
/// Due to limitations in `serde_yaml`, we need to use an untagged enum
-/// instead of a regular enum.
-#[derive(Clone, Debug, Deserialize, Serialize)]
+/// instead of a regular enum for serialization. Using a regular enum
+/// causes issues with flow-style objects in YAML.
+#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(untagged)]
pub enum MatchType {
Equals { equals: String },
@@ -469,6 +523,19 @@ pub enum MatchType {
Regex { regex: String },
}
+impl MatchType {
+ /// Whether the given value is a match for the match type.
+ fn is_match(&self, value: &str) -> bool {
+ match self {
+ MatchType::Equals { equals } => value == equals,
+ MatchType::Includes { includes } => value.contains(includes),
+ MatchType::Regex { regex } => regex::Regex::new(regex)
+ .map(|re| re.is_match(value))
+ .unwrap_or(false),
+ }
+ }
+}
+
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum WindowRuleEvent {
diff --git a/packages/wm/src/windows/commands/manage_window.rs b/packages/wm/src/windows/commands/manage_window.rs
index f0f77746..0af495f0 100644
--- a/packages/wm/src/windows/commands/manage_window.rs
+++ b/packages/wm/src/windows/commands/manage_window.rs
@@ -133,6 +133,7 @@ fn create_window(
border_delta,
floating_placement,
inner_gap,
+ Vec::new(),
)
.into(),
_ => NonTilingWindow::new(
@@ -143,6 +144,7 @@ fn create_window(
border_delta,
None,
floating_placement,
+ Vec::new(),
)
.into(),
};
diff --git a/packages/wm/src/windows/commands/mod.rs b/packages/wm/src/windows/commands/mod.rs
index 0561c91f..63abc576 100644
--- a/packages/wm/src/windows/commands/mod.rs
+++ b/packages/wm/src/windows/commands/mod.rs
@@ -3,6 +3,7 @@ mod manage_window;
mod move_window_in_direction;
mod move_window_to_workspace;
mod resize_window;
+mod run_window_rules;
mod set_window_size;
mod unmanage_window;
mod update_window_state;
@@ -12,6 +13,7 @@ pub use manage_window::*;
pub use move_window_in_direction::*;
pub use move_window_to_workspace::*;
pub use resize_window::*;
+pub use run_window_rules::*;
pub use set_window_size::*;
pub use unmanage_window::*;
pub use update_window_state::*;
diff --git a/packages/wm/src/windows/commands/run_window_rules.rs b/packages/wm/src/windows/commands/run_window_rules.rs
new file mode 100644
index 00000000..6c97c271
--- /dev/null
+++ b/packages/wm/src/windows/commands/run_window_rules.rs
@@ -0,0 +1,13 @@
+use anyhow::Context;
+
+use crate::{
+ containers::WindowContainer, user_config::UserConfig, wm_state::WmState,
+};
+
+pub fn run_window_rules(
+ window: WindowContainer,
+ state: &mut WmState,
+ config: &UserConfig,
+) -> anyhow::Result<()> {
+ Ok(())
+}
diff --git a/packages/wm/src/windows/non_tiling_window.rs b/packages/wm/src/windows/non_tiling_window.rs
index 72276c28..84c53ca9 100644
--- a/packages/wm/src/windows/non_tiling_window.rs
+++ b/packages/wm/src/windows/non_tiling_window.rs
@@ -17,6 +17,7 @@ use crate::{
WindowContainer,
},
impl_common_getters, impl_container_debug, impl_window_getters,
+ user_config::WindowRuleConfig,
};
use super::{traits::WindowGetters, TilingWindow, WindowDto, WindowState};
@@ -37,6 +38,7 @@ struct NonTilingWindowInner {
border_delta: RectDelta,
has_pending_dpi_adjustment: bool,
floating_placement: Rect,
+ done_window_rules: Vec<WindowRuleConfig>,
}
impl NonTilingWindow {
@@ -48,6 +50,7 @@ impl NonTilingWindow {
border_delta: RectDelta,
insertion_target: Option<(Container, usize)>,
floating_placement: Rect,
+ done_window_rules: Vec<WindowRuleConfig>,
) -> Self {
let window = NonTilingWindowInner {
id: id.unwrap_or_else(|| Uuid::new_v4()),
@@ -62,6 +65,7 @@ impl NonTilingWindow {
border_delta,
has_pending_dpi_adjustment: false,
floating_placement,
+ done_window_rules,
};
Self(Rc::new(RefCell::new(window)))
@@ -86,6 +90,7 @@ impl NonTilingWindow {
self.border_delta(),
self.floating_placement(),
inner_gap,
+ self.done_window_rules(),
)
}
diff --git a/packages/wm/src/windows/tiling_window.rs b/packages/wm/src/windows/tiling_window.rs
index 8089b4ce..6d713693 100644
--- a/packages/wm/src/windows/tiling_window.rs
+++ b/packages/wm/src/windows/tiling_window.rs
@@ -23,6 +23,7 @@ use crate::{
impl_common_getters, impl_container_debug,
impl_position_getters_as_resizable, impl_tiling_size_getters,
impl_window_getters,
+ user_config::WindowRuleConfig,
};
use super::{
@@ -46,6 +47,7 @@ struct TilingWindowInner {
has_pending_dpi_adjustment: bool,
floating_placement: Rect,
inner_gap: LengthValue,
+ done_window_rules: Vec<WindowRuleConfig>,
}
impl TilingWindow {
@@ -56,6 +58,7 @@ impl TilingWindow {
border_delta: RectDelta,
floating_placement: Rect,
inner_gap: LengthValue,
+ done_window_rules: Vec<WindowRuleConfig>,
) -> Self {
let window = TilingWindowInner {
id: id.unwrap_or_else(|| Uuid::new_v4()),
@@ -71,6 +74,7 @@ impl TilingWindow {
has_pending_dpi_adjustment: false,
floating_placement,
inner_gap,
+ done_window_rules,
};
Self(Rc::new(RefCell::new(window)))
@@ -89,6 +93,7 @@ impl TilingWindow {
self.border_delta(),
insertion_target,
self.floating_placement(),
+ self.done_window_rules(),
)
}
diff --git a/packages/wm/src/windows/traits/window_getters.rs b/packages/wm/src/windows/traits/window_getters.rs
index 25603f43..e6a71703 100644
--- a/packages/wm/src/windows/traits/window_getters.rs
+++ b/packages/wm/src/windows/traits/window_getters.rs
@@ -7,7 +7,7 @@ use crate::{
platform::NativeWindow, DisplayState, LengthValue, Rect, RectDelta,
},
containers::WindowContainer,
- user_config::UserConfig,
+ user_config::{UserConfig, WindowRuleConfig},
windows::WindowState,
};
@@ -101,12 +101,20 @@ pub trait WindowGetters {
fn floating_placement(&self) -> Rect;
fn set_floating_placement(&self, floating_placement: Rect);
+
+ fn done_window_rules(&self) -> Vec<WindowRuleConfig>;
+
+ fn set_done_window_rules(
+ &self,
+ done_window_rules: Vec<WindowRuleConfig>,
+ );
}
/// Implements the `WindowGetters` trait for a given struct.
///
/// Expects that the struct has a wrapping `RefCell` containing a struct
-/// with a `state`, `native`, and a `display_state` field.
+/// with a `state`, `prev_state`, `native`, `border_delta`, `display_state`,
+/// `has_pending_dpi_adjustment`, and a `done_window_rules` field.
#[macro_export]
macro_rules! impl_window_getters {
($struct_name:ident) => {
@@ -166,6 +174,17 @@ macro_rules! impl_window_getters {
fn set_floating_placement(&self, floating_placement: Rect) {
self.0.borrow_mut().floating_placement = floating_placement;
}
+
+ fn done_window_rules(&self) -> Vec<WindowRuleConfig> {
+ self.0.borrow().done_window_rules.clone()
+ }
+
+ fn set_done_window_rules(
+ &self,
+ done_window_rules: Vec<WindowRuleConfig>,
+ ) {
+ self.0.borrow_mut().done_window_rules = done_window_rules;
+ }
}
};
}