oxwm

https://git.tonybtw.com/oxwm.git git://git.tonybtw.com/oxwm.git

add focus option to window rules

Commit
b025365d5b33aab305457f90ed269255d7fad4c1
Parent
7fdee0e
Author
emzywastaken <amiamemetoo@gmail.com>
Date
2025-12-29 19:27:41
Closes #103

Diff

diff --git a/src/config/lua_api.rs b/src/config/lua_api.rs
index 3471462..063eb2e 100644
--- a/src/config/lua_api.rs
+++ b/src/config/lua_api.rs
@@ -392,6 +392,7 @@ fn register_rule_module(
         let title: Option<String> = config.get("title").ok();
         let is_floating: Option<bool> = config.get("floating").ok();
         let monitor: Option<usize> = config.get("monitor").ok();
+        let focus: Option<bool> = config.get("focus").ok();
 
         let tags: Option<u32> = if let Ok(tag_index) = config.get::<i32>("tag") {
             if tag_index > 0 {
@@ -408,6 +409,7 @@ fn register_rule_module(
             instance,
             title,
             tags,
+            focus,
             is_floating,
             monitor,
         };
diff --git a/src/lib.rs b/src/lib.rs
index 87aab35..31463d9 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -34,6 +34,7 @@ pub struct WindowRule {
     pub instance: Option<String>,
     pub title: Option<String>,
     pub tags: Option<u32>,
+    pub focus: Option<bool>,
     pub is_floating: Option<bool>,
     pub monitor: Option<usize>,
 }
diff --git a/src/window_manager.rs b/src/window_manager.rs
index 4e4a0cb..c175318 100644
--- a/src/window_manager.rs
+++ b/src/window_manager.rs
@@ -1655,6 +1655,7 @@ impl WindowManager {
         let mut rule_tags: Option<u32> = None;
         let mut rule_floating: Option<bool> = None;
         let mut rule_monitor: Option<usize> = None;
+        let mut rule_focus = false;
 
         for rule in &self.config.window_rules {
             if rule.matches(&class, &instance, &title) {
@@ -1667,6 +1668,7 @@ impl WindowManager {
                 if rule.monitor.is_some() {
                     rule_monitor = rule.monitor;
                 }
+                rule_focus = rule.focus.unwrap_or(false);
             }
         }
 
@@ -1686,14 +1688,27 @@ impl WindowManager {
                 client.monitor_index = monitor_index;
             }
 
-            let tags = rule_tags.unwrap_or_else(|| {
+            let monitor_tagset = || {
                 self.monitors
                     .get(client.monitor_index)
-                    .map(|m| m.tagset[m.selected_tags_index])
+                    .map(|m| m.get_selected_tag())
                     .unwrap_or(tag_mask(0))
-            });
+            };
+
+            let tags = rule_tags.unwrap_or_else(monitor_tagset);
 
             client.tags = tags;
+
+            // Focus window rule tag if config option is set
+            if rule_focus && let Some(tag_mask) = rule_tags {
+                let tag_index = unmask_tag(tag_mask);
+                let monitor_tagset = monitor_tagset();
+                let is_tag_focused = monitor_tagset & tag_mask == tag_mask;
+
+                if !is_tag_focused {
+                    self.view_tag(tag_index)?;
+                }
+            }
         }
 
         Ok(())
diff --git a/templates/config.lua b/templates/config.lua
index b0f09c8..1892373 100644
--- a/templates/config.lua
+++ b/templates/config.lua
@@ -144,6 +144,7 @@ oxwm.gaps.set_outer(5, 5)
 
 -- Examples (uncomment to use):
 oxwm.rule.add({ instance = "gimp", floating = true })                             
+-- oxwm.rule.add({ class = "Alacritty", tag = 9, focus = true })                             
 -- oxwm.rule.add({ class = "firefox", title = "Library", floating = true })  
 -- oxwm.rule.add({ class = "firefox", tag = 2 })  
 -- oxwm.rule.add({ instance = "mpv", floating = true })                      
diff --git a/templates/oxwm.lua b/templates/oxwm.lua
index 77a2d47..3f6d124 100644
--- a/templates/oxwm.lua
+++ b/templates/oxwm.lua
@@ -51,7 +51,7 @@ function oxwm.set_layout_symbol(name, symbol) end
 oxwm.rule = {}
 
 ---Add a window rule
----@param rule {class: string?, instance: string?, title: string?, role: string?, floating: boolean?, tag: integer?, fullscreen: boolean?} Rule configuration
+---@param rule {class: string?, instance: string?, title: string?, role: string?, floating: boolean?, tag: integer?, fullscreen: boolean?, focus: boolean?} Rule configuration
 function oxwm.rule.add(rule) end
 
 ---Quit the window manager