input: Refactor screenshot binds to work like others
Browse files at this commit9aa245c1b6ea73742b067e3f15db269b97cc96b5Parent: 75db0a32 changed files +115 -91
- src/input/mod.rs +47 -56
- src/ui/screenshot_ui.rs +68 -35
src/input/mod.rsmodified+47 -56
| @@ -614,10 +614,15 @@ impl State { | ||
| 614 | 614 | } |
| 615 | 615 | |
| 616 | 616 | let is_locked = this.niri.is_locked(); |
| 617 | + let is_in_screenshot = this.niri.screenshot_ui.is_open(); | |
| 617 | 618 | let res = { |
| 618 | 619 | 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 | + ); | |
| 621 | 626 | |
| 622 | 627 | should_intercept_key( |
| 623 | 628 | &mut this.niri.suppressed_keys, |
| @@ -630,10 +635,10 @@ impl State { | ||
| 630 | 635 | raw, |
| 631 | 636 | pressed, |
| 632 | 637 | *mods, |
| 633 | - &this.niri.screenshot_ui, | |
| 634 | 638 | this.niri.config.borrow().input.disable_power_key_handling, |
| 635 | 639 | is_inhibiting_shortcuts, |
| 636 | 640 | is_locked, |
| 641 | + is_in_screenshot, | |
| 637 | 642 | ) |
| 638 | 643 | }; |
| 639 | 644 | |
| @@ -754,10 +759,7 @@ impl State { | ||
| 754 | 759 | if self.niri.is_locked() && !allowed_when_locked { |
| 755 | 760 | return; |
| 756 | 761 | } |
| 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 { | |
| 761 | 763 | return; |
| 762 | 764 | } |
| 763 | 765 | |
| @@ -2938,11 +2940,14 @@ impl State { | ||
| 2938 | 2940 | let modifiers = modifiers_from_state(mods); |
| 2939 | 2941 | |
| 2940 | 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| { | |
| 2944 | - !self.niri.screenshot_ui.is_open() || bind_allowed_during_screenshot(bind, true) | |
| 2945 | - }) | |
| 2950 | + find_configured_bind(bindings, mod_key, trigger, mods) | |
| 2946 | 2951 | } |
| 2947 | 2952 | |
| 2948 | 2953 | fn on_pointer_button<I: InputBackend>(&mut self, event: I::PointerButtonEvent) { |
| @@ -3001,6 +3006,8 @@ impl State { | ||
| 3001 | 3006 | |
| 3002 | 3007 | if is_mru_open || self.niri.mods_with_mouse_binds.contains(&modifiers) { |
| 3003 | 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 | 3011 | Some(MouseButton::Left) => Some(Trigger::MouseLeft), |
| 3005 | 3012 | Some(MouseButton::Right) => Some(Trigger::MouseRight), |
| 3006 | 3013 | Some(MouseButton::Middle) => Some(Trigger::MouseMiddle), |
| @@ -3014,6 +3021,7 @@ impl State { | ||
| 3014 | 3021 | if should_record_release_bind( |
| 3015 | 3022 | &bind, |
| 3016 | 3023 | self.niri.is_locked(), |
| 3024 | + self.niri.screenshot_ui.is_open(), | |
| 3017 | 3025 | &self.niri.bind_cooldown_timers, |
| 3018 | 3026 | ) { |
| 3019 | 3027 | self.niri |
| @@ -4064,6 +4072,7 @@ impl State { | ||
| 4064 | 4072 | if should_record_release_bind( |
| 4065 | 4073 | &bind, |
| 4066 | 4074 | self.niri.is_locked(), |
| 4075 | + self.niri.screenshot_ui.is_open(), | |
| 4067 | 4076 | &self.niri.bind_cooldown_timers, |
| 4068 | 4077 | ) { |
| 4069 | 4078 | self.niri |
| @@ -4743,10 +4752,10 @@ fn should_intercept_key<'a>( | ||
| 4743 | 4752 | raw: Option<Keysym>, |
| 4744 | 4753 | pressed: bool, |
| 4745 | 4754 | mods: ModifiersState, |
| 4746 | - screenshot_ui: &ScreenshotUi, | |
| 4747 | 4755 | disable_power_key_handling: bool, |
| 4748 | 4756 | is_inhibiting_shortcuts: bool, |
| 4749 | 4757 | is_locked: bool, |
| 4758 | + is_in_screenshot: bool, | |
| 4750 | 4759 | ) -> ShouldInterceptResult { |
| 4751 | 4760 | // A release never runs a press action, so its result depends only on the bind recorded when |
| 4752 | 4761 | // the key was pressed, and on whether that press was suppressed. |
| @@ -4754,7 +4763,7 @@ fn should_intercept_key<'a>( | ||
| 4754 | 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 | 4767 | bindings, |
| 4759 | 4768 | mod_key, |
| 4760 | 4769 | modified, |
| @@ -4763,39 +4772,6 @@ fn should_intercept_key<'a>( | ||
| 4763 | 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 | 4775 | match final_bind { |
| 4800 | 4776 | Some(bind) => { |
| 4801 | 4777 | if is_inhibiting_shortcuts && bind.allow_inhibiting { |
| @@ -4806,7 +4782,7 @@ fn should_intercept_key<'a>( | ||
| 4806 | 4782 | suppressed_keys.insert(key_code); |
| 4807 | 4783 | ShouldInterceptResult::InterceptOnly |
| 4808 | 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 | 4786 | pending_release_binds.insert(key_code, bind.clone()); |
| 4811 | 4787 | } |
| 4812 | 4788 | if bind.has_press() { |
| @@ -4821,7 +4797,7 @@ fn should_intercept_key<'a>( | ||
| 4821 | 4797 | ShouldInterceptResult::Forward |
| 4822 | 4798 | } |
| 4823 | 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 | 4801 | pending_release_binds.insert(key_code, bind.clone()); |
| 4826 | 4802 | } |
| 4827 | 4803 | suppressed_keys.insert(key_code); |
| @@ -5145,13 +5121,16 @@ fn bind_allowed_when_locked(bind: &Bind, pressed: bool) -> bool { | ||
| 5145 | 5121 | fn should_record_release_bind( |
| 5146 | 5122 | bind: &Bind, |
| 5147 | 5123 | is_locked: bool, |
| 5124 | + is_in_screenshot: bool, | |
| 5148 | 5125 | cooldown_timers: &HashMap<Key, RegistrationToken>, |
| 5149 | 5126 | ) -> bool { |
| 5150 | 5127 | if bind.has_press() && is_bind_on_cooldown(bind, cooldown_timers) { |
| 5151 | 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 | 5136 | fn allowed_during_screenshot(action: &Action) -> bool { |
| @@ -5192,6 +5171,10 @@ fn allowed_during_screenshot(action: &Action) -> bool { | ||
| 5192 | 5171 | | Action::SetWindowWidth(_) |
| 5193 | 5172 | | Action::SetWindowHeight(_) |
| 5194 | 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 | 5628 | /// Includes dynamically populated bindings like the MRU UI. |
| 5646 | 5629 | fn make_binds_iter<'a>( |
| 5647 | 5630 | config: &'a Config, |
| 5631 | + screenshot_ui: &'a ScreenshotUi, | |
| 5648 | 5632 | mru: &'a mut WindowMruUi, |
| 5649 | 5633 | mods: Modifiers, |
| 5650 | 5634 | ) -> impl Iterator<Item = &'a Bind> { |
| 5635 | + let general_binds = config.binds.0.iter(); | |
| 5636 | + | |
| 5651 | 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 | 5639 | let general_binds = general_binds.into_iter().flatten(); |
| 5654 | 5640 | |
| 5655 | 5641 | let mru_binds = |
| @@ -5659,22 +5645,27 @@ fn make_binds_iter<'a>( | ||
| 5659 | 5645 | let mru_open_binds = mru.is_open().then(|| mru.opened_bindings(mods)); |
| 5660 | 5646 | let mru_open_binds = mru_open_binds.into_iter().flatten(); |
| 5661 | 5647 | |
| 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) | |
| 5664 | 5656 | } |
| 5665 | 5657 | |
| 5666 | 5658 | #[cfg(test)] |
| 5667 | 5659 | mod tests { |
| 5668 | 5660 | use super::*; |
| 5669 | - use crate::animation::Clock; | |
| 5670 | 5661 | |
| 5671 | 5662 | const CLOSE_KEYSYM: Keysym = Keysym::q; |
| 5672 | 5663 | const CLOSE_KEY_CODE: Keycode = Keycode::new(CLOSE_KEYSYM.raw()); |
| 5673 | 5664 | struct TestState { |
| 5674 | - screenshot_ui: ScreenshotUi, | |
| 5675 | 5665 | disable_power_key_handling: bool, |
| 5676 | 5666 | is_inhibiting: bool, |
| 5677 | 5667 | is_locked: bool, |
| 5668 | + is_in_screenshot: bool, | |
| 5678 | 5669 | suppressed_keys: HashSet<Keycode>, |
| 5679 | 5670 | pending_release_binds: HashMap<Keycode, Bind>, |
| 5680 | 5671 | cooldown_timers: HashMap<Key, RegistrationToken>, |
| @@ -5682,10 +5673,10 @@ mod tests { | ||
| 5682 | 5673 | |
| 5683 | 5674 | fn create_test_state() -> TestState { |
| 5684 | 5675 | TestState { |
| 5685 | - screenshot_ui: ScreenshotUi::new(Clock::default(), Default::default()), | |
| 5686 | 5676 | disable_power_key_handling: false, |
| 5687 | 5677 | is_inhibiting: false, |
| 5688 | 5678 | is_locked: false, |
| 5679 | + is_in_screenshot: false, | |
| 5689 | 5680 | suppressed_keys: HashSet::new(), |
| 5690 | 5681 | pending_release_binds: HashMap::new(), |
| 5691 | 5682 | cooldown_timers: HashMap::new(), |
| @@ -5758,10 +5749,10 @@ mod tests { | ||
| 5758 | 5749 | Some(CLOSE_KEYSYM), |
| 5759 | 5750 | pressed, |
| 5760 | 5751 | mods, |
| 5761 | - &state.screenshot_ui, | |
| 5762 | 5752 | state.disable_power_key_handling, |
| 5763 | 5753 | state.is_inhibiting, |
| 5764 | 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 | 8 | use anyhow::Context; |
| 9 | 9 | use arrayvec::ArrayVec; |
| 10 | -use niri_config::{Action, Config}; | |
| 10 | +use niri_config::{Action, Bind, BoundAction, Config, Key, Modifiers, Trigger}; | |
| 11 | 11 | use niri_ipc::SizeChange; |
| 12 | 12 | use pango::{Alignment, FontDescription}; |
| 13 | 13 | use pangocairo::cairo::{self, ImageSurface}; |
| @@ -17,7 +17,7 @@ use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement | ||
| 17 | 17 | use smithay::backend::renderer::element::Kind; |
| 18 | 18 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; |
| 19 | 19 | use smithay::backend::renderer::{ExportMem, Texture as _}; |
| 20 | -use smithay::input::keyboard::{Keysym, ModifiersState}; | |
| 20 | +use smithay::input::keyboard::Keysym; | |
| 21 | 21 | use smithay::output::{Output, WeakOutput}; |
| 22 | 22 | use smithay::utils::{Buffer, Physical, Point, Rectangle, Scale, Size, Transform}; |
| 23 | 23 | |
| @@ -764,17 +764,77 @@ impl ScreenshotUi { | ||
| 764 | 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 | 826 | let Self::Open { button, .. } = self else { |
| 769 | - return None; | |
| 827 | + return [].iter().chain([].iter()); | |
| 770 | 828 | }; |
| 771 | 829 | |
| 772 | 830 | // 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 | + }; | |
| 776 | 836 | |
| 777 | - action(raw, mods) | |
| 837 | + space.iter().chain(BINDS) | |
| 778 | 838 | } |
| 779 | 839 | |
| 780 | 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 | 1131 | pub fn rect_from_corner_points( |
| 1099 | 1132 | a: Point<i32, Physical>, |
| 1100 | 1133 | b: Point<i32, Physical>, |