forge
Explore
Log in

Don't you dare go Hollow

input: Make functions return the whole bind

Browse files at this commit

6c08ba307aef03938e009601ac81bc140c38a1ddIvan MolodetskikhParent: 4b2fdd0
niri-config/src/lib.rsmodified+1 -1
@@ -716,7 +716,7 @@ impl PartialEq for Match {
716716 #[derive(Debug, Default, PartialEq)]
717717 pub struct Binds(pub Vec<Bind>);
718718
719-#[derive(Debug, PartialEq)]
719+#[derive(Debug, Clone, PartialEq)]
720720 pub struct Bind {
721721 pub key: Key,
722722 pub action: Action,
src/input.rsmodified+93 -60
@@ -3,7 +3,7 @@ use std::collections::HashSet;
33 use std::time::Duration;
44
55 use input::event::gesture::GestureEventCoordinates as _;
6-use niri_config::{Action, Binds, Modifiers, Trigger};
6+use niri_config::{Action, Bind, Binds, Key, Modifiers, Trigger};
77 use niri_ipc::LayoutSwitchTarget;
88 use smithay::backend::input::{
99 AbsolutePositionEvent, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event,
@@ -248,7 +248,7 @@ impl State {
248248 let time = Event::time_msec(&event);
249249 let pressed = event.state() == KeyState::Pressed;
250250
251- let Some(Some(action)) = self.niri.seat.get_keyboard().unwrap().input(
251+ let Some(Some(bind)) = self.niri.seat.get_keyboard().unwrap().input(
252252 self,
253253 event.key_code(),
254254 event.state(),
@@ -289,7 +289,7 @@ impl State {
289289 return;
290290 }
291291
292- self.do_action(action);
292+ self.do_action(bind.action);
293293 }
294294
295295 pub fn do_action(&mut self, action: Action) {
@@ -1089,22 +1089,23 @@ impl State {
10891089 if let Some(v120) = horizontal_amount_v120 {
10901090 let config = self.niri.config.borrow();
10911091 let bindings = &config.binds;
1092- let action_left = bound_action(bindings, comp_mod, Trigger::WheelLeft, mods);
1093- let action_right = bound_action(bindings, comp_mod, Trigger::WheelRight, mods);
1092+ let bind_left = find_configured_bind(bindings, comp_mod, Trigger::WheelLeft, mods);
1093+ let bind_right =
1094+ find_configured_bind(bindings, comp_mod, Trigger::WheelRight, mods);
10941095 drop(config);
10951096
10961097 // If we have a bind with current modifiers along the scroll direction, then
10971098 // accumulate and don't pass to Wayland. If there's no bind, reset the accumulator.
1098- if action_left.is_some() || action_right.is_some() {
1099+ if bind_left.is_some() || bind_right.is_some() {
10991100 let ticks = self.niri.horizontal_wheel_tracker.accumulate(v120);
1100- if let Some(right) = action_right {
1101+ if let Some(right) = bind_right {
11011102 for _ in 0..ticks {
1102- self.do_action(right.clone());
1103+ self.do_action(right.action.clone());
11031104 }
11041105 }
1105- if let Some(left) = action_left {
1106+ if let Some(left) = bind_left {
11061107 for _ in ticks..0 {
1107- self.do_action(left.clone());
1108+ self.do_action(left.action.clone());
11081109 }
11091110 }
11101111 return;
@@ -1116,20 +1117,20 @@ impl State {
11161117 if let Some(v120) = vertical_amount_v120 {
11171118 let config = self.niri.config.borrow();
11181119 let bindings = &config.binds;
1119- let action_up = bound_action(bindings, comp_mod, Trigger::WheelUp, mods);
1120- let action_down = bound_action(bindings, comp_mod, Trigger::WheelDown, mods);
1120+ let bind_up = find_configured_bind(bindings, comp_mod, Trigger::WheelUp, mods);
1121+ let bind_down = find_configured_bind(bindings, comp_mod, Trigger::WheelDown, mods);
11211122 drop(config);
11221123
1123- if action_up.is_some() || action_down.is_some() {
1124+ if bind_up.is_some() || bind_down.is_some() {
11241125 let ticks = self.niri.vertical_wheel_tracker.accumulate(v120);
1125- if let Some(down) = action_down {
1126+ if let Some(down) = bind_down {
11261127 for _ in 0..ticks {
1127- self.do_action(down.clone());
1128+ self.do_action(down.action.clone());
11281129 }
11291130 }
1130- if let Some(up) = action_up {
1131+ if let Some(up) = bind_up {
11311132 for _ in ticks..0 {
1132- self.do_action(up.clone());
1133+ self.do_action(up.action.clone());
11331134 }
11341135 }
11351136 return;
@@ -1687,7 +1688,7 @@ fn should_intercept_key(
16871688 mods: ModifiersState,
16881689 screenshot_ui: &ScreenshotUi,
16891690 disable_power_key_handling: bool,
1690-) -> FilterResult<Option<Action>> {
1691+) -> FilterResult<Option<Bind>> {
16911692 // Actions are only triggered on presses, release of the key
16921693 // shouldn't try to intercept anything unless we have marked
16931694 // the key to suppress.
@@ -1695,7 +1696,7 @@ fn should_intercept_key(
16951696 return FilterResult::Forward;
16961697 }
16971698
1698- let mut final_action = action(
1699+ let mut final_bind = find_bind(
16991700 bindings,
17001701 comp_mod,
17011702 modified,
@@ -1709,21 +1710,30 @@ fn should_intercept_key(
17091710 if screenshot_ui.is_open() {
17101711 let mut use_screenshot_ui_action = true;
17111712
1712- if let Some(action) = &final_action {
1713- if allowed_during_screenshot(action) {
1713+ if let Some(bind) = &final_bind {
1714+ if allowed_during_screenshot(&bind.action) {
17141715 use_screenshot_ui_action = false;
17151716 }
17161717 }
17171718
17181719 if use_screenshot_ui_action {
1719- final_action = screenshot_ui.action(raw, mods);
1720+ if let Some(raw) = raw {
1721+ final_bind = screenshot_ui.action(raw, mods).map(|action| Bind {
1722+ key: Key {
1723+ trigger: Trigger::Keysym(raw),
1724+ // Not entirely correct but it doesn't matter in how we currently use it.
1725+ modifiers: Modifiers::empty(),
1726+ },
1727+ action,
1728+ });
1729+ }
17201730 }
17211731 }
17221732
1723- match (final_action, pressed) {
1724- (Some(action), true) => {
1733+ match (final_bind, pressed) {
1734+ (Some(bind), true) => {
17251735 suppressed_keys.insert(key_code);
1726- FilterResult::Intercept(Some(action))
1736+ FilterResult::Intercept(Some(bind))
17271737 }
17281738 (_, false) => {
17291739 suppressed_keys.remove(&key_code);
@@ -1733,37 +1743,48 @@ fn should_intercept_key(
17331743 }
17341744 }
17351745
1736-fn action(
1746+fn find_bind(
17371747 bindings: &Binds,
17381748 comp_mod: CompositorMod,
17391749 modified: Keysym,
17401750 raw: Option<Keysym>,
17411751 mods: ModifiersState,
17421752 disable_power_key_handling: bool,
1743-) -> Option<Action> {
1753+) -> Option<Bind> {
17441754 use keysyms::*;
17451755
17461756 // Handle hardcoded binds.
17471757 #[allow(non_upper_case_globals)] // wat
1748- match modified.raw() {
1758+ let hardcoded_action = match modified.raw() {
17491759 modified @ KEY_XF86Switch_VT_1..=KEY_XF86Switch_VT_12 => {
17501760 let vt = (modified - KEY_XF86Switch_VT_1 + 1) as i32;
1751- return Some(Action::ChangeVt(vt));
1761+ Some(Action::ChangeVt(vt))
17521762 }
1753- KEY_XF86PowerOff if !disable_power_key_handling => return Some(Action::Suspend),
1754- _ => (),
1763+ KEY_XF86PowerOff if !disable_power_key_handling => Some(Action::Suspend),
1764+ _ => None,
1765+ };
1766+
1767+ if let Some(action) = hardcoded_action {
1768+ return Some(Bind {
1769+ key: Key {
1770+ // Not entirely correct but it doesn't matter in how we currently use it.
1771+ trigger: Trigger::Keysym(modified),
1772+ modifiers: Modifiers::empty(),
1773+ },
1774+ action,
1775+ });
17551776 }
17561777
17571778 let trigger = Trigger::Keysym(raw?);
1758- bound_action(bindings, comp_mod, trigger, mods)
1779+ find_configured_bind(bindings, comp_mod, trigger, mods)
17591780 }
17601781
1761-fn bound_action(
1782+fn find_configured_bind(
17621783 bindings: &Binds,
17631784 comp_mod: CompositorMod,
17641785 trigger: Trigger,
17651786 mods: ModifiersState,
1766-) -> Option<Action> {
1787+) -> Option<Bind> {
17671788 // Handle configured binds.
17681789 let mut modifiers = Modifiers::empty();
17691790 if mods.ctrl {
@@ -1800,7 +1821,7 @@ fn bound_action(
18001821 }
18011822
18021823 if bind_modifiers == modifiers {
1803- return Some(bind.action.clone());
1824+ return Some(bind.clone());
18041825 }
18051826 }
18061827
@@ -1959,8 +1980,6 @@ pub fn apply_libinput_settings(config: &niri_config::Input, device: &mut input::
19591980
19601981 #[cfg(test)]
19611982 mod tests {
1962- use niri_config::{Bind, Key};
1963-
19641983 use super::*;
19651984
19661985 #[test]
@@ -2026,7 +2045,10 @@ mod tests {
20262045 let filter = close_key_event(&mut suppressed_keys, mods, true);
20272046 assert!(matches!(
20282047 filter,
2029- FilterResult::Intercept(Some(Action::CloseWindow))
2048+ FilterResult::Intercept(Some(Bind {
2049+ action: Action::CloseWindow,
2050+ ..
2051+ }))
20302052 ));
20312053 assert!(suppressed_keys.contains(&close_key_code));
20322054
@@ -2057,7 +2079,10 @@ mod tests {
20572079 let filter = close_key_event(&mut suppressed_keys, mods, true);
20582080 assert!(matches!(
20592081 filter,
2060- FilterResult::Intercept(Some(Action::CloseWindow))
2082+ FilterResult::Intercept(Some(Bind {
2083+ action: Action::CloseWindow,
2084+ ..
2085+ }))
20612086 ));
20622087
20632088 let filter = none_key_event(&mut suppressed_keys, mods, true);
@@ -2074,7 +2099,10 @@ mod tests {
20742099 let filter = close_key_event(&mut suppressed_keys, mods, true);
20752100 assert!(matches!(
20762101 filter,
2077- FilterResult::Intercept(Some(Action::CloseWindow))
2102+ FilterResult::Intercept(Some(Bind {
2103+ action: Action::CloseWindow,
2104+ ..
2105+ }))
20782106 ));
20792107
20802108 mods = Default::default();
@@ -2126,7 +2154,7 @@ mod tests {
21262154 ]);
21272155
21282156 assert_eq!(
2129- bound_action(
2157+ find_configured_bind(
21302158 &bindings,
21312159 CompositorMod::Super,
21322160 Trigger::Keysym(Keysym::q),
@@ -2134,11 +2162,12 @@ mod tests {
21342162 logo: true,
21352163 ..Default::default()
21362164 }
2137- ),
2138- Some(Action::CloseWindow)
2165+ )
2166+ .as_ref(),
2167+ Some(&bindings.0[0])
21392168 );
21402169 assert_eq!(
2141- bound_action(
2170+ find_configured_bind(
21422171 &bindings,
21432172 CompositorMod::Super,
21442173 Trigger::Keysym(Keysym::q),
@@ -2148,7 +2177,7 @@ mod tests {
21482177 );
21492178
21502179 assert_eq!(
2151- bound_action(
2180+ find_configured_bind(
21522181 &bindings,
21532182 CompositorMod::Super,
21542183 Trigger::Keysym(Keysym::h),
@@ -2156,11 +2185,12 @@ mod tests {
21562185 logo: true,
21572186 ..Default::default()
21582187 }
2159- ),
2160- Some(Action::FocusColumnLeft)
2188+ )
2189+ .as_ref(),
2190+ Some(&bindings.0[1])
21612191 );
21622192 assert_eq!(
2163- bound_action(
2193+ find_configured_bind(
21642194 &bindings,
21652195 CompositorMod::Super,
21662196 Trigger::Keysym(Keysym::h),
@@ -2170,7 +2200,7 @@ mod tests {
21702200 );
21712201
21722202 assert_eq!(
2173- bound_action(
2203+ find_configured_bind(
21742204 &bindings,
21752205 CompositorMod::Super,
21762206 Trigger::Keysym(Keysym::j),
@@ -2182,17 +2212,18 @@ mod tests {
21822212 None,
21832213 );
21842214 assert_eq!(
2185- bound_action(
2215+ find_configured_bind(
21862216 &bindings,
21872217 CompositorMod::Super,
21882218 Trigger::Keysym(Keysym::j),
21892219 ModifiersState::default(),
2190- ),
2191- Some(Action::FocusWindowDown)
2220+ )
2221+ .as_ref(),
2222+ Some(&bindings.0[2])
21922223 );
21932224
21942225 assert_eq!(
2195- bound_action(
2226+ find_configured_bind(
21962227 &bindings,
21972228 CompositorMod::Super,
21982229 Trigger::Keysym(Keysym::k),
@@ -2200,11 +2231,12 @@ mod tests {
22002231 logo: true,
22012232 ..Default::default()
22022233 }
2203- ),
2204- Some(Action::FocusWindowUp)
2234+ )
2235+ .as_ref(),
2236+ Some(&bindings.0[3])
22052237 );
22062238 assert_eq!(
2207- bound_action(
2239+ find_configured_bind(
22082240 &bindings,
22092241 CompositorMod::Super,
22102242 Trigger::Keysym(Keysym::k),
@@ -2214,7 +2246,7 @@ mod tests {
22142246 );
22152247
22162248 assert_eq!(
2217- bound_action(
2249+ find_configured_bind(
22182250 &bindings,
22192251 CompositorMod::Super,
22202252 Trigger::Keysym(Keysym::l),
@@ -2223,11 +2255,12 @@ mod tests {
22232255 alt: true,
22242256 ..Default::default()
22252257 }
2226- ),
2227- Some(Action::FocusColumnRight)
2258+ )
2259+ .as_ref(),
2260+ Some(&bindings.0[4])
22282261 );
22292262 assert_eq!(
2230- bound_action(
2263+ find_configured_bind(
22312264 &bindings,
22322265 CompositorMod::Super,
22332266 Trigger::Keysym(Keysym::l),
src/ui/screenshot_ui.rsmodified+2 -2
@@ -316,12 +316,12 @@ impl ScreenshotUi {
316316 Ok((rect.size, copy.to_vec()))
317317 }
318318
319- pub fn action(&self, raw: Option<Keysym>, mods: ModifiersState) -> Option<Action> {
319+ pub fn action(&self, raw: Keysym, mods: ModifiersState) -> Option<Action> {
320320 if !matches!(self, Self::Open { .. }) {
321321 return None;
322322 }
323323
324- action(raw?, mods)
324+ action(raw, mods)
325325 }
326326
327327 pub fn selection_output(&self) -> Option<&Output> {