forge
Explore
Log in

Don't you dare go Hollow

Implement custom hotkey overlay titles

Browse files at this commit

a605e7f6227448bbaa18932a251c9cbab9a07686Ivan MolodetskikhParent: 513488f
niri-config/src/lib.rsmodified+20 -2
@@ -1334,6 +1334,7 @@ pub struct Bind {
1334 pub cooldown: Option<Duration>,1334 pub cooldown: Option<Duration>,
1335 pub allow_when_locked: bool,1335 pub allow_when_locked: bool,
1336 pub allow_inhibiting: bool,1336 pub allow_inhibiting: bool,
1337+ pub hotkey_overlay_title: Option<Option<String>>,
1337 }1338 }
1338 1339
1339 #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]1340 #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
@@ -3232,6 +3233,7 @@ where
3232 let mut allow_when_locked = false;3233 let mut allow_when_locked = false;
3233 let mut allow_when_locked_node = None;3234 let mut allow_when_locked_node = None;
3234 let mut allow_inhibiting = true;3235 let mut allow_inhibiting = true;
3236+ let mut hotkey_overlay_title = None;
3235 for (name, val) in &node.properties {3237 for (name, val) in &node.properties {
3236 match &***name {3238 match &***name {
3237 "repeat" => {3239 "repeat" => {
@@ -3249,6 +3251,9 @@ where
3249 "allow-inhibiting" => {3251 "allow-inhibiting" => {
3250 allow_inhibiting = knuffel::traits::DecodeScalar::decode(val, ctx)?;3252 allow_inhibiting = knuffel::traits::DecodeScalar::decode(val, ctx)?;
3251 }3253 }
3254+ "hotkey-overlay-title" => {
3255+ hotkey_overlay_title = Some(knuffel::traits::DecodeScalar::decode(val, ctx)?);
3256+ }
3252 name_str => {3257 name_str => {
3253 ctx.emit_error(DecodeError::unexpected(3258 ctx.emit_error(DecodeError::unexpected(
3254 name,3259 name,
@@ -3271,6 +3276,7 @@ where
3271 cooldown: None,3276 cooldown: None,
3272 allow_when_locked: false,3277 allow_when_locked: false,
3273 allow_inhibiting: true,3278 allow_inhibiting: true,
3279+ hotkey_overlay_title: None,
3274 };3280 };
3275 3281
3276 if let Some(child) = children.next() {3282 if let Some(child) = children.next() {
@@ -3306,6 +3312,7 @@ where
3306 cooldown,3312 cooldown,
3307 allow_when_locked,3313 allow_when_locked,
3308 allow_inhibiting,3314 allow_inhibiting,
3315+ hotkey_overlay_title,
3309 })3316 })
3310 }3317 }
3311 Err(e) => {3318 Err(e) => {
@@ -3707,10 +3714,10 @@ mod tests {
3707 }3714 }
3708 3715
3709 binds {3716 binds {
3710- Mod+Escape { toggle-keyboard-shortcuts-inhibit; }3717+ Mod+Escape hotkey-overlay-title="Inhibit" { toggle-keyboard-shortcuts-inhibit; }
3711 Mod+Shift+Escape allow-inhibiting=true { toggle-keyboard-shortcuts-inhibit; }3718 Mod+Shift+Escape allow-inhibiting=true { toggle-keyboard-shortcuts-inhibit; }
3712 Mod+T allow-when-locked=true { spawn "alacritty"; }3719 Mod+T allow-when-locked=true { spawn "alacritty"; }
3713- Mod+Q { close-window; }3720+ Mod+Q hotkey-overlay-title=null { close-window; }
3714 Mod+Shift+H { focus-monitor-left; }3721 Mod+Shift+H { focus-monitor-left; }
3715 Mod+Ctrl+Shift+L { move-window-to-monitor-right; }3722 Mod+Ctrl+Shift+L { move-window-to-monitor-right; }
3716 Mod+Comma { consume-window-into-column; }3723 Mod+Comma { consume-window-into-column; }
@@ -4055,6 +4062,7 @@ mod tests {
4055 cooldown: None,4062 cooldown: None,
4056 allow_when_locked: false,4063 allow_when_locked: false,
4057 allow_inhibiting: false,4064 allow_inhibiting: false,
4065+ hotkey_overlay_title: Some(Some("Inhibit".to_owned())),
4058 },4066 },
4059 Bind {4067 Bind {
4060 key: Key {4068 key: Key {
@@ -4066,6 +4074,7 @@ mod tests {
4066 cooldown: None,4074 cooldown: None,
4067 allow_when_locked: false,4075 allow_when_locked: false,
4068 allow_inhibiting: false,4076 allow_inhibiting: false,
4077+ hotkey_overlay_title: None,
4069 },4078 },
4070 Bind {4079 Bind {
4071 key: Key {4080 key: Key {
@@ -4077,6 +4086,7 @@ mod tests {
4077 cooldown: None,4086 cooldown: None,
4078 allow_when_locked: true,4087 allow_when_locked: true,
4079 allow_inhibiting: true,4088 allow_inhibiting: true,
4089+ hotkey_overlay_title: None,
4080 },4090 },
4081 Bind {4091 Bind {
4082 key: Key {4092 key: Key {
@@ -4088,6 +4098,7 @@ mod tests {
4088 cooldown: None,4098 cooldown: None,
4089 allow_when_locked: false,4099 allow_when_locked: false,
4090 allow_inhibiting: true,4100 allow_inhibiting: true,
4101+ hotkey_overlay_title: Some(None),
4091 },4102 },
4092 Bind {4103 Bind {
4093 key: Key {4104 key: Key {
@@ -4099,6 +4110,7 @@ mod tests {
4099 cooldown: None,4110 cooldown: None,
4100 allow_when_locked: false,4111 allow_when_locked: false,
4101 allow_inhibiting: true,4112 allow_inhibiting: true,
4113+ hotkey_overlay_title: None,
4102 },4114 },
4103 Bind {4115 Bind {
4104 key: Key {4116 key: Key {
@@ -4110,6 +4122,7 @@ mod tests {
4110 cooldown: None,4122 cooldown: None,
4111 allow_when_locked: false,4123 allow_when_locked: false,
4112 allow_inhibiting: true,4124 allow_inhibiting: true,
4125+ hotkey_overlay_title: None,
4113 },4126 },
4114 Bind {4127 Bind {
4115 key: Key {4128 key: Key {
@@ -4121,6 +4134,7 @@ mod tests {
4121 cooldown: None,4134 cooldown: None,
4122 allow_when_locked: false,4135 allow_when_locked: false,
4123 allow_inhibiting: true,4136 allow_inhibiting: true,
4137+ hotkey_overlay_title: None,
4124 },4138 },
4125 Bind {4139 Bind {
4126 key: Key {4140 key: Key {
@@ -4132,6 +4146,7 @@ mod tests {
4132 cooldown: None,4146 cooldown: None,
4133 allow_when_locked: false,4147 allow_when_locked: false,
4134 allow_inhibiting: true,4148 allow_inhibiting: true,
4149+ hotkey_overlay_title: None,
4135 },4150 },
4136 Bind {4151 Bind {
4137 key: Key {4152 key: Key {
@@ -4145,6 +4160,7 @@ mod tests {
4145 cooldown: None,4160 cooldown: None,
4146 allow_when_locked: false,4161 allow_when_locked: false,
4147 allow_inhibiting: true,4162 allow_inhibiting: true,
4163+ hotkey_overlay_title: None,
4148 },4164 },
4149 Bind {4165 Bind {
4150 key: Key {4166 key: Key {
@@ -4156,6 +4172,7 @@ mod tests {
4156 cooldown: None,4172 cooldown: None,
4157 allow_when_locked: false,4173 allow_when_locked: false,
4158 allow_inhibiting: false,4174 allow_inhibiting: false,
4175+ hotkey_overlay_title: None,
4159 },4176 },
4160 Bind {4177 Bind {
4161 key: Key {4178 key: Key {
@@ -4167,6 +4184,7 @@ mod tests {
4167 cooldown: Some(Duration::from_millis(150)),4184 cooldown: Some(Duration::from_millis(150)),
4168 allow_when_locked: false,4185 allow_when_locked: false,
4169 allow_inhibiting: true,4186 allow_inhibiting: true,
4187+ hotkey_overlay_title: None,
4170 },4188 },
4171 ]),4189 ]),
4172 switch_events: SwitchBinds {4190 switch_events: SwitchBinds {
src/input/mod.rsmodified+8 -0
@@ -3009,6 +3009,7 @@ fn should_intercept_key(
3009 // But logically, nothing can inhibit its actions. Only opening it can be3009 // But logically, nothing can inhibit its actions. Only opening it can be
3010 // inhibited.3010 // inhibited.
3011 allow_inhibiting: false,3011 allow_inhibiting: false,
3012+ hotkey_overlay_title: None,
3012 });3013 });
3013 }3014 }
3014 }3015 }
@@ -3074,6 +3075,7 @@ fn find_bind(
3074 // It also makes no sense to inhibit the default power key handling.3075 // It also makes no sense to inhibit the default power key handling.
3075 // Hardcoded binds must never be inhibited.3076 // Hardcoded binds must never be inhibited.
3076 allow_inhibiting: false,3077 allow_inhibiting: false,
3078+ hotkey_overlay_title: None,
3077 });3079 });
3078 }3080 }
3079 3081
@@ -3541,6 +3543,7 @@ mod tests {
3541 cooldown: None,3543 cooldown: None,
3542 allow_when_locked: false,3544 allow_when_locked: false,
3543 allow_inhibiting: true,3545 allow_inhibiting: true,
3546+ hotkey_overlay_title: None,
3544 }]);3547 }]);
3545 3548
3546 let comp_mod = CompositorMod::Super;3549 let comp_mod = CompositorMod::Super;
@@ -3726,6 +3729,7 @@ mod tests {
3726 cooldown: None,3729 cooldown: None,
3727 allow_when_locked: false,3730 allow_when_locked: false,
3728 allow_inhibiting: true,3731 allow_inhibiting: true,
3732+ hotkey_overlay_title: None,
3729 },3733 },
3730 Bind {3734 Bind {
3731 key: Key {3735 key: Key {
@@ -3737,6 +3741,7 @@ mod tests {
3737 cooldown: None,3741 cooldown: None,
3738 allow_when_locked: false,3742 allow_when_locked: false,
3739 allow_inhibiting: true,3743 allow_inhibiting: true,
3744+ hotkey_overlay_title: None,
3740 },3745 },
3741 Bind {3746 Bind {
3742 key: Key {3747 key: Key {
@@ -3748,6 +3753,7 @@ mod tests {
3748 cooldown: None,3753 cooldown: None,
3749 allow_when_locked: false,3754 allow_when_locked: false,
3750 allow_inhibiting: true,3755 allow_inhibiting: true,
3756+ hotkey_overlay_title: None,
3751 },3757 },
3752 Bind {3758 Bind {
3753 key: Key {3759 key: Key {
@@ -3759,6 +3765,7 @@ mod tests {
3759 cooldown: None,3765 cooldown: None,
3760 allow_when_locked: false,3766 allow_when_locked: false,
3761 allow_inhibiting: true,3767 allow_inhibiting: true,
3768+ hotkey_overlay_title: None,
3762 },3769 },
3763 Bind {3770 Bind {
3764 key: Key {3771 key: Key {
@@ -3770,6 +3777,7 @@ mod tests {
3770 cooldown: None,3777 cooldown: None,
3771 allow_when_locked: false,3778 allow_when_locked: false,
3772 allow_inhibiting: true,3779 allow_inhibiting: true,
3780+ hotkey_overlay_title: None,
3773 },3781 },
3774 ]);3782 ]);
3775 3783
src/ui/hotkey_overlay.rsmodified+175 -18
@@ -4,7 +4,7 @@ use std::collections::HashMap;
4 use std::iter::zip;4 use std::iter::zip;
5 use std::rc::Rc;5 use std::rc::Rc;
6 6
7-use niri_config::{Action, Config, Key, Modifiers, Trigger};7+use niri_config::{Action, Bind, Config, Key, Modifiers, Trigger};
8 use pangocairo::cairo::{self, ImageSurface};8 use pangocairo::cairo::{self, ImageSurface};
9 use pangocairo::pango::{AttrColor, AttrInt, AttrList, AttrString, FontDescription, Weight};9 use pangocairo::pango::{AttrColor, AttrInt, AttrList, AttrString, FontDescription, Weight};
10 use smithay::backend::renderer::element::Kind;10 use smithay::backend::renderer::element::Kind;
@@ -125,6 +125,52 @@ impl HotkeyOverlay {
125 }125 }
126 }126 }
127 127
128+fn format_bind(
129+ binds: &[Bind],
130+ comp_mod: CompositorMod,
131+ action: &Action,
132+) -> Option<(String, String)> {
133+ let mut bind_with_non_null = None;
134+ let mut bind_with_custom_title = None;
135+ let mut found_null_title = false;
136+
137+ for bind in binds {
138+ if bind.action != *action {
139+ continue;
140+ }
141+
142+ match &bind.hotkey_overlay_title {
143+ Some(Some(_)) => {
144+ bind_with_custom_title.get_or_insert(bind);
145+ }
146+ Some(None) => {
147+ found_null_title = true;
148+ }
149+ None => {
150+ bind_with_non_null.get_or_insert(bind);
151+ }
152+ }
153+ }
154+
155+ if bind_with_custom_title.is_none() && found_null_title {
156+ return None;
157+ }
158+
159+ let mut title = None;
160+ let key = if let Some(bind) = bind_with_custom_title.or(bind_with_non_null) {
161+ if let Some(Some(custom)) = &bind.hotkey_overlay_title {
162+ title = Some(custom.clone());
163+ }
164+
165+ key_name(comp_mod, &bind.key)
166+ } else {
167+ String::from("(not bound)")
168+ };
169+ let title = title.unwrap_or_else(|| action_name(action));
170+
171+ Some((format!(" {key} "), title))
172+}
173+
128 fn render(174 fn render(
129 renderer: &mut GlesRenderer,175 renderer: &mut GlesRenderer,
130 config: &Config,176 config: &Config,
@@ -212,8 +258,17 @@ fn render(
212 actions.push(&Action::Screenshot);258 actions.push(&Action::Screenshot);
213 }259 }
214 260
261+ // Add actions with a custom hotkey-overlay-title.
262+ for bind in binds {
263+ if matches!(bind.hotkey_overlay_title, Some(Some(_))) {
264+ // Avoid duplicate actions.
265+ if !actions.contains(&&bind.action) {
266+ actions.push(&bind.action);
267+ }
268+ }
269+ }
270+
215 // Add the spawn actions.271 // Add the spawn actions.
216- let mut spawn_actions = Vec::new();
217 for bind in binds.iter().filter(|bind| {272 for bind in binds.iter().filter(|bind| {
218 matches!(bind.action, Action::Spawn(_))273 matches!(bind.action, Action::Spawn(_))
219 // Only show binds with Mod or Super to filter out stuff like volume up/down.274 // Only show binds with Mod or Super to filter out stuff like volume up/down.
@@ -225,25 +280,14 @@ fn render(
225 let action = &bind.action;280 let action = &bind.action;
226 281
227 // We only show one bind for each action, so we need to deduplicate the Spawn actions.282 // We only show one bind for each action, so we need to deduplicate the Spawn actions.
228- if !spawn_actions.contains(&action) {283+ if !actions.contains(&action) {
229- spawn_actions.push(action);284+ actions.push(action);
230 }285 }
231 }286 }
232- actions.extend(spawn_actions);
233 287
234 let strings = actions288 let strings = actions
235 .into_iter()289 .into_iter()
236- .map(|action| {290+ .filter_map(|action| format_bind(binds, comp_mod, action))
237- let key = config
238- .binds
239- .0
240- .iter()
241- .find(|bind| bind.action == *action)
242- .map(|bind| key_name(comp_mod, &bind.key))
243- .unwrap_or_else(|| String::from("(not bound)"));
244-
245- (format!(" {key} "), action_name(action))
246- })
247 .collect::<Vec<_>>();291 .collect::<Vec<_>>();
248 292
249 let mut font = FontDescription::from_string(FONT);293 let mut font = FontDescription::from_string(FONT);
@@ -323,8 +367,16 @@ fn render(
323 367
324 cr.rel_move_to((key_width + padding).into(), 0.);368 cr.rel_move_to((key_width + padding).into(), 0.);
325 369
326- layout.set_attributes(None);370+ let (attrs, text) = match pango::parse_markup(action, '\0') {
327- layout.set_markup(action);371+ Ok((attrs, text, _accel)) => (Some(attrs), text),
372+ Err(err) => {
373+ warn!("error parsing markup for key {key}: {err}");
374+ (None, action.into())
375+ }
376+ };
377+
378+ layout.set_attributes(attrs.as_ref());
379+ layout.set_text(&text);
328 pangocairo::functions::show_layout(&cr, &layout);380 pangocairo::functions::show_layout(&cr, &layout);
329 381
330 cr.rel_move_to(382 cr.rel_move_to(
@@ -473,3 +525,108 @@ fn prettify_keysym_name(name: &str) -> String {
473 name.into()525 name.into()
474 }526 }
475 }527 }
528+
529+#[cfg(test)]
530+mod tests {
531+ use insta::assert_snapshot;
532+
533+ use super::*;
534+
535+ #[track_caller]
536+ fn check(config: &str, action: Action) -> String {
537+ let config = Config::parse("test.kdl", config).unwrap();
538+ if let Some((key, title)) = format_bind(&config.binds.0, CompositorMod::Super, &action) {
539+ format!("{key}: {title}")
540+ } else {
541+ String::from("None")
542+ }
543+ }
544+
545+ #[test]
546+ fn test_format_bind() {
547+ // Not bound.
548+ assert_snapshot!(check("", Action::Screenshot), @" (not bound) : Take a Screenshot");
549+
550+ // Bound with a default title.
551+ assert_snapshot!(
552+ check(
553+ r#"binds {
554+ Mod+P { screenshot; }
555+ }"#,
556+ Action::Screenshot,
557+ ),
558+ @" Super + P : Take a Screenshot"
559+ );
560+
561+ // Custom title.
562+ assert_snapshot!(
563+ check(
564+ r#"binds {
565+ Mod+P hotkey-overlay-title="Hello" { screenshot; }
566+ }"#,
567+ Action::Screenshot,
568+ ),
569+ @" Super + P : Hello"
570+ );
571+
572+ // Prefer first bind.
573+ assert_snapshot!(
574+ check(
575+ r#"binds {
576+ Mod+P { screenshot; }
577+ Print { screenshot; }
578+ }"#,
579+ Action::Screenshot,
580+ ),
581+ @" Super + P : Take a Screenshot"
582+ );
583+
584+ // Prefer bind with custom title.
585+ assert_snapshot!(
586+ check(
587+ r#"binds {
588+ Mod+P { screenshot; }
589+ Print hotkey-overlay-title="My Cool Bind" { screenshot; }
590+ }"#,
591+ Action::Screenshot,
592+ ),
593+ @" PrtSc : My Cool Bind"
594+ );
595+
596+ // Prefer first bind with custom title.
597+ assert_snapshot!(
598+ check(
599+ r#"binds {
600+ Mod+P hotkey-overlay-title="First" { screenshot; }
601+ Print hotkey-overlay-title="My Cool Bind" { screenshot; }
602+ }"#,
603+ Action::Screenshot,
604+ ),
605+ @" Super + P : First"
606+ );
607+
608+ // Any bind with null title hides it.
609+ assert_snapshot!(
610+ check(
611+ r#"binds {
612+ Mod+P { screenshot; }
613+ Print hotkey-overlay-title=null { screenshot; }
614+ }"#,
615+ Action::Screenshot,
616+ ),
617+ @"None"
618+ );
619+
620+ // Custom title takes preference over null.
621+ assert_snapshot!(
622+ check(
623+ r#"binds {
624+ Mod+P hotkey-overlay-title="Hello" { screenshot; }
625+ Print hotkey-overlay-title=null { screenshot; }
626+ }"#,
627+ Action::Screenshot,
628+ ),
629+ @" Super + P : Hello"
630+ );
631+ }
632+}
wiki/Configuration:-Key-Bindings.mdmodified+39 -0
@@ -119,6 +119,45 @@ Mouse clicks operate on the window that was focused at the time of the click, no
119 119
120 Note that binding `Mod+MouseLeft` or `Mod+MouseRight` will override the corresponding gesture (moving or resizing the window).120 Note that binding `Mod+MouseLeft` or `Mod+MouseRight` will override the corresponding gesture (moving or resizing the window).
121 121
122+### Custom Hotkey Overlay Titles
123+
124+<sup>Since: next release</sup>
125+
126+The hotkey overlay (the Important Hotkeys dialog) shows a hardcoded list of binds.
127+You can customize this list using the `hotkey-overlay-title` property.
128+
129+To add a bind to the hotkey overlay, set the property to the title that you want to show:
130+```kdl
131+binds {
132+ Mod+Shift+S hotkey-overlay-title="Toggle Dark/Light Style" { spawn "some-script.sh"; }
133+}
134+```
135+
136+Binds with custom titles are listed after the hardcoded binds and before non-customized Spawn binds.
137+
138+To remove a hardcoded bind from the hotkey overlay, set the property to null:
139+```kdl
140+binds {
141+ Mod+Q hotkey-overlay-title=null { close-window; }
142+}
143+```
144+
145+> [!TIP]
146+> When multiple key combinations are bound to the same action:
147+> - If any of the binds has a custom hotkey overlay title, niri will show that bind.
148+> - Otherwise, if any of the binds has a null title, niri will hide the bind.
149+> - Otherwise, niri will show the first key combination.
150+
151+Custom titles support [Pango markup](https://docs.gtk.org/Pango/pango_markup.html):
152+
153+```kdl
154+binds {
155+ Mod+Shift+S hotkey-overlay-title="<b>Toggle</b> <span foreground='red'>Dark</span>/Light Style" { spawn "some-script.sh"; }
156+}
157+```
158+
159+![Custom markup example.](https://github.com/user-attachments/assets/2a2ba914-bfa7-4dfa-bb5e-49839034765d)
160+
122 ### Actions161 ### Actions
123 162
124 Every action that you can bind is also available for programmatic invocation via `niri msg action`.163 Every action that you can bind is also available for programmatic invocation via `niri msg action`.
wiki/Configuration:-Miscellaneous.mdmodified+3 -0
@@ -166,3 +166,6 @@ hotkey-overlay {
166 skip-at-startup166 skip-at-startup
167 }167 }
168 ```168 ```
169+
170+You can customize which binds the hotkey overlay shows using the `hotkey-overlay-title` property.
171+Check the [key bindings](./Configuration:-Key-Bindings.md) wiki page for details.