GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: implement `run_window_rules` command
Lars Bergera89bdae1d6d1
diff --git a/packages/wm/src/common/events/handle_window_shown.rs b/packages/wm/src/common/events/handle_window_shown.rsindex 7bbc4acb..9594b801 100644--- a/packages/wm/src/common/events/handle_window_shown.rs+++ b/packages/wm/src/common/events/handle_window_shown.rs@@ -10,7 +10,7 @@ use crate::{pub fn handle_window_shown(native_window: NativeWindow,state: &mut WmState,- config: &UserConfig,+ config: &mut UserConfig,) -> anyhow::Result<()> {let found_window = state.window_from_native(&native_window);diff --git a/packages/wm/src/common/platform/native_window.rs b/packages/wm/src/common/platform/native_window.rsindex 9676ac8d..f5e6bfd2 100644--- a/packages/wm/src/common/platform/native_window.rs+++ b/packages/wm/src/common/platform/native_window.rs@@ -1,3 +1,4 @@+use anyhow::Context;use tracing::warn;use windows::{core::PWSTR,@@ -118,7 +119,15 @@ impl NativeWindow {CloseHandle(process_handle)?;};- Ok(String::from_utf16_lossy(&buffer[..length as usize]))+ let exe_path = String::from_utf16_lossy(&buffer[..length as usize]);++ exe_path+ .split('\\')+ .last()+ .map(|file_name| {+ file_name.split('.').next().unwrap_or(file_name).to_string()+ })+ .context("Failed to parse process name.")}/// Gets the class name of the window.diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rsindex 472c558c..6220608b 100644--- a/packages/wm/src/main.rs+++ b/packages/wm/src/main.rs@@ -88,7 +88,7 @@ async fn start_wm(let mut ipc_server = IpcServer::start().await?;- let mut wm = WindowManager::new(&config)?;+ let mut wm = WindowManager::new(&mut config)?;// Start listening for platform events after populating initial state.let mut event_listener = Platform::start_event_listener(&config)?;diff --git a/packages/wm/src/user_config.rs b/packages/wm/src/user_config.rsindex 4ecc35ea..3f36c635 100644--- a/packages/wm/src/user_config.rs+++ b/packages/wm/src/user_config.rs@@ -238,7 +238,7 @@ impl UserConfig {}// Check if the window matches the rule.- rule.match_window.iter().all(|match_config| {+ rule.match_window.iter().any(|match_config| {let is_process_match = match_config.window_process.as_ref()diff --git a/packages/wm/src/windows/commands/manage_window.rs b/packages/wm/src/windows/commands/manage_window.rsindex 0af495f0..711f010b 100644--- a/packages/wm/src/windows/commands/manage_window.rs+++ b/packages/wm/src/windows/commands/manage_window.rs@@ -9,9 +9,10 @@ use crate::{Container, WindowContainer,},monitors::Monitor,- user_config::UserConfig,+ user_config::{UserConfig, WindowRuleEvent},windows::{- traits::WindowGetters, NonTilingWindow, TilingWindow, WindowState,+ commands::run_window_rules, traits::WindowGetters, NonTilingWindow,+ TilingWindow, WindowState,},wm_event::WmEvent,wm_state::WmState,@@ -21,46 +22,47 @@ pub fn manage_window(native_window: NativeWindow,target_parent: Option<Container>,state: &mut WmState,- config: &UserConfig,+ config: &mut UserConfig,) -> anyhow::Result<()> {// Create the window instance.let window = create_window(native_window, target_parent, state, config)?;- // let window_rules = config.matching_window_rules(&window);- // let window_rule_commands =- // window_rules.iter().flat_map(|rule| &rule.commands);-// Set the newly added window as focus descendant. This means the window// rules will be run as if the window is focused.set_focused_descendant(window.clone().into(), None);- // run_with_subject_container(window_rule_commands, window.clone());-- // // Update window in case the reference changes.- // let window = window_service.get_window_by_handle(window.handle());-- // // Window might be detached if 'ignore' command has been invoked.- // if window.is_none() || !window.unwrap().is_detached() {- // return Ok(());- // }-- // TODO: Log window details.- info!("New window managed");- state.emit_event(WmEvent::WindowManaged {- managed_window: window.to_dto()?,- });-- // OS focus should be set to the newly added window in case it's not- // already focused.- state.pending_sync.focus_change = true;-- // Sibling containers need to be redrawn if the window is tiling.- state.pending_sync.containers_to_redraw.push(- if window.state() == WindowState::Tiling {- window.parent().context("No parent.")?- } else {- window.into()- },- );++ run_window_rules(+ window.clone(),+ WindowRuleEvent::Manage,+ state,+ config,+ )?;++ // Update window in case the reference changes. Window might be detached+ // if `ignore` command has been invoked.+ let window = state.window_from_native(&window.native());++ if let Some(window) = window {+ // TODO: Log window details.+ info!("New window managed");++ state.emit_event(WmEvent::WindowManaged {+ managed_window: window.to_dto()?,+ });++ // OS focus should be set to the newly added window in case it's not+ // already focused.+ state.pending_sync.focus_change = true;++ // Sibling containers need to be redrawn if the window is tiling.+ state.pending_sync.containers_to_redraw.push(+ if window.state() == WindowState::Tiling {+ window.parent().context("No parent.")?+ } else {+ window.into()+ },+ );+ }Ok(())}diff --git a/packages/wm/src/windows/commands/run_window_rules.rs b/packages/wm/src/windows/commands/run_window_rules.rsindex 6c97c271..376c57b9 100644--- a/packages/wm/src/windows/commands/run_window_rules.rs+++ b/packages/wm/src/windows/commands/run_window_rules.rs@@ -1,13 +1,49 @@-use anyhow::Context;+use tracing::info;use crate::{- containers::WindowContainer, user_config::UserConfig, wm_state::WmState,+ containers::{traits::CommonGetters, WindowContainer},+ user_config::{UserConfig, WindowRuleEvent},+ windows::traits::WindowGetters,+ wm_state::WmState,};pub fn run_window_rules(window: WindowContainer,+ event_type: WindowRuleEvent,state: &mut WmState,- config: &UserConfig,+ config: &mut UserConfig,) -> anyhow::Result<()> {+ let pending_window_rules =+ config.pending_window_rules(&window, &event_type)?;++ let mut subject_window = window;++ for rule in pending_window_rules {+ info!("Running window rule with commands: {:?}", rule.commands);++ for command in &rule.commands {+ command.run(subject_window.clone().into(), state, config)?;++ // Update the subject container in case the container type changes.+ // For example, when going from a tiling to a floating window.+ subject_window = match subject_window.is_detached() {+ false => subject_window,+ true => match state.window_from_native(&subject_window.native()) {+ Some(window) => window,+ None => break,+ },+ }+ }++ if rule.run_once {+ let window_rules = subject_window+ .done_window_rules()+ .into_iter()+ .chain(std::iter::once(rule));++ subject_window.set_done_window_rules(window_rules.collect());+ }+ }+Ok(())}diff --git a/packages/wm/src/wm.rs b/packages/wm/src/wm.rsindex 08c97a8c..24c96162 100644--- a/packages/wm/src/wm.rs+++ b/packages/wm/src/wm.rs@@ -28,11 +28,11 @@ pub struct WindowManager {}impl WindowManager {- pub fn new(config: &UserConfig) -> anyhow::Result<Self> {+ pub fn new(config: &mut UserConfig) -> anyhow::Result<Self> {let (event_tx, event_rx) = mpsc::unbounded_channel();let mut state = WmState::new(event_tx);- state.populate(&config)?;+ state.populate(config)?;Ok(Self { event_rx, state })}diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rsindex 896868ab..bf97f468 100644--- a/packages/wm/src/wm_state.rs+++ b/packages/wm/src/wm_state.rs@@ -92,7 +92,10 @@ impl WmState {/// Populates the initial WM state by creating containers for all/// existing windows and monitors.- pub fn populate(&mut self, config: &UserConfig) -> anyhow::Result<()> {+ pub fn populate(+ &mut self,+ config: &mut UserConfig,+ ) -> anyhow::Result<()> {// Get the originally focused window when the WM was started.let foreground_window = Platform::foreground_window();