diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ee17242..3c79a212 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,10 @@ Notable changes to the `alacritty_terminal` crate are documented in its - Fixed invalid logo SVG attributes +### Added + +- Ability to bind `WheelUp` and `WheelDown` in `mouse.bindings` + ### Changed - Don't highlight hints on hover when the mouse cursor is hidden diff --git a/alacritty/src/config/bindings.rs b/alacritty/src/config/bindings.rs index 4e140dd7..25903bea 100644 --- a/alacritty/src/config/bindings.rs +++ b/alacritty/src/config/bindings.rs @@ -46,8 +46,8 @@ pub struct Binding { /// Bindings that are triggered by a keyboard key. pub type KeyBinding = Binding; -/// Bindings that are triggered by a mouse button. -pub type MouseBinding = Binding; +/// Bindings that are triggered by a mouse event. +pub type MouseBinding = Binding; impl Binding { #[inline] @@ -363,6 +363,14 @@ pub enum MouseAction { ExpandSelection, } +/// Mouse binding specific events. +#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] +pub enum MouseEvent { + Button(MouseButton), + WheelUp, + WheelDown, +} + macro_rules! bindings { ( $ty:ident; @@ -404,7 +412,8 @@ macro_rules! trigger { (KeyBinding, $key:literal,) => {{ BindingKey::Keycode { key: Key::Character($key.into()), location: KeyLocation::Any } }}; (KeyBinding, $key:ident, $location:expr) => {{ BindingKey::Keycode { key: Key::Named(NamedKey::$key), location: $location } }}; (KeyBinding, $key:ident,) => {{ BindingKey::Keycode { key: Key::Named(NamedKey::$key), location: KeyLocation::Any } }}; - (MouseBinding, $base:ident::$button:ident,) => {{ $base::$button }}; + (MouseBinding, MouseButton::$button:ident,) => {{ MouseEvent::Button(MouseButton::$button) }}; + (MouseBinding, MouseEvent::$event:ident,) => {{ MouseEvent::$event }}; } pub fn default_mouse_bindings() -> Vec { @@ -832,15 +841,7 @@ impl<'a> Deserialize<'a> for ModeWrapper { } } -struct MouseButtonWrapper(MouseButton); - -impl MouseButtonWrapper { - fn into_inner(self) -> MouseButton { - self.0 - } -} - -impl<'a> Deserialize<'a> for MouseButtonWrapper { +impl<'a> Deserialize<'a> for MouseEvent { fn deserialize(deserializer: D) -> Result where D: Deserializer<'a>, @@ -848,42 +849,47 @@ impl<'a> Deserialize<'a> for MouseButtonWrapper { struct MouseButtonVisitor; impl Visitor<'_> for MouseButtonVisitor { - type Value = MouseButtonWrapper; + type Value = MouseEvent; fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("Left, Right, Middle, Back, Forward, or a number from 0 to 65536") + f.write_str( + "Left, Right, Middle, Back, Forward, WheelUp, WheelDown, or a number from 0 \ + to 65536", + ) } - fn visit_i64(self, value: i64) -> Result + fn visit_i64(self, value: i64) -> Result where E: de::Error, { match value { - 0..=65536 => Ok(MouseButtonWrapper(MouseButton::Other(value as u16))), + 0..=65536 => Ok(MouseEvent::Button(MouseButton::Other(value as u16))), _ => Err(E::invalid_value(Unexpected::Signed(value), &self)), } } - fn visit_u64(self, value: u64) -> Result + fn visit_u64(self, value: u64) -> Result where E: de::Error, { match value { - 0..=65536 => Ok(MouseButtonWrapper(MouseButton::Other(value as u16))), + 0..=65536 => Ok(MouseEvent::Button(MouseButton::Other(value as u16))), _ => Err(E::invalid_value(Unexpected::Unsigned(value), &self)), } } - fn visit_str(self, value: &str) -> Result + fn visit_str(self, value: &str) -> Result where E: de::Error, { match value { - "Left" => Ok(MouseButtonWrapper(MouseButton::Left)), - "Right" => Ok(MouseButtonWrapper(MouseButton::Right)), - "Middle" => Ok(MouseButtonWrapper(MouseButton::Middle)), - "Back" => Ok(MouseButtonWrapper(MouseButton::Back)), - "Forward" => Ok(MouseButtonWrapper(MouseButton::Forward)), + "Left" => Ok(MouseEvent::Button(MouseButton::Left)), + "Right" => Ok(MouseEvent::Button(MouseButton::Right)), + "Middle" => Ok(MouseEvent::Button(MouseButton::Middle)), + "Back" => Ok(MouseEvent::Button(MouseButton::Back)), + "Forward" => Ok(MouseEvent::Button(MouseButton::Forward)), + "WheelUp" => Ok(MouseEvent::WheelUp), + "WheelDown" => Ok(MouseEvent::WheelDown), _ => Err(E::invalid_value(Unexpected::Str(value), &self)), } } @@ -898,7 +904,7 @@ impl<'a> Deserialize<'a> for MouseButtonWrapper { #[derive(PartialEq, Eq)] struct RawBinding { key: Option, - mouse: Option, + mouse: Option, mods: ModifiersState, mode: BindingMode, notmode: BindingMode, @@ -1005,7 +1011,7 @@ impl<'a> Deserialize<'a> for RawBinding { let mut action: Option = None; let mut mode: Option = None; let mut not_mode: Option = None; - let mut mouse: Option = None; + let mut mouse: Option = None; let mut command: Option = None; use de::Error; @@ -1100,7 +1106,7 @@ impl<'a> Deserialize<'a> for RawBinding { return Err(::duplicate_field("mouse")); } - mouse = Some(map.next_value::()?.into_inner()); + mouse = Some(map.next_value::()?); }, Field::Command => { if command.is_some() { diff --git a/alacritty/src/config/mod.rs b/alacritty/src/config/mod.rs index 5d35c2e0..223b51c2 100644 --- a/alacritty/src/config/mod.rs +++ b/alacritty/src/config/mod.rs @@ -31,7 +31,7 @@ use crate::cli::Options; #[cfg(test)] pub use crate::config::bindings::Binding; pub use crate::config::bindings::{ - Action, BindingKey, BindingMode, KeyBinding, MouseAction, SearchAction, ViAction, + Action, BindingKey, BindingMode, KeyBinding, MouseAction, MouseEvent, SearchAction, ViAction, }; pub use crate::config::ui_config::UiConfig; use crate::logging::LOG_TARGET_CONFIG; diff --git a/alacritty/src/input/mod.rs b/alacritty/src/input/mod.rs index ddae21d8..26aa25d7 100644 --- a/alacritty/src/input/mod.rs +++ b/alacritty/src/input/mod.rs @@ -38,7 +38,9 @@ use alacritty_terminal::vte::ansi::{ClearMode, Handler}; use crate::clipboard::Clipboard; #[cfg(target_os = "macos")] use crate::config::window::Decorations; -use crate::config::{Action, BindingMode, MouseAction, SearchAction, UiConfig, ViAction}; +use crate::config::{ + Action, BindingMode, MouseAction, MouseEvent, SearchAction, UiConfig, ViAction, +}; use crate::display::hint::HintMatch; use crate::display::window::{ImeInhibitor, Window}; use crate::display::{Display, SizeInfo}; @@ -819,7 +821,16 @@ impl> Processor { let lines = (self.ctx.mouse().accumulated_scroll.y / height) as i32; if lines != 0 { - self.ctx.scroll(Scroll::Delta(lines)); + let event = if lines > 0 { MouseEvent::WheelUp } else { MouseEvent::WheelDown }; + if self.process_mouse_bindings(event) { + // Repeat for remaining number of lines. + for _ in 1..lines.unsigned_abs() { + self.process_mouse_bindings(event); + } + } else { + // Only scroll if no wheel binding was found. + self.ctx.scroll(Scroll::Delta(lines)); + } } } @@ -1025,7 +1036,7 @@ impl> Processor { ElementState::Pressed => { // Process mouse press before bindings to update the `click_state`. self.on_mouse_press(button); - self.process_mouse_bindings(button); + self.process_mouse_bindings(MouseEvent::Button(button)); }, ElementState::Released => self.on_mouse_release(button), } @@ -1036,7 +1047,7 @@ impl> Processor { /// /// The provided mode, mods, and key must match what is allowed by a binding /// for its action to be executed. - fn process_mouse_bindings(&mut self, button: MouseButton) { + fn process_mouse_bindings(&mut self, event: MouseEvent) -> bool { let mode = BindingMode::new(self.ctx.terminal().mode(), self.ctx.search_active()); let mouse_mode = self.ctx.mouse_mode(); let mods = self.ctx.modifiers().state(); @@ -1044,24 +1055,27 @@ impl> Processor { // If mouse mode is active, also look for bindings without shift. let fallback_allowed = mouse_mode && mods.contains(ModifiersState::SHIFT); - let mut exact_match_found = false; + let mut match_found: bool = false; for binding in &mouse_bindings { // Don't trigger normal bindings in mouse mode unless Shift is pressed. - if binding.is_triggered_by(mode, mods, &button) && (fallback_allowed || !mouse_mode) { + if binding.is_triggered_by(mode, mods, &event) && (fallback_allowed || !mouse_mode) { binding.action.execute(&mut self.ctx); - exact_match_found = true; + match_found = true; } } - if fallback_allowed && !exact_match_found { + if fallback_allowed && !match_found { let fallback_mods = mods & !ModifiersState::SHIFT; for binding in &mouse_bindings { - if binding.is_triggered_by(mode, fallback_mods, &button) { + if binding.is_triggered_by(mode, fallback_mods, &event) { binding.action.execute(&mut self.ctx); + match_found = true; } } } + + match_found } /// Check mouse icon state in relation to the message bar. diff --git a/extra/man/alacritty.5.scd b/extra/man/alacritty.5.scd index af4a79fa..109fe021 100644 --- a/extra/man/alacritty.5.scd +++ b/extra/man/alacritty.5.scd @@ -645,7 +645,7 @@ This section documents the *[mouse]* table of the configuration file. the event, actions for the event without the `Shift` modifier are triggered instead. - *mouse* = _"Middle"_ | _"Left"_ | _"Right"_ | _"Back"_ | _"Forward"_ | __ + *mouse* = _"Middle"_ | _"Left"_ | _"Right"_ | _"Back"_ | _"Forward"_ | _"WheelUp"_ | _"WheelDown"_ | __ Mouse button which needs to be pressed to trigger this binding.