input: Make functions return the whole bind
Browse files at this commit6c08ba307aef03938e009601ac81bc140c38a1ddParent: 4b2fdd03 changed files +96 -63
- niri-config/src/lib.rs +1 -1
- src/input.rs +93 -60
- src/ui/screenshot_ui.rs +2 -2
niri-config/src/lib.rsmodified+1 -1
| @@ -716,7 +716,7 @@ impl PartialEq for Match { | ||
| 716 | 716 | #[derive(Debug, Default, PartialEq)] |
| 717 | 717 | pub struct Binds(pub Vec<Bind>); |
| 718 | 718 | |
| 719 | -#[derive(Debug, PartialEq)] | |
| 719 | +#[derive(Debug, Clone, PartialEq)] | |
| 720 | 720 | pub struct Bind { |
| 721 | 721 | pub key: Key, |
| 722 | 722 | pub action: Action, |
src/input.rsmodified+93 -60
| @@ -3,7 +3,7 @@ use std::collections::HashSet; | ||
| 3 | 3 | use std::time::Duration; |
| 4 | 4 | |
| 5 | 5 | use input::event::gesture::GestureEventCoordinates as _; |
| 6 | -use niri_config::{Action, Binds, Modifiers, Trigger}; | |
| 6 | +use niri_config::{Action, Bind, Binds, Key, Modifiers, Trigger}; | |
| 7 | 7 | use niri_ipc::LayoutSwitchTarget; |
| 8 | 8 | use smithay::backend::input::{ |
| 9 | 9 | AbsolutePositionEvent, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event, |
| @@ -248,7 +248,7 @@ impl State { | ||
| 248 | 248 | let time = Event::time_msec(&event); |
| 249 | 249 | let pressed = event.state() == KeyState::Pressed; |
| 250 | 250 | |
| 251 | - let Some(Some(action)) = self.niri.seat.get_keyboard().unwrap().input( | |
| 251 | + let Some(Some(bind)) = self.niri.seat.get_keyboard().unwrap().input( | |
| 252 | 252 | self, |
| 253 | 253 | event.key_code(), |
| 254 | 254 | event.state(), |
| @@ -289,7 +289,7 @@ impl State { | ||
| 289 | 289 | return; |
| 290 | 290 | } |
| 291 | 291 | |
| 292 | - self.do_action(action); | |
| 292 | + self.do_action(bind.action); | |
| 293 | 293 | } |
| 294 | 294 | |
| 295 | 295 | pub fn do_action(&mut self, action: Action) { |
| @@ -1089,22 +1089,23 @@ impl State { | ||
| 1089 | 1089 | if let Some(v120) = horizontal_amount_v120 { |
| 1090 | 1090 | let config = self.niri.config.borrow(); |
| 1091 | 1091 | 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); | |
| 1094 | 1095 | drop(config); |
| 1095 | 1096 | |
| 1096 | 1097 | // If we have a bind with current modifiers along the scroll direction, then |
| 1097 | 1098 | // 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() { | |
| 1099 | 1100 | let ticks = self.niri.horizontal_wheel_tracker.accumulate(v120); |
| 1100 | - if let Some(right) = action_right { | |
| 1101 | + if let Some(right) = bind_right { | |
| 1101 | 1102 | for _ in 0..ticks { |
| 1102 | - self.do_action(right.clone()); | |
| 1103 | + self.do_action(right.action.clone()); | |
| 1103 | 1104 | } |
| 1104 | 1105 | } |
| 1105 | - if let Some(left) = action_left { | |
| 1106 | + if let Some(left) = bind_left { | |
| 1106 | 1107 | for _ in ticks..0 { |
| 1107 | - self.do_action(left.clone()); | |
| 1108 | + self.do_action(left.action.clone()); | |
| 1108 | 1109 | } |
| 1109 | 1110 | } |
| 1110 | 1111 | return; |
| @@ -1116,20 +1117,20 @@ impl State { | ||
| 1116 | 1117 | if let Some(v120) = vertical_amount_v120 { |
| 1117 | 1118 | let config = self.niri.config.borrow(); |
| 1118 | 1119 | 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); | |
| 1121 | 1122 | drop(config); |
| 1122 | 1123 | |
| 1123 | - if action_up.is_some() || action_down.is_some() { | |
| 1124 | + if bind_up.is_some() || bind_down.is_some() { | |
| 1124 | 1125 | let ticks = self.niri.vertical_wheel_tracker.accumulate(v120); |
| 1125 | - if let Some(down) = action_down { | |
| 1126 | + if let Some(down) = bind_down { | |
| 1126 | 1127 | for _ in 0..ticks { |
| 1127 | - self.do_action(down.clone()); | |
| 1128 | + self.do_action(down.action.clone()); | |
| 1128 | 1129 | } |
| 1129 | 1130 | } |
| 1130 | - if let Some(up) = action_up { | |
| 1131 | + if let Some(up) = bind_up { | |
| 1131 | 1132 | for _ in ticks..0 { |
| 1132 | - self.do_action(up.clone()); | |
| 1133 | + self.do_action(up.action.clone()); | |
| 1133 | 1134 | } |
| 1134 | 1135 | } |
| 1135 | 1136 | return; |
| @@ -1687,7 +1688,7 @@ fn should_intercept_key( | ||
| 1687 | 1688 | mods: ModifiersState, |
| 1688 | 1689 | screenshot_ui: &ScreenshotUi, |
| 1689 | 1690 | disable_power_key_handling: bool, |
| 1690 | -) -> FilterResult<Option<Action>> { | |
| 1691 | +) -> FilterResult<Option<Bind>> { | |
| 1691 | 1692 | // Actions are only triggered on presses, release of the key |
| 1692 | 1693 | // shouldn't try to intercept anything unless we have marked |
| 1693 | 1694 | // the key to suppress. |
| @@ -1695,7 +1696,7 @@ fn should_intercept_key( | ||
| 1695 | 1696 | return FilterResult::Forward; |
| 1696 | 1697 | } |
| 1697 | 1698 | |
| 1698 | - let mut final_action = action( | |
| 1699 | + let mut final_bind = find_bind( | |
| 1699 | 1700 | bindings, |
| 1700 | 1701 | comp_mod, |
| 1701 | 1702 | modified, |
| @@ -1709,21 +1710,30 @@ fn should_intercept_key( | ||
| 1709 | 1710 | if screenshot_ui.is_open() { |
| 1710 | 1711 | let mut use_screenshot_ui_action = true; |
| 1711 | 1712 | |
| 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) { | |
| 1714 | 1715 | use_screenshot_ui_action = false; |
| 1715 | 1716 | } |
| 1716 | 1717 | } |
| 1717 | 1718 | |
| 1718 | 1719 | 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 | + } | |
| 1720 | 1730 | } |
| 1721 | 1731 | } |
| 1722 | 1732 | |
| 1723 | - match (final_action, pressed) { | |
| 1724 | - (Some(action), true) => { | |
| 1733 | + match (final_bind, pressed) { | |
| 1734 | + (Some(bind), true) => { | |
| 1725 | 1735 | suppressed_keys.insert(key_code); |
| 1726 | - FilterResult::Intercept(Some(action)) | |
| 1736 | + FilterResult::Intercept(Some(bind)) | |
| 1727 | 1737 | } |
| 1728 | 1738 | (_, false) => { |
| 1729 | 1739 | suppressed_keys.remove(&key_code); |
| @@ -1733,37 +1743,48 @@ fn should_intercept_key( | ||
| 1733 | 1743 | } |
| 1734 | 1744 | } |
| 1735 | 1745 | |
| 1736 | -fn action( | |
| 1746 | +fn find_bind( | |
| 1737 | 1747 | bindings: &Binds, |
| 1738 | 1748 | comp_mod: CompositorMod, |
| 1739 | 1749 | modified: Keysym, |
| 1740 | 1750 | raw: Option<Keysym>, |
| 1741 | 1751 | mods: ModifiersState, |
| 1742 | 1752 | disable_power_key_handling: bool, |
| 1743 | -) -> Option<Action> { | |
| 1753 | +) -> Option<Bind> { | |
| 1744 | 1754 | use keysyms::*; |
| 1745 | 1755 | |
| 1746 | 1756 | // Handle hardcoded binds. |
| 1747 | 1757 | #[allow(non_upper_case_globals)] // wat |
| 1748 | - match modified.raw() { | |
| 1758 | + let hardcoded_action = match modified.raw() { | |
| 1749 | 1759 | modified @ KEY_XF86Switch_VT_1..=KEY_XF86Switch_VT_12 => { |
| 1750 | 1760 | let vt = (modified - KEY_XF86Switch_VT_1 + 1) as i32; |
| 1751 | - return Some(Action::ChangeVt(vt)); | |
| 1761 | + Some(Action::ChangeVt(vt)) | |
| 1752 | 1762 | } |
| 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 | + }); | |
| 1755 | 1776 | } |
| 1756 | 1777 | |
| 1757 | 1778 | let trigger = Trigger::Keysym(raw?); |
| 1758 | - bound_action(bindings, comp_mod, trigger, mods) | |
| 1779 | + find_configured_bind(bindings, comp_mod, trigger, mods) | |
| 1759 | 1780 | } |
| 1760 | 1781 | |
| 1761 | -fn bound_action( | |
| 1782 | +fn find_configured_bind( | |
| 1762 | 1783 | bindings: &Binds, |
| 1763 | 1784 | comp_mod: CompositorMod, |
| 1764 | 1785 | trigger: Trigger, |
| 1765 | 1786 | mods: ModifiersState, |
| 1766 | -) -> Option<Action> { | |
| 1787 | +) -> Option<Bind> { | |
| 1767 | 1788 | // Handle configured binds. |
| 1768 | 1789 | let mut modifiers = Modifiers::empty(); |
| 1769 | 1790 | if mods.ctrl { |
| @@ -1800,7 +1821,7 @@ fn bound_action( | ||
| 1800 | 1821 | } |
| 1801 | 1822 | |
| 1802 | 1823 | if bind_modifiers == modifiers { |
| 1803 | - return Some(bind.action.clone()); | |
| 1824 | + return Some(bind.clone()); | |
| 1804 | 1825 | } |
| 1805 | 1826 | } |
| 1806 | 1827 | |
| @@ -1959,8 +1980,6 @@ pub fn apply_libinput_settings(config: &niri_config::Input, device: &mut input:: | ||
| 1959 | 1980 | |
| 1960 | 1981 | #[cfg(test)] |
| 1961 | 1982 | mod tests { |
| 1962 | - use niri_config::{Bind, Key}; | |
| 1963 | - | |
| 1964 | 1983 | use super::*; |
| 1965 | 1984 | |
| 1966 | 1985 | #[test] |
| @@ -2026,7 +2045,10 @@ mod tests { | ||
| 2026 | 2045 | let filter = close_key_event(&mut suppressed_keys, mods, true); |
| 2027 | 2046 | assert!(matches!( |
| 2028 | 2047 | filter, |
| 2029 | - FilterResult::Intercept(Some(Action::CloseWindow)) | |
| 2048 | + FilterResult::Intercept(Some(Bind { | |
| 2049 | + action: Action::CloseWindow, | |
| 2050 | + .. | |
| 2051 | + })) | |
| 2030 | 2052 | )); |
| 2031 | 2053 | assert!(suppressed_keys.contains(&close_key_code)); |
| 2032 | 2054 | |
| @@ -2057,7 +2079,10 @@ mod tests { | ||
| 2057 | 2079 | let filter = close_key_event(&mut suppressed_keys, mods, true); |
| 2058 | 2080 | assert!(matches!( |
| 2059 | 2081 | filter, |
| 2060 | - FilterResult::Intercept(Some(Action::CloseWindow)) | |
| 2082 | + FilterResult::Intercept(Some(Bind { | |
| 2083 | + action: Action::CloseWindow, | |
| 2084 | + .. | |
| 2085 | + })) | |
| 2061 | 2086 | )); |
| 2062 | 2087 | |
| 2063 | 2088 | let filter = none_key_event(&mut suppressed_keys, mods, true); |
| @@ -2074,7 +2099,10 @@ mod tests { | ||
| 2074 | 2099 | let filter = close_key_event(&mut suppressed_keys, mods, true); |
| 2075 | 2100 | assert!(matches!( |
| 2076 | 2101 | filter, |
| 2077 | - FilterResult::Intercept(Some(Action::CloseWindow)) | |
| 2102 | + FilterResult::Intercept(Some(Bind { | |
| 2103 | + action: Action::CloseWindow, | |
| 2104 | + .. | |
| 2105 | + })) | |
| 2078 | 2106 | )); |
| 2079 | 2107 | |
| 2080 | 2108 | mods = Default::default(); |
| @@ -2126,7 +2154,7 @@ mod tests { | ||
| 2126 | 2154 | ]); |
| 2127 | 2155 | |
| 2128 | 2156 | assert_eq!( |
| 2129 | - bound_action( | |
| 2157 | + find_configured_bind( | |
| 2130 | 2158 | &bindings, |
| 2131 | 2159 | CompositorMod::Super, |
| 2132 | 2160 | Trigger::Keysym(Keysym::q), |
| @@ -2134,11 +2162,12 @@ mod tests { | ||
| 2134 | 2162 | logo: true, |
| 2135 | 2163 | ..Default::default() |
| 2136 | 2164 | } |
| 2137 | - ), | |
| 2138 | - Some(Action::CloseWindow) | |
| 2165 | + ) | |
| 2166 | + .as_ref(), | |
| 2167 | + Some(&bindings.0[0]) | |
| 2139 | 2168 | ); |
| 2140 | 2169 | assert_eq!( |
| 2141 | - bound_action( | |
| 2170 | + find_configured_bind( | |
| 2142 | 2171 | &bindings, |
| 2143 | 2172 | CompositorMod::Super, |
| 2144 | 2173 | Trigger::Keysym(Keysym::q), |
| @@ -2148,7 +2177,7 @@ mod tests { | ||
| 2148 | 2177 | ); |
| 2149 | 2178 | |
| 2150 | 2179 | assert_eq!( |
| 2151 | - bound_action( | |
| 2180 | + find_configured_bind( | |
| 2152 | 2181 | &bindings, |
| 2153 | 2182 | CompositorMod::Super, |
| 2154 | 2183 | Trigger::Keysym(Keysym::h), |
| @@ -2156,11 +2185,12 @@ mod tests { | ||
| 2156 | 2185 | logo: true, |
| 2157 | 2186 | ..Default::default() |
| 2158 | 2187 | } |
| 2159 | - ), | |
| 2160 | - Some(Action::FocusColumnLeft) | |
| 2188 | + ) | |
| 2189 | + .as_ref(), | |
| 2190 | + Some(&bindings.0[1]) | |
| 2161 | 2191 | ); |
| 2162 | 2192 | assert_eq!( |
| 2163 | - bound_action( | |
| 2193 | + find_configured_bind( | |
| 2164 | 2194 | &bindings, |
| 2165 | 2195 | CompositorMod::Super, |
| 2166 | 2196 | Trigger::Keysym(Keysym::h), |
| @@ -2170,7 +2200,7 @@ mod tests { | ||
| 2170 | 2200 | ); |
| 2171 | 2201 | |
| 2172 | 2202 | assert_eq!( |
| 2173 | - bound_action( | |
| 2203 | + find_configured_bind( | |
| 2174 | 2204 | &bindings, |
| 2175 | 2205 | CompositorMod::Super, |
| 2176 | 2206 | Trigger::Keysym(Keysym::j), |
| @@ -2182,17 +2212,18 @@ mod tests { | ||
| 2182 | 2212 | None, |
| 2183 | 2213 | ); |
| 2184 | 2214 | assert_eq!( |
| 2185 | - bound_action( | |
| 2215 | + find_configured_bind( | |
| 2186 | 2216 | &bindings, |
| 2187 | 2217 | CompositorMod::Super, |
| 2188 | 2218 | Trigger::Keysym(Keysym::j), |
| 2189 | 2219 | ModifiersState::default(), |
| 2190 | - ), | |
| 2191 | - Some(Action::FocusWindowDown) | |
| 2220 | + ) | |
| 2221 | + .as_ref(), | |
| 2222 | + Some(&bindings.0[2]) | |
| 2192 | 2223 | ); |
| 2193 | 2224 | |
| 2194 | 2225 | assert_eq!( |
| 2195 | - bound_action( | |
| 2226 | + find_configured_bind( | |
| 2196 | 2227 | &bindings, |
| 2197 | 2228 | CompositorMod::Super, |
| 2198 | 2229 | Trigger::Keysym(Keysym::k), |
| @@ -2200,11 +2231,12 @@ mod tests { | ||
| 2200 | 2231 | logo: true, |
| 2201 | 2232 | ..Default::default() |
| 2202 | 2233 | } |
| 2203 | - ), | |
| 2204 | - Some(Action::FocusWindowUp) | |
| 2234 | + ) | |
| 2235 | + .as_ref(), | |
| 2236 | + Some(&bindings.0[3]) | |
| 2205 | 2237 | ); |
| 2206 | 2238 | assert_eq!( |
| 2207 | - bound_action( | |
| 2239 | + find_configured_bind( | |
| 2208 | 2240 | &bindings, |
| 2209 | 2241 | CompositorMod::Super, |
| 2210 | 2242 | Trigger::Keysym(Keysym::k), |
| @@ -2214,7 +2246,7 @@ mod tests { | ||
| 2214 | 2246 | ); |
| 2215 | 2247 | |
| 2216 | 2248 | assert_eq!( |
| 2217 | - bound_action( | |
| 2249 | + find_configured_bind( | |
| 2218 | 2250 | &bindings, |
| 2219 | 2251 | CompositorMod::Super, |
| 2220 | 2252 | Trigger::Keysym(Keysym::l), |
| @@ -2223,11 +2255,12 @@ mod tests { | ||
| 2223 | 2255 | alt: true, |
| 2224 | 2256 | ..Default::default() |
| 2225 | 2257 | } |
| 2226 | - ), | |
| 2227 | - Some(Action::FocusColumnRight) | |
| 2258 | + ) | |
| 2259 | + .as_ref(), | |
| 2260 | + Some(&bindings.0[4]) | |
| 2228 | 2261 | ); |
| 2229 | 2262 | assert_eq!( |
| 2230 | - bound_action( | |
| 2263 | + find_configured_bind( | |
| 2231 | 2264 | &bindings, |
| 2232 | 2265 | CompositorMod::Super, |
| 2233 | 2266 | Trigger::Keysym(Keysym::l), |
src/ui/screenshot_ui.rsmodified+2 -2
| @@ -316,12 +316,12 @@ impl ScreenshotUi { | ||
| 316 | 316 | Ok((rect.size, copy.to_vec())) |
| 317 | 317 | } |
| 318 | 318 | |
| 319 | - pub fn action(&self, raw: Option<Keysym>, mods: ModifiersState) -> Option<Action> { | |
| 319 | + pub fn action(&self, raw: Keysym, mods: ModifiersState) -> Option<Action> { | |
| 320 | 320 | if !matches!(self, Self::Open { .. }) { |
| 321 | 321 | return None; |
| 322 | 322 | } |
| 323 | 323 | |
| 324 | - action(raw?, mods) | |
| 324 | + action(raw, mods) | |
| 325 | 325 | } |
| 326 | 326 | |
| 327 | 327 | pub fn selection_output(&self) -> Option<&Output> { |