Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

Add native context menu support and terminal menu

RoversX457a0830e49e
diff --git a/wezterm-gui/src/termwindow/mouseevent.rs b/wezterm-gui/src/termwindow/mouseevent.rs
index 65289d44..07bc58e2 100644
--- a/wezterm-gui/src/termwindow/mouseevent.rs
+++ b/wezterm-gui/src/termwindow/mouseevent.rs
@@ -3,10 +3,13 @@ use crate::termwindow::{
GuiWin, MouseCapture, PositionedSplit, ScrollHit, TermWindowNotif, UIItem, UIItemType, TMB,
};
use ::window::{
- MouseButtons as WMB, MouseCursor, MouseEvent, MouseEventKind as WMEK, MousePress,
- WindowDecorations, WindowOps, WindowState,
+ ContextMenuItem, MouseButtons as WMB, MouseCursor, MouseEvent, MouseEventKind as WMEK,
+ MousePress, WindowDecorations, WindowOps, WindowState,
+};
+use config::keyassignment::{
+ ClipboardPasteSource, KeyAssignment, MouseEventTrigger, PaneDirection, SpawnCommand,
+ SpawnTabDomain, SplitPane, SplitSize,
};
-use config::keyassignment::{KeyAssignment, MouseEventTrigger, SpawnTabDomain};
use config::MouseEventAltScreen;
use mux::pane::{Pane, WithPaneLines};
use mux::tab::SplitDirection;
@@ -645,6 +648,40 @@ impl super::TermWindow {
}
}
+ fn terminal_context_menu_items(&self) -> Vec<ContextMenuItem> {
+ fn split_item(label: &str, icon: &str, direction: PaneDirection) -> ContextMenuItem {
+ ContextMenuItem::item_with_icon(
+ label,
+ icon,
+ KeyAssignment::SplitPane(SplitPane {
+ direction,
+ size: SplitSize::Percent(50),
+ command: SpawnCommand::default(),
+ top_level: false,
+ }),
+ )
+ }
+
+ vec![
+ ContextMenuItem::item_with_icon(
+ "Paste",
+ "doc.on.clipboard",
+ KeyAssignment::PasteFrom(ClipboardPasteSource::Clipboard),
+ ),
+ ContextMenuItem::Separator,
+ split_item("Split Right", "rectangle.split.2x1", PaneDirection::Right),
+ split_item("Split Left", "rectangle.split.2x1", PaneDirection::Left),
+ split_item("Split Down", "rectangle.split.1x2", PaneDirection::Down),
+ split_item("Split Up", "rectangle.split.1x2", PaneDirection::Up),
+ ContextMenuItem::Separator,
+ ContextMenuItem::item_with_icon(
+ "Reset Terminal",
+ "arrow.clockwise",
+ KeyAssignment::ResetTerminal,
+ ),
+ ]
+ }
+
fn mouse_event_terminal(
&mut self,
mut pane: Arc<dyn Pane>,
@@ -988,6 +1025,14 @@ impl super::TermWindow {
}
}
+ if allow_action
+ && matches!(event.kind, WMEK::Release(MousePress::Right))
+ && !pane.is_mouse_grabbed()
+ {
+ context.show_context_menu(event.coords, self.terminal_context_menu_items());
+ return;
+ }
+
let mouse_event = wezterm_term::MouseEvent {
kind: match event.kind {
WMEK::Move => TMEK::Move,
diff --git a/window/src/lib.rs b/window/src/lib.rs
index 29c670b3..2b1f7cb2 100644
--- a/window/src/lib.rs
+++ b/window/src/lib.rs
@@ -57,6 +57,38 @@ pub struct Dimensions {
pub dpi: usize,
}
+#[derive(Debug, Clone)]
+pub enum ContextMenuItem {
+ Item {
+ label: String,
+ icon: Option<String>,
+ action: config::keyassignment::KeyAssignment,
+ },
+ Separator,
+}
+
+impl ContextMenuItem {
+ pub fn item(label: impl Into<String>, action: config::keyassignment::KeyAssignment) -> Self {
+ Self::Item {
+ label: label.into(),
+ icon: None,
+ action,
+ }
+ }
+
+ pub fn item_with_icon(
+ label: impl Into<String>,
+ icon: impl Into<String>,
+ action: config::keyassignment::KeyAssignment,
+ ) -> Self {
+ Self::Item {
+ label: label.into(),
+ icon: Some(icon.into()),
+ action,
+ }
+ }
+}
+
pub type ULength = euclid::Length<usize, PixelUnit>;
pub type Rect = euclid::Rect<isize, PixelUnit>;
pub type RectF = euclid::Rect<f32, PixelUnit>;
@@ -273,6 +305,9 @@ pub trait WindowOps {
/// Change the cursor
fn set_cursor(&self, cursor: Option<MouseCursor>);
+ /// Show a native context menu at the specified client-area pixel coordinate.
+ fn show_context_menu(&self, _coords: Point, _items: Vec<ContextMenuItem>) {}
+
/// Invalidate the window so that the entire client area will
/// be repainted shortly
fn invalidate(&self);
diff --git a/window/src/os/macos/menu.rs b/window/src/os/macos/menu.rs
index 338899e3..1671e9c6 100644
--- a/window/src/os/macos/menu.rs
+++ b/window/src/os/macos/menu.rs
@@ -4,7 +4,7 @@ pub use cocoa::appkit::NSEventModifierFlags;
use cocoa::appkit::{NSApp, NSApplication, NSMenu, NSMenuItem};
pub use cocoa::base::SEL;
use cocoa::base::{id, nil};
-use cocoa::foundation::NSInteger;
+use cocoa::foundation::{NSInteger, NSPoint};
use config::keyassignment::KeyAssignment;
use objc::declare::ClassDecl;
use objc::rc::StrongPtr;
@@ -96,6 +96,18 @@ impl Menu {
}
}
+ pub fn pop_up_at(&self, view: id, x: f64, y: f64) {
+ unsafe {
+ let point = NSPoint::new(x, y);
+ let _: BOOL = msg_send![
+ *self.menu,
+ popUpMenuPositioningItem:nil
+ atLocation:point
+ inView:view
+ ];
+ }
+ }
+
pub fn item_with_title(&self, title: &str) -> Option<MenuItem> {
unsafe {
let item: id = msg_send![*self.menu, itemWithTitle:*nsstring(title)];
@@ -212,7 +224,7 @@ impl MenuItem {
}
pub fn new_separator() -> Self {
- let item = unsafe { StrongPtr::new(NSMenuItem::separatorItem(nil)) };
+ let item = unsafe { StrongPtr::retain(NSMenuItem::separatorItem(nil)) };
Self { item }
}
@@ -254,6 +266,33 @@ impl MenuItem {
}
}
+ pub fn set_system_symbol_image(&self, symbol_name: &str) {
+ unsafe {
+ let ns_image_cls = class!(NSImage);
+ let supports_system_symbols: BOOL = msg_send![
+ ns_image_cls,
+ respondsToSelector: sel!(imageWithSystemSymbolName:accessibilityDescription:)
+ ];
+
+ if supports_system_symbols == NO {
+ return;
+ }
+
+ let image: id = msg_send![
+ ns_image_cls,
+ imageWithSystemSymbolName:*nsstring(symbol_name)
+ accessibilityDescription:nil
+ ];
+
+ if image.is_null() {
+ return;
+ }
+
+ let () = msg_send![image, setTemplate:YES];
+ let () = msg_send![*self.item, setImage:image];
+ }
+ }
+
pub fn set_sub_menu(&self, menu: &Menu) {
unsafe {
self.item.setSubmenu_(*menu.menu);
diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs
index 6d71a710..49a67ca6 100644
--- a/window/src/os/macos/window.rs
+++ b/window/src/os/macos/window.rs
@@ -5,13 +5,13 @@ use super::keycodes::*;
use super::{nsstring, nsstring_to_str};
use crate::clipboard::Clipboard as ClipboardContext;
use crate::connection::ConnectionOps;
-use crate::os::macos::menu::{MenuItem, RepresentedItem};
+use crate::os::macos::menu::{Menu, MenuItem, RepresentedItem};
use crate::parameters::{Border, Parameters, TitleBar};
use crate::{
- Clipboard, Connection, DeadKeyStatus, Dimensions, Handled, KeyCode, KeyEvent, Modifiers,
- MouseButtons, MouseCursor, MouseEvent, MouseEventKind, MousePress, Point, RawKeyEvent, Rect,
- RequestedWindowGeometry, ResizeIncrement, ResolvedGeometry, ScreenPoint, Size, ULength,
- WindowDecorations, WindowEvent, WindowEventSender, WindowOps, WindowState,
+ Clipboard, Connection, ContextMenuItem, DeadKeyStatus, Dimensions, Handled, KeyCode, KeyEvent,
+ Modifiers, MouseButtons, MouseCursor, MouseEvent, MouseEventKind, MousePress, Point,
+ RawKeyEvent, Rect, RequestedWindowGeometry, ResizeIncrement, ResolvedGeometry, ScreenPoint,
+ Size, ULength, WindowDecorations, WindowEvent, WindowEventSender, WindowOps, WindowState,
};
use anyhow::{anyhow, bail, ensure};
use async_trait::async_trait;
@@ -769,6 +769,13 @@ impl WindowOps for Window {
});
}
+ fn show_context_menu(&self, coords: Point, items: Vec<ContextMenuItem>) {
+ Connection::with_window_inner(self.id, move |inner| {
+ inner.show_context_menu(coords, items);
+ Ok(())
+ });
+ }
+
fn invalidate(&self) {
Connection::with_window_inner(self.id, |inner| {
inner.invalidate();
@@ -1229,6 +1236,56 @@ impl WindowInner {
}
}
+ fn show_context_menu(&mut self, coords: Point, items: Vec<ContextMenuItem>) {
+ if items.is_empty() {
+ return;
+ }
+
+ let menu = Menu::new_with_title("");
+ let mut has_items = false;
+
+ for item in items {
+ match item {
+ ContextMenuItem::Item {
+ label,
+ icon,
+ action,
+ } => {
+ let menu_item =
+ MenuItem::new_with(&label, Some(sel!(weztermPerformKeyAssignment:)), "");
+ if let Some(icon) = icon {
+ menu_item.set_system_symbol_image(&icon);
+ }
+ menu_item.set_target(*self.view);
+ menu_item.set_represented_item(RepresentedItem::KeyAssignment(action));
+ menu.add_item(&menu_item);
+ has_items = true;
+ }
+ ContextMenuItem::Separator => {
+ if has_items {
+ menu.add_item(&MenuItem::new_separator());
+ }
+ }
+ }
+ }
+
+ if !has_items {
+ return;
+ }
+
+ unsafe {
+ let frame = NSView::frame(*self.view as *mut _);
+ let backing_frame = NSView::convertRectToBacking(*self.view as *mut _, frame);
+ let scale = if frame.size.width > 0.0 {
+ backing_frame.size.width / frame.size.width
+ } else {
+ 1.0
+ };
+
+ menu.pop_up_at(*self.view, coords.x as f64 / scale, coords.y as f64 / scale);
+ }
+ }
+
fn invalidate(&mut self) {
unsafe {
let () = msg_send![*self.view, setNeedsDisplay: YES];