Added bar. 2mb!
Browse files
+132 −61 readme.org
| @@ -6,9 +6,6 @@ A dynamic window manager written in Rust, inspired by dwm but designed to evolve | ||
| 6 | 6 | on its own. Configuration is done in Rust source code, keeping with the suckless |
| 7 | 7 | philosophy of *"edit + recompile."* |
| 8 | 8 | |
| 9 | -This project is still in its early stages. Currently, it can claim ownership of | |
| 10 | -an X display and log incoming events. | |
| 11 | - | |
| 12 | 9 | * Project Structure |
| 13 | 10 | |
| 14 | 11 | #+begin_src sh |
| @@ -22,34 +19,58 @@ src/ | ||
| 22 | 19 | │ │ ├── connection: RustConnection [X11 connection] |
| 23 | 20 | │ │ ├── windows: Vec<Window> [All managed windows] |
| 24 | 21 | │ │ ├── focused_window: Option<Window> |
| 25 | -│ │ └── layout: Box<dyn Layout> | |
| 22 | +│ │ ├── layout: Box<dyn Layout> | |
| 23 | +│ │ ├── window_tags: HashMap<Window, TagMask> | |
| 24 | +│ │ ├── selected_tags: TagMask | |
| 25 | +│ │ └── bar: Bar [Status bar] | |
| 26 | 26 | │ │ |
| 27 | -│ ├── new() [Initialize WM, grab root] | |
| 27 | +│ ├── new() [Initialize WM, grab root, create bar] | |
| 28 | 28 | │ ├── run() [Main event loop] |
| 29 | 29 | │ ├── handle_event() [Route X11 events] |
| 30 | -│ │ ├── MapRequest → add window, apply layout | |
| 31 | -│ │ ├── UnmapNotify → remove window | |
| 32 | -│ │ ├── DestroyNotify → remove window | |
| 33 | -│ │ └── KeyPress → get action, handle it | |
| 30 | +│ │ ├── MapRequest → add window, apply layout, update bar | |
| 31 | +│ │ ├── UnmapNotify → remove window, update bar | |
| 32 | +│ │ ├── DestroyNotify → remove window, update bar | |
| 33 | +│ │ ├── KeyPress → get action, handle it | |
| 34 | +│ │ ├── ButtonPress → handle bar clicks | |
| 35 | +│ │ └── Expose → redraw bar | |
| 34 | 36 | │ ├── handle_key_action() [Execute keyboard actions] |
| 35 | 37 | │ ├── remove_window() [Remove from Vec, reapply layout] |
| 36 | 38 | │ ├── set_focus() [Focus window, update visuals] |
| 37 | -│ ├── cycle_focus() [Move focus to next window] | |
| 39 | +│ ├── cycle_focus() [Move focus to next/prev window] | |
| 40 | +│ ├── view_tag() [Switch to tag/workspace] | |
| 41 | +│ ├── move_to_tag() [Move window to tag] | |
| 42 | +│ ├── update_bar() [Calculate occupied tags, redraw bar] | |
| 38 | 43 | │ ├── update_focus_visuals() [Set border colors] |
| 39 | -│ └── apply_layout() [Position all windows] | |
| 44 | +│ └── apply_layout() [Position all windows below bar] | |
| 45 | +│ | |
| 46 | +├── config.rs [CONFIGURATION - all settings here] | |
| 47 | +│ ├── BORDER_WIDTH, BORDER_FOCUSED, BORDER_UNFOCUSED | |
| 48 | +│ ├── TAG_COUNT, TAGS [Workspace configuration] | |
| 49 | +│ ├── TERMINAL, MODKEY | |
| 50 | +│ └── KEYBINDINGS [All keybinds as const array] | |
| 51 | +│ | |
| 52 | +├── bar/ | |
| 53 | +│ ├── mod.rs [Re-exports, constants] | |
| 54 | +│ └── bar.rs | |
| 55 | +│ ├── struct Bar [Status bar window] | |
| 56 | +│ ├── new() [Create bar X11 window] | |
| 57 | +│ ├── draw() [Render tags with state indicators] | |
| 58 | +│ ├── handle_click() [Detect which tag was clicked] | |
| 59 | +│ └── invalidate() [Mark bar as needing redraw] | |
| 40 | 60 | │ |
| 41 | 61 | ├── keyboard/ |
| 42 | 62 | │ ├── mod.rs [Re-exports] |
| 43 | 63 | │ ├── keycodes.rs [Key constants: Q, J, RETURN, etc] |
| 44 | 64 | │ └── handlers.rs |
| 45 | -│ ├── enum KeyAction [SpawnTerminal, CloseWindow, CycleWindow, Quit, None] | |
| 65 | +│ ├── enum KeyAction [Spawn, KillClient, FocusStack, ViewTag, etc] | |
| 66 | +│ ├── enum Arg [None, Int, Str, Array] | |
| 46 | 67 | │ ├── setup_keybinds() [Register keys with X11] |
| 47 | 68 | │ └── handle_key_press() [Parse KeyPressEvent → KeyAction] |
| 48 | 69 | │ |
| 49 | 70 | └── layout/ |
| 50 | - ├── mod.rs [Layout trait definition] | |
| 51 | - └── tiling.rs | |
| 52 | - └── TilingLayout::arrange() [Calculate window positions] | |
| 71 | + ├── mod.rs [Layout trait definition] | |
| 72 | + └── tiling.rs | |
| 73 | + └── TilingLayout::arrange() [Calculate window positions] | |
| 53 | 74 | #+end_src |
| 54 | 75 | |
| 55 | 76 | * Event Flow |
| @@ -59,19 +80,27 @@ src/ | ||
| 59 | 80 | 3. For KeyPress: |
| 60 | 81 | - keyboard::handle_key_press() → KeyAction |
| 61 | 82 | - handle_key_action() executes action |
| 83 | + - update_bar() if tags/windows changed | |
| 62 | 84 | 4. For Map/Unmap: |
| 63 | - - Modify windows Vec | |
| 64 | - - apply_layout() repositions everything | |
| 85 | + - Modify windows Vec and window_tags HashMap | |
| 86 | + - apply_layout() repositions everything (accounting for bar) | |
| 87 | + - update_bar() shows occupied tags | |
| 65 | 88 | - update_focus_visuals() redraws borders |
| 89 | +5. For ButtonPress on bar: | |
| 90 | + - bar.handle_click() determines clicked tag | |
| 91 | + - view_tag() switches workspace | |
| 66 | 92 | |
| 67 | 93 | * Key Bindings |
| 68 | 94 | |
| 69 | -| Binding | Action | | |
| 70 | -|---------------+----------------------| | |
| 71 | -| Alt+Return | Spawn terminal | | |
| 72 | -| Alt+J | Cycle focus | | |
| 73 | -| Alt+Shift+Q | Close focused window | | |
| 74 | -| Alt+Shift+Q | Quit WM | | |
| 95 | +| Binding | Action | | |
| 96 | +|-----------------+-------------------------| | |
| 97 | +| Alt+Return | Spawn terminal | | |
| 98 | +| Alt+J/K | Cycle focus down/up | | |
| 99 | +| Alt+Q | Kill focused window | | |
| 100 | +| Alt+Shift+Q | Quit WM | | |
| 101 | +| Alt+1-9 | View tag 1-9 | | |
| 102 | +| Alt+Shift+1-9 | Move window to tag 1-9 | | |
| 103 | +| Alt+S | Screenshot (maim) | | |
| 75 | 104 | |
| 76 | 105 | * ⚙ Installation — Running with Nix Flakes |
| 77 | 106 | You can set up a reproducible development environment with Rust, Cargo, Xephyr, xterm, and |
| @@ -100,55 +129,97 @@ just test | ||
| 100 | 129 | This should open a new Xephyr window. oxwm will attach to it and log X11 |
| 101 | 130 | events in your host terminal. Clients like xterm/xclock will appear inside Xephyr. |
| 102 | 131 | |
| 103 | -* OXWM Todo List: | |
| 104 | -** TODO Reorganization Tasks [1/2] | |
| 105 | -- [X] Move keyboard module to folder structure: | |
| 106 | - - [X] Create =src/keyboard/mod.rs= with re-exports | |
| 107 | - - [X] Move constants to =src/keyboard/keycodes.rs= | |
| 108 | - - [X] Move key handlers to =src/keyboard/handlers.rs= | |
| 109 | - - [X] Update imports in main.rs and window_manager.rs | |
| 110 | -- [ ] Create =src/config.rs= in root directory for future configuration system | |
| 111 | - | |
| 112 | -** TODO Core Window Management [1/2] | |
| 132 | +* Current Status | |
| 133 | +** Working Features | |
| 134 | +- ✓ X11 event handling and window management | |
| 135 | +- ✓ Tag system (9 workspaces) with keyboard switching | |
| 136 | +- ✓ Window focus cycling (Alt+J/K) | |
| 137 | +- ✓ Tiling layout with border indicators | |
| 138 | +- ✓ Status bar showing tags | |
| 139 | + - Visual indicators: selected (white), occupied (gray line), empty (dim) | |
| 140 | + - Click-to-switch tags | |
| 141 | + - Performance-optimized redrawing | |
| 142 | +- ✓ Basic keybindings (spawn, kill, focus, tags) | |
| 143 | +- ✓ Configuration via Rust constants in config.rs | |
| 144 | + | |
| 145 | +** Immediate Next Steps | |
| 146 | +- [ ] Status text in bar (date, time, system info) | |
| 147 | +- [ ] dmenu integration for application launcher | |
| 148 | +- [ ] Additional widgets (clock, battery, etc.) | |
| 149 | + | |
| 150 | +** Long Term Roadmap | |
| 151 | +- [ ] Multi-monitor support | |
| 152 | +- [ ] Additional layouts (monocle, floating, etc.) | |
| 153 | +- [ ] Per-window floating behavior | |
| 154 | +- [ ] Per-program rules (auto-tag assignment, floating rules) | |
| 155 | +- [ ] Master area resizing | |
| 156 | +- [ ] Window swapping in layout | |
| 157 | +- [ ] Configurable gaps between windows | |
| 158 | +- [ ] External bar support (polybar, lemonbar, etc.) | |
| 159 | + | |
| 160 | +* OXWM Development Todo | |
| 161 | +** DONE Core Window Management [2/2] | |
| 113 | 162 | - [X] Fix layout after program is closed (handle UnmapNotify events) |
| 114 | 163 | - [X] Add UnmapNotify to event handling |
| 115 | 164 | - [X] Remove closed windows from windows vector |
| 116 | 165 | - [X] Re-apply layout after window removal |
| 117 | -- [ ] Add keybind to swap focus between windows | |
| 118 | - - [ ] Track focused window in WindowManager struct | |
| 119 | - - [ ] Implement focus cycling logic | |
| 120 | - - [ ] Add visual focus indication (borders/colors) | |
| 121 | - | |
| 122 | -** Key System Improvements | |
| 123 | -- [ ] Connect config.rs to keyboard system for dynamic keybind generation | |
| 124 | -- [ ] Add more dwm-like keybinds: | |
| 125 | - - [ ] Window focus switching (Alt+J/K) | |
| 126 | - - [ ] Master area resizing | |
| 127 | - - [ ] Layout switching | |
| 128 | - - [ ] Workspace/tag management | |
| 129 | -- [ ] Better error handling for failed key grabs | |
| 130 | - | |
| 131 | -** Layout System | |
| 132 | -- [ ] Add more layout types (monocle, floating) | |
| 166 | +- [X] Add keybind to swap focus between windows | |
| 167 | + - [X] Track focused window in WindowManager struct | |
| 168 | + - [X] Implement focus cycling logic | |
| 169 | + - [X] Add visual focus indication (borders/colors) | |
| 170 | + | |
| 171 | +** DONE Tag System [3/3] | |
| 172 | +- [X] Implement tag/workspace system (9 tags) | |
| 173 | +- [X] Keybinds to switch tags (Alt+1-9) | |
| 174 | +- [X] Keybinds to move windows to tags (Alt+Shift+1-9) | |
| 175 | + | |
| 176 | +** DONE Status Bar [2/2] | |
| 177 | +- [X] Create basic bar window at screen top | |
| 178 | +- [X] Display tag indicators with state (selected/occupied/empty) | |
| 179 | + | |
| 180 | +** IN PROGRESS Bar Enhancements [0/3] | |
| 181 | +- [ ] Add status text area (right side of bar) | |
| 182 | +- [ ] Implement clock widget | |
| 183 | +- [ ] Add system information widgets | |
| 184 | + | |
| 185 | +** TODO Key System Improvements [0/2] | |
| 186 | +- [ ] dmenu integration for application launching | |
| 187 | +- [ ] More spawn commands in config (screenshot, volume, etc.) | |
| 188 | + | |
| 189 | +** TODO Layout System [0/4] | |
| 190 | +- [ ] Add monocle layout | |
| 191 | +- [ ] Add floating layout mode | |
| 133 | 192 | - [ ] Handle window resize requests properly |
| 134 | 193 | - [ ] Add configurable gaps between windows |
| 135 | -- [ ] Implement layout switching keybinds | |
| 194 | + | |
| 195 | +** TODO Advanced Features [0/3] | |
| 196 | +- [ ] Multi-monitor support | |
| 197 | +- [ ] Per-window rules (floating, tag assignment) | |
| 198 | +- [ ] Master area resizing keybinds | |
| 136 | 199 | |
| 137 | 200 | ** Polish & Features |
| 138 | 201 | - [ ] Clean window destruction/cleanup |
| 139 | 202 | - [ ] Handle edge cases (empty window list, invalid windows) |
| 140 | -- [ ] Add status bar integration | |
| 141 | 203 | - [ ] Better error messages and logging |
| 142 | - | |
| 143 | -** Priority | |
| 144 | -Reorganization and UnmapNotify handling should be immediate priorities. | |
| 145 | - | |
| 146 | -* Status | |
| 147 | -- Rust + x11rb skeleton running | |
| 148 | -- Nix flake devShell available | |
| 149 | -- =just test= launches Xephyr, clients, and oxwm | |
| 204 | +- [ ] Proper font rendering in bar (currently using basic X11 text) | |
| 205 | + | |
| 206 | +* Architecture Notes | |
| 207 | +** Tag System | |
| 208 | +Tags are implemented as bitmasks (TagMask = u32), allowing windows to belong to | |
| 209 | +multiple tags simultaneously (though current UI only supports single tags). | |
| 210 | +Each window has an associated TagMask in window_tags HashMap. | |
| 211 | + | |
| 212 | +** Bar Design | |
| 213 | +The bar uses a performance-optimized approach: | |
| 214 | +- Only redraws when invalidate() is called | |
| 215 | +- Pre-calculates tag widths on creation | |
| 216 | +- Uses X11 graphics context for efficient drawing | |
| 217 | +- Click handling uses O(n) tag width lookup | |
| 218 | + | |
| 219 | +** Configuration Philosophy | |
| 220 | +Following dwm's approach: all configuration is in Rust source code. No runtime | |
| 221 | +config files. Edit config.rs and recompile. This ensures type safety and | |
| 222 | +compile-time validation of all settings. | |
| 150 | 223 | |
| 151 | 224 | * License |
| 152 | 225 | [[https://www.gnu.org/licenses/gpl-3.0.en.html][GPL]] |
| 153 | - | |
| 154 | - |
+185 −0 src/bar/bar.rs new file mode 100644
| @@ -0,0 +1,185 @@ | ||
| 1 | +use super::BAR_HEIGHT; | |
| 2 | +use crate::config::TAGS; | |
| 3 | +use anyhow::Result; | |
| 4 | +use x11rb::COPY_DEPTH_FROM_PARENT; | |
| 5 | +use x11rb::connection::Connection; | |
| 6 | +use x11rb::protocol::xproto::*; | |
| 7 | + | |
| 8 | +pub struct Bar { | |
| 9 | + window: Window, | |
| 10 | + width: u16, | |
| 11 | + height: u16, | |
| 12 | + graphics_context: Gcontext, | |
| 13 | + | |
| 14 | + tag_widths: Vec<u16>, | |
| 15 | + needs_redraw: bool, | |
| 16 | +} | |
| 17 | + | |
| 18 | +impl Bar { | |
| 19 | + pub fn new<C: Connection>(connection: &C, screen: &Screen) -> Result<Self> { | |
| 20 | + let window = connection.generate_id()?; | |
| 21 | + let graphics_context = connection.generate_id()?; | |
| 22 | + | |
| 23 | + let width = screen.width_in_pixels; | |
| 24 | + let height = BAR_HEIGHT; | |
| 25 | + | |
| 26 | + connection.create_window( | |
| 27 | + COPY_DEPTH_FROM_PARENT, | |
| 28 | + window, | |
| 29 | + screen.root, | |
| 30 | + 0, | |
| 31 | + 0, | |
| 32 | + width, | |
| 33 | + height, | |
| 34 | + 0, | |
| 35 | + WindowClass::INPUT_OUTPUT, | |
| 36 | + screen.root_visual, | |
| 37 | + &CreateWindowAux::new() | |
| 38 | + .background_pixel(screen.black_pixel) | |
| 39 | + .event_mask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS) | |
| 40 | + .override_redirect(1), | |
| 41 | + )?; | |
| 42 | + | |
| 43 | + connection.create_gc( | |
| 44 | + graphics_context, | |
| 45 | + window, | |
| 46 | + &CreateGCAux::new() | |
| 47 | + .foreground(screen.white_pixel) | |
| 48 | + .background(screen.black_pixel), | |
| 49 | + )?; | |
| 50 | + | |
| 51 | + connection.map_window(window)?; | |
| 52 | + connection.flush()?; | |
| 53 | + | |
| 54 | + // TODO: actual text width calculation when we add fonts | |
| 55 | + let tag_widths = TAGS.iter().map(|tag| (tag.len() as u16 * 8) + 10).collect(); | |
| 56 | + | |
| 57 | + Ok(Bar { | |
| 58 | + window, | |
| 59 | + width, | |
| 60 | + height, | |
| 61 | + graphics_context, | |
| 62 | + tag_widths, | |
| 63 | + needs_redraw: true, | |
| 64 | + }) | |
| 65 | + } | |
| 66 | + | |
| 67 | + pub fn window(&self) -> Window { | |
| 68 | + self.window | |
| 69 | + } | |
| 70 | + | |
| 71 | + pub fn height(&self) -> u16 { | |
| 72 | + self.height | |
| 73 | + } | |
| 74 | + | |
| 75 | + pub fn invalidate(&mut self) { | |
| 76 | + self.needs_redraw = true; | |
| 77 | + } | |
| 78 | + | |
| 79 | + pub fn draw<C: Connection>( | |
| 80 | + &mut self, | |
| 81 | + connection: &C, | |
| 82 | + current_tags: u32, | |
| 83 | + occupied_tags: u32, | |
| 84 | + ) -> Result<()> { | |
| 85 | + if !self.needs_redraw { | |
| 86 | + return Ok(()); | |
| 87 | + } | |
| 88 | + | |
| 89 | + connection.poly_fill_rectangle( | |
| 90 | + self.window, | |
| 91 | + self.graphics_context, | |
| 92 | + &[Rectangle { | |
| 93 | + x: 0, | |
| 94 | + y: 0, | |
| 95 | + width: self.width, | |
| 96 | + height: self.height, | |
| 97 | + }], | |
| 98 | + )?; | |
| 99 | + | |
| 100 | + let mut x_position: i16 = 0; | |
| 101 | + | |
| 102 | + for (tag_index, tag) in TAGS.iter().enumerate() { | |
| 103 | + let tag_mask = 1 << tag_index; | |
| 104 | + let is_selected = (current_tags & tag_mask) != 0; | |
| 105 | + let is_occupied = (occupied_tags & tag_mask) != 0; | |
| 106 | + | |
| 107 | + let tag_width = self.tag_widths[tag_index]; | |
| 108 | + | |
| 109 | + if is_selected { | |
| 110 | + connection.change_gc( | |
| 111 | + self.graphics_context, | |
| 112 | + &ChangeGCAux::new().foreground(0xFFFFFF), | |
| 113 | + )?; | |
| 114 | + connection.poly_fill_rectangle( | |
| 115 | + self.window, | |
| 116 | + self.graphics_context, | |
| 117 | + &[Rectangle { | |
| 118 | + x: x_position, | |
| 119 | + y: 0, | |
| 120 | + width: tag_width, | |
| 121 | + height: self.height, | |
| 122 | + }], | |
| 123 | + )?; | |
| 124 | + | |
| 125 | + connection.change_gc( | |
| 126 | + self.graphics_context, | |
| 127 | + &ChangeGCAux::new().foreground(0x000000), | |
| 128 | + )?; | |
| 129 | + } else if is_occupied { | |
| 130 | + connection.change_gc( | |
| 131 | + self.graphics_context, | |
| 132 | + &ChangeGCAux::new().foreground(0x888888), | |
| 133 | + )?; | |
| 134 | + connection.poly_fill_rectangle( | |
| 135 | + self.window, | |
| 136 | + self.graphics_context, | |
| 137 | + &[Rectangle { | |
| 138 | + x: x_position, | |
| 139 | + y: 0, | |
| 140 | + width: tag_width, | |
| 141 | + height: 2, | |
| 142 | + }], | |
| 143 | + )?; | |
| 144 | + | |
| 145 | + connection.change_gc( | |
| 146 | + self.graphics_context, | |
| 147 | + &ChangeGCAux::new().foreground(0xFFFFFF), | |
| 148 | + )?; | |
| 149 | + } else { | |
| 150 | + connection.change_gc( | |
| 151 | + self.graphics_context, | |
| 152 | + &ChangeGCAux::new().foreground(0x666666), | |
| 153 | + )?; | |
| 154 | + } | |
| 155 | + | |
| 156 | + // TODO: Replace with actual font rendering later | |
| 157 | + connection.image_text8( | |
| 158 | + self.window, | |
| 159 | + self.graphics_context, | |
| 160 | + x_position + 5, | |
| 161 | + self.height as i16 - 5, | |
| 162 | + tag.as_bytes(), | |
| 163 | + )?; | |
| 164 | + | |
| 165 | + x_position += tag_width as i16; | |
| 166 | + } | |
| 167 | + | |
| 168 | + connection.flush()?; | |
| 169 | + self.needs_redraw = false; | |
| 170 | + | |
| 171 | + Ok(()) | |
| 172 | + } | |
| 173 | + | |
| 174 | + pub fn handle_click(&self, click_x: i16) -> Option<usize> { | |
| 175 | + let mut current_x_position = 0; | |
| 176 | + | |
| 177 | + for (tag_index, &tag_width) in self.tag_widths.iter().enumerate() { | |
| 178 | + if click_x >= current_x_position && click_x < current_x_position + tag_width as i16 { | |
| 179 | + return Some(tag_index); | |
| 180 | + } | |
| 181 | + current_x_position += tag_width as i16; | |
| 182 | + } | |
| 183 | + None | |
| 184 | + } | |
| 185 | +} |
+14 −0 src/bar/mod.rs new file mode 100644
| @@ -0,0 +1,14 @@ | ||
| 1 | +mod bar; | |
| 2 | +// mod widgets; // TODO: implement later | |
| 3 | + | |
| 4 | +pub use bar::Bar; | |
| 5 | + | |
| 6 | +// TODO: this should live in config.rs | |
| 7 | +pub const BAR_HEIGHT: u16 = 25; | |
| 8 | + | |
| 9 | +// Bar position (for future use) | |
| 10 | +#[derive(Debug, Clone, Copy)] | |
| 11 | +pub enum BarPosition { | |
| 12 | + Top, | |
| 13 | + Bottom, | |
| 14 | +} |
+12 −1 src/config.rs
| @@ -15,10 +15,20 @@ pub const BORDER_UNFOCUSED: u32 = 0x888888; | ||
| 15 | 15 | pub const TERMINAL: &str = "alacritty"; |
| 16 | 16 | pub const MODKEY: KeyButMask = KeyButMask::MOD1; |
| 17 | 17 | |
| 18 | +// ======================================== | |
| 19 | +// Commands | |
| 20 | +// ======================================== | |
| 21 | +const SCREENSHOT_CMD: &[&str] = &[ | |
| 22 | + "sh", | |
| 23 | + "-c", | |
| 24 | + "maim ~/screenshots/screenshot_$(date +%Y%m%d_%H%M%S).png", | |
| 25 | +]; | |
| 26 | + | |
| 18 | 27 | // ======================================== |
| 19 | 28 | // TAGS |
| 20 | 29 | // ======================================== |
| 21 | 30 | pub const TAG_COUNT: usize = 9; |
| 31 | +pub const TAGS: [&str; TAG_COUNT] = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]; | |
| 22 | 32 | |
| 23 | 33 | // ======================================== |
| 24 | 34 | // KEYBINDINGS |
| @@ -26,7 +36,8 @@ pub const TAG_COUNT: usize = 9; | ||
| 26 | 36 | #[rustfmt::skip] |
| 27 | 37 | pub const KEYBINDINGS: &[Key] = &[ |
| 28 | 38 | Key::new(&[MODKEY], keycodes::RETURN, KeyAction::Spawn, Arg::Str(TERMINAL)), |
| 29 | - | |
| 39 | + | |
| 40 | + Key::new(&[MODKEY], keycodes::S, KeyAction::Spawn, Arg::Array(SCREENSHOT_CMD)), | |
| 30 | 41 | Key::new(&[MODKEY], keycodes::Q, KeyAction::KillClient, Arg::None), |
| 31 | 42 | Key::new(&[MODKEY, SHIFT], keycodes::Q, KeyAction::Quit, Arg::None), |
| 32 | 43 | Key::new(&[MODKEY], keycodes::J, KeyAction::FocusStack, Arg::Int(-1)), |
+3 −2 src/keyboard/handlers.rs
| @@ -16,9 +16,10 @@ pub enum KeyAction { | ||
| 16 | 16 | |
| 17 | 17 | #[derive(Debug)] |
| 18 | 18 | pub enum Arg { |
| 19 | - Str(&'static str), | |
| 20 | - Int(i32), | |
| 21 | 19 | None, |
| 20 | + Int(i32), | |
| 21 | + Str(&'static str), | |
| 22 | + Array(&'static [&'static str]), | |
| 22 | 23 | } |
| 23 | 24 | |
| 24 | 25 | pub struct Key { |
+1 −0 src/main.rs
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | use anyhow::Result; |
| 2 | +mod bar; | |
| 2 | 3 | mod config; |
| 3 | 4 | mod keyboard; |
| 4 | 5 | mod layout; |
+54 −12 src/window_manager.rs
| @@ -1,3 +1,4 @@ | ||
| 1 | +use crate::bar::Bar; | |
| 1 | 2 | use crate::config::{BORDER_FOCUSED, BORDER_UNFOCUSED, BORDER_WIDTH, TAG_COUNT}; |
| 2 | 3 | use crate::keyboard::{self, Arg, KeyAction}; |
| 3 | 4 | use crate::layout::Layout; |
| @@ -24,6 +25,7 @@ pub struct WindowManager { | ||
| 24 | 25 | layout: Box<dyn Layout>, |
| 25 | 26 | window_tags: std::collections::HashMap<Window, TagMask>, |
| 26 | 27 | selected_tags: TagMask, |
| 28 | + bar: Bar, | |
| 27 | 29 | } |
| 28 | 30 | |
| 29 | 31 | impl WindowManager { |
| @@ -44,6 +46,8 @@ impl WindowManager { | ||
| 44 | 46 | )? |
| 45 | 47 | .check()?; |
| 46 | 48 | |
| 49 | + let bar = Bar::new(&connection, &screen)?; | |
| 50 | + | |
| 47 | 51 | return Ok(Self { |
| 48 | 52 | connection, |
| 49 | 53 | screen_number, |
| @@ -54,6 +58,7 @@ impl WindowManager { | ||
| 54 | 58 | layout: Box::new(TilingLayout), |
| 55 | 59 | window_tags: std::collections::HashMap::new(), |
| 56 | 60 | selected_tags: tag_mask(0), |
| 61 | + bar, | |
| 57 | 62 | }); |
| 58 | 63 | } |
| 59 | 64 | |
| @@ -62,19 +67,40 @@ impl WindowManager { | ||
| 62 | 67 | |
| 63 | 68 | keyboard::setup_keybinds(&self.connection, self.root)?; |
| 64 | 69 | |
| 70 | + // Initial bar draw | |
| 71 | + self.update_bar()?; | |
| 72 | + | |
| 65 | 73 | loop { |
| 66 | 74 | let event = self.connection.wait_for_event()?; |
| 67 | 75 | self.handle_event(event)?; |
| 68 | 76 | } |
| 69 | 77 | } |
| 70 | 78 | |
| 79 | + fn update_bar(&mut self) -> Result<()> { | |
| 80 | + let mut occupied_tags: TagMask = 0; | |
| 81 | + for &tags in self.window_tags.values() { | |
| 82 | + occupied_tags |= tags; | |
| 83 | + } | |
| 84 | + | |
| 85 | + self.bar.invalidate(); | |
| 86 | + self.bar | |
| 87 | + .draw(&self.connection, self.selected_tags, occupied_tags)?; | |
| 88 | + Ok(()) | |
| 89 | + } | |
| 90 | + | |
| 71 | 91 | fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> { |
| 72 | 92 | match action { |
| 73 | - KeyAction::Spawn => { | |
| 74 | - if let Arg::Str(command) = arg { | |
| 93 | + KeyAction::Spawn => match arg { | |
| 94 | + Arg::Str(command) => { | |
| 75 | 95 | std::process::Command::new(command).spawn()?; |
| 76 | 96 | } |
| 77 | - } | |
| 97 | + Arg::Array(cmd) => { | |
| 98 | + if let Some((program, args)) = cmd.split_first() { | |
| 99 | + std::process::Command::new(program).args(args).spawn()?; | |
| 100 | + } | |
| 101 | + } | |
| 102 | + _ => {} | |
| 103 | + }, | |
| 78 | 104 | KeyAction::KillClient => { |
| 79 | 105 | if let Some(focused) = self.focused_window { |
| 80 | 106 | match self.connection.kill_client(focused) { |
| @@ -148,6 +174,7 @@ impl WindowManager { | ||
| 148 | 174 | self.selected_tags = tag_mask(tag_index); |
| 149 | 175 | self.update_window_visibility()?; |
| 150 | 176 | self.apply_layout()?; |
| 177 | + self.update_bar()?; // Update bar to show new tag | |
| 151 | 178 | |
| 152 | 179 | let visible = self.visible_windows(); |
| 153 | 180 | self.set_focus(visible.first().copied())?; |
| @@ -165,6 +192,7 @@ impl WindowManager { | ||
| 165 | 192 | self.window_tags.insert(focused, mask); |
| 166 | 193 | self.update_window_visibility()?; |
| 167 | 194 | self.apply_layout()?; |
| 195 | + self.update_bar()?; // Update bar to show occupied tags changed | |
| 168 | 196 | } |
| 169 | 197 | |
| 170 | 198 | Ok(()) |
| @@ -238,6 +266,7 @@ impl WindowManager { | ||
| 238 | 266 | self.windows.push(event.window); |
| 239 | 267 | self.window_tags.insert(event.window, self.selected_tags); |
| 240 | 268 | self.apply_layout()?; |
| 269 | + self.update_bar()?; | |
| 241 | 270 | self.set_focus(Some(event.window))?; |
| 242 | 271 | } |
| 243 | 272 | Event::UnmapNotify(event) => { |
| @@ -245,13 +274,6 @@ impl WindowManager { | ||
| 245 | 274 | self.remove_window(event.window)?; |
| 246 | 275 | } |
| 247 | 276 | } |
| 248 | - // Event::UnmapNotify(event) => { | |
| 249 | - // if self.windows.contains(&event.window) { | |
| 250 | - // if self.is_window_visible(event.window) { | |
| 251 | - // self.remove_window(event.window)?; | |
| 252 | - // } | |
| 253 | - // } | |
| 254 | - // } | |
| 255 | 277 | Event::DestroyNotify(event) => { |
| 256 | 278 | if self.windows.contains(&event.window) { |
| 257 | 279 | self.remove_window(event.window)?; |
| @@ -261,6 +283,20 @@ impl WindowManager { | ||
| 261 | 283 | let (action, arg) = keyboard::handle_key_press(event)?; |
| 262 | 284 | self.handle_key_action(action, arg)?; |
| 263 | 285 | } |
| 286 | + Event::ButtonPress(event) => { | |
| 287 | + // Check if click was on the bar | |
| 288 | + if event.event == self.bar.window() { | |
| 289 | + if let Some(tag_index) = self.bar.handle_click(event.event_x) { | |
| 290 | + self.view_tag(tag_index)?; | |
| 291 | + } | |
| 292 | + } | |
| 293 | + } | |
| 294 | + Event::Expose(event) => { | |
| 295 | + if event.window == self.bar.window() { | |
| 296 | + self.bar.invalidate(); | |
| 297 | + self.update_bar()?; | |
| 298 | + } | |
| 299 | + } | |
| 264 | 300 | _ => {} |
| 265 | 301 | } |
| 266 | 302 | Ok(()) |
| @@ -271,18 +307,23 @@ impl WindowManager { | ||
| 271 | 307 | let screen_height = self.screen.height_in_pixels as u32; |
| 272 | 308 | let border_width = BORDER_WIDTH; |
| 273 | 309 | |
| 310 | + let bar_height = self.bar.height() as u32; | |
| 311 | + let usable_height = screen_height.saturating_sub(bar_height); | |
| 312 | + | |
| 274 | 313 | let visible = self.visible_windows(); |
| 275 | - let geometries = self.layout.arrange(&visible, screen_width, screen_height); | |
| 314 | + let geometries = self.layout.arrange(&visible, screen_width, usable_height); | |
| 276 | 315 | |
| 277 | 316 | for (window, geometry) in visible.iter().zip(geometries.iter()) { |
| 278 | 317 | let adjusted_width = geometry.width.saturating_sub(2 * border_width); |
| 279 | 318 | let adjusted_height = geometry.height.saturating_sub(2 * border_width); |
| 280 | 319 | |
| 320 | + let adjusted_y = geometry.y_coordinate + bar_height as i32; | |
| 321 | + | |
| 281 | 322 | self.connection.configure_window( |
| 282 | 323 | *window, |
| 283 | 324 | &ConfigureWindowAux::new() |
| 284 | 325 | .x(geometry.x_coordinate) |
| 285 | - .y(geometry.y_coordinate) | |
| 326 | + .y(adjusted_y) | |
| 286 | 327 | .width(adjusted_width) |
| 287 | 328 | .height(adjusted_height), |
| 288 | 329 | )?; |
| @@ -307,6 +348,7 @@ impl WindowManager { | ||
| 307 | 348 | } |
| 308 | 349 | |
| 309 | 350 | self.apply_layout()?; |
| 351 | + self.update_bar()?; | |
| 310 | 352 | } |
| 311 | 353 | Ok(()) |
| 312 | 354 | } |