Merge pull request #81 from tonybanters/more-tag-actions
Browse files
Add key actions for viewing next/previous tags and next_nonempty/previous_nonempty tags
+6 −0 resources/test-config.lua
| @@ -145,6 +145,12 @@ oxwm.key.bind({ modkey, "Control", "Shift" }, "7", oxwm.tag.toggletag(6)) | ||
| 145 | 145 | oxwm.key.bind({ modkey, "Control", "Shift" }, "8", oxwm.tag.toggletag(7)) |
| 146 | 146 | oxwm.key.bind({ modkey, "Control", "Shift" }, "9", oxwm.tag.toggletag(8)) |
| 147 | 147 | |
| 148 | +oxwm.key.bind({ modkey }, "Tab", oxwm.tag.view_next()) | |
| 149 | +oxwm.key.bind({ modkey, "Shift" }, "Tab", oxwm.tag.view_previous()) | |
| 150 | + | |
| 151 | +oxwm.key.bind({ modkey, "Control" }, "Tab", oxwm.tag.view_next_nonempty()) | |
| 152 | +oxwm.key.bind({ modkey, "Control", "Shift" }, "Tab", oxwm.tag.view_previous_nonempty()) | |
| 153 | + | |
| 148 | 154 | oxwm.bar.set_blocks({ |
| 149 | 155 | oxwm.bar.block.battery({ |
| 150 | 156 | format = "Bat: {}%", |
+21 −0 src/config/lua_api.rs
| @@ -303,6 +303,19 @@ fn register_tag_module(lua: &Lua, parent: &Table) -> Result<(), ConfigError> { | ||
| 303 | 303 | create_action_table(lua, "ViewTag", Value::Integer(idx as i64)) |
| 304 | 304 | })?; |
| 305 | 305 | |
| 306 | + let view_next = | |
| 307 | + lua.create_function(|lua, ()| create_action_table(lua, "ViewNextTag", Value::Nil))?; | |
| 308 | + | |
| 309 | + let view_previous = | |
| 310 | + lua.create_function(|lua, ()| create_action_table(lua, "ViewPreviousTag", Value::Nil))?; | |
| 311 | + | |
| 312 | + let view_next_nonempty = | |
| 313 | + lua.create_function(|lua, ()| create_action_table(lua, "ViewNextNonEmptyTag", Value::Nil))?; | |
| 314 | + | |
| 315 | + let view_previous_nonempty = lua.create_function(|lua, ()| { | |
| 316 | + create_action_table(lua, "ViewPreviousNonEmptyTag", Value::Nil) | |
| 317 | + })?; | |
| 318 | + | |
| 306 | 319 | let toggleview = lua.create_function(|lua, idx: i32| { |
| 307 | 320 | create_action_table(lua, "ToggleView", Value::Integer(idx as i64)) |
| 308 | 321 | })?; |
| @@ -316,6 +329,10 @@ fn register_tag_module(lua: &Lua, parent: &Table) -> Result<(), ConfigError> { | ||
| 316 | 329 | })?; |
| 317 | 330 | |
| 318 | 331 | tag_table.set("view", view)?; |
| 332 | + tag_table.set("view_next", view_next)?; | |
| 333 | + tag_table.set("view_previous", view_previous)?; | |
| 334 | + tag_table.set("view_next_nonempty", view_next_nonempty)?; | |
| 335 | + tag_table.set("view_previous_nonempty", view_previous_nonempty)?; | |
| 319 | 336 | tag_table.set("toggleview", toggleview)?; |
| 320 | 337 | tag_table.set("move_to", move_to)?; |
| 321 | 338 | tag_table.set("toggletag", toggletag)?; |
| @@ -849,6 +866,10 @@ fn string_to_action(s: &str) -> mlua::Result<KeyAction> { | ||
| 849 | 866 | "Quit" => Ok(KeyAction::Quit), |
| 850 | 867 | "Restart" => Ok(KeyAction::Restart), |
| 851 | 868 | "ViewTag" => Ok(KeyAction::ViewTag), |
| 869 | + "ViewNextTag" => Ok(KeyAction::ViewNextTag), | |
| 870 | + "ViewPreviousTag" => Ok(KeyAction::ViewPreviousTag), | |
| 871 | + "ViewNextNonEmptyTag" => Ok(KeyAction::ViewNextNonEmptyTag), | |
| 872 | + "ViewPreviousNonEmptyTag" => Ok(KeyAction::ViewPreviousNonEmptyTag), | |
| 852 | 873 | "ToggleView" => Ok(KeyAction::ToggleView), |
| 853 | 874 | "MoveToTag" => Ok(KeyAction::MoveToTag), |
| 854 | 875 | "ToggleTag" => Ok(KeyAction::ToggleTag), |
+4 −0 src/keyboard/handlers.rs
| @@ -17,6 +17,10 @@ pub enum KeyAction { | ||
| 17 | 17 | Quit, |
| 18 | 18 | Restart, |
| 19 | 19 | ViewTag, |
| 20 | + ViewNextTag, | |
| 21 | + ViewPreviousTag, | |
| 22 | + ViewNextNonEmptyTag, | |
| 23 | + ViewPreviousNonEmptyTag, | |
| 20 | 24 | ToggleView, |
| 21 | 25 | MoveToTag, |
| 22 | 26 | ToggleTag, |
+5 −0 src/monitor.rs
| @@ -1,3 +1,4 @@ | ||
| 1 | +use crate::client::TagMask; | |
| 1 | 2 | use crate::errors::WmError; |
| 2 | 3 | use x11rb::protocol::xinerama::ConnectionExt as _; |
| 3 | 4 | use x11rb::protocol::xproto::{Screen, Window}; |
| @@ -75,6 +76,10 @@ impl Monitor { | ||
| 75 | 76 | && y >= self.screen_y |
| 76 | 77 | && y < self.screen_y + self.screen_height |
| 77 | 78 | } |
| 79 | + | |
| 80 | + pub fn get_selected_tag(&self) -> TagMask { | |
| 81 | + self.tagset[self.selected_tags_index] | |
| 82 | + } | |
| 78 | 83 | } |
| 79 | 84 | |
| 80 | 85 | pub fn detect_monitors( |
+4 −0 src/overlay/keybind.rs
| @@ -220,6 +220,10 @@ impl KeybindOverlay { | ||
| 220 | 220 | Arg::Int(n) => format!("View Workspace {}", n), |
| 221 | 221 | _ => "View Workspace".to_string(), |
| 222 | 222 | }, |
| 223 | + KeyAction::ViewNextTag => "View Next Workspace".to_string(), | |
| 224 | + KeyAction::ViewPreviousTag => "View Previous Workspace".to_string(), | |
| 225 | + KeyAction::ViewNextNonEmptyTag => "View Next Non-Empty Workspace".to_string(), | |
| 226 | + KeyAction::ViewPreviousNonEmptyTag => "View Previous Non-Empty Workspace".to_string(), | |
| 223 | 227 | KeyAction::ToggleView => match &binding.arg { |
| 224 | 228 | Arg::Int(n) => format!("Toggle View Workspace {}", n), |
| 225 | 229 | _ => "Toggle View Workspace".to_string(), |
+71 −0 src/window_manager.rs
| @@ -21,6 +21,12 @@ pub fn tag_mask(tag: usize) -> TagMask { | ||
| 21 | 21 | 1 << tag |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | +/// Get back a tag index from a [`TagMask`] | |
| 25 | +pub fn unmask_tag(mask: TagMask) -> usize { | |
| 26 | + // mask only has one bit set, so this works. | |
| 27 | + mask.trailing_zeros() as usize | |
| 28 | +} | |
| 29 | + | |
| 24 | 30 | struct AtomCache { |
| 25 | 31 | net_current_desktop: Atom, |
| 26 | 32 | net_client_info: Atom, |
| @@ -805,6 +811,46 @@ impl WindowManager { | ||
| 805 | 811 | self.view_tag(*tag_index as usize)?; |
| 806 | 812 | } |
| 807 | 813 | } |
| 814 | + KeyAction::ViewNextTag => { | |
| 815 | + let monitor = self.get_selected_monitor(); | |
| 816 | + let current_tag_index = unmask_tag(monitor.get_selected_tag()) as i32; | |
| 817 | + let len = self.config.tags.len() as i32; | |
| 818 | + self.view_tag((current_tag_index + 1).rem_euclid(len) as usize)?; | |
| 819 | + } | |
| 820 | + KeyAction::ViewPreviousTag => { | |
| 821 | + let monitor = self.get_selected_monitor(); | |
| 822 | + let current_tag_index = unmask_tag(monitor.get_selected_tag()) as i32; | |
| 823 | + let len = self.config.tags.len() as i32; | |
| 824 | + self.view_tag((current_tag_index - 1).rem_euclid(len) as usize)?; | |
| 825 | + } | |
| 826 | + KeyAction::ViewNextNonEmptyTag => { | |
| 827 | + let monitor = self.get_selected_monitor(); | |
| 828 | + let current = unmask_tag(monitor.get_selected_tag()) as i32; | |
| 829 | + let len = self.config.tags.len() as i32; | |
| 830 | + let mon_num = monitor.monitor_number; | |
| 831 | + | |
| 832 | + for offset in 1..len { | |
| 833 | + let next = (current + offset).rem_euclid(len) as usize; | |
| 834 | + if self.has_windows_on_tag(mon_num, next) { | |
| 835 | + self.view_tag(next)?; | |
| 836 | + break; | |
| 837 | + } | |
| 838 | + } | |
| 839 | + } | |
| 840 | + KeyAction::ViewPreviousNonEmptyTag => { | |
| 841 | + let monitor = self.get_selected_monitor(); | |
| 842 | + let current = unmask_tag(monitor.get_selected_tag()) as i32; | |
| 843 | + let len = self.config.tags.len() as i32; | |
| 844 | + let mon_num = monitor.monitor_number; | |
| 845 | + | |
| 846 | + for offset in 1..len { | |
| 847 | + let prev = (current - offset).rem_euclid(len) as usize; | |
| 848 | + if self.has_windows_on_tag(mon_num, prev) { | |
| 849 | + self.view_tag(prev)?; | |
| 850 | + break; | |
| 851 | + } | |
| 852 | + } | |
| 853 | + } | |
| 808 | 854 | KeyAction::ToggleView => { |
| 809 | 855 | if let Arg::Int(tag_index) = arg { |
| 810 | 856 | self.toggleview(*tag_index as usize)?; |
| @@ -4174,6 +4220,31 @@ impl WindowManager { | ||
| 4174 | 4220 | Ok(()) |
| 4175 | 4221 | } |
| 4176 | 4222 | |
| 4223 | + fn get_selected_monitor(&self) -> &Monitor { | |
| 4224 | + &self.monitors[self.selected_monitor] | |
| 4225 | + } | |
| 4226 | + | |
| 4227 | + fn has_windows_on_tag(&self, monitor_number: usize, tag_index: usize) -> bool { | |
| 4228 | + let Some(monitor) = self.monitors.get(monitor_number) else { | |
| 4229 | + return false; | |
| 4230 | + }; | |
| 4231 | + | |
| 4232 | + let mut current = monitor.clients_head; | |
| 4233 | + while let Some(window) = current { | |
| 4234 | + // A window should always have a client attatched to it. | |
| 4235 | + let Some(client) = self.clients.get(&window) else { | |
| 4236 | + break; | |
| 4237 | + }; | |
| 4238 | + | |
| 4239 | + if unmask_tag(client.tags) == tag_index { | |
| 4240 | + return true; | |
| 4241 | + } | |
| 4242 | + current = client.next; | |
| 4243 | + } | |
| 4244 | + | |
| 4245 | + false | |
| 4246 | + } | |
| 4247 | + | |
| 4177 | 4248 | fn run_autostart_commands(&self) { |
| 4178 | 4249 | for command in &self.config.autostart { |
| 4179 | 4250 | crate::signal::spawn_detached(command); |
+16 −0 templates/oxwm.lua
| @@ -205,6 +205,22 @@ oxwm.tag = {} | ||
| 205 | 205 | ---@return table Action table for keybinding |
| 206 | 206 | function oxwm.tag.view(index) end |
| 207 | 207 | |
| 208 | +---View/switch to next tag | |
| 209 | +---@return table Action table for keybinding | |
| 210 | +function oxwm.tag.view_next() end | |
| 211 | + | |
| 212 | +---View/switch to previous tag | |
| 213 | +---@return table Action table for keybinding | |
| 214 | +function oxwm.tag.view_previous() end | |
| 215 | + | |
| 216 | +---View/switch to next non-empty tag | |
| 217 | +---@return table Action table for keybinding | |
| 218 | +function oxwm.tag.view_next_nonempty() end | |
| 219 | + | |
| 220 | +---View/switch to previous non-empty tag | |
| 221 | +---@return table Action table for keybinding | |
| 222 | +function oxwm.tag.view_previous_nonempty() end | |
| 223 | + | |
| 208 | 224 | ---Move focused window to tag |
| 209 | 225 | ---@param index integer Tag index (0-based) |
| 210 | 226 | ---@return table Action table for keybinding |