1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-10 15:04:32 +03:00

lua: WheelUp and WheelDown support for mouse_bindings

This commit is contained in:
Funami580 2022-07-23 23:41:58 +02:00 committed by Wez Furlong
parent ae2e78d87c
commit cf423dc614
4 changed files with 87 additions and 8 deletions

View File

@ -132,6 +132,7 @@ impl Default for Pattern {
/// A mouse event that can trigger an action
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, FromDynamic, ToDynamic)]
#[dynamic(try_from = "String")]
pub enum MouseEventTrigger {
/// Mouse button is pressed. streak is how many times in a row
/// it was pressed.
@ -144,6 +145,34 @@ pub enum MouseEventTrigger {
Up { streak: usize, button: MouseButton },
}
impl MouseEventTrigger {
pub const fn default_wheel_up() -> MouseEventTrigger {
MouseEventTrigger::Down {
streak: 1,
button: MouseButton::WheelUp(1),
}
}
pub const fn default_wheel_down() -> MouseEventTrigger {
MouseEventTrigger::Down {
streak: 1,
button: MouseButton::WheelDown(1),
}
}
}
impl TryFrom<String> for MouseEventTrigger {
type Error = String;
fn try_from(s: String) -> Result<MouseEventTrigger, String> {
match &*s {
"WheelUp" => Ok(Self::default_wheel_up()),
"WheelDown" => Ok(Self::default_wheel_down()),
_ => Err(format!("Could not parse '{}'", s)),
}
}
}
/// When spawning a tab, specify which domain should be used to
/// host/spawn that tab.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, FromDynamic, ToDynamic)]

View File

@ -133,6 +133,15 @@ impl UserData for GuiWin {
Ok(text)
},
);
methods.add_async_method("current_event", |lua, this, _: ()| async move {
let (tx, rx) = smol::channel::bounded(1);
this.window
.notify(TermWindowNotif::Apply(Box::new(move |term_window| {
tx.try_send(term_window.current_event.to_dynamic()).ok();
})));
let result = rx.recv().await.map_err(mlua::Error::external)?;
luahelper::dynamic_to_lua_value(lua, result)
});
methods.add_method(
"perform_action",
|_, this, (assignment, pane): (KeyAssignment, PaneObject)| {

View File

@ -53,6 +53,7 @@ use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use termwiz::hyperlink::Hyperlink;
use termwiz::surface::SequenceNo;
use wezterm_dynamic::Value;
use wezterm_font::FontConfiguration;
use wezterm_gui_subcommands::GuiPosition;
use wezterm_term::color::ColorPalette;
@ -408,6 +409,7 @@ pub struct TermWindow {
modal: RefCell<Option<Rc<dyn Modal>>>,
event_states: HashMap<String, EventState>,
pub current_event: Option<Value>,
has_animation: RefCell<Option<Instant>>,
/// We use this to attempt to do something reasonable
/// if we run out of texture space
@ -695,6 +697,7 @@ impl TermWindow {
None,
)),
event_states: HashMap::new(),
current_event: None,
has_animation: RefCell::new(None),
scheduled_animation: RefCell::new(None),
allow_images: true,

View File

@ -14,7 +14,8 @@ use std::ops::Sub;
use std::rc::Rc;
use std::sync::Arc;
use std::time::Duration;
use wezterm_term::input::MouseEventKind as TMEK;
use wezterm_dynamic::ToDynamic;
use wezterm_term::input::{MouseButton, MouseEventKind as TMEK};
use wezterm_term::{ClickPosition, LastMouseClick, StableRowIndex};
impl super::TermWindow {
@ -754,7 +755,11 @@ impl super::TermWindow {
None
}
}
WMEK::VertWheel(amount) if !pane.is_mouse_grabbed() && !pane.is_alt_screen_active() => {
WMEK::VertWheel(amount)
if !pane.is_mouse_grabbed()
&& !pane.is_alt_screen_active()
&& event.modifiers.is_empty() =>
{
// adjust viewport
let dims = pane.get_dimensions();
let position = self
@ -765,7 +770,18 @@ impl super::TermWindow {
context.invalidate();
return;
}
WMEK::VertWheel(_) | WMEK::HorzWheel(_) => None,
WMEK::VertWheel(amount) => Some(match *amount {
0 => return,
1.. => MouseEventTrigger::Down {
streak: 1,
button: MouseButton::WheelUp(*amount as usize),
},
_ => MouseEventTrigger::Down {
streak: 1,
button: MouseButton::WheelDown(-amount as usize),
},
}),
WMEK::HorzWheel(_) => None,
};
if allow_action
@ -774,7 +790,8 @@ impl super::TermWindow {
.modifiers
.contains(self.config.bypass_mouse_reporting_modifiers))
{
if let Some(event_trigger_type) = event_trigger_type {
if let Some(mut event_trigger_type) = event_trigger_type {
self.current_event = Some(event_trigger_type.to_dynamic());
let mut modifiers = event.modifiers;
// Since we use shift to force assessing the mouse bindings, pretend
@ -785,10 +802,31 @@ impl super::TermWindow {
}
}
if let Some(action) = self
.input_map
.lookup_mouse(event_trigger_type.clone(), modifiers)
{
// normalize delta and streak to make mouse assignment
// easier to wrangle
match event_trigger_type {
MouseEventTrigger::Down {
ref mut streak,
button:
MouseButton::WheelUp(ref mut delta) | MouseButton::WheelDown(ref mut delta),
}
| MouseEventTrigger::Up {
ref mut streak,
button:
MouseButton::WheelUp(ref mut delta) | MouseButton::WheelDown(ref mut delta),
}
| MouseEventTrigger::Drag {
ref mut streak,
button:
MouseButton::WheelUp(ref mut delta) | MouseButton::WheelDown(ref mut delta),
} => {
*streak = 1;
*delta = 1;
}
_ => {}
};
if let Some(action) = self.input_map.lookup_mouse(event_trigger_type, modifiers) {
self.perform_key_assignment(&pane, &action).ok();
return;
}