forge
Explore
Log in

Don't you dare go Hollow

input: Refactor screenshot binds to work like others

Browse files at this commit

9aa245c1b6ea73742b067e3f15db269b97cc96b5Ivan MolodetskikhParent: 75db0a3
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.rsmodified+47 -56
@@ -614,10 +614,15 @@ impl State {
614614 }
615615
616616 let is_locked = this.niri.is_locked();
617+ let is_in_screenshot = this.niri.screenshot_ui.is_open();
617618 let res = {
618619 let config = this.niri.config.borrow();
619- let bindings =
620- make_binds_iter(&config, &mut this.niri.window_mru_ui, modifiers);
620+ let bindings = make_binds_iter(
621+ &config,
622+ &this.niri.screenshot_ui,
623+ &mut this.niri.window_mru_ui,
624+ modifiers,
625+ );
621626
622627 should_intercept_key(
623628 &mut this.niri.suppressed_keys,
@@ -630,10 +635,10 @@ impl State {
630635 raw,
631636 pressed,
632637 *mods,
633- &this.niri.screenshot_ui,
634638 this.niri.config.borrow().input.disable_power_key_handling,
635639 is_inhibiting_shortcuts,
636640 is_locked,
641+ is_in_screenshot,
637642 )
638643 };
639644
@@ -754,10 +759,7 @@ impl State {
754759 if self.niri.is_locked() && !allowed_when_locked {
755760 return;
756761 }
757-
758- // The bind lookup already substitutes the screenshot UI's own
759- // actions for presses, so only releases need to be filtered here.
760- if !pressed && self.niri.screenshot_ui.is_open() && !allowed_during_screenshot {
762+ if self.niri.screenshot_ui.is_open() && !allowed_during_screenshot {
761763 return;
762764 }
763765
@@ -2938,11 +2940,14 @@ impl State {
29382940 let modifiers = modifiers_from_state(mods);
29392941
29402942 let config = self.niri.config.borrow();
2941- let bindings = make_binds_iter(&config, &mut self.niri.window_mru_ui, modifiers);
2943+ let bindings = make_binds_iter(
2944+ &config,
2945+ &self.niri.screenshot_ui,
2946+ &mut self.niri.window_mru_ui,
2947+ modifiers,
2948+ );
29422949
2943- find_configured_bind(bindings, mod_key, trigger, mods).filter(|bind| {
2944- !self.niri.screenshot_ui.is_open() || bind_allowed_during_screenshot(bind, true)
2945- })
2950+ find_configured_bind(bindings, mod_key, trigger, mods)
29462951 }
29472952
29482953 fn on_pointer_button<I: InputBackend>(&mut self, event: I::PointerButtonEvent) {
@@ -3001,6 +3006,8 @@ impl State {
30013006
30023007 if is_mru_open || self.niri.mods_with_mouse_binds.contains(&modifiers) {
30033008 if let Some(bind) = match button {
3009+ // Screenshot UI takes precedence over binds.
3010+ Some(MouseButton::Left) if self.niri.screenshot_ui.is_open() => None,
30043011 Some(MouseButton::Left) => Some(Trigger::MouseLeft),
30053012 Some(MouseButton::Right) => Some(Trigger::MouseRight),
30063013 Some(MouseButton::Middle) => Some(Trigger::MouseMiddle),
@@ -3014,6 +3021,7 @@ impl State {
30143021 if should_record_release_bind(
30153022 &bind,
30163023 self.niri.is_locked(),
3024+ self.niri.screenshot_ui.is_open(),
30173025 &self.niri.bind_cooldown_timers,
30183026 ) {
30193027 self.niri
@@ -4064,6 +4072,7 @@ impl State {
40644072 if should_record_release_bind(
40654073 &bind,
40664074 self.niri.is_locked(),
4075+ self.niri.screenshot_ui.is_open(),
40674076 &self.niri.bind_cooldown_timers,
40684077 ) {
40694078 self.niri
@@ -4743,10 +4752,10 @@ fn should_intercept_key<'a>(
47434752 raw: Option<Keysym>,
47444753 pressed: bool,
47454754 mods: ModifiersState,
4746- screenshot_ui: &ScreenshotUi,
47474755 disable_power_key_handling: bool,
47484756 is_inhibiting_shortcuts: bool,
47494757 is_locked: bool,
4758+ is_in_screenshot: bool,
47504759 ) -> ShouldInterceptResult {
47514760 // A release never runs a press action, so its result depends only on the bind recorded when
47524761 // the key was pressed, and on whether that press was suppressed.
@@ -4754,7 +4763,7 @@ fn should_intercept_key<'a>(
47544763 return released_key_intercept_result(suppressed_keys, pending_release_binds, key_code);
47554764 }
47564765
4757- let mut final_bind = find_bind(
4766+ let final_bind = find_bind(
47584767 bindings,
47594768 mod_key,
47604769 modified,
@@ -4763,39 +4772,6 @@ fn should_intercept_key<'a>(
47634772 disable_power_key_handling,
47644773 );
47654774
4766- // Allow only a subset of compositor actions while the screenshot UI is open, since the user
4767- // cannot see the screen.
4768- if screenshot_ui.is_open() {
4769- let mut use_screenshot_ui_action = true;
4770-
4771- if let Some(bind) = &final_bind {
4772- if bind_allowed_during_screenshot(bind, true) {
4773- use_screenshot_ui_action = false;
4774- }
4775- }
4776-
4777- if use_screenshot_ui_action {
4778- if let Some(raw) = raw {
4779- final_bind = screenshot_ui.action(raw, mods).map(|action| Bind {
4780- key: Key {
4781- trigger: Trigger::Keysym(raw),
4782- // Not entirely correct but it doesn't matter in how we currently use it.
4783- modifiers: Modifiers::empty(),
4784- },
4785- action: BoundAction::Press(action),
4786- repeat: true,
4787- cooldown: None,
4788- allow_when_locked: false,
4789- // The screenshot UI owns the focus anyway, so this doesn't really matter.
4790- // But logically, nothing can inhibit its actions. Only opening it can be
4791- // inhibited.
4792- allow_inhibiting: false,
4793- hotkey_overlay_title: None,
4794- });
4795- }
4796- }
4797- }
4798-
47994775 match final_bind {
48004776 Some(bind) => {
48014777 if is_inhibiting_shortcuts && bind.allow_inhibiting {
@@ -4806,7 +4782,7 @@ fn should_intercept_key<'a>(
48064782 suppressed_keys.insert(key_code);
48074783 ShouldInterceptResult::InterceptOnly
48084784 } else if is_keysym_modifier(modified) {
4809- if should_record_release_bind(&bind, is_locked, cooldown_timers) {
4785+ if should_record_release_bind(&bind, is_locked, is_in_screenshot, cooldown_timers) {
48104786 pending_release_binds.insert(key_code, bind.clone());
48114787 }
48124788 if bind.has_press() {
@@ -4821,7 +4797,7 @@ fn should_intercept_key<'a>(
48214797 ShouldInterceptResult::Forward
48224798 }
48234799 } else {
4824- if should_record_release_bind(&bind, is_locked, cooldown_timers) {
4800+ if should_record_release_bind(&bind, is_locked, is_in_screenshot, cooldown_timers) {
48254801 pending_release_binds.insert(key_code, bind.clone());
48264802 }
48274803 suppressed_keys.insert(key_code);
@@ -5145,13 +5121,16 @@ fn bind_allowed_when_locked(bind: &Bind, pressed: bool) -> bool {
51455121 fn should_record_release_bind(
51465122 bind: &Bind,
51475123 is_locked: bool,
5124+ is_in_screenshot: bool,
51485125 cooldown_timers: &HashMap<Key, RegistrationToken>,
51495126 ) -> bool {
51505127 if bind.has_press() && is_bind_on_cooldown(bind, cooldown_timers) {
51515128 return false;
51525129 }
51535130
5154- bind.has_release() && (!is_locked || bind_allowed_when_locked(bind, true))
5131+ bind.has_release()
5132+ && (!is_locked || bind_allowed_when_locked(bind, true))
5133+ && (!is_in_screenshot || bind_allowed_during_screenshot(bind, true))
51555134 }
51565135
51575136 fn allowed_during_screenshot(action: &Action) -> bool {
@@ -5192,6 +5171,10 @@ fn allowed_during_screenshot(action: &Action) -> bool {
51925171 | Action::SetWindowWidth(_)
51935172 | Action::SetWindowHeight(_)
51945173 | Action::SetColumnWidth(_)
5174+ // Screenshot UI's own actions.
5175+ | Action::CancelScreenshot
5176+ | Action::ConfirmScreenshot { .. }
5177+ | Action::ScreenshotTogglePointer
51955178 )
51965179 }
51975180
@@ -5645,11 +5628,14 @@ fn grab_allows_hot_corner(grab: &(dyn PointerGrab<State> + 'static)) -> bool {
56455628 /// Includes dynamically populated bindings like the MRU UI.
56465629 fn make_binds_iter<'a>(
56475630 config: &'a Config,
5631+ screenshot_ui: &'a ScreenshotUi,
56485632 mru: &'a mut WindowMruUi,
56495633 mods: Modifiers,
56505634 ) -> impl Iterator<Item = &'a Bind> {
5635+ let general_binds = config.binds.0.iter();
5636+
56515637 // Figure out the binds to use depending on whether the MRU is enabled and/or open.
5652- let general_binds = (!mru.is_open()).then_some(config.binds.0.iter());
5638+ let general_binds = (!mru.is_open()).then_some(general_binds);
56535639 let general_binds = general_binds.into_iter().flatten();
56545640
56555641 let mru_binds =
@@ -5659,22 +5645,27 @@ fn make_binds_iter<'a>(
56595645 let mru_open_binds = mru.is_open().then(|| mru.opened_bindings(mods));
56605646 let mru_open_binds = mru_open_binds.into_iter().flatten();
56615647
5662- // General binds take precedence over the MRU binds.
5663- general_binds.chain(mru_binds).chain(mru_open_binds)
5648+ // Screenshot UI binds already disable themselves when it's closed.
5649+ let screenshot_binds = screenshot_ui.binds();
5650+
5651+ // The precedence is screenshot UI, then general, then MRU binds.
5652+ screenshot_binds
5653+ .chain(general_binds)
5654+ .chain(mru_binds)
5655+ .chain(mru_open_binds)
56645656 }
56655657
56665658 #[cfg(test)]
56675659 mod tests {
56685660 use super::*;
5669- use crate::animation::Clock;
56705661
56715662 const CLOSE_KEYSYM: Keysym = Keysym::q;
56725663 const CLOSE_KEY_CODE: Keycode = Keycode::new(CLOSE_KEYSYM.raw());
56735664 struct TestState {
5674- screenshot_ui: ScreenshotUi,
56755665 disable_power_key_handling: bool,
56765666 is_inhibiting: bool,
56775667 is_locked: bool,
5668+ is_in_screenshot: bool,
56785669 suppressed_keys: HashSet<Keycode>,
56795670 pending_release_binds: HashMap<Keycode, Bind>,
56805671 cooldown_timers: HashMap<Key, RegistrationToken>,
@@ -5682,10 +5673,10 @@ mod tests {
56825673
56835674 fn create_test_state() -> TestState {
56845675 TestState {
5685- screenshot_ui: ScreenshotUi::new(Clock::default(), Default::default()),
56865676 disable_power_key_handling: false,
56875677 is_inhibiting: false,
56885678 is_locked: false,
5679+ is_in_screenshot: false,
56895680 suppressed_keys: HashSet::new(),
56905681 pending_release_binds: HashMap::new(),
56915682 cooldown_timers: HashMap::new(),
@@ -5758,10 +5749,10 @@ mod tests {
57585749 Some(CLOSE_KEYSYM),
57595750 pressed,
57605751 mods,
5761- &state.screenshot_ui,
57625752 state.disable_power_key_handling,
57635753 state.is_inhibiting,
57645754 state.is_locked,
5755+ state.is_in_screenshot,
57655756 )
57665757 }
57675758
src/ui/screenshot_ui.rsmodified+68 -35
@@ -7,7 +7,7 @@ use std::rc::Rc;
77
88 use anyhow::Context;
99 use arrayvec::ArrayVec;
10-use niri_config::{Action, Config};
10+use niri_config::{Action, Bind, BoundAction, Config, Key, Modifiers, Trigger};
1111 use niri_ipc::SizeChange;
1212 use pango::{Alignment, FontDescription};
1313 use pangocairo::cairo::{self, ImageSurface};
@@ -17,7 +17,7 @@ use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement
1717 use smithay::backend::renderer::element::Kind;
1818 use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};
1919 use smithay::backend::renderer::{ExportMem, Texture as _};
20-use smithay::input::keyboard::{Keysym, ModifiersState};
20+use smithay::input::keyboard::Keysym;
2121 use smithay::output::{Output, WeakOutput};
2222 use smithay::utils::{Buffer, Physical, Point, Rectangle, Scale, Size, Transform};
2323
@@ -764,17 +764,77 @@ impl ScreenshotUi {
764764 Ok((rect.size, copy.to_vec()))
765765 }
766766
767- pub fn action(&self, raw: Keysym, mods: ModifiersState) -> Option<Action> {
767+ pub fn binds(&self) -> impl Iterator<Item = &Bind> + Clone {
768+ const fn bind(key: Key, action: Action) -> Bind {
769+ Bind {
770+ key,
771+ action: BoundAction::Press(action),
772+ repeat: false, // Not useful for these actions.
773+ cooldown: None,
774+ allow_when_locked: true,
775+ allow_inhibiting: false,
776+ hotkey_overlay_title: None,
777+ }
778+ }
779+
780+ const BINDS: &[Bind] = &[
781+ bind(
782+ Key {
783+ trigger: Trigger::Keysym(Keysym::Escape),
784+ modifiers: Modifiers::empty(),
785+ },
786+ Action::CancelScreenshot,
787+ ),
788+ bind(
789+ Key {
790+ trigger: Trigger::Keysym(Keysym::Return),
791+ modifiers: Modifiers::empty(),
792+ },
793+ Action::ConfirmScreenshot {
794+ write_to_disk: true,
795+ },
796+ ),
797+ bind(
798+ Key {
799+ trigger: Trigger::Keysym(Keysym::c),
800+ modifiers: Modifiers::CTRL,
801+ },
802+ Action::ConfirmScreenshot {
803+ write_to_disk: false,
804+ },
805+ ),
806+ bind(
807+ Key {
808+ trigger: Trigger::Keysym(Keysym::p),
809+ modifiers: Modifiers::empty(),
810+ },
811+ Action::ScreenshotTogglePointer,
812+ ),
813+ ];
814+
815+ const SPACE: &[Bind] = &[bind(
816+ Key {
817+ trigger: Trigger::Keysym(Keysym::space),
818+ modifiers: Modifiers::empty(),
819+ },
820+ Action::ConfirmScreenshot {
821+ write_to_disk: true,
822+ },
823+ )];
824+
825+ // A bit of gymnastics to return an iterator over constant &Bind values.
768826 let Self::Open { button, .. } = self else {
769- return None;
827+ return [].iter().chain([].iter());
770828 };
771829
772830 // Pressing Space while the button is down goes into origin moving rather than capture.
773- if matches!(button, Button::Down { .. }) && raw == Keysym::space {
774- return None;
775- }
831+ let space: &[Bind] = if matches!(button, Button::Down { .. }) {
832+ &[]
833+ } else {
834+ SPACE
835+ };
776836
777- action(raw, mods)
837+ space.iter().chain(BINDS)
778838 }
779839
780840 pub fn selection_output(&self) -> Option<&Output> {
@@ -1068,33 +1128,6 @@ impl OutputScreenshot {
10681128 }
10691129 }
10701130
1071-fn action(raw: Keysym, mods: ModifiersState) -> Option<Action> {
1072- if raw == Keysym::Escape {
1073- return Some(Action::CancelScreenshot);
1074- }
1075-
1076- if mods.alt || mods.shift {
1077- return None;
1078- }
1079-
1080- if !mods.ctrl && (raw == Keysym::space || raw == Keysym::Return) {
1081- return Some(Action::ConfirmScreenshot {
1082- write_to_disk: true,
1083- });
1084- }
1085- if mods.ctrl && raw == Keysym::c {
1086- return Some(Action::ConfirmScreenshot {
1087- write_to_disk: false,
1088- });
1089- }
1090-
1091- if !mods.ctrl && raw == Keysym::p {
1092- return Some(Action::ScreenshotTogglePointer);
1093- }
1094-
1095- None
1096-}
1097-
10981131 pub fn rect_from_corner_points(
10991132 a: Point<i32, Physical>,
11001133 b: Point<i32, Physical>,