From 9aa245c1b6ea73742b067e3f15db269b97cc96b5 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Mon, 26 Oct 2026 09:10:26 +0300 Subject: [PATCH] input: Refactor screenshot binds to work like others Remove all screenshot hardcoding in the code. Behavior changes: - Escape to close now works only when no modifiers are held. - Other screenshot binds like Ctrl+C or Space used to ignore the state of some modifiers like Super, they now need them released. - Repeat is disabled for screenshot binds, practically holding P won't repeatedly cycle the pointer. - Inside the screenshot UI, the screenshot binds take precedence over allowed configured binds (e.g. if you configured Ctrl+C to spawn, or binds with MouseLeft). --- src/input/mod.rs | 103 ++++++++++++++++++---------------------- src/ui/screenshot_ui.rs | 103 ++++++++++++++++++++++++++-------------- 2 files changed, 115 insertions(+), 91 deletions(-) diff --git a/src/input/mod.rs b/src/input/mod.rs index 5d15d934..c3163326 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -614,10 +614,15 @@ impl State { } let is_locked = this.niri.is_locked(); + let is_in_screenshot = this.niri.screenshot_ui.is_open(); let res = { let config = this.niri.config.borrow(); - let bindings = - make_binds_iter(&config, &mut this.niri.window_mru_ui, modifiers); + let bindings = make_binds_iter( + &config, + &this.niri.screenshot_ui, + &mut this.niri.window_mru_ui, + modifiers, + ); should_intercept_key( &mut this.niri.suppressed_keys, @@ -630,10 +635,10 @@ impl State { raw, pressed, *mods, - &this.niri.screenshot_ui, this.niri.config.borrow().input.disable_power_key_handling, is_inhibiting_shortcuts, is_locked, + is_in_screenshot, ) }; @@ -754,10 +759,7 @@ impl State { if self.niri.is_locked() && !allowed_when_locked { return; } - - // The bind lookup already substitutes the screenshot UI's own - // actions for presses, so only releases need to be filtered here. - if !pressed && self.niri.screenshot_ui.is_open() && !allowed_during_screenshot { + if self.niri.screenshot_ui.is_open() && !allowed_during_screenshot { return; } @@ -2938,11 +2940,14 @@ impl State { let modifiers = modifiers_from_state(mods); let config = self.niri.config.borrow(); - let bindings = make_binds_iter(&config, &mut self.niri.window_mru_ui, modifiers); + let bindings = make_binds_iter( + &config, + &self.niri.screenshot_ui, + &mut self.niri.window_mru_ui, + modifiers, + ); - find_configured_bind(bindings, mod_key, trigger, mods).filter(|bind| { - !self.niri.screenshot_ui.is_open() || bind_allowed_during_screenshot(bind, true) - }) + find_configured_bind(bindings, mod_key, trigger, mods) } fn on_pointer_button(&mut self, event: I::PointerButtonEvent) { @@ -3001,6 +3006,8 @@ impl State { if is_mru_open || self.niri.mods_with_mouse_binds.contains(&modifiers) { if let Some(bind) = match button { + // Screenshot UI takes precedence over binds. + Some(MouseButton::Left) if self.niri.screenshot_ui.is_open() => None, Some(MouseButton::Left) => Some(Trigger::MouseLeft), Some(MouseButton::Right) => Some(Trigger::MouseRight), Some(MouseButton::Middle) => Some(Trigger::MouseMiddle), @@ -3014,6 +3021,7 @@ impl State { if should_record_release_bind( &bind, self.niri.is_locked(), + self.niri.screenshot_ui.is_open(), &self.niri.bind_cooldown_timers, ) { self.niri @@ -4064,6 +4072,7 @@ impl State { if should_record_release_bind( &bind, self.niri.is_locked(), + self.niri.screenshot_ui.is_open(), &self.niri.bind_cooldown_timers, ) { self.niri @@ -4743,10 +4752,10 @@ fn should_intercept_key<'a>( raw: Option, pressed: bool, mods: ModifiersState, - screenshot_ui: &ScreenshotUi, disable_power_key_handling: bool, is_inhibiting_shortcuts: bool, is_locked: bool, + is_in_screenshot: bool, ) -> ShouldInterceptResult { // A release never runs a press action, so its result depends only on the bind recorded when // the key was pressed, and on whether that press was suppressed. @@ -4754,7 +4763,7 @@ fn should_intercept_key<'a>( return released_key_intercept_result(suppressed_keys, pending_release_binds, key_code); } - let mut final_bind = find_bind( + let final_bind = find_bind( bindings, mod_key, modified, @@ -4763,39 +4772,6 @@ fn should_intercept_key<'a>( disable_power_key_handling, ); - // Allow only a subset of compositor actions while the screenshot UI is open, since the user - // cannot see the screen. - if screenshot_ui.is_open() { - let mut use_screenshot_ui_action = true; - - if let Some(bind) = &final_bind { - if bind_allowed_during_screenshot(bind, true) { - use_screenshot_ui_action = false; - } - } - - if use_screenshot_ui_action { - if let Some(raw) = raw { - final_bind = screenshot_ui.action(raw, mods).map(|action| Bind { - key: Key { - trigger: Trigger::Keysym(raw), - // Not entirely correct but it doesn't matter in how we currently use it. - modifiers: Modifiers::empty(), - }, - action: BoundAction::Press(action), - repeat: true, - cooldown: None, - allow_when_locked: false, - // The screenshot UI owns the focus anyway, so this doesn't really matter. - // But logically, nothing can inhibit its actions. Only opening it can be - // inhibited. - allow_inhibiting: false, - hotkey_overlay_title: None, - }); - } - } - } - match final_bind { Some(bind) => { if is_inhibiting_shortcuts && bind.allow_inhibiting { @@ -4806,7 +4782,7 @@ fn should_intercept_key<'a>( suppressed_keys.insert(key_code); ShouldInterceptResult::InterceptOnly } else if is_keysym_modifier(modified) { - if should_record_release_bind(&bind, is_locked, cooldown_timers) { + if should_record_release_bind(&bind, is_locked, is_in_screenshot, cooldown_timers) { pending_release_binds.insert(key_code, bind.clone()); } if bind.has_press() { @@ -4821,7 +4797,7 @@ fn should_intercept_key<'a>( ShouldInterceptResult::Forward } } else { - if should_record_release_bind(&bind, is_locked, cooldown_timers) { + if should_record_release_bind(&bind, is_locked, is_in_screenshot, cooldown_timers) { pending_release_binds.insert(key_code, bind.clone()); } suppressed_keys.insert(key_code); @@ -5145,13 +5121,16 @@ fn bind_allowed_when_locked(bind: &Bind, pressed: bool) -> bool { fn should_record_release_bind( bind: &Bind, is_locked: bool, + is_in_screenshot: bool, cooldown_timers: &HashMap, ) -> bool { if bind.has_press() && is_bind_on_cooldown(bind, cooldown_timers) { return false; } - bind.has_release() && (!is_locked || bind_allowed_when_locked(bind, true)) + bind.has_release() + && (!is_locked || bind_allowed_when_locked(bind, true)) + && (!is_in_screenshot || bind_allowed_during_screenshot(bind, true)) } fn allowed_during_screenshot(action: &Action) -> bool { @@ -5192,6 +5171,10 @@ fn allowed_during_screenshot(action: &Action) -> bool { | Action::SetWindowWidth(_) | Action::SetWindowHeight(_) | Action::SetColumnWidth(_) + // Screenshot UI's own actions. + | Action::CancelScreenshot + | Action::ConfirmScreenshot { .. } + | Action::ScreenshotTogglePointer ) } @@ -5645,11 +5628,14 @@ fn grab_allows_hot_corner(grab: &(dyn PointerGrab + 'static)) -> bool { /// Includes dynamically populated bindings like the MRU UI. fn make_binds_iter<'a>( config: &'a Config, + screenshot_ui: &'a ScreenshotUi, mru: &'a mut WindowMruUi, mods: Modifiers, ) -> impl Iterator { + let general_binds = config.binds.0.iter(); + // Figure out the binds to use depending on whether the MRU is enabled and/or open. - let general_binds = (!mru.is_open()).then_some(config.binds.0.iter()); + let general_binds = (!mru.is_open()).then_some(general_binds); let general_binds = general_binds.into_iter().flatten(); let mru_binds = @@ -5659,22 +5645,27 @@ fn make_binds_iter<'a>( let mru_open_binds = mru.is_open().then(|| mru.opened_bindings(mods)); let mru_open_binds = mru_open_binds.into_iter().flatten(); - // General binds take precedence over the MRU binds. - general_binds.chain(mru_binds).chain(mru_open_binds) + // Screenshot UI binds already disable themselves when it's closed. + let screenshot_binds = screenshot_ui.binds(); + + // The precedence is screenshot UI, then general, then MRU binds. + screenshot_binds + .chain(general_binds) + .chain(mru_binds) + .chain(mru_open_binds) } #[cfg(test)] mod tests { use super::*; - use crate::animation::Clock; const CLOSE_KEYSYM: Keysym = Keysym::q; const CLOSE_KEY_CODE: Keycode = Keycode::new(CLOSE_KEYSYM.raw()); struct TestState { - screenshot_ui: ScreenshotUi, disable_power_key_handling: bool, is_inhibiting: bool, is_locked: bool, + is_in_screenshot: bool, suppressed_keys: HashSet, pending_release_binds: HashMap, cooldown_timers: HashMap, @@ -5682,10 +5673,10 @@ mod tests { fn create_test_state() -> TestState { TestState { - screenshot_ui: ScreenshotUi::new(Clock::default(), Default::default()), disable_power_key_handling: false, is_inhibiting: false, is_locked: false, + is_in_screenshot: false, suppressed_keys: HashSet::new(), pending_release_binds: HashMap::new(), cooldown_timers: HashMap::new(), @@ -5758,10 +5749,10 @@ mod tests { Some(CLOSE_KEYSYM), pressed, mods, - &state.screenshot_ui, state.disable_power_key_handling, state.is_inhibiting, state.is_locked, + state.is_in_screenshot, ) } diff --git a/src/ui/screenshot_ui.rs b/src/ui/screenshot_ui.rs index a1586361..75dada3c 100644 --- a/src/ui/screenshot_ui.rs +++ b/src/ui/screenshot_ui.rs @@ -7,7 +7,7 @@ use std::rc::Rc; use anyhow::Context; use arrayvec::ArrayVec; -use niri_config::{Action, Config}; +use niri_config::{Action, Bind, BoundAction, Config, Key, Modifiers, Trigger}; use niri_ipc::SizeChange; use pango::{Alignment, FontDescription}; use pangocairo::cairo::{self, ImageSurface}; @@ -17,7 +17,7 @@ use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement use smithay::backend::renderer::element::Kind; use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; use smithay::backend::renderer::{ExportMem, Texture as _}; -use smithay::input::keyboard::{Keysym, ModifiersState}; +use smithay::input::keyboard::Keysym; use smithay::output::{Output, WeakOutput}; use smithay::utils::{Buffer, Physical, Point, Rectangle, Scale, Size, Transform}; @@ -764,17 +764,77 @@ impl ScreenshotUi { Ok((rect.size, copy.to_vec())) } - pub fn action(&self, raw: Keysym, mods: ModifiersState) -> Option { + pub fn binds(&self) -> impl Iterator + Clone { + const fn bind(key: Key, action: Action) -> Bind { + Bind { + key, + action: BoundAction::Press(action), + repeat: false, // Not useful for these actions. + cooldown: None, + allow_when_locked: true, + allow_inhibiting: false, + hotkey_overlay_title: None, + } + } + + const BINDS: &[Bind] = &[ + bind( + Key { + trigger: Trigger::Keysym(Keysym::Escape), + modifiers: Modifiers::empty(), + }, + Action::CancelScreenshot, + ), + bind( + Key { + trigger: Trigger::Keysym(Keysym::Return), + modifiers: Modifiers::empty(), + }, + Action::ConfirmScreenshot { + write_to_disk: true, + }, + ), + bind( + Key { + trigger: Trigger::Keysym(Keysym::c), + modifiers: Modifiers::CTRL, + }, + Action::ConfirmScreenshot { + write_to_disk: false, + }, + ), + bind( + Key { + trigger: Trigger::Keysym(Keysym::p), + modifiers: Modifiers::empty(), + }, + Action::ScreenshotTogglePointer, + ), + ]; + + const SPACE: &[Bind] = &[bind( + Key { + trigger: Trigger::Keysym(Keysym::space), + modifiers: Modifiers::empty(), + }, + Action::ConfirmScreenshot { + write_to_disk: true, + }, + )]; + + // A bit of gymnastics to return an iterator over constant &Bind values. let Self::Open { button, .. } = self else { - return None; + return [].iter().chain([].iter()); }; // Pressing Space while the button is down goes into origin moving rather than capture. - if matches!(button, Button::Down { .. }) && raw == Keysym::space { - return None; - } + let space: &[Bind] = if matches!(button, Button::Down { .. }) { + &[] + } else { + SPACE + }; - action(raw, mods) + space.iter().chain(BINDS) } pub fn selection_output(&self) -> Option<&Output> { @@ -1068,33 +1128,6 @@ impl OutputScreenshot { } } -fn action(raw: Keysym, mods: ModifiersState) -> Option { - if raw == Keysym::Escape { - return Some(Action::CancelScreenshot); - } - - if mods.alt || mods.shift { - return None; - } - - if !mods.ctrl && (raw == Keysym::space || raw == Keysym::Return) { - return Some(Action::ConfirmScreenshot { - write_to_disk: true, - }); - } - if mods.ctrl && raw == Keysym::c { - return Some(Action::ConfirmScreenshot { - write_to_disk: false, - }); - } - - if !mods.ctrl && raw == Keysym::p { - return Some(Action::ScreenshotTogglePointer); - } - - None -} - pub fn rect_from_corner_points( a: Point, b: Point,