Merge pull request #72 from tonybanters/autotile
Browse files
Autotile Feature
+10 −9 resources/test-config.lua
| @@ -33,6 +33,7 @@ local modkey = "Mod1" | ||
| 33 | 33 | oxwm.set_terminal("st") |
| 34 | 34 | oxwm.set_modkey(modkey) |
| 35 | 35 | oxwm.set_tags({ "1", "2", "3", "4", "5", "6", "7", "8", "9" }) |
| 36 | +oxwm.auto_tile(true); | |
| 36 | 37 | |
| 37 | 38 | oxwm.set_layout_symbol("tiling", "[T]") |
| 38 | 39 | oxwm.set_layout_symbol("normie", "[F]") |
| @@ -42,7 +43,7 @@ oxwm.border.set_focused_color(colors.blue) | ||
| 42 | 43 | oxwm.border.set_unfocused_color(colors.grey) |
| 43 | 44 | |
| 44 | 45 | oxwm.gaps.set_enabled(true) |
| 45 | -oxwm.gaps.set_smart(true) -- Disable outer gaps when only 1 window (dwm smartgaps) | |
| 46 | +oxwm.gaps.set_smart(true) -- Disable outer gaps when only 1 window (dwm smartgaps) | |
| 46 | 47 | oxwm.gaps.set_inner(5, 5) |
| 47 | 48 | oxwm.gaps.set_outer(5, 5) |
| 48 | 49 | |
| @@ -78,16 +79,16 @@ oxwm.key.bind({ modkey }, "N", oxwm.layout.cycle()) | ||
| 78 | 79 | oxwm.key.bind({ modkey }, "A", oxwm.toggle_gaps()) |
| 79 | 80 | |
| 80 | 81 | -- Master area controls |
| 81 | -oxwm.key.bind({ modkey }, "BracketLeft", oxwm.set_master_factor(-5)) -- Decrease master area | |
| 82 | -oxwm.key.bind({ modkey }, "BracketRight", oxwm.set_master_factor(5)) -- Increase master area | |
| 83 | -oxwm.key.bind({ modkey }, "I", oxwm.inc_num_master(1)) -- More master windows | |
| 84 | -oxwm.key.bind({ modkey }, "P", oxwm.inc_num_master(-1)) -- Fewer master windows | |
| 82 | +oxwm.key.bind({ modkey }, "BracketLeft", oxwm.set_master_factor(-5)) -- Decrease master area | |
| 83 | +oxwm.key.bind({ modkey }, "BracketRight", oxwm.set_master_factor(5)) -- Increase master area | |
| 84 | +oxwm.key.bind({ modkey }, "I", oxwm.inc_num_master(1)) -- More master windows | |
| 85 | +oxwm.key.bind({ modkey }, "P", oxwm.inc_num_master(-1)) -- Fewer master windows | |
| 85 | 86 | |
| 86 | 87 | -- Multi-monitor controls (dwm-style) |
| 87 | -oxwm.key.bind({ modkey }, "Comma", oxwm.monitor.focus(-1)) -- Focus previous monitor | |
| 88 | -oxwm.key.bind({ modkey }, "Period", oxwm.monitor.focus(1)) -- Focus next monitor | |
| 89 | -oxwm.key.bind({ modkey, "Shift" }, "Comma", oxwm.monitor.tag(-1)) -- Send window to previous monitor | |
| 90 | -oxwm.key.bind({ modkey, "Shift" }, "Period", oxwm.monitor.tag(1)) -- Send window to next monitor | |
| 88 | +oxwm.key.bind({ modkey }, "Comma", oxwm.monitor.focus(-1)) -- Focus previous monitor | |
| 89 | +oxwm.key.bind({ modkey }, "Period", oxwm.monitor.focus(1)) -- Focus next monitor | |
| 90 | +oxwm.key.bind({ modkey, "Shift" }, "Comma", oxwm.monitor.tag(-1)) -- Send window to previous monitor | |
| 91 | +oxwm.key.bind({ modkey, "Shift" }, "Period", oxwm.monitor.tag(1)) -- Send window to next monitor | |
| 91 | 92 | |
| 92 | 93 | oxwm.key.bind({ modkey, "Shift" }, "Q", oxwm.quit()) |
| 93 | 94 | oxwm.key.bind({ modkey, "Shift" }, "R", oxwm.restart()) |
+1 −0 src/config/lua.rs
| @@ -48,5 +48,6 @@ pub fn parse_lua_config( | ||
| 48 | 48 | scheme_occupied: builder_data.scheme_occupied, |
| 49 | 49 | scheme_selected: builder_data.scheme_selected, |
| 50 | 50 | autostart: builder_data.autostart, |
| 51 | + auto_tile: builder_data.auto_tile, | |
| 51 | 52 | }); |
| 52 | 53 | } |
+9 −0 src/config/lua_api.rs
| @@ -32,6 +32,7 @@ pub struct ConfigBuilder { | ||
| 32 | 32 | pub scheme_occupied: ColorScheme, |
| 33 | 33 | pub scheme_selected: ColorScheme, |
| 34 | 34 | pub autostart: Vec<String>, |
| 35 | + pub auto_tile: bool, | |
| 35 | 36 | } |
| 36 | 37 | |
| 37 | 38 | impl Default for ConfigBuilder { |
| @@ -70,6 +71,7 @@ impl Default for ConfigBuilder { | ||
| 70 | 71 | underline: 0x444444, |
| 71 | 72 | }, |
| 72 | 73 | autostart: Vec::new(), |
| 74 | + auto_tile: false, | |
| 73 | 75 | } |
| 74 | 76 | } |
| 75 | 77 | } |
| @@ -738,6 +740,12 @@ fn register_misc(lua: &Lua, parent: &Table, builder: SharedBuilder) -> Result<() | ||
| 738 | 740 | Ok(()) |
| 739 | 741 | })?; |
| 740 | 742 | |
| 743 | + let builder_clone = builder.clone(); | |
| 744 | + let auto_tile = lua.create_function(move |_, enabled: bool| { | |
| 745 | + builder_clone.borrow_mut().auto_tile = enabled; | |
| 746 | + Ok(()) | |
| 747 | + })?; | |
| 748 | + | |
| 741 | 749 | parent.set("set_terminal", set_terminal)?; |
| 742 | 750 | parent.set("set_modkey", set_modkey)?; |
| 743 | 751 | parent.set("set_tags", set_tags)?; |
| @@ -750,6 +758,7 @@ fn register_misc(lua: &Lua, parent: &Table, builder: SharedBuilder) -> Result<() | ||
| 750 | 758 | parent.set("inc_num_master", inc_num_master)?; |
| 751 | 759 | parent.set("show_keybinds", show_keybinds)?; |
| 752 | 760 | parent.set("focus_monitor", focus_monitor)?; |
| 761 | + parent.set("auto_tile", auto_tile)?; | |
| 753 | 762 | Ok(()) |
| 754 | 763 | } |
| 755 | 764 |
+2 −1 src/lib.rs
| @@ -94,8 +94,8 @@ pub struct Config { | ||
| 94 | 94 | pub scheme_occupied: ColorScheme, |
| 95 | 95 | pub scheme_selected: ColorScheme, |
| 96 | 96 | |
| 97 | - // Autostart commands | |
| 98 | 97 | pub autostart: Vec<String>, |
| 98 | + pub auto_tile: bool, | |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | #[derive(Debug, Clone, Copy)] |
| @@ -338,6 +338,7 @@ impl Default for Config { | ||
| 338 | 338 | underline: 0xad8ee6, |
| 339 | 339 | }, |
| 340 | 340 | autostart: vec![], |
| 341 | + auto_tile: false, | |
| 341 | 342 | } |
| 342 | 343 | } |
| 343 | 344 | } |
+114 −3 src/window_manager.rs
| @@ -2535,9 +2535,92 @@ impl WindowManager { | ||
| 2535 | 2535 | } |
| 2536 | 2536 | } |
| 2537 | 2537 | |
| 2538 | + if self.config.auto_tile && !was_floating && !is_normie { | |
| 2539 | + let drop_monitor_idx = self | |
| 2540 | + .clients | |
| 2541 | + .get(&window) | |
| 2542 | + .map(|c| c.monitor_index) | |
| 2543 | + .unwrap_or(monitor_idx); | |
| 2544 | + | |
| 2545 | + if let Some((x, y, w, h)) = final_client { | |
| 2546 | + let center = (x as i32 + w as i32 / 2, y as i32 + h as i32 / 2); | |
| 2547 | + if let Some(target) = self.tiled_window_at(window, drop_monitor_idx, center) { | |
| 2548 | + self.detach(window); | |
| 2549 | + self.insert_before(window, target, drop_monitor_idx); | |
| 2550 | + } | |
| 2551 | + } | |
| 2552 | + | |
| 2553 | + self.floating_windows.remove(&window); | |
| 2554 | + if let Some(client) = self.clients.get_mut(&window) { | |
| 2555 | + client.is_floating = false; | |
| 2556 | + } | |
| 2557 | + self.apply_layout()?; | |
| 2558 | + } | |
| 2559 | + | |
| 2538 | 2560 | Ok(()) |
| 2539 | 2561 | } |
| 2540 | 2562 | |
| 2563 | + fn tiled_window_at( | |
| 2564 | + &self, | |
| 2565 | + exclude: Window, | |
| 2566 | + monitor_idx: usize, | |
| 2567 | + (px, py): (i32, i32), | |
| 2568 | + ) -> Option<Window> { | |
| 2569 | + let monitor = self.monitors.get(monitor_idx)?; | |
| 2570 | + let tags = monitor.tagset[monitor.selected_tags_index]; | |
| 2571 | + let mut current = monitor.clients_head; | |
| 2572 | + | |
| 2573 | + while let Some(win) = current { | |
| 2574 | + let c = self.clients.get(&win)?; | |
| 2575 | + current = c.next; | |
| 2576 | + | |
| 2577 | + if win == exclude || c.is_floating || (c.tags & tags) == 0 { | |
| 2578 | + continue; | |
| 2579 | + } | |
| 2580 | + | |
| 2581 | + let (x, y) = (c.x_position as i32, c.y_position as i32); | |
| 2582 | + let (w, h) = ( | |
| 2583 | + c.width as i32 + c.border_width as i32 * 2, | |
| 2584 | + c.height as i32 + c.border_width as i32 * 2, | |
| 2585 | + ); | |
| 2586 | + | |
| 2587 | + if px >= x && px < x + w && py >= y && py < y + h { | |
| 2588 | + return Some(win); | |
| 2589 | + } | |
| 2590 | + } | |
| 2591 | + None | |
| 2592 | + } | |
| 2593 | + | |
| 2594 | + fn insert_before(&mut self, window: Window, target: Window, monitor_idx: usize) { | |
| 2595 | + let Some(monitor) = self.monitors.get_mut(monitor_idx) else { | |
| 2596 | + return; | |
| 2597 | + }; | |
| 2598 | + | |
| 2599 | + if monitor.clients_head == Some(target) { | |
| 2600 | + if let Some(c) = self.clients.get_mut(&window) { | |
| 2601 | + c.next = Some(target); | |
| 2602 | + } | |
| 2603 | + monitor.clients_head = Some(window); | |
| 2604 | + return; | |
| 2605 | + } | |
| 2606 | + | |
| 2607 | + let mut current = monitor.clients_head; | |
| 2608 | + while let Some(w) = current { | |
| 2609 | + let Some(c) = self.clients.get(&w) else { break }; | |
| 2610 | + if c.next != Some(target) { | |
| 2611 | + current = c.next; | |
| 2612 | + continue; | |
| 2613 | + } | |
| 2614 | + if let Some(prev) = self.clients.get_mut(&w) { | |
| 2615 | + prev.next = Some(window); | |
| 2616 | + } | |
| 2617 | + if let Some(inserted) = self.clients.get_mut(&window) { | |
| 2618 | + inserted.next = Some(target); | |
| 2619 | + } | |
| 2620 | + break; | |
| 2621 | + } | |
| 2622 | + } | |
| 2623 | + | |
| 2541 | 2624 | fn resize_window_with_mouse(&mut self, window: Window) -> WmResult<()> { |
| 2542 | 2625 | let is_fullscreen = self |
| 2543 | 2626 | .clients |
| @@ -2574,12 +2657,32 @@ impl WindowManager { | ||
| 2574 | 2657 | return Ok(()); |
| 2575 | 2658 | }; |
| 2576 | 2659 | |
| 2577 | - if self.monitors.get(monitor_idx).is_none() { | |
| 2578 | - return Ok(()); | |
| 2579 | - } | |
| 2660 | + let monitor = match self.monitors.get(monitor_idx) { | |
| 2661 | + Some(m) => m, | |
| 2662 | + None => return Ok(()), | |
| 2663 | + }; | |
| 2580 | 2664 | |
| 2581 | 2665 | let is_normie = self.layout.name() == "normie"; |
| 2582 | 2666 | |
| 2667 | + if self.config.auto_tile && !was_floating && !is_normie { | |
| 2668 | + let mut tiled_count = 0; | |
| 2669 | + let mut current = monitor.clients_head; | |
| 2670 | + while let Some(w) = current { | |
| 2671 | + if let Some(c) = self.clients.get(&w) { | |
| 2672 | + let visible = (c.tags & monitor.tagset[monitor.selected_tags_index]) != 0; | |
| 2673 | + if visible && !c.is_floating { | |
| 2674 | + tiled_count += 1; | |
| 2675 | + } | |
| 2676 | + current = c.next; | |
| 2677 | + } else { | |
| 2678 | + break; | |
| 2679 | + } | |
| 2680 | + } | |
| 2681 | + if tiled_count <= 1 { | |
| 2682 | + return Ok(()); | |
| 2683 | + } | |
| 2684 | + } | |
| 2685 | + | |
| 2583 | 2686 | if !was_floating && !is_normie { |
| 2584 | 2687 | self.toggle_floating()?; |
| 2585 | 2688 | } |
| @@ -2698,6 +2801,14 @@ impl WindowManager { | ||
| 2698 | 2801 | } |
| 2699 | 2802 | } |
| 2700 | 2803 | |
| 2804 | + if self.config.auto_tile && !was_floating && !is_normie { | |
| 2805 | + self.floating_windows.remove(&window); | |
| 2806 | + if let Some(client) = self.clients.get_mut(&window) { | |
| 2807 | + client.is_floating = false; | |
| 2808 | + } | |
| 2809 | + self.apply_layout()?; | |
| 2810 | + } | |
| 2811 | + | |
| 2701 | 2812 | Ok(()) |
| 2702 | 2813 | } |
| 2703 | 2814 |
+4 −0 templates/oxwm.lua
| @@ -37,6 +37,10 @@ function oxwm.set_modkey(modkey) end | ||
| 37 | 37 | ---@param tags string[] Array of tag names |
| 38 | 38 | function oxwm.set_tags(tags) end |
| 39 | 39 | |
| 40 | +---Enable or disable automatic tiling of new windows | |
| 41 | +---@param enabled boolean Enable or disable auto-tiling | |
| 42 | +function oxwm.auto_tile(enabled) end | |
| 43 | + | |
| 40 | 44 | ---Set layout symbol override |
| 41 | 45 | ---@param name string Layout name (e.g., "tiling", "normie", "tabbed", "grid", "monocle") |
| 42 | 46 | ---@param symbol string Symbol to display (e.g., "[T]", "[F]", "[=]") |