Add an important hotkeys overlay
Browse files at this commit011c91c98a76880744d35afa763c20db074d9c90Parent: edafa137 changed files +500 -22
- README.md +1 -0
- niri-config/src/lib.rs +1 -0
- resources/default-config.kdl +26 -22
- src/hotkey_overlay.rs +429 -0
- src/input.rs +29 -0
- src/main.rs +1 -0
- src/niri.rs +13 -0
README.mdmodified+1 -0
| @@ -144,6 +144,7 @@ The general system is: if a hotkey switches somewhere, then adding <kbd>Ctrl</kb | |||
| 144 | 144 | ||
| 145 | | Hotkey | Description | | 145 | | Hotkey | Description | |
| 146 | | ------ | ----------- | | 146 | | ------ | ----------- | |
| 147 | +| <kbd>Mod</kbd><kbd>Shift</kbd><kbd>/</kbd> | Show a list of important niri hotkeys | | ||
| 147 | | <kbd>Mod</kbd><kbd>T</kbd> | Spawn `alacritty` (terminal) | | 148 | | <kbd>Mod</kbd><kbd>T</kbd> | Spawn `alacritty` (terminal) | |
| 148 | | <kbd>Mod</kbd><kbd>D</kbd> | Spawn `fuzzel` (application launcher) | | 149 | | <kbd>Mod</kbd><kbd>D</kbd> | Spawn `fuzzel` (application launcher) | |
| 149 | | <kbd>Mod</kbd><kbd>Alt</kbd><kbd>L</kbd> | Spawn `swaylock` (screen locker) | | 150 | | <kbd>Mod</kbd><kbd>Alt</kbd><kbd>L</kbd> | Spawn `swaylock` (screen locker) | |
niri-config/src/lib.rsmodified+1 -0
| @@ -429,6 +429,7 @@ pub enum Action { | |||
| 429 | MaximizeColumn, | 429 | MaximizeColumn, |
| 430 | SetColumnWidth(#[knuffel(argument, str)] SizeChange), | 430 | SetColumnWidth(#[knuffel(argument, str)] SizeChange), |
| 431 | SwitchLayout(#[knuffel(argument)] LayoutAction), | 431 | SwitchLayout(#[knuffel(argument)] LayoutAction), |
| 432 | + ShowHotkeyOverlay, | ||
| 432 | } | 433 | } |
| 433 | 434 | ||
| 434 | #[derive(Debug, Clone, Copy, PartialEq)] | 435 | #[derive(Debug, Clone, Copy, PartialEq)] |
resources/default-config.kdlmodified+26 -22
| @@ -187,6 +187,10 @@ binds { | |||
| 187 | // "Mod" is a special modifier equal to Super when running on a TTY, and to Alt | 187 | // "Mod" is a special modifier equal to Super when running on a TTY, and to Alt |
| 188 | // when running as a winit window. | 188 | // when running as a winit window. |
| 189 | 189 | ||
| 190 | + // Mod-Shift-/, which is usually the same as Mod-?, | ||
| 191 | + // shows a list of important hotkeys. | ||
| 192 | + Mod+Shift+Slash { show-hotkey-overlay; } | ||
| 193 | + | ||
| 190 | // Suggested binds for running programs: terminal, app launcher, screen locker. | 194 | // Suggested binds for running programs: terminal, app launcher, screen locker. |
| 191 | Mod+T { spawn "alacritty"; } | 195 | Mod+T { spawn "alacritty"; } |
| 192 | Mod+D { spawn "fuzzel"; } | 196 | Mod+D { spawn "fuzzel"; } |
| @@ -201,23 +205,23 @@ binds { | |||
| 201 | 205 | ||
| 202 | Mod+Q { close-window; } | 206 | Mod+Q { close-window; } |
| 203 | 207 | ||
| 204 | - Mod+H { focus-column-left; } | ||
| 205 | - Mod+J { focus-window-down; } | ||
| 206 | - Mod+K { focus-window-up; } | ||
| 207 | - Mod+L { focus-column-right; } | ||
| 208 | Mod+Left { focus-column-left; } | 208 | Mod+Left { focus-column-left; } |
| 209 | Mod+Down { focus-window-down; } | 209 | Mod+Down { focus-window-down; } |
| 210 | Mod+Up { focus-window-up; } | 210 | Mod+Up { focus-window-up; } |
| 211 | Mod+Right { focus-column-right; } | 211 | Mod+Right { focus-column-right; } |
| 212 | + Mod+H { focus-column-left; } | ||
| 213 | + Mod+J { focus-window-down; } | ||
| 214 | + Mod+K { focus-window-up; } | ||
| 215 | + Mod+L { focus-column-right; } | ||
| 212 | 216 | ||
| 213 | - Mod+Ctrl+H { move-column-left; } | ||
| 214 | - Mod+Ctrl+J { move-window-down; } | ||
| 215 | - Mod+Ctrl+K { move-window-up; } | ||
| 216 | - Mod+Ctrl+L { move-column-right; } | ||
| 217 | Mod+Ctrl+Left { move-column-left; } | 217 | Mod+Ctrl+Left { move-column-left; } |
| 218 | Mod+Ctrl+Down { move-window-down; } | 218 | Mod+Ctrl+Down { move-window-down; } |
| 219 | Mod+Ctrl+Up { move-window-up; } | 219 | Mod+Ctrl+Up { move-window-up; } |
| 220 | Mod+Ctrl+Right { move-column-right; } | 220 | Mod+Ctrl+Right { move-column-right; } |
| 221 | + Mod+Ctrl+H { move-column-left; } | ||
| 222 | + Mod+Ctrl+J { move-window-down; } | ||
| 223 | + Mod+Ctrl+K { move-window-up; } | ||
| 224 | + Mod+Ctrl+L { move-column-right; } | ||
| 221 | 225 | ||
| 222 | // Alternative commands that move across workspaces when reaching | 226 | // Alternative commands that move across workspaces when reaching |
| 223 | // the first or last window in a column. | 227 | // the first or last window in a column. |
| @@ -231,45 +235,45 @@ binds { | |||
| 231 | Mod+Ctrl+Home { move-column-to-first; } | 235 | Mod+Ctrl+Home { move-column-to-first; } |
| 232 | Mod+Ctrl+End { move-column-to-last; } | 236 | Mod+Ctrl+End { move-column-to-last; } |
| 233 | 237 | ||
| 234 | - Mod+Shift+H { focus-monitor-left; } | ||
| 235 | - Mod+Shift+J { focus-monitor-down; } | ||
| 236 | - Mod+Shift+K { focus-monitor-up; } | ||
| 237 | - Mod+Shift+L { focus-monitor-right; } | ||
| 238 | Mod+Shift+Left { focus-monitor-left; } | 238 | Mod+Shift+Left { focus-monitor-left; } |
| 239 | Mod+Shift+Down { focus-monitor-down; } | 239 | Mod+Shift+Down { focus-monitor-down; } |
| 240 | Mod+Shift+Up { focus-monitor-up; } | 240 | Mod+Shift+Up { focus-monitor-up; } |
| 241 | Mod+Shift+Right { focus-monitor-right; } | 241 | Mod+Shift+Right { focus-monitor-right; } |
| 242 | + Mod+Shift+H { focus-monitor-left; } | ||
| 243 | + Mod+Shift+J { focus-monitor-down; } | ||
| 244 | + Mod+Shift+K { focus-monitor-up; } | ||
| 245 | + Mod+Shift+L { focus-monitor-right; } | ||
| 242 | 246 | ||
| 243 | - Mod+Shift+Ctrl+H { move-column-to-monitor-left; } | ||
| 244 | - Mod+Shift+Ctrl+J { move-column-to-monitor-down; } | ||
| 245 | - Mod+Shift+Ctrl+K { move-column-to-monitor-up; } | ||
| 246 | - Mod+Shift+Ctrl+L { move-column-to-monitor-right; } | ||
| 247 | Mod+Shift+Ctrl+Left { move-column-to-monitor-left; } | 247 | Mod+Shift+Ctrl+Left { move-column-to-monitor-left; } |
| 248 | Mod+Shift+Ctrl+Down { move-column-to-monitor-down; } | 248 | Mod+Shift+Ctrl+Down { move-column-to-monitor-down; } |
| 249 | Mod+Shift+Ctrl+Up { move-column-to-monitor-up; } | 249 | Mod+Shift+Ctrl+Up { move-column-to-monitor-up; } |
| 250 | Mod+Shift+Ctrl+Right { move-column-to-monitor-right; } | 250 | Mod+Shift+Ctrl+Right { move-column-to-monitor-right; } |
| 251 | + Mod+Shift+Ctrl+H { move-column-to-monitor-left; } | ||
| 252 | + Mod+Shift+Ctrl+J { move-column-to-monitor-down; } | ||
| 253 | + Mod+Shift+Ctrl+K { move-column-to-monitor-up; } | ||
| 254 | + Mod+Shift+Ctrl+L { move-column-to-monitor-right; } | ||
| 251 | 255 | ||
| 252 | // Alternatively, there are commands to move just a single window: | 256 | // Alternatively, there are commands to move just a single window: |
| 253 | // Mod+Shift+Ctrl+Left { move-window-to-monitor-left; } | 257 | // Mod+Shift+Ctrl+Left { move-window-to-monitor-left; } |
| 254 | // ... | 258 | // ... |
| 255 | 259 | ||
| 256 | - Mod+U { focus-workspace-down; } | ||
| 257 | - Mod+I { focus-workspace-up; } | ||
| 258 | Mod+Page_Down { focus-workspace-down; } | 260 | Mod+Page_Down { focus-workspace-down; } |
| 259 | Mod+Page_Up { focus-workspace-up; } | 261 | Mod+Page_Up { focus-workspace-up; } |
| 260 | - Mod+Ctrl+U { move-column-to-workspace-down; } | 262 | + Mod+U { focus-workspace-down; } |
| 261 | - Mod+Ctrl+I { move-column-to-workspace-up; } | 263 | + Mod+I { focus-workspace-up; } |
| 262 | Mod+Ctrl+Page_Down { move-column-to-workspace-down; } | 264 | Mod+Ctrl+Page_Down { move-column-to-workspace-down; } |
| 263 | Mod+Ctrl+Page_Up { move-column-to-workspace-up; } | 265 | Mod+Ctrl+Page_Up { move-column-to-workspace-up; } |
| 266 | + Mod+Ctrl+U { move-column-to-workspace-down; } | ||
| 267 | + Mod+Ctrl+I { move-column-to-workspace-up; } | ||
| 264 | 268 | ||
| 265 | // Alternatively, there are commands to move just a single window: | 269 | // Alternatively, there are commands to move just a single window: |
| 266 | // Mod+Ctrl+Page_Down { move-window-to-workspace-down; } | 270 | // Mod+Ctrl+Page_Down { move-window-to-workspace-down; } |
| 267 | // ... | 271 | // ... |
| 268 | 272 | ||
| 269 | - Mod+Shift+U { move-workspace-down; } | ||
| 270 | - Mod+Shift+I { move-workspace-up; } | ||
| 271 | Mod+Shift+Page_Down { move-workspace-down; } | 273 | Mod+Shift+Page_Down { move-workspace-down; } |
| 272 | Mod+Shift+Page_Up { move-workspace-up; } | 274 | Mod+Shift+Page_Up { move-workspace-up; } |
| 275 | + Mod+Shift+U { move-workspace-down; } | ||
| 276 | + Mod+Shift+I { move-workspace-up; } | ||
| 273 | 277 | ||
| 274 | Mod+1 { focus-workspace 1; } | 278 | Mod+1 { focus-workspace 1; } |
| 275 | Mod+2 { focus-workspace 2; } | 279 | Mod+2 { focus-workspace 2; } |
src/hotkey_overlay.rsadded+429 -0
| @@ -0,0 +1,429 @@ | |||
| 1 | +use std::cell::RefCell; | ||
| 2 | +use std::cmp::max; | ||
| 3 | +use std::collections::HashMap; | ||
| 4 | +use std::iter::zip; | ||
| 5 | +use std::rc::Rc; | ||
| 6 | + | ||
| 7 | +use niri_config::{Action, Config, Key, Modifiers}; | ||
| 8 | +use pangocairo::cairo::{self, ImageSurface}; | ||
| 9 | +use pangocairo::pango::{AttrColor, AttrInt, AttrList, AttrString, FontDescription, Weight}; | ||
| 10 | +use smithay::backend::renderer::element::memory::{ | ||
| 11 | + MemoryRenderBuffer, MemoryRenderBufferRenderElement, | ||
| 12 | +}; | ||
| 13 | +use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; | ||
| 14 | +use smithay::backend::renderer::element::Kind; | ||
| 15 | +use smithay::input::keyboard::xkb::keysym_get_name; | ||
| 16 | +use smithay::output::{Output, WeakOutput}; | ||
| 17 | +use smithay::reexports::gbm::Format as Fourcc; | ||
| 18 | +use smithay::utils::{Physical, Size, Transform}; | ||
| 19 | + | ||
| 20 | +use crate::input::CompositorMod; | ||
| 21 | +use crate::render_helpers::NiriRenderer; | ||
| 22 | + | ||
| 23 | +const PADDING: i32 = 8; | ||
| 24 | +const MARGIN: i32 = PADDING * 2; | ||
| 25 | +const FONT: &str = "sans 14px"; | ||
| 26 | +const BORDER: i32 = 4; | ||
| 27 | +const LINE_INTERVAL: i32 = 2; | ||
| 28 | +const TITLE: &str = "Important Hotkeys"; | ||
| 29 | + | ||
| 30 | +pub struct HotkeyOverlay { | ||
| 31 | + is_open: bool, | ||
| 32 | + config: Rc<RefCell<Config>>, | ||
| 33 | + comp_mod: CompositorMod, | ||
| 34 | + buffers: RefCell<HashMap<WeakOutput, RenderedOverlay>>, | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +pub struct RenderedOverlay { | ||
| 38 | + buffer: Option<MemoryRenderBuffer>, | ||
| 39 | + size: Size<i32, Physical>, | ||
| 40 | + scale: i32, | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +pub type HotkeyOverlayRenderElement<R> = RelocateRenderElement<MemoryRenderBufferRenderElement<R>>; | ||
| 44 | + | ||
| 45 | +impl HotkeyOverlay { | ||
| 46 | + pub fn new(config: Rc<RefCell<Config>>, comp_mod: CompositorMod) -> Self { | ||
| 47 | + Self { | ||
| 48 | + // Start the compositor with the overlay open. | ||
| 49 | + is_open: true, | ||
| 50 | + config, | ||
| 51 | + comp_mod, | ||
| 52 | + buffers: RefCell::new(HashMap::new()), | ||
| 53 | + } | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + pub fn show(&mut self) -> bool { | ||
| 57 | + if !self.is_open { | ||
| 58 | + self.is_open = true; | ||
| 59 | + true | ||
| 60 | + } else { | ||
| 61 | + false | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + pub fn hide(&mut self) -> bool { | ||
| 66 | + if self.is_open { | ||
| 67 | + self.is_open = false; | ||
| 68 | + true | ||
| 69 | + } else { | ||
| 70 | + false | ||
| 71 | + } | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + pub fn is_open(&self) -> bool { | ||
| 75 | + self.is_open | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + pub fn on_hotkey_config_updated(&mut self) { | ||
| 79 | + self.buffers.borrow_mut().clear(); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + pub fn render<R: NiriRenderer>( | ||
| 83 | + &self, | ||
| 84 | + renderer: &mut R, | ||
| 85 | + output: &Output, | ||
| 86 | + ) -> Option<HotkeyOverlayRenderElement<R>> { | ||
| 87 | + if !self.is_open { | ||
| 88 | + return None; | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + let scale = output.current_scale().integer_scale(); | ||
| 92 | + let margin = MARGIN * scale; | ||
| 93 | + | ||
| 94 | + let output_transform = output.current_transform(); | ||
| 95 | + let output_mode = output.current_mode().unwrap(); | ||
| 96 | + let output_size = output_transform.transform_size(output_mode.size); | ||
| 97 | + | ||
| 98 | + let mut buffers = self.buffers.borrow_mut(); | ||
| 99 | + buffers.retain(|output, _| output.upgrade().is_some()); | ||
| 100 | + | ||
| 101 | + // FIXME: should probably use the working area rather than view size. | ||
| 102 | + let weak = output.downgrade(); | ||
| 103 | + if let Some(rendered) = buffers.get(&weak) { | ||
| 104 | + if rendered.scale != scale { | ||
| 105 | + buffers.remove(&weak); | ||
| 106 | + } | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + let rendered = buffers.entry(weak).or_insert_with(|| { | ||
| 110 | + render(&self.config.borrow(), self.comp_mod, scale).unwrap_or_else(|_| { | ||
| 111 | + // This can go negative but whatever, as long as there's no rerender loop. | ||
| 112 | + let mut size = output_size; | ||
| 113 | + size.w -= margin * 2; | ||
| 114 | + size.h -= margin * 2; | ||
| 115 | + RenderedOverlay { | ||
| 116 | + buffer: None, | ||
| 117 | + size, | ||
| 118 | + scale, | ||
| 119 | + } | ||
| 120 | + }) | ||
| 121 | + }); | ||
| 122 | + let buffer = rendered.buffer.as_ref()?; | ||
| 123 | + | ||
| 124 | + let elem = MemoryRenderBufferRenderElement::from_buffer( | ||
| 125 | + renderer, | ||
| 126 | + (0., 0.), | ||
| 127 | + buffer, | ||
| 128 | + Some(0.9), | ||
| 129 | + None, | ||
| 130 | + None, | ||
| 131 | + Kind::Unspecified, | ||
| 132 | + ) | ||
| 133 | + .ok()?; | ||
| 134 | + | ||
| 135 | + let x = (output_size.w / 2 - rendered.size.w / 2).max(0); | ||
| 136 | + let y = (output_size.h / 2 - rendered.size.h / 2).max(0); | ||
| 137 | + let elem = RelocateRenderElement::from_element(elem, (x, y), Relocate::Absolute); | ||
| 138 | + | ||
| 139 | + Some(elem) | ||
| 140 | + } | ||
| 141 | +} | ||
| 142 | + | ||
| 143 | +fn render(config: &Config, comp_mod: CompositorMod, scale: i32) -> anyhow::Result<RenderedOverlay> { | ||
| 144 | + let _span = tracy_client::span!("hotkey_overlay::render"); | ||
| 145 | + | ||
| 146 | + // let margin = MARGIN * scale; | ||
| 147 | + let padding = PADDING * scale; | ||
| 148 | + let line_interval = LINE_INTERVAL * scale; | ||
| 149 | + | ||
| 150 | + // FIXME: if it doesn't fit, try splitting in two columns or something. | ||
| 151 | + // let mut target_size = output_size; | ||
| 152 | + // target_size.w -= margin * 2; | ||
| 153 | + // target_size.h -= margin * 2; | ||
| 154 | + // anyhow::ensure!(target_size.w > 0 && target_size.h > 0); | ||
| 155 | + | ||
| 156 | + let binds = &config.binds.0; | ||
| 157 | + | ||
| 158 | + // Collect actions that we want to show. | ||
| 159 | + let mut actions = vec![ | ||
| 160 | + &Action::ShowHotkeyOverlay, | ||
| 161 | + &Action::Quit, | ||
| 162 | + &Action::CloseWindow, | ||
| 163 | + ]; | ||
| 164 | + | ||
| 165 | + actions.extend(&[ | ||
| 166 | + &Action::FocusColumnLeft, | ||
| 167 | + &Action::FocusColumnRight, | ||
| 168 | + &Action::MoveColumnLeft, | ||
| 169 | + &Action::MoveColumnRight, | ||
| 170 | + &Action::FocusWorkspaceDown, | ||
| 171 | + &Action::FocusWorkspaceUp, | ||
| 172 | + ]); | ||
| 173 | + | ||
| 174 | + // Prefer move-column-to-workspace-down, but fall back to move-window-to-workspace-down. | ||
| 175 | + if binds | ||
| 176 | + .iter() | ||
| 177 | + .any(|bind| bind.actions.first() == Some(&Action::MoveColumnToWorkspaceDown)) | ||
| 178 | + { | ||
| 179 | + actions.push(&Action::MoveColumnToWorkspaceDown); | ||
| 180 | + } else if binds | ||
| 181 | + .iter() | ||
| 182 | + .any(|bind| bind.actions.first() == Some(&Action::MoveWindowToWorkspaceDown)) | ||
| 183 | + { | ||
| 184 | + actions.push(&Action::MoveWindowToWorkspaceDown); | ||
| 185 | + } else { | ||
| 186 | + actions.push(&Action::MoveColumnToWorkspaceDown); | ||
| 187 | + } | ||
| 188 | + | ||
| 189 | + // Same for -up. | ||
| 190 | + if binds | ||
| 191 | + .iter() | ||
| 192 | + .any(|bind| bind.actions.first() == Some(&Action::MoveColumnToWorkspaceUp)) | ||
| 193 | + { | ||
| 194 | + actions.push(&Action::MoveColumnToWorkspaceUp); | ||
| 195 | + } else if binds | ||
| 196 | + .iter() | ||
| 197 | + .any(|bind| bind.actions.first() == Some(&Action::MoveWindowToWorkspaceUp)) | ||
| 198 | + { | ||
| 199 | + actions.push(&Action::MoveWindowToWorkspaceUp); | ||
| 200 | + } else { | ||
| 201 | + actions.push(&Action::MoveColumnToWorkspaceUp); | ||
| 202 | + } | ||
| 203 | + | ||
| 204 | + actions.extend(&[ | ||
| 205 | + &Action::SwitchPresetColumnWidth, | ||
| 206 | + &Action::MaximizeColumn, | ||
| 207 | + &Action::ConsumeWindowIntoColumn, | ||
| 208 | + &Action::ExpelWindowFromColumn, | ||
| 209 | + ]); | ||
| 210 | + | ||
| 211 | + // Screenshot is not as important, can omit if not bound. | ||
| 212 | + if binds | ||
| 213 | + .iter() | ||
| 214 | + .any(|bind| bind.actions.first() == Some(&Action::Screenshot)) | ||
| 215 | + { | ||
| 216 | + actions.push(&Action::Screenshot); | ||
| 217 | + } | ||
| 218 | + | ||
| 219 | + // Add the spawn actions. | ||
| 220 | + for bind in binds | ||
| 221 | + .iter() | ||
| 222 | + .filter(|bind| matches!(bind.actions.first(), Some(Action::Spawn(_)))) | ||
| 223 | + { | ||
| 224 | + actions.push(bind.actions.first().unwrap()); | ||
| 225 | + } | ||
| 226 | + | ||
| 227 | + let strings = actions | ||
| 228 | + .into_iter() | ||
| 229 | + .map(|action| { | ||
| 230 | + let key = config | ||
| 231 | + .binds | ||
| 232 | + .0 | ||
| 233 | + .iter() | ||
| 234 | + .find(|bind| bind.actions.first() == Some(action)) | ||
| 235 | + .map(|bind| key_name(comp_mod, &bind.key)) | ||
| 236 | + .unwrap_or_else(|| String::from("(not bound)")); | ||
| 237 | + | ||
| 238 | + (format!(" {key} "), action_name(action)) | ||
| 239 | + }) | ||
| 240 | + .collect::<Vec<_>>(); | ||
| 241 | + | ||
| 242 | + let mut font = FontDescription::from_string(FONT); | ||
| 243 | + font.set_absolute_size((font.size() * scale).into()); | ||
| 244 | + | ||
| 245 | + let surface = ImageSurface::create(cairo::Format::ARgb32, 0, 0)?; | ||
| 246 | + let cr = cairo::Context::new(&surface)?; | ||
| 247 | + let layout = pangocairo::create_layout(&cr); | ||
| 248 | + layout.set_font_description(Some(&font)); | ||
| 249 | + | ||
| 250 | + let bold = AttrList::new(); | ||
| 251 | + bold.insert(AttrInt::new_weight(Weight::Bold)); | ||
| 252 | + layout.set_attributes(Some(&bold)); | ||
| 253 | + layout.set_text(TITLE); | ||
| 254 | + let title_size = layout.pixel_size(); | ||
| 255 | + | ||
| 256 | + let attrs = AttrList::new(); | ||
| 257 | + attrs.insert(AttrString::new_family("Monospace")); | ||
| 258 | + attrs.insert(AttrColor::new_background(12000, 12000, 12000)); | ||
| 259 | + | ||
| 260 | + layout.set_attributes(Some(&attrs)); | ||
| 261 | + let key_sizes = strings | ||
| 262 | + .iter() | ||
| 263 | + .map(|(key, _)| { | ||
| 264 | + layout.set_text(key); | ||
| 265 | + layout.pixel_size() | ||
| 266 | + }) | ||
| 267 | + .collect::<Vec<_>>(); | ||
| 268 | + | ||
| 269 | + layout.set_attributes(None); | ||
| 270 | + let action_sizes = strings | ||
| 271 | + .iter() | ||
| 272 | + .map(|(_, action)| { | ||
| 273 | + layout.set_markup(action); | ||
| 274 | + layout.pixel_size() | ||
| 275 | + }) | ||
| 276 | + .collect::<Vec<_>>(); | ||
| 277 | + | ||
| 278 | + let key_width = key_sizes.iter().map(|(w, _)| w).max().unwrap(); | ||
| 279 | + let action_width = action_sizes.iter().map(|(w, _)| w).max().unwrap(); | ||
| 280 | + let mut width = key_width + padding + action_width; | ||
| 281 | + | ||
| 282 | + let mut height = zip(&key_sizes, &action_sizes) | ||
| 283 | + .map(|((_, key_h), (_, act_h))| max(key_h, act_h)) | ||
| 284 | + .sum::<i32>() | ||
| 285 | + + (key_sizes.len() - 1) as i32 * line_interval | ||
| 286 | + + title_size.1 | ||
| 287 | + + padding; | ||
| 288 | + | ||
| 289 | + width += padding * 2; | ||
| 290 | + height += padding * 2; | ||
| 291 | + | ||
| 292 | + // FIXME: fix bug in Smithay that rounds pixel sizes down to scale. | ||
| 293 | + width = (width + scale - 1) / scale * scale; | ||
| 294 | + height = (height + scale - 1) / scale * scale; | ||
| 295 | + | ||
| 296 | + let surface = ImageSurface::create(cairo::Format::ARgb32, width, height)?; | ||
| 297 | + let cr = cairo::Context::new(&surface)?; | ||
| 298 | + cr.set_source_rgb(0.1, 0.1, 0.1); | ||
| 299 | + cr.paint()?; | ||
| 300 | + | ||
| 301 | + cr.move_to(padding.into(), padding.into()); | ||
| 302 | + let layout = pangocairo::create_layout(&cr); | ||
| 303 | + layout.set_font_description(Some(&font)); | ||
| 304 | + | ||
| 305 | + cr.set_source_rgb(1., 1., 1.); | ||
| 306 | + | ||
| 307 | + cr.move_to(((width - title_size.0) / 2).into(), padding.into()); | ||
| 308 | + layout.set_attributes(Some(&bold)); | ||
| 309 | + layout.set_text(TITLE); | ||
| 310 | + pangocairo::show_layout(&cr, &layout); | ||
| 311 | + | ||
| 312 | + cr.move_to(padding.into(), (padding + title_size.1 + padding).into()); | ||
| 313 | + | ||
| 314 | + for ((key, action), ((_, key_h), (_, act_h))) in zip(&strings, zip(&key_sizes, &action_sizes)) { | ||
| 315 | + layout.set_attributes(Some(&attrs)); | ||
| 316 | + layout.set_text(key); | ||
| 317 | + pangocairo::show_layout(&cr, &layout); | ||
| 318 | + | ||
| 319 | + cr.rel_move_to((key_width + padding).into(), 0.); | ||
| 320 | + | ||
| 321 | + layout.set_attributes(None); | ||
| 322 | + layout.set_markup(action); | ||
| 323 | + pangocairo::show_layout(&cr, &layout); | ||
| 324 | + | ||
| 325 | + cr.rel_move_to( | ||
| 326 | + (-(key_width + padding)).into(), | ||
| 327 | + (max(key_h, act_h) + line_interval).into(), | ||
| 328 | + ); | ||
| 329 | + } | ||
| 330 | + | ||
| 331 | + cr.move_to(0., 0.); | ||
| 332 | + cr.line_to(width.into(), 0.); | ||
| 333 | + cr.line_to(width.into(), height.into()); | ||
| 334 | + cr.line_to(0., height.into()); | ||
| 335 | + cr.line_to(0., 0.); | ||
| 336 | + cr.set_source_rgb(0.5, 0.8, 1.0); | ||
| 337 | + cr.set_line_width((BORDER * scale).into()); | ||
| 338 | + cr.stroke()?; | ||
| 339 | + drop(cr); | ||
| 340 | + | ||
| 341 | + let data = surface.take_data().unwrap(); | ||
| 342 | + let buffer = MemoryRenderBuffer::from_memory( | ||
| 343 | + &data, | ||
| 344 | + Fourcc::Argb8888, | ||
| 345 | + (width, height), | ||
| 346 | + scale, | ||
| 347 | + Transform::Normal, | ||
| 348 | + None, | ||
| 349 | + ); | ||
| 350 | + | ||
| 351 | + Ok(RenderedOverlay { | ||
| 352 | + buffer: Some(buffer), | ||
| 353 | + size: Size::from((width, height)), | ||
| 354 | + scale, | ||
| 355 | + }) | ||
| 356 | +} | ||
| 357 | + | ||
| 358 | +fn action_name(action: &Action) -> String { | ||
| 359 | + match action { | ||
| 360 | + Action::Quit => String::from("Exit niri"), | ||
| 361 | + Action::ShowHotkeyOverlay => String::from("Show Important Hotkeys"), | ||
| 362 | + Action::CloseWindow => String::from("Close Focused Window"), | ||
| 363 | + Action::FocusColumnLeft => String::from("Focus Column to the Left"), | ||
| 364 | + Action::FocusColumnRight => String::from("Focus Column to the Right"), | ||
| 365 | + Action::MoveColumnLeft => String::from("Move Column Left"), | ||
| 366 | + Action::MoveColumnRight => String::from("Move Column Right"), | ||
| 367 | + Action::FocusWorkspaceDown => String::from("Switch Workspace Down"), | ||
| 368 | + Action::FocusWorkspaceUp => String::from("Switch Workspace Up"), | ||
| 369 | + Action::MoveColumnToWorkspaceDown => String::from("Move Column to Workspace Down"), | ||
| 370 | + Action::MoveColumnToWorkspaceUp => String::from("Move Column to Workspace Up"), | ||
| 371 | + Action::MoveWindowToWorkspaceDown => String::from("Move Window to Workspace Down"), | ||
| 372 | + Action::MoveWindowToWorkspaceUp => String::from("Move Window to Workspace Up"), | ||
| 373 | + Action::SwitchPresetColumnWidth => String::from("Switch Preset Column Widths"), | ||
| 374 | + Action::MaximizeColumn => String::from("Maximize Column"), | ||
| 375 | + Action::ConsumeWindowIntoColumn => String::from("Consume Window Into Column"), | ||
| 376 | + Action::ExpelWindowFromColumn => String::from("Expel Window From Column"), | ||
| 377 | + Action::Screenshot => String::from("Take a Screenshot"), | ||
| 378 | + Action::Spawn(args) => format!( | ||
| 379 | + "Spawn <span face='monospace' bgcolor='#000000'>{}</span>", | ||
| 380 | + args.first().unwrap_or(&String::new()) | ||
| 381 | + ), | ||
| 382 | + _ => String::from("FIXME: Unknown"), | ||
| 383 | + } | ||
| 384 | +} | ||
| 385 | + | ||
| 386 | +fn key_name(comp_mod: CompositorMod, key: &Key) -> String { | ||
| 387 | + let mut name = String::new(); | ||
| 388 | + | ||
| 389 | + let has_comp_mod = key.modifiers.contains(Modifiers::COMPOSITOR); | ||
| 390 | + | ||
| 391 | + if key.modifiers.contains(Modifiers::SUPER) | ||
| 392 | + || (has_comp_mod && comp_mod == CompositorMod::Super) | ||
| 393 | + { | ||
| 394 | + name.push_str("Super + "); | ||
| 395 | + } | ||
| 396 | + if key.modifiers.contains(Modifiers::ALT) || (has_comp_mod && comp_mod == CompositorMod::Alt) { | ||
| 397 | + name.push_str("Alt + "); | ||
| 398 | + } | ||
| 399 | + if key.modifiers.contains(Modifiers::SHIFT) { | ||
| 400 | + name.push_str("Shift + "); | ||
| 401 | + } | ||
| 402 | + if key.modifiers.contains(Modifiers::CTRL) { | ||
| 403 | + name.push_str("Ctrl + "); | ||
| 404 | + } | ||
| 405 | + name.push_str(&prettify_keysym_name(&keysym_get_name(key.keysym))); | ||
| 406 | + | ||
| 407 | + name | ||
| 408 | +} | ||
| 409 | + | ||
| 410 | +fn prettify_keysym_name(name: &str) -> String { | ||
| 411 | + let name = match name { | ||
| 412 | + "slash" => "/", | ||
| 413 | + "comma" => ",", | ||
| 414 | + "period" => ".", | ||
| 415 | + "minus" => "-", | ||
| 416 | + "equal" => "=", | ||
| 417 | + "grave" => "`", | ||
| 418 | + "Next" => "Page Down", | ||
| 419 | + "Prior" => "Page Up", | ||
| 420 | + "Print" => "PrtSc", | ||
| 421 | + _ => name, | ||
| 422 | + }; | ||
| 423 | + | ||
| 424 | + if name.len() == 1 && name.is_ascii() { | ||
| 425 | + name.to_ascii_uppercase() | ||
| 426 | + } else { | ||
| 427 | + name.into() | ||
| 428 | + } | ||
| 429 | +} | ||
src/input.rsmodified+29 -0
| @@ -54,6 +54,9 @@ impl State { | |||
| 54 | self.niri.activate_monitors(&self.backend); | 54 | self.niri.activate_monitors(&self.backend); |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | + let hide_hotkey_overlay = | ||
| 58 | + self.niri.hotkey_overlay.is_open() && should_hide_hotkey_overlay(&event); | ||
| 59 | + | ||
| 57 | use InputEvent::*; | 60 | use InputEvent::*; |
| 58 | match event { | 61 | match event { |
| 59 | DeviceAdded { device } => self.on_device_added(device), | 62 | DeviceAdded { device } => self.on_device_added(device), |
| @@ -82,6 +85,12 @@ impl State { | |||
| 82 | TouchFrame { .. } => (), | 85 | TouchFrame { .. } => (), |
| 83 | Special(_) => (), | 86 | Special(_) => (), |
| 84 | } | 87 | } |
| 88 | + | ||
| 89 | + // Do this last so that screenshot still gets it. | ||
| 90 | + // FIXME: do this in a less cursed fashion somehow. | ||
| 91 | + if hide_hotkey_overlay && self.niri.hotkey_overlay.hide() { | ||
| 92 | + self.niri.queue_redraw_all(); | ||
| 93 | + } | ||
| 85 | } | 94 | } |
| 86 | 95 | ||
| 87 | pub fn process_libinput_event(&mut self, event: &mut InputEvent<LibinputInputBackend>) { | 96 | pub fn process_libinput_event(&mut self, event: &mut InputEvent<LibinputInputBackend>) { |
| @@ -557,6 +566,11 @@ impl State { | |||
| 557 | Action::SetWindowHeight(change) => { | 566 | Action::SetWindowHeight(change) => { |
| 558 | self.niri.layout.set_window_height(change); | 567 | self.niri.layout.set_window_height(change); |
| 559 | } | 568 | } |
| 569 | + Action::ShowHotkeyOverlay => { | ||
| 570 | + if self.niri.hotkey_overlay.show() { | ||
| 571 | + self.niri.queue_redraw_all(); | ||
| 572 | + } | ||
| 573 | + } | ||
| 560 | } | 574 | } |
| 561 | } | 575 | } |
| 562 | 576 | ||
| @@ -1372,6 +1386,21 @@ fn should_activate_monitors<I: InputBackend>(event: &InputEvent<I>) -> bool { | |||
| 1372 | } | 1386 | } |
| 1373 | } | 1387 | } |
| 1374 | 1388 | ||
| 1389 | +fn should_hide_hotkey_overlay<I: InputBackend>(event: &InputEvent<I>) -> bool { | ||
| 1390 | + match event { | ||
| 1391 | + InputEvent::Keyboard { event } if event.state() == KeyState::Pressed => true, | ||
| 1392 | + InputEvent::PointerButton { .. } | ||
| 1393 | + | InputEvent::PointerAxis { .. } | ||
| 1394 | + | InputEvent::GestureSwipeBegin { .. } | ||
| 1395 | + | InputEvent::GesturePinchBegin { .. } | ||
| 1396 | + | InputEvent::TouchDown { .. } | ||
| 1397 | + | InputEvent::TouchMotion { .. } | ||
| 1398 | + | InputEvent::TabletToolTip { .. } | ||
| 1399 | + | InputEvent::TabletToolButton { .. } => true, | ||
| 1400 | + _ => false, | ||
| 1401 | + } | ||
| 1402 | +} | ||
| 1403 | + | ||
| 1375 | fn allowed_when_locked(action: &Action) -> bool { | 1404 | fn allowed_when_locked(action: &Action) -> bool { |
| 1376 | matches!( | 1405 | matches!( |
| 1377 | action, | 1406 | action, |
src/main.rsmodified+1 -0
| @@ -9,6 +9,7 @@ mod cursor; | |||
| 9 | mod dbus; | 9 | mod dbus; |
| 10 | mod frame_clock; | 10 | mod frame_clock; |
| 11 | mod handlers; | 11 | mod handlers; |
| 12 | +mod hotkey_overlay; | ||
| 12 | mod input; | 13 | mod input; |
| 13 | mod ipc; | 14 | mod ipc; |
| 14 | mod layout; | 15 | mod layout; |
src/niri.rsmodified+13 -0
| @@ -100,6 +100,7 @@ use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri}; | |||
| 100 | use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri}; | 100 | use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri}; |
| 101 | use crate::frame_clock::FrameClock; | 101 | use crate::frame_clock::FrameClock; |
| 102 | use crate::handlers::configure_lock_surface; | 102 | use crate::handlers::configure_lock_surface; |
| 103 | +use crate::hotkey_overlay::HotkeyOverlay; | ||
| 103 | use crate::input::{apply_libinput_settings, TabletData}; | 104 | use crate::input::{apply_libinput_settings, TabletData}; |
| 104 | use crate::ipc::server::IpcServer; | 105 | use crate::ipc::server::IpcServer; |
| 105 | use crate::layout::{Layout, MonitorRenderElement}; | 106 | use crate::layout::{Layout, MonitorRenderElement}; |
| @@ -189,6 +190,7 @@ pub struct Niri { | |||
| 189 | 190 | ||
| 190 | pub screenshot_ui: ScreenshotUi, | 191 | pub screenshot_ui: ScreenshotUi, |
| 191 | pub config_error_notification: ConfigErrorNotification, | 192 | pub config_error_notification: ConfigErrorNotification, |
| 193 | + pub hotkey_overlay: HotkeyOverlay, | ||
| 192 | 194 | ||
| 193 | #[cfg(feature = "dbus")] | 195 | #[cfg(feature = "dbus")] |
| 194 | pub dbus: Option<crate::dbus::DBusServers>, | 196 | pub dbus: Option<crate::dbus::DBusServers>, |
| @@ -594,6 +596,10 @@ impl State { | |||
| 594 | output_config_changed = true; | 596 | output_config_changed = true; |
| 595 | } | 597 | } |
| 596 | 598 | ||
| 599 | + if config.binds != old_config.binds { | ||
| 600 | + self.niri.hotkey_overlay.on_hotkey_config_updated(); | ||
| 601 | + } | ||
| 602 | + | ||
| 597 | *old_config = config; | 603 | *old_config = config; |
| 598 | 604 | ||
| 599 | // Release the borrow. | 605 | // Release the borrow. |
| @@ -859,6 +865,7 @@ impl Niri { | |||
| 859 | 865 | ||
| 860 | let screenshot_ui = ScreenshotUi::new(); | 866 | let screenshot_ui = ScreenshotUi::new(); |
| 861 | let config_error_notification = ConfigErrorNotification::new(); | 867 | let config_error_notification = ConfigErrorNotification::new(); |
| 868 | + let hotkey_overlay = HotkeyOverlay::new(config.clone(), backend.mod_key()); | ||
| 862 | 869 | ||
| 863 | let socket_source = ListeningSocketSource::new_auto().unwrap(); | 870 | let socket_source = ListeningSocketSource::new_auto().unwrap(); |
| 864 | let socket_name = socket_source.socket_name().to_os_string(); | 871 | let socket_name = socket_source.socket_name().to_os_string(); |
| @@ -964,6 +971,7 @@ impl Niri { | |||
| 964 | 971 | ||
| 965 | screenshot_ui, | 972 | screenshot_ui, |
| 966 | config_error_notification, | 973 | config_error_notification, |
| 974 | + hotkey_overlay, | ||
| 967 | 975 | ||
| 968 | #[cfg(feature = "dbus")] | 976 | #[cfg(feature = "dbus")] |
| 969 | dbus: None, | 977 | dbus: None, |
| @@ -1858,6 +1866,11 @@ impl Niri { | |||
| 1858 | return elements; | 1866 | return elements; |
| 1859 | } | 1867 | } |
| 1860 | 1868 | ||
| 1869 | + // Draw the hotkey overlay on top. | ||
| 1870 | + if let Some(element) = self.hotkey_overlay.render(renderer, output) { | ||
| 1871 | + elements.push(element.into()); | ||
| 1872 | + } | ||
| 1873 | + | ||
| 1861 | // Get monitor elements. | 1874 | // Get monitor elements. |
| 1862 | let mon = self.layout.monitor_for_output(output).unwrap(); | 1875 | let mon = self.layout.monitor_for_output(output).unwrap(); |
| 1863 | let monitor_elements = mon.render_elements(renderer); | 1876 | let monitor_elements = mon.render_elements(renderer); |