Make binds accept wheel names

This commit is contained in:
Ivan Molodetskikh
2024-03-22 10:36:19 +04:00
parent 977f1487c2
commit a0c8c39b06
3 changed files with 59 additions and 23 deletions
+37 -9
View File
@@ -724,10 +724,19 @@ pub struct Bind {
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub struct Key { pub struct Key {
pub keysym: Keysym, pub trigger: Trigger,
pub modifiers: Modifiers, pub modifiers: Modifiers,
} }
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum Trigger {
Keysym(Keysym),
WheelDown,
WheelUp,
WheelLeft,
WheelRight,
}
bitflags! { bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Modifiers : u8 { pub struct Modifiers : u8 {
@@ -1499,12 +1508,23 @@ impl FromStr for Key {
} }
} }
let trigger = if key.eq_ignore_ascii_case("WheelDown") {
Trigger::WheelDown
} else if key.eq_ignore_ascii_case("WheelUp") {
Trigger::WheelUp
} else if key.eq_ignore_ascii_case("WheelLeft") {
Trigger::WheelLeft
} else if key.eq_ignore_ascii_case("WheelRight") {
Trigger::WheelRight
} else {
let keysym = keysym_from_name(key, KEYSYM_CASE_INSENSITIVE); let keysym = keysym_from_name(key, KEYSYM_CASE_INSENSITIVE);
if keysym.raw() == KEY_NoSymbol { if keysym.raw() == KEY_NoSymbol {
return Err(miette!("invalid key: {key}")); return Err(miette!("invalid key: {key}"));
} }
Trigger::Keysym(keysym)
};
Ok(Key { keysym, modifiers }) Ok(Key { trigger, modifiers })
} }
} }
@@ -1712,6 +1732,7 @@ mod tests {
Mod+Comma { consume-window-into-column; } Mod+Comma { consume-window-into-column; }
Mod+1 { focus-workspace 1; } Mod+1 { focus-workspace 1; }
Mod+Shift+E { quit skip-confirmation=true; } Mod+Shift+E { quit skip-confirmation=true; }
Mod+WheelDown { focus-workspace-down; }
} }
debug { debug {
@@ -1895,53 +1916,60 @@ mod tests {
binds: Binds(vec![ binds: Binds(vec![
Bind { Bind {
key: Key { key: Key {
keysym: Keysym::t, trigger: Trigger::Keysym(Keysym::t),
modifiers: Modifiers::COMPOSITOR, modifiers: Modifiers::COMPOSITOR,
}, },
action: Action::Spawn(vec!["alacritty".to_owned()]), action: Action::Spawn(vec!["alacritty".to_owned()]),
}, },
Bind { Bind {
key: Key { key: Key {
keysym: Keysym::q, trigger: Trigger::Keysym(Keysym::q),
modifiers: Modifiers::COMPOSITOR, modifiers: Modifiers::COMPOSITOR,
}, },
action: Action::CloseWindow, action: Action::CloseWindow,
}, },
Bind { Bind {
key: Key { key: Key {
keysym: Keysym::h, trigger: Trigger::Keysym(Keysym::h),
modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT, modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT,
}, },
action: Action::FocusMonitorLeft, action: Action::FocusMonitorLeft,
}, },
Bind { Bind {
key: Key { key: Key {
keysym: Keysym::l, trigger: Trigger::Keysym(Keysym::l),
modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT | Modifiers::CTRL, modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT | Modifiers::CTRL,
}, },
action: Action::MoveWindowToMonitorRight, action: Action::MoveWindowToMonitorRight,
}, },
Bind { Bind {
key: Key { key: Key {
keysym: Keysym::comma, trigger: Trigger::Keysym(Keysym::comma),
modifiers: Modifiers::COMPOSITOR, modifiers: Modifiers::COMPOSITOR,
}, },
action: Action::ConsumeWindowIntoColumn, action: Action::ConsumeWindowIntoColumn,
}, },
Bind { Bind {
key: Key { key: Key {
keysym: Keysym::_1, trigger: Trigger::Keysym(Keysym::_1),
modifiers: Modifiers::COMPOSITOR, modifiers: Modifiers::COMPOSITOR,
}, },
action: Action::FocusWorkspace(1), action: Action::FocusWorkspace(1),
}, },
Bind { Bind {
key: Key { key: Key {
keysym: Keysym::e, trigger: Trigger::Keysym(Keysym::e),
modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT, modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT,
}, },
action: Action::Quit(true), action: Action::Quit(true),
}, },
Bind {
key: Key {
trigger: Trigger::WheelDown,
modifiers: Modifiers::COMPOSITOR,
},
action: Action::FocusWorkspaceDown,
},
]), ]),
debug: DebugConfig { debug: DebugConfig {
render_drm_device: Some(PathBuf::from("/dev/dri/renderD129")), render_drm_device: Some(PathBuf::from("/dev/dri/renderD129")),
+8 -8
View File
@@ -3,7 +3,7 @@ use std::collections::HashSet;
use std::time::Duration; use std::time::Duration;
use input::event::gesture::GestureEventCoordinates as _; use input::event::gesture::GestureEventCoordinates as _;
use niri_config::{Action, Binds, Modifiers}; use niri_config::{Action, Binds, Modifiers, Trigger};
use niri_ipc::LayoutSwitchTarget; use niri_ipc::LayoutSwitchTarget;
use smithay::backend::input::{ use smithay::backend::input::{
AbsolutePositionEvent, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event, AbsolutePositionEvent, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event,
@@ -1731,7 +1731,7 @@ fn bound_action(
let raw = raw?; let raw = raw?;
for bind in &bindings.0 { for bind in &bindings.0 {
if bind.key.keysym != raw { if bind.key.trigger != Trigger::Keysym(raw) {
continue; continue;
} }
@@ -1911,7 +1911,7 @@ mod tests {
let close_keysym = Keysym::q; let close_keysym = Keysym::q;
let bindings = Binds(vec![Bind { let bindings = Binds(vec![Bind {
key: Key { key: Key {
keysym: close_keysym, trigger: Trigger::Keysym(close_keysym),
modifiers: Modifiers::COMPOSITOR | Modifiers::CTRL, modifiers: Modifiers::COMPOSITOR | Modifiers::CTRL,
}, },
action: Action::CloseWindow, action: Action::CloseWindow,
@@ -2033,35 +2033,35 @@ mod tests {
let bindings = Binds(vec![ let bindings = Binds(vec![
Bind { Bind {
key: Key { key: Key {
keysym: Keysym::q, trigger: Trigger::Keysym(Keysym::q),
modifiers: Modifiers::COMPOSITOR, modifiers: Modifiers::COMPOSITOR,
}, },
action: Action::CloseWindow, action: Action::CloseWindow,
}, },
Bind { Bind {
key: Key { key: Key {
keysym: Keysym::h, trigger: Trigger::Keysym(Keysym::h),
modifiers: Modifiers::SUPER, modifiers: Modifiers::SUPER,
}, },
action: Action::FocusColumnLeft, action: Action::FocusColumnLeft,
}, },
Bind { Bind {
key: Key { key: Key {
keysym: Keysym::j, trigger: Trigger::Keysym(Keysym::j),
modifiers: Modifiers::empty(), modifiers: Modifiers::empty(),
}, },
action: Action::FocusWindowDown, action: Action::FocusWindowDown,
}, },
Bind { Bind {
key: Key { key: Key {
keysym: Keysym::k, trigger: Trigger::Keysym(Keysym::k),
modifiers: Modifiers::COMPOSITOR | Modifiers::SUPER, modifiers: Modifiers::COMPOSITOR | Modifiers::SUPER,
}, },
action: Action::FocusWindowUp, action: Action::FocusWindowUp,
}, },
Bind { Bind {
key: Key { key: Key {
keysym: Keysym::l, trigger: Trigger::Keysym(Keysym::l),
modifiers: Modifiers::SUPER | Modifiers::ALT, modifiers: Modifiers::SUPER | Modifiers::ALT,
}, },
action: Action::FocusColumnRight, action: Action::FocusColumnRight,
+10 -2
View File
@@ -4,7 +4,7 @@ use std::collections::HashMap;
use std::iter::zip; use std::iter::zip;
use std::rc::Rc; use std::rc::Rc;
use niri_config::{Action, Config, Key, Modifiers}; use niri_config::{Action, Config, Key, Modifiers, Trigger};
use pangocairo::cairo::{self, ImageSurface}; use pangocairo::cairo::{self, ImageSurface};
use pangocairo::pango::{AttrColor, AttrInt, AttrList, AttrString, FontDescription, Weight}; use pangocairo::pango::{AttrColor, AttrInt, AttrList, AttrString, FontDescription, Weight};
use smithay::backend::renderer::element::memory::{ use smithay::backend::renderer::element::memory::{
@@ -414,7 +414,15 @@ fn key_name(comp_mod: CompositorMod, key: &Key) -> String {
if key.modifiers.contains(Modifiers::CTRL) { if key.modifiers.contains(Modifiers::CTRL) {
name.push_str("Ctrl + "); name.push_str("Ctrl + ");
} }
name.push_str(&prettify_keysym_name(&keysym_get_name(key.keysym)));
let pretty = match key.trigger {
Trigger::Keysym(keysym) => prettify_keysym_name(&keysym_get_name(keysym)),
Trigger::WheelDown => String::from("Wheel Down"),
Trigger::WheelUp => String::from("Wheel Up"),
Trigger::WheelLeft => String::from("Wheel Left"),
Trigger::WheelRight => String::from("Wheel Right"),
};
name.push_str(&pretty);
name name
} }