mirror of
https://github.com/alacritty/alacritty.git
synced 2026-11-20 21:23:23 +03:00
Add support for mouse wheel bindings
This commit is contained in:
1 parent
18372031d1
commit
83f6443537
5 files changed
+62
-38
No files matched your search
@@ -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
|
||||
|
||||
@@ -46,8 +46,8 @@ pub struct Binding<T> {
|
||||
/// Bindings that are triggered by a keyboard key.
|
||||
pub type KeyBinding = Binding<BindingKey>;
|
||||
|
||||
/// Bindings that are triggered by a mouse button.
|
||||
pub type MouseBinding = Binding<MouseButton>;
|
||||
/// Bindings that are triggered by a mouse event.
|
||||
pub type MouseBinding = Binding<MouseEvent>;
|
||||
|
||||
impl<T: Eq> Binding<T> {
|
||||
#[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<MouseBinding> {
|
||||
@@ -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<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
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<E>(self, value: i64) -> Result<MouseButtonWrapper, E>
|
||||
fn visit_i64<E>(self, value: i64) -> Result<MouseEvent, E>
|
||||
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<E>(self, value: u64) -> Result<MouseButtonWrapper, E>
|
||||
fn visit_u64<E>(self, value: u64) -> Result<MouseEvent, E>
|
||||
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<E>(self, value: &str) -> Result<MouseButtonWrapper, E>
|
||||
fn visit_str<E>(self, value: &str) -> Result<MouseEvent, E>
|
||||
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<BindingKey>,
|
||||
mouse: Option<MouseButton>,
|
||||
mouse: Option<MouseEvent>,
|
||||
mods: ModifiersState,
|
||||
mode: BindingMode,
|
||||
notmode: BindingMode,
|
||||
@@ -1005,7 +1011,7 @@ impl<'a> Deserialize<'a> for RawBinding {
|
||||
let mut action: Option<Action> = None;
|
||||
let mut mode: Option<BindingMode> = None;
|
||||
let mut not_mode: Option<BindingMode> = None;
|
||||
let mut mouse: Option<MouseButton> = None;
|
||||
let mut mouse: Option<MouseEvent> = None;
|
||||
let mut command: Option<Program> = None;
|
||||
|
||||
use de::Error;
|
||||
@@ -1100,7 +1106,7 @@ impl<'a> Deserialize<'a> for RawBinding {
|
||||
return Err(<V::Error as Error>::duplicate_field("mouse"));
|
||||
}
|
||||
|
||||
mouse = Some(map.next_value::<MouseButtonWrapper>()?.into_inner());
|
||||
mouse = Some(map.next_value::<MouseEvent>()?);
|
||||
},
|
||||
Field::Command => {
|
||||
if command.is_some() {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<T: EventListener, A: ActionContext<T>> Processor<T, A> {
|
||||
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<T: EventListener, A: ActionContext<T>> Processor<T, A> {
|
||||
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<T: EventListener, A: ActionContext<T>> Processor<T, A> {
|
||||
///
|
||||
/// 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<T: EventListener, A: ActionContext<T>> Processor<T, A> {
|
||||
|
||||
// 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.
|
||||
|
||||
@@ -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"_ | _<integer>_
|
||||
*mouse* = _"Middle"_ | _"Left"_ | _"Right"_ | _"Back"_ | _"Forward"_ | _"WheelUp"_ | _"WheelDown"_ | _<integer>_
|
||||
|
||||
Mouse button which needs to be pressed to trigger this binding.
|
||||
|
||||
|
||||
Reference in new issue
Block a user