1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-24 22:01:47 +03:00

Add new-tab-button-click event

refs: #323
refs: https://github.com/wez/wezterm/discussions/3364
This commit is contained in:
Wez Furlong 2023-03-25 13:26:16 -07:00
parent 2a2b075d71
commit 133faf8305
No known key found for this signature in database
GPG Key ID: 7A7F66A31EC9B387
3 changed files with 126 additions and 8 deletions

View File

@ -31,6 +31,8 @@ As features stabilize some brief notes about them will accumulate here.
* macOS:
[macos_window_background_blur](config/lua/config/macos_window_background_blur.md)
enables a nice translucent window effect. Thanks to @Gkirito! #3344
* [new-tab-button-click event](config/lua/window-events/new-tab-button-click.md)
allows overriding the effect of clicking the New Tab button. #323
#### Fixed
* ssh ProxyCommand didn't parse command lines containing `=` correctly. #3307

View File

@ -0,0 +1,62 @@
# `new-tab-button-click`
{{since('nightly')}}
The `new-tab-button-click` event is emitted when the user clicks on the
"new tab" button in the tab bar. This is the `+` button that is drawn
to the right of the last tab.
The first event parameter is a [`window` object](../window/index.md) that
represents the gui window.
The second event parameter is a [`pane` object](../pane/index.md) that
represents the active pane in the window.
The third event parameter is an indication of which mouse button was clicked.
The following values are possible:
* `"Left"` - the left mouse button
* `"Right"` - the right mouse button
* `"Middle"` - the middle mouse button
The last event parameter is a [KeyAssignment](../keyassignment/index.md) which
encodes the default, built-in action that wezterm will take. It may be `nil`
in the case where wezterm would not take any action.
You may take any action you wish in this event handler.
If you return `false` then you will prevent wezterm from carrying out its
default action.
Otherwise, wezterm will proceed to perform that action once your event
handler returns.
This following two examples are equivalent in functionality:
```lua
wezterm.on(
'new-tab-button-click',
function(window, pane, button, default_action)
-- just log the default action and allow wezterm to perform it
wezterm.log_info('new-tab', window, pane, button, default_action)
end
)
```
```lua
wezterm.on(
'new-tab-button-click',
function(window, pane, button, default_action)
wezterm.log_info('new-tab', window, pane, button, default_action)
-- We're explicitly going to perform the default action
if default_action then
window:perform_action(default_action, pane)
end
-- and tell wezterm that we handled the event so that it doesn't
-- perform it a second time.
return false
end
)
```
See also [window:perform_action()](../window/perform_action.md).

View File

@ -1,17 +1,22 @@
use crate::termwindow::TermWindowNotif;
use crate::tabbar::TabBarItem;
use crate::termwindow::keyevent::window_mods_to_termwiz_mods;
use crate::termwindow::{MouseCapture, PositionedSplit, ScrollHit, UIItem, UIItemType, TMB};
use crate::termwindow::{
GuiWin, MouseCapture, PositionedSplit, ScrollHit, UIItem, UIItemType, TMB,
};
use ::window::{
MouseButtons as WMB, MouseCursor, MouseEvent, MouseEventKind as WMEK, MousePress, WindowOps,
WindowState,
};
use config::keyassignment::{MouseEventTrigger, SpawnTabDomain};
use config::keyassignment::{KeyAssignment, MouseEventTrigger, SpawnTabDomain};
use config::MouseEventAltScreen;
use mux::pane::{Pane, WithPaneLines};
use mux::tab::SplitDirection;
use mux::Mux;
use mux_lua::MuxPane;
use std::convert::TryInto;
use std::ops::Sub;
use std::rc::Rc;
use std::sync::Arc;
use std::time::Duration;
use termwiz::hyperlink::Hyperlink;
@ -398,6 +403,55 @@ impl super::TermWindow {
context.set_cursor(Some(MouseCursor::Arrow));
}
fn do_new_tab_button_click(&mut self, button: MousePress) {
let pane = match self.get_active_pane_or_overlay() {
Some(pane) => pane,
None => return,
};
let action = match button {
MousePress::Left => Some(KeyAssignment::SpawnTab(SpawnTabDomain::CurrentPaneDomain)),
MousePress::Right => Some(KeyAssignment::ShowLauncher),
MousePress::Middle => None,
};
async fn dispatch_new_tab_button(
lua: Option<Rc<mlua::Lua>>,
window: GuiWin,
pane: MuxPane,
button: MousePress,
action: Option<KeyAssignment>,
) -> anyhow::Result<()> {
let default_action = match lua {
Some(lua) => {
let args = lua.pack_multi((
window.clone(),
pane,
format!("{button:?}"),
action.clone(),
))?;
config::lua::emit_event(&lua, ("new-tab-button-click".to_string(), args))
.await
.map_err(|e| {
log::error!("while processing new-tab-button-click event: {:#}", e);
e
})?
}
None => true,
};
if let (true, Some(assignment)) = (default_action, action) {
log::info!("do it {assignment:?}");
window.window.notify(TermWindowNotif::PerformAssignment{ pane_id: pane.0, assignment });
}
Ok(())
}
let window = GuiWin::new(self);
let pane = MuxPane(pane.pane_id());
promise::spawn::spawn(config::with_lua_config_on_main_thread(move |lua| {
dispatch_new_tab_button(lua, window, pane, button, action)
}))
.detach();
}
pub fn mouse_event_tab_bar(
&mut self,
item: TabBarItem,
@ -410,7 +464,7 @@ impl super::TermWindow {
self.activate_tab(tab_idx as isize).ok();
}
TabBarItem::NewTabButton { .. } => {
self.spawn_tab(&SpawnTabDomain::CurrentPaneDomain);
self.do_new_tab_button_click(MousePress::Left);
}
TabBarItem::None | TabBarItem::LeftStatus | TabBarItem::RightStatus => {
// Potentially starting a drag by the tab bar
@ -427,17 +481,17 @@ impl super::TermWindow {
TabBarItem::Tab { tab_idx, .. } => {
self.close_specific_tab(tab_idx, true);
}
TabBarItem::NewTabButton { .. }
| TabBarItem::None
| TabBarItem::LeftStatus
| TabBarItem::RightStatus => {}
TabBarItem::NewTabButton { .. } => {
self.do_new_tab_button_click(MousePress::Middle);
}
TabBarItem::None | TabBarItem::LeftStatus | TabBarItem::RightStatus => {}
},
WMEK::Press(MousePress::Right) => match item {
TabBarItem::Tab { .. } => {
self.show_tab_navigator();
}
TabBarItem::NewTabButton { .. } => {
self.show_launcher();
self.do_new_tab_button_click(MousePress::Right);
}
TabBarItem::None | TabBarItem::LeftStatus | TabBarItem::RightStatus => {}
},