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 {
614 }614 }
615 615
616 let is_locked = this.niri.is_locked();616 let is_locked = this.niri.is_locked();
617+ let is_in_screenshot = this.niri.screenshot_ui.is_open();
617 let res = {618 let res = {
618 let config = this.niri.config.borrow();619 let config = this.niri.config.borrow();
619- let bindings =620+ let bindings = make_binds_iter(
620- make_binds_iter(&config, &mut this.niri.window_mru_ui, modifiers);621+ &config,
622+ &this.niri.screenshot_ui,
623+ &mut this.niri.window_mru_ui,
624+ modifiers,
625+ );
621 626
622 should_intercept_key(627 should_intercept_key(
623 &mut this.niri.suppressed_keys,628 &mut this.niri.suppressed_keys,
@@ -630,10 +635,10 @@ impl State {
630 raw,635 raw,
631 pressed,636 pressed,
632 *mods,637 *mods,
633- &this.niri.screenshot_ui,
634 this.niri.config.borrow().input.disable_power_key_handling,638 this.niri.config.borrow().input.disable_power_key_handling,
635 is_inhibiting_shortcuts,639 is_inhibiting_shortcuts,
636 is_locked,640 is_locked,
641+ is_in_screenshot,
637 )642 )
638 };643 };
639 644
@@ -754,10 +759,7 @@ impl State {
754 if self.niri.is_locked() && !allowed_when_locked {759 if self.niri.is_locked() && !allowed_when_locked {
755 return;760 return;
756 }761 }
757-762+ if self.niri.screenshot_ui.is_open() && !allowed_during_screenshot {
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 {
761 return;763 return;
762 }764 }
763 765
@@ -2938,11 +2940,14 @@ impl State {
2938 let modifiers = modifiers_from_state(mods);2940 let modifiers = modifiers_from_state(mods);
2939 2941
2940 let config = self.niri.config.borrow();2942 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+ );
2942 2949
2943- find_configured_bind(bindings, mod_key, trigger, mods).filter(|bind| {2950+ find_configured_bind(bindings, mod_key, trigger, mods)
2944- !self.niri.screenshot_ui.is_open() || bind_allowed_during_screenshot(bind, true)
2945- })
2946 }2951 }
2947 2952
2948 fn on_pointer_button<I: InputBackend>(&mut self, event: I::PointerButtonEvent) {2953 fn on_pointer_button<I: InputBackend>(&mut self, event: I::PointerButtonEvent) {
@@ -3001,6 +3006,8 @@ impl State {
3001 3006
3002 if is_mru_open || self.niri.mods_with_mouse_binds.contains(&modifiers) {3007 if is_mru_open || self.niri.mods_with_mouse_binds.contains(&modifiers) {
3003 if let Some(bind) = match button {3008 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,
3004 Some(MouseButton::Left) => Some(Trigger::MouseLeft),3011 Some(MouseButton::Left) => Some(Trigger::MouseLeft),
3005 Some(MouseButton::Right) => Some(Trigger::MouseRight),3012 Some(MouseButton::Right) => Some(Trigger::MouseRight),
3006 Some(MouseButton::Middle) => Some(Trigger::MouseMiddle),3013 Some(MouseButton::Middle) => Some(Trigger::MouseMiddle),
@@ -3014,6 +3021,7 @@ impl State {
3014 if should_record_release_bind(3021 if should_record_release_bind(
3015 &bind,3022 &bind,
3016 self.niri.is_locked(),3023 self.niri.is_locked(),
3024+ self.niri.screenshot_ui.is_open(),
3017 &self.niri.bind_cooldown_timers,3025 &self.niri.bind_cooldown_timers,
3018 ) {3026 ) {
3019 self.niri3027 self.niri
@@ -4064,6 +4072,7 @@ impl State {
4064 if should_record_release_bind(4072 if should_record_release_bind(
4065 &bind,4073 &bind,
4066 self.niri.is_locked(),4074 self.niri.is_locked(),
4075+ self.niri.screenshot_ui.is_open(),
4067 &self.niri.bind_cooldown_timers,4076 &self.niri.bind_cooldown_timers,
4068 ) {4077 ) {
4069 self.niri4078 self.niri
@@ -4743,10 +4752,10 @@ fn should_intercept_key<'a>(
4743 raw: Option<Keysym>,4752 raw: Option<Keysym>,
4744 pressed: bool,4753 pressed: bool,
4745 mods: ModifiersState,4754 mods: ModifiersState,
4746- screenshot_ui: &ScreenshotUi,
4747 disable_power_key_handling: bool,4755 disable_power_key_handling: bool,
4748 is_inhibiting_shortcuts: bool,4756 is_inhibiting_shortcuts: bool,
4749 is_locked: bool,4757 is_locked: bool,
4758+ is_in_screenshot: bool,
4750 ) -> ShouldInterceptResult {4759 ) -> ShouldInterceptResult {
4751 // A release never runs a press action, so its result depends only on the bind recorded when4760 // A release never runs a press action, so its result depends only on the bind recorded when
4752 // the key was pressed, and on whether that press was suppressed.4761 // the key was pressed, and on whether that press was suppressed.
@@ -4754,7 +4763,7 @@ fn should_intercept_key<'a>(
4754 return released_key_intercept_result(suppressed_keys, pending_release_binds, key_code);4763 return released_key_intercept_result(suppressed_keys, pending_release_binds, key_code);
4755 }4764 }
4756 4765
4757- let mut final_bind = find_bind(4766+ let final_bind = find_bind(
4758 bindings,4767 bindings,
4759 mod_key,4768 mod_key,
4760 modified,4769 modified,
@@ -4763,39 +4772,6 @@ fn should_intercept_key<'a>(
4763 disable_power_key_handling,4772 disable_power_key_handling,
4764 );4773 );
4765 4774
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-
4799 match final_bind {4775 match final_bind {
4800 Some(bind) => {4776 Some(bind) => {
4801 if is_inhibiting_shortcuts && bind.allow_inhibiting {4777 if is_inhibiting_shortcuts && bind.allow_inhibiting {
@@ -4806,7 +4782,7 @@ fn should_intercept_key<'a>(
4806 suppressed_keys.insert(key_code);4782 suppressed_keys.insert(key_code);
4807 ShouldInterceptResult::InterceptOnly4783 ShouldInterceptResult::InterceptOnly
4808 } else if is_keysym_modifier(modified) {4784 } 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) {
4810 pending_release_binds.insert(key_code, bind.clone());4786 pending_release_binds.insert(key_code, bind.clone());
4811 }4787 }
4812 if bind.has_press() {4788 if bind.has_press() {
@@ -4821,7 +4797,7 @@ fn should_intercept_key<'a>(
4821 ShouldInterceptResult::Forward4797 ShouldInterceptResult::Forward
4822 }4798 }
4823 } else {4799 } 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) {
4825 pending_release_binds.insert(key_code, bind.clone());4801 pending_release_binds.insert(key_code, bind.clone());
4826 }4802 }
4827 suppressed_keys.insert(key_code);4803 suppressed_keys.insert(key_code);
@@ -5145,13 +5121,16 @@ fn bind_allowed_when_locked(bind: &Bind, pressed: bool) -> bool {
5145 fn should_record_release_bind(5121 fn should_record_release_bind(
5146 bind: &Bind,5122 bind: &Bind,
5147 is_locked: bool,5123 is_locked: bool,
5124+ is_in_screenshot: bool,
5148 cooldown_timers: &HashMap<Key, RegistrationToken>,5125 cooldown_timers: &HashMap<Key, RegistrationToken>,
5149 ) -> bool {5126 ) -> bool {
5150 if bind.has_press() && is_bind_on_cooldown(bind, cooldown_timers) {5127 if bind.has_press() && is_bind_on_cooldown(bind, cooldown_timers) {
5151 return false;5128 return false;
5152 }5129 }
5153 5130
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))
5155 }5134 }
5156 5135
5157 fn allowed_during_screenshot(action: &Action) -> bool {5136 fn allowed_during_screenshot(action: &Action) -> bool {
@@ -5192,6 +5171,10 @@ fn allowed_during_screenshot(action: &Action) -> bool {
5192 | Action::SetWindowWidth(_)5171 | Action::SetWindowWidth(_)
5193 | Action::SetWindowHeight(_)5172 | Action::SetWindowHeight(_)
5194 | Action::SetColumnWidth(_)5173 | Action::SetColumnWidth(_)
5174+ // Screenshot UI's own actions.
5175+ | Action::CancelScreenshot
5176+ | Action::ConfirmScreenshot { .. }
5177+ | Action::ScreenshotTogglePointer
5195 )5178 )
5196 }5179 }
5197 5180
@@ -5645,11 +5628,14 @@ fn grab_allows_hot_corner(grab: &(dyn PointerGrab<State> + 'static)) -> bool {
5645 /// Includes dynamically populated bindings like the MRU UI.5628 /// Includes dynamically populated bindings like the MRU UI.
5646 fn make_binds_iter<'a>(5629 fn make_binds_iter<'a>(
5647 config: &'a Config,5630 config: &'a Config,
5631+ screenshot_ui: &'a ScreenshotUi,
5648 mru: &'a mut WindowMruUi,5632 mru: &'a mut WindowMruUi,
5649 mods: Modifiers,5633 mods: Modifiers,
5650 ) -> impl Iterator<Item = &'a Bind> {5634 ) -> impl Iterator<Item = &'a Bind> {
5635+ let general_binds = config.binds.0.iter();
5636+
5651 // Figure out the binds to use depending on whether the MRU is enabled and/or open.5637 // 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);
5653 let general_binds = general_binds.into_iter().flatten();5639 let general_binds = general_binds.into_iter().flatten();
5654 5640
5655 let mru_binds =5641 let mru_binds =
@@ -5659,22 +5645,27 @@ fn make_binds_iter<'a>(
5659 let mru_open_binds = mru.is_open().then(|| mru.opened_bindings(mods));5645 let mru_open_binds = mru.is_open().then(|| mru.opened_bindings(mods));
5660 let mru_open_binds = mru_open_binds.into_iter().flatten();5646 let mru_open_binds = mru_open_binds.into_iter().flatten();
5661 5647
5662- // General binds take precedence over the MRU binds.5648+ // Screenshot UI binds already disable themselves when it's closed.
5663- general_binds.chain(mru_binds).chain(mru_open_binds)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)
5664 }5656 }
5665 5657
5666 #[cfg(test)]5658 #[cfg(test)]
5667 mod tests {5659 mod tests {
5668 use super::*;5660 use super::*;
5669- use crate::animation::Clock;
5670 5661
5671 const CLOSE_KEYSYM: Keysym = Keysym::q;5662 const CLOSE_KEYSYM: Keysym = Keysym::q;
5672 const CLOSE_KEY_CODE: Keycode = Keycode::new(CLOSE_KEYSYM.raw());5663 const CLOSE_KEY_CODE: Keycode = Keycode::new(CLOSE_KEYSYM.raw());
5673 struct TestState {5664 struct TestState {
5674- screenshot_ui: ScreenshotUi,
5675 disable_power_key_handling: bool,5665 disable_power_key_handling: bool,
5676 is_inhibiting: bool,5666 is_inhibiting: bool,
5677 is_locked: bool,5667 is_locked: bool,
5668+ is_in_screenshot: bool,
5678 suppressed_keys: HashSet<Keycode>,5669 suppressed_keys: HashSet<Keycode>,
5679 pending_release_binds: HashMap<Keycode, Bind>,5670 pending_release_binds: HashMap<Keycode, Bind>,
5680 cooldown_timers: HashMap<Key, RegistrationToken>,5671 cooldown_timers: HashMap<Key, RegistrationToken>,
@@ -5682,10 +5673,10 @@ mod tests {
5682 5673
5683 fn create_test_state() -> TestState {5674 fn create_test_state() -> TestState {
5684 TestState {5675 TestState {
5685- screenshot_ui: ScreenshotUi::new(Clock::default(), Default::default()),
5686 disable_power_key_handling: false,5676 disable_power_key_handling: false,
5687 is_inhibiting: false,5677 is_inhibiting: false,
5688 is_locked: false,5678 is_locked: false,
5679+ is_in_screenshot: false,
5689 suppressed_keys: HashSet::new(),5680 suppressed_keys: HashSet::new(),
5690 pending_release_binds: HashMap::new(),5681 pending_release_binds: HashMap::new(),
5691 cooldown_timers: HashMap::new(),5682 cooldown_timers: HashMap::new(),
@@ -5758,10 +5749,10 @@ mod tests {
5758 Some(CLOSE_KEYSYM),5749 Some(CLOSE_KEYSYM),
5759 pressed,5750 pressed,
5760 mods,5751 mods,
5761- &state.screenshot_ui,
5762 state.disable_power_key_handling,5752 state.disable_power_key_handling,
5763 state.is_inhibiting,5753 state.is_inhibiting,
5764 state.is_locked,5754 state.is_locked,
5755+ state.is_in_screenshot,
5765 )5756 )
5766 }5757 }
5767 5758
src/ui/screenshot_ui.rsmodified+68 -35
@@ -7,7 +7,7 @@ use std::rc::Rc;
7 7
8 use anyhow::Context;8 use anyhow::Context;
9 use arrayvec::ArrayVec;9 use arrayvec::ArrayVec;
10-use niri_config::{Action, Config};10+use niri_config::{Action, Bind, BoundAction, Config, Key, Modifiers, Trigger};
11 use niri_ipc::SizeChange;11 use niri_ipc::SizeChange;
12 use pango::{Alignment, FontDescription};12 use pango::{Alignment, FontDescription};
13 use pangocairo::cairo::{self, ImageSurface};13 use pangocairo::cairo::{self, ImageSurface};
@@ -17,7 +17,7 @@ use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement
17 use smithay::backend::renderer::element::Kind;17 use smithay::backend::renderer::element::Kind;
18 use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};18 use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};
19 use smithay::backend::renderer::{ExportMem, Texture as _};19 use smithay::backend::renderer::{ExportMem, Texture as _};
20-use smithay::input::keyboard::{Keysym, ModifiersState};20+use smithay::input::keyboard::Keysym;
21 use smithay::output::{Output, WeakOutput};21 use smithay::output::{Output, WeakOutput};
22 use smithay::utils::{Buffer, Physical, Point, Rectangle, Scale, Size, Transform};22 use smithay::utils::{Buffer, Physical, Point, Rectangle, Scale, Size, Transform};
23 23
@@ -764,17 +764,77 @@ impl ScreenshotUi {
764 Ok((rect.size, copy.to_vec()))764 Ok((rect.size, copy.to_vec()))
765 }765 }
766 766
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.
768 let Self::Open { button, .. } = self else {826 let Self::Open { button, .. } = self else {
769- return None;827+ return [].iter().chain([].iter());
770 };828 };
771 829
772 // Pressing Space while the button is down goes into origin moving rather than capture.830 // Pressing Space while the button is down goes into origin moving rather than capture.
773- if matches!(button, Button::Down { .. }) && raw == Keysym::space {831+ let space: &[Bind] = if matches!(button, Button::Down { .. }) {
774- return None;832+ &[]
775- }833+ } else {
834+ SPACE
835+ };
776 836
777- action(raw, mods)837+ space.iter().chain(BINDS)
778 }838 }
779 839
780 pub fn selection_output(&self) -> Option<&Output> {840 pub fn selection_output(&self) -> Option<&Output> {
@@ -1068,33 +1128,6 @@ impl OutputScreenshot {
1068 }1128 }
1069 }1129 }
1070 1130
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-
1098 pub fn rect_from_corner_points(1131 pub fn rect_from_corner_points(
1099 a: Point<i32, Physical>,1132 a: Point<i32, Physical>,
1100 b: Point<i32, Physical>,1133 b: Point<i32, Physical>,