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 fallback context menu UI and setting

RoversX692878e417fe
diff --git a/wezterm-gui/src/native_settings.rs b/wezterm-gui/src/native_settings.rs
index 3f3e2205..e3b823c0 100644
--- a/wezterm-gui/src/native_settings.rs
+++ b/wezterm-gui/src/native_settings.rs
@@ -196,6 +196,7 @@ pub(crate) struct NativeChromeSettings {
#[serde(default)]
pub(crate) struct NativeDeveloperSettings {
pub(crate) developer_mode: bool,
+ pub(crate) force_fallback_context_menu: bool,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
@@ -431,6 +432,11 @@ pub(crate) fn right_sidebar_width() -> Option<usize> {
load().chrome.right_sidebar_width
}
+pub(crate) fn force_fallback_context_menu() -> bool {
+ std::env::var_os("THINKTERM_FORCE_FALLBACK_CONTEXT_MENU").is_some()
+ || load().developer.force_fallback_context_menu
+}
+
pub(crate) fn save_right_sidebar_width(width: usize) -> anyhow::Result<()> {
let mut settings = load();
settings.chrome.right_sidebar_width = Some(width);
diff --git a/wezterm-gui/src/settings_window.rs b/wezterm-gui/src/settings_window.rs
index 332f1e67..74623cc0 100644
--- a/wezterm-gui/src/settings_window.rs
+++ b/wezterm-gui/src/settings_window.rs
@@ -184,6 +184,9 @@ impl SettingsSection {
"Memory Diagnostics",
"Input Diagnostics",
"UI Kit",
+ "Context Menu",
+ "Fallback Menu",
+ "Right Click",
],
Self::UiKit => &[
"Search Field",
@@ -230,6 +233,7 @@ enum SettingsAction {
ToggleImportField(ImportFieldId),
ToggleMainWindowFrameRestore,
ToggleDeveloperMode,
+ ToggleFallbackContextMenu,
ShowOnboardingNow,
ToggleMemoryMonitoring,
RefreshMemorySnapshot,
@@ -2290,6 +2294,31 @@ impl SettingsWindow {
}
}
}
+ SettingsAction::ToggleFallbackContextMenu => {
+ self.ui.open_dropdown = None;
+ self.native_settings.developer.force_fallback_context_menu =
+ !self.native_settings.developer.force_fallback_context_menu;
+ match crate::native_settings::save(&self.native_settings) {
+ Ok(()) => {
+ self.status = if self.native_settings.developer.force_fallback_context_menu
+ {
+ "macOS fallback context menu enabled for future right-click menus."
+ .to_string()
+ } else if std::env::var_os("THINKTERM_FORCE_FALLBACK_CONTEXT_MENU")
+ .is_some()
+ {
+ "Fallback context menu setting disabled, but the environment variable still forces fallback."
+ .to_string()
+ } else {
+ "macOS native context menu restored for future right-click menus."
+ .to_string()
+ };
+ }
+ Err(err) => {
+ self.status = format!("Unable to save context menu setting: {err:#}");
+ }
+ }
+ }
SettingsAction::ShowOnboardingNow => {
self.ui.open_dropdown = None;
let Some(front_end) = crate::frontend::try_front_end() else {
@@ -3321,8 +3350,8 @@ impl SettingsWindow {
let scroll = self.ui.content_scroll.offset;
let row_step = self.settings_row_step();
let section_y = CONTENT_SECTION_Y - scroll;
- let (card_y, first_row_y) = self.settings_card_geometry(section_y, 3);
- let card_height = self.settings_card_height(3);
+ let (card_y, first_row_y) = self.settings_card_geometry(section_y, 4);
+ let card_height = self.settings_card_height(4);
let button_y = card_y + card_height + self.settings_section_card_gap();
self.ui.content_scroll.set_extents(
self.content_bottom(),
@@ -3357,11 +3386,24 @@ impl SettingsWindow {
},
false,
)?;
- self.paint_setting_row(
+ self.paint_toggle_setting_row(
layers,
row_x,
first_row_y + row_step,
row_width,
+ "Use Fallback Context Menu",
+ "On macOS, route right-click menus through the app-rendered fallback for local testing.",
+ self.native_settings
+ .developer
+ .force_fallback_context_menu,
+ SettingsAction::ToggleFallbackContextMenu,
+ true,
+ )?;
+ self.paint_setting_row(
+ layers,
+ row_x,
+ first_row_y + row_step * 2.0,
+ row_width,
"Visible Developer Tabs",
"UI Kit and Memory/Input tabs are shown here; diagnostics do not run automatically.",
if self.developer_mode_enabled() {
@@ -3374,7 +3416,7 @@ impl SettingsWindow {
self.paint_setting_row(
layers,
row_x,
- first_row_y + row_step * 2.0,
+ first_row_y + row_step * 3.0,
row_width,
"Onboarding",
"Open the setup wizard without changing the saved seen version.",
diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs
index 270e7863..dd7e2978 100644
--- a/wezterm-gui/src/termwindow/mod.rs
+++ b/wezterm-gui/src/termwindow/mod.rs
@@ -253,6 +253,8 @@ pub enum UIItemType {
RightSidebarSnippetDelete(String),
RightSidebarSnippetScrollTrack,
RightSidebarSnippetScrollThumb,
+ ContextMenuBackdrop,
+ ContextMenuItem(Vec<usize>),
AboveScrollThumb,
ScrollThumb,
BelowScrollThumb,
@@ -760,6 +762,8 @@ pub struct TermWindow {
palette: Option<ColorPalette>,
ui_items: Vec<UIItem>,
+ context_menu: Option<ui::context_menu::ContextMenuState>,
+ context_menu_suppressed_release: Option<MousePress>,
dragging: Option<(UIItem, MouseEvent)>,
/// When `Some`, a content view (e.g. SSH hosts) is available as a synthetic
/// tab. `content_view_active` is whether it is the foreground content (vs a
@@ -1262,6 +1266,8 @@ impl TermWindow {
allow_images: AllowImage::Yes,
semantic_zones: HashMap::new(),
ui_items: vec![],
+ context_menu: None,
+ context_menu_suppressed_release: None,
dragging: None,
content_view: None,
content_view_active: false,
diff --git a/wezterm-gui/src/termwindow/mouseevent.rs b/wezterm-gui/src/termwindow/mouseevent.rs
index 8e1f87d4..f3c714a3 100644
--- a/wezterm-gui/src/termwindow/mouseevent.rs
+++ b/wezterm-gui/src/termwindow/mouseevent.rs
@@ -588,6 +588,8 @@ impl super::TermWindow {
| UIItemType::RightSidebarSnippetDelete(_)
| UIItemType::RightSidebarSnippetScrollTrack
| UIItemType::RightSidebarSnippetScrollThumb
+ | UIItemType::ContextMenuBackdrop
+ | UIItemType::ContextMenuItem(_)
| UIItemType::AboveScrollThumb
| UIItemType::BelowScrollThumb
| UIItemType::ScrollThumb
@@ -635,6 +637,8 @@ impl super::TermWindow {
| UIItemType::RightSidebarSnippetDelete(_)
| UIItemType::RightSidebarSnippetScrollTrack
| UIItemType::RightSidebarSnippetScrollThumb
+ | UIItemType::ContextMenuBackdrop
+ | UIItemType::ContextMenuItem(_)
| UIItemType::AboveScrollThumb
| UIItemType::BelowScrollThumb
| UIItemType::ScrollThumb
@@ -714,6 +718,14 @@ impl super::TermWindow {
self.current_mouse_event.replace(event.clone());
+ if self.consume_context_menu_suppressed_release(&event) {
+ return;
+ }
+
+ if self.mouse_event_context_menu(&event, &pane, context) {
+ return;
+ }
+
if self.mouse_wheel_right_sidebar(&event, context) {
return;
}
@@ -1131,6 +1143,7 @@ impl super::TermWindow {
UIItemType::WorkspaceSidebarScrollThumb => {
self.drag_workspace_sidebar_scroll_thumb(item, start_event, event, context);
}
+ UIItemType::ContextMenuBackdrop | UIItemType::ContextMenuItem(_) => {}
UIItemType::PaneNav { .. } => {}
_ => {
log::error!("drag not implemented for {:?}", item);
@@ -1305,6 +1318,12 @@ impl super::TermWindow {
UIItemType::WorkspaceSidebarNotifications => {
context.set_cursor(Some(MouseCursor::Hand));
}
+ UIItemType::ContextMenuBackdrop => {
+ context.set_cursor(Some(MouseCursor::Arrow));
+ }
+ UIItemType::ContextMenuItem(_) => {
+ context.set_cursor(Some(MouseCursor::Hand));
+ }
}
}
@@ -1517,7 +1536,11 @@ impl super::TermWindow {
item.x.saturating_add(item.width) as isize,
item.y.saturating_add(item.height / 2) as isize,
);
- context.show_context_menu(coords, self.workspace_sidebar_view_options_menu_items());
+ self.show_term_context_menu(
+ context,
+ coords,
+ self.workspace_sidebar_view_options_menu_items(),
+ );
}
}
@@ -1585,10 +1608,10 @@ impl super::TermWindow {
item.x as isize,
item.y.saturating_add(item.height) as isize,
);
- context.show_context_menu(coords, self.space_menu_items());
+ self.show_term_context_menu(context, coords, self.space_menu_items());
}
WMEK::Press(MousePress::Right) => {
- context.show_context_menu(event.coords, self.space_menu_items());
+ self.show_term_context_menu(context, event.coords, self.space_menu_items());
}
_ => {}
}
@@ -1606,8 +1629,11 @@ impl super::TermWindow {
context.invalidate();
}
WMEK::Press(MousePress::Right) => {
- context
- .show_context_menu(event.coords, self.project_context_menu_items(&project_id));
+ self.show_term_context_menu(
+ context,
+ event.coords,
+ self.project_context_menu_items(&project_id),
+ );
}
_ => {}
}
@@ -1626,8 +1652,11 @@ impl super::TermWindow {
context.invalidate();
}
WMEK::Press(MousePress::Right) => {
- context
- .show_context_menu(event.coords, self.project_context_menu_items(&project_id));
+ self.show_term_context_menu(
+ context,
+ event.coords,
+ self.project_context_menu_items(&project_id),
+ );
}
_ => {}
}
@@ -1645,7 +1674,8 @@ impl super::TermWindow {
self.activate_workspace_thread(thread_id, context);
}
WMEK::Press(MousePress::Right) => {
- context.show_context_menu(
+ self.show_term_context_menu(
+ context,
event.coords,
self.workspace_thread_context_menu_items(&thread_id),
);
@@ -1667,7 +1697,8 @@ impl super::TermWindow {
context.invalidate();
}
WMEK::Press(MousePress::Right) => {
- context.show_context_menu(
+ self.show_term_context_menu(
+ context,
event.coords,
self.workspace_thread_context_menu_items(&thread_id),
);
@@ -1700,7 +1731,8 @@ impl super::TermWindow {
}
}
WMEK::Press(MousePress::Right) => {
- context.show_context_menu(
+ self.show_term_context_menu(
+ context,
event.coords,
self.workspace_thread_context_menu_items(&thread_id),
);
@@ -2157,7 +2189,8 @@ impl super::TermWindow {
if event.kind == WMEK::Press(MousePress::Right) {
if let PaneNavAction::Activate(target_pane_id) = action {
- context.show_context_menu(
+ self.show_term_context_menu(
+ context,
event.coords,
self.pane_nav_tab_context_menu_items(target_pane_id),
);
@@ -2593,7 +2626,11 @@ impl super::TermWindow {
},
WMEK::Press(MousePress::Right) => match item {
TabBarItem::Tab { tab_idx, .. } => {
- context.show_context_menu(event.coords, self.tab_context_menu_items(tab_idx));
+ self.show_term_context_menu(
+ context,
+ event.coords,
+ self.tab_context_menu_items(tab_idx),
+ );
}
TabBarItem::NewTabButton { .. } => {
self.do_new_tab_button_click(MousePress::Right);
@@ -3090,7 +3127,7 @@ impl super::TermWindow {
&& matches!(event.kind, WMEK::Release(MousePress::Right))
&& !pane.is_mouse_grabbed()
{
- context.show_context_menu(event.coords, self.terminal_context_menu_items());
+ self.show_term_context_menu(context, event.coords, self.terminal_context_menu_items());
return;
}
diff --git a/wezterm-gui/src/termwindow/render/paint.rs b/wezterm-gui/src/termwindow/render/paint.rs
index 3160504a..e26d4268 100644
--- a/wezterm-gui/src/termwindow/render/paint.rs
+++ b/wezterm-gui/src/termwindow/render/paint.rs
@@ -544,6 +544,7 @@ impl crate::TermWindow {
.context("paint_window_borders")?;
drop(layers);
self.paint_modal().context("paint_modal")?;
+ self.paint_context_menu().context("paint_context_menu")?;
Ok(())
}
diff --git a/wezterm-gui/src/termwindow/ui/context_menu.rs b/wezterm-gui/src/termwindow/ui/context_menu.rs
new file mode 100644
index 00000000..a6e7a0ef
--- /dev/null
+++ b/wezterm-gui/src/termwindow/ui/context_menu.rs
@@ -0,0 +1,784 @@
+use crate::quad::TripleLayerQuadAllocator;
+use crate::termwindow::ui::icons::SvgIcon;
+use crate::termwindow::{UIItem, UIItemType};
+use crate::ui::UiPalette;
+use crate::utilsprites::RenderMetrics;
+use ::window::color::LinearRgba;
+use ::window::{
+ Appearance, ContextMenuItem, MouseCursor, MouseEvent, MouseEventKind, MousePress, WindowOps,
+};
+use anyhow::Context;
+use config::keyassignment::KeyAssignment;
+use mux::pane::Pane;
+use std::rc::Rc;
+use std::sync::Arc;
+use wezterm_font::LoadedFont;
+
+const MENU_MIN_WIDTH: usize = 220;
+const MENU_MAX_WIDTH: usize = 500;
+const MENU_WINDOW_MARGIN: usize = 12;
+const MENU_FONT_SIZE: f64 = 15.0;
+const MENU_PADDING_X: usize = 14;
+const MENU_PADDING_Y: usize = 10;
+const MENU_LABEL_GAP: usize = 12;
+const MENU_ICON_SIZE: usize = 21;
+const MENU_ICON_SLOT: usize = 32;
+const MENU_CHECK_SLOT: usize = 28;
+const MENU_ARROW_SLOT: usize = 28;
+const MENU_ROW_EXTRA_HEIGHT: usize = 22;
+const MENU_SEPARATOR_HEIGHT: usize = 9;
+const MENU_BORDER_WIDTH: f32 = 1.0;
+const MENU_RADIUS: f32 = 22.0;
+const MENU_ROW_RADIUS: f32 = 15.0;
+const MENU_ROW_HOVER_INSET_X: usize = 8;
+const MENU_ROW_HOVER_INSET_Y: usize = 2;
+
+#[derive(Clone, Debug)]
+pub(crate) struct ContextMenuState {
+ anchor: window::Point,
+ items: Vec<ContextMenuItem>,
+ active_path: Option<Vec<usize>>,
+ layout: Vec<ContextMenuLayoutItem>,
+}
+
+#[derive(Clone, Debug)]
+struct ContextMenuLayoutItem {
+ path: Vec<usize>,
+ rect: MenuRect,
+}
+
+#[derive(Clone, Copy, Debug)]
+struct MenuRect {
+ x: usize,
+ y: usize,
+ width: usize,
+ height: usize,
+}
+
+#[derive(Clone, Copy)]
+enum MenuRow<'a> {
+ Item {
+ index: usize,
+ item: &'a ContextMenuItem,
+ },
+ Separator,
+}
+
+#[derive(Clone, Copy)]
+struct MenuMetrics {
+ width: usize,
+ height: usize,
+ row_height: usize,
+}
+
+impl ContextMenuState {
+ pub(crate) fn new(anchor: window::Point, items: Vec<ContextMenuItem>) -> Self {
+ Self {
+ anchor,
+ items,
+ active_path: None,
+ layout: Vec::new(),
+ }
+ }
+
+ pub(crate) fn has_renderable_items(items: &[ContextMenuItem]) -> bool {
+ items
+ .iter()
+ .any(|item| matches!(item, ContextMenuItem::Item { .. }))
+ }
+
+ fn set_active_path(&mut self, path: Option<Vec<usize>>) -> bool {
+ if self.active_path == path {
+ return false;
+ }
+ self.active_path = path;
+ true
+ }
+
+ fn hit_path(&self, coords: window::Point) -> Option<Vec<usize>> {
+ self.layout
+ .iter()
+ .rev()
+ .find(|item| item.rect.hit_test(coords.x, coords.y))
+ .map(|item| item.path.clone())
+ }
+
+ fn item_at_path(&self, path: &[usize]) -> Option<&ContextMenuItem> {
+ item_at_path(&self.items, path)
+ }
+
+ fn path_has_submenu(&self, path: &[usize]) -> bool {
+ match self.item_at_path(path) {
+ Some(ContextMenuItem::Item {
+ submenu, enabled, ..
+ }) => *enabled && Self::has_renderable_items(submenu),
+ _ => false,
+ }
+ }
+
+ fn action_for_path(&self, path: &[usize]) -> Option<KeyAssignment> {
+ match self.item_at_path(path) {
+ Some(ContextMenuItem::Item {
+ action,
+ enabled,
+ submenu,
+ ..
+ }) if *enabled && !Self::has_renderable_items(submenu) => Some(action.clone()),
+ _ => None,
+ }
+ }
+
+ fn path_is_enabled(&self, path: &[usize]) -> bool {
+ match self.item_at_path(path) {
+ Some(ContextMenuItem::Item { enabled, .. }) => *enabled,
+ _ => false,
+ }
+ }
+}
+
+impl MenuRect {
+ fn hit_test(self, x: isize, y: isize) -> bool {
+ x >= self.x as isize
+ && x < self.x.saturating_add(self.width) as isize
+ && y >= self.y as isize
+ && y < self.y.saturating_add(self.height) as isize
+ }
+}
+
+impl crate::TermWindow {
+ pub(crate) fn show_term_context_menu(
+ &mut self,
+ context: &dyn WindowOps,
+ coords: window::Point,
+ items: Vec<ContextMenuItem>,
+ ) {
+ self.close_fallback_context_menu();
+ if !ContextMenuState::has_renderable_items(&items) {
+ return;
+ }
+
+ if cfg!(target_os = "macos") && !crate::native_settings::force_fallback_context_menu() {
+ context.show_context_menu(coords, items);
+ return;
+ }
+
+ self.context_menu = Some(ContextMenuState::new(coords, items));
+ context.invalidate();
+ }
+
+ pub(crate) fn mouse_event_context_menu(
+ &mut self,
+ event: &MouseEvent,
+ pane: &Arc<dyn Pane>,
+ context: &dyn WindowOps,
+ ) -> bool {
+ let Some(menu) = self.context_menu.as_mut() else {
+ return false;
+ };
+
+ match event.kind {
+ MouseEventKind::Move => {
+ let path = menu.hit_path(event.coords);
+ let cursor = path
+ .as_deref()
+ .filter(|path| menu.path_is_enabled(path))
+ .map(|_| MouseCursor::Hand)
+ .unwrap_or(MouseCursor::Arrow);
+ context.set_cursor(Some(cursor));
+ if menu.set_active_path(path) {
+ context.invalidate();
+ }
+ true
+ }
+ MouseEventKind::Press(MousePress::Left) => {
+ self.context_menu_suppressed_release = Some(MousePress::Left);
+ let path = menu.hit_path(event.coords);
+ if let Some(path) = path {
+ if menu.path_has_submenu(&path) {
+ if menu.set_active_path(Some(path)) {
+ context.invalidate();
+ }
+ return true;
+ }
+
+ if let Some(action) = menu.action_for_path(&path) {
+ self.close_fallback_context_menu();
+ context.invalidate();
+ if let Err(err) = self.perform_key_assignment(pane, &action) {
+ log::error!("context menu action failed: {err:#}");
+ }
+ return true;
+ }
+
+ if menu.set_active_path(Some(path)) {
+ context.invalidate();
+ }
+ return true;
+ }
+
+ self.close_fallback_context_menu();
+ context.invalidate();
+ true
+ }
+ MouseEventKind::Press(MousePress::Right) => {
+ if menu.hit_path(event.coords).is_some() {
+ return true;
+ }
+ self.close_fallback_context_menu();
+ context.invalidate();
+ false
+ }
+ MouseEventKind::Release(_)
+ | MouseEventKind::VertWheel(_)
+ | MouseEventKind::HorzWheel(_) => true,
+ MouseEventKind::Press(MousePress::Middle) => true,
+ }
+ }
+
+ pub(crate) fn consume_context_menu_suppressed_release(&mut self, event: &MouseEvent) -> bool {
+ let MouseEventKind::Release(press) = event.kind else {
+ return false;
+ };
+ if self.context_menu_suppressed_release == Some(press) {
+ self.context_menu_suppressed_release = None;
+ return true;
+ }
+ false
+ }
+
+ pub(crate) fn close_fallback_context_menu(&mut self) {
+ if self.context_menu.is_none() {
+ return;
+ }
+
+ self.context_menu = None;
+ self.last_ui_item = None;
+ self.ui_items.retain(|item| {
+ !matches!(
+ item.item_type,
+ UIItemType::ContextMenuItem(_) | UIItemType::ContextMenuBackdrop
+ )
+ });
+ }
+
+ pub(crate) fn paint_context_menu(&mut self) -> anyhow::Result<()> {
+ let Some(menu) = self.context_menu.as_ref() else {
+ return Ok(());
+ };
+
+ let anchor = menu.anchor;
+ let items = menu.items.clone();
+ let active_path = menu.active_path.clone();
+
+ let ui_font = self
+ .fonts
+ .title_font_with_size(MENU_FONT_SIZE)
+ .context("context menu ui font")?;
+ let ui_metrics = RenderMetrics::with_font_metrics(&ui_font.metrics());
+ let row_height = (ui_metrics.cell_size.height as usize + MENU_ROW_EXTRA_HEIGHT).max(28);
+ let palette = context_menu_palette(crate::native_settings::effective_appearance());
+
+ let gl_state = self.render_state.as_ref().unwrap();
+ let layer = gl_state.layer_for_zindex(0).context("context menu layer")?;
+ let mut layers = layer.quad_allocator();
+
+ self.ui_items.push(UIItem {
+ x: 0,
+ y: 0,
+ width: self.dimensions.pixel_width,
+ height: self.dimensions.pixel_height,
+ item_type: UIItemType::ContextMenuBackdrop,
+ });
+
+ let mut layout: Vec<ContextMenuLayoutItem> = Vec::new();
+ let mut menu_paths = vec![Vec::new()];
+ if let Some(active_path) = active_path.as_ref() {
+ let mut prefix = Vec::new();
+ for idx in active_path {
+ prefix.push(*idx);
+ if item_at_path(&items, &prefix)
+ .and_then(renderable_submenu)
+ .is_some()
+ {
+ menu_paths.push(prefix.clone());
+ }
+ }
+ }
+
+ for menu_path in menu_paths {
+ let Some(menu_items) = items_for_menu_path(&items, &menu_path) else {
+ continue;
+ };
+ let rows = render_rows(menu_items);
+ if rows.is_empty() {
+ continue;
+ }
+
+ let metrics = compute_menu_metrics(
+ self,
+ &ui_font,
+ row_height,
+ &rows,
+ self.dimensions.pixel_width,
+ )?;
+ let origin = if menu_path.is_empty() {
+ clamp_menu_origin(
+ anchor.x,
+ anchor.y,
+ metrics.width,
+ metrics.height,
+ self.dimensions.pixel_width,
+ self.dimensions.pixel_height,
+ )
+ } else {
+ let parent_rect = layout
+ .iter()
+ .find(|item| item.path == menu_path)
+ .map(|item| item.rect);
+ let Some(parent_rect) = parent_rect else {
+ continue;
+ };
+ submenu_origin(
+ parent_rect,
+ metrics.width,
+ metrics.height,
+ self.dimensions.pixel_width,
+ self.dimensions.pixel_height,
+ )
+ };
+
+ paint_menu_panel(self, &mut layers, palette, origin, metrics)?;
+ paint_menu_rows(
+ self,
+ &mut layers,
+ &ui_font,
+ ui_metrics,
+ palette,
+ &menu_path,
+ &rows,
+ origin,
+ metrics,
+ active_path.as_deref(),
+ &mut layout,
+ )?;
+ }
+
+ drop(layers);
+ if let Some(menu) = self.context_menu.as_mut() {
+ menu.layout = layout;
+ }
+
+ Ok(())
+ }
+}
+
+fn item_at_path<'a>(items: &'a [ContextMenuItem], path: &[usize]) -> Option<&'a ContextMenuItem> {
+ let mut current = items;
+ let mut item = None;
+ for idx in path {
+ item = current.get(*idx);
+ current = match item {
+ Some(ContextMenuItem::Item { submenu, .. }) => submenu,
+ _ => return item,
+ };
+ }
+ item
+}
+
+fn items_for_menu_path<'a>(
+ items: &'a [ContextMenuItem],
+ path: &[usize],
+) -> Option<&'a [ContextMenuItem]> {
+ if path.is_empty() {
+ return Some(items);
+ }
+ item_at_path(items, path).and_then(renderable_submenu)
+}
+
+fn renderable_submenu(item: &ContextMenuItem) -> Option<&[ContextMenuItem]> {
+ match item {
+ ContextMenuItem::Item {
+ submenu, enabled, ..
+ } if *enabled && ContextMenuState::has_renderable_items(submenu) => Some(submenu),
+ _ => None,
+ }
+}
+
+fn render_rows(items: &[ContextMenuItem]) -> Vec<MenuRow<'_>> {
+ let mut rows = Vec::new();
+ let mut last_was_separator = true;
+
+ for (index, item) in items.iter().enumerate() {
+ match item {
+ ContextMenuItem::Item { .. } => {
+ rows.push(MenuRow::Item { index, item });
+ last_was_separator = false;
+ }
+ ContextMenuItem::Separator => {
+ if !last_was_separator {
+ rows.push(MenuRow::Separator);
+ last_was_separator = true;
+ }
+ }
+ }
+ }
+
+ while matches!(rows.last(), Some(MenuRow::Separator)) {
+ rows.pop();
+ }
+
+ rows
+}
+
+fn compute_menu_metrics(
+ term: &crate::TermWindow,
+ font: &Rc<LoadedFont>,
+ row_height: usize,
+ rows: &[MenuRow<'_>],
+ window_width: usize,
+) -> anyhow::Result<MenuMetrics> {
+ let mut label_width = 0usize;
+ let mut height = MENU_PADDING_Y * 2;
+
+ for row in rows {
+ match row {
+ MenuRow::Item { item, .. } => {
+ if let ContextMenuItem::Item { label, .. } = item {
+ label_width =
+ label_width.max(term.sidebar_text_width(font, label)?.ceil() as usize);
+ }
+ height = height.saturating_add(row_height);
+ }
+ MenuRow::Separator => {
+ height = height.saturating_add(MENU_SEPARATOR_HEIGHT);
+ }
+ }
+ }
+
+ let ideal_width = MENU_PADDING_X * 2
+ + MENU_CHECK_SLOT
+ + MENU_ICON_SLOT
+ + MENU_LABEL_GAP
+ + label_width
+ + MENU_ARROW_SLOT;
+ let max_width = MENU_MAX_WIDTH
+ .min(window_width.saturating_sub(MENU_WINDOW_MARGIN * 2))
+ .max(1);
+ let min_width = MENU_MIN_WIDTH.min(max_width);
+ let width = ideal_width.clamp(min_width, max_width);
+
+ Ok(MenuMetrics {
+ width,
+ height,
+ row_height,
+ })
+}
+
+fn clamp_menu_origin(
+ x: isize,
+ y: isize,
+ width: usize,
+ height: usize,
+ window_width: usize,
+ window_height: usize,
+) -> (usize, usize) {
+ let max_x = window_width.saturating_sub(width) as isize;
+ let max_y = window_height.saturating_sub(height) as isize;
+ (
+ x.clamp(0, max_x).max(0) as usize,
+ y.clamp(0, max_y).max(0) as usize,
+ )
+}
+
+fn submenu_origin(
+ parent: MenuRect,
+ width: usize,
+ height: usize,
+ window_width: usize,
+ window_height: usize,
+) -> (usize, usize) {
+ let right_x = parent.x.saturating_add(parent.width).saturating_sub(4);
+ let left_x = parent.x.saturating_sub(width.saturating_sub(4));
+ let x = if right_x.saturating_add(width) <= window_width {
+ right_x
+ } else {
+ left_x
+ };
+ let y = parent.y.min(window_height.saturating_sub(height)).max(0);
+ (x, y)
+}
+
+fn paint_menu_panel(
+ term: &crate::TermWindow,
+ layers: &mut TripleLayerQuadAllocator<'_>,
+ palette: UiPalette,
+ origin: (usize, usize),
+ metrics: MenuMetrics,
+) -> anyhow::Result<()> {
+ let (x, y) = origin;
+ let rect = euclid::rect(
+ x as f32,
+ y as f32,
+ metrics.width as f32,
+ metrics.height as f32,
+ );
+ term.fill_rounded_rectangle(
+ layers,
+ 2,
+ euclid::rect(
+ x.saturating_add(4) as f32,
+ y.saturating_add(5) as f32,
+ metrics.width as f32,
+ metrics.height as f32,
+ ),
+ LinearRgba::with_components(0.0, 0.0, 0.0, 0.18),
+ MENU_RADIUS,
+ )?;
+ term.fill_rounded_rectangle_with_border(
+ layers,
+ 2,
+ rect,
+ palette.control_bg,
+ palette.control_border,
+ MENU_RADIUS,
+ MENU_BORDER_WIDTH,
+ )
+}
+
+#[allow(clippy::too_many_arguments)]
+fn paint_menu_rows(
+ term: &mut crate::TermWindow,
+ layers: &mut TripleLayerQuadAllocator<'_>,
+ font: &Rc<LoadedFont>,
+ font_metrics: RenderMetrics,
+ palette: UiPalette,
+ menu_path: &[usize],
+ rows: &[MenuRow<'_>],
+ origin: (usize, usize),
+ metrics: MenuMetrics,
+ active_path: Option<&[usize]>,
+ layout: &mut Vec<ContextMenuLayoutItem>,
+) -> anyhow::Result<()> {
+ let (x, y) = origin;
+ let mut cursor_y = y + MENU_PADDING_Y;
+
+ for row in rows {
+ match row {
+ MenuRow::Separator => {
+ let sep_y = cursor_y + MENU_SEPARATOR_HEIGHT / 2;
+ term.filled_rectangle(
+ layers,
+ 2,
+ euclid::rect(
+ (x + MENU_PADDING_X) as f32,
+ sep_y as f32,
+ metrics.width.saturating_sub(MENU_PADDING_X * 2) as f32,
+ 1.0,
+ ),
+ palette.separator,
+ )?;
+ cursor_y += MENU_SEPARATOR_HEIGHT;
+ }
+ MenuRow::Item { index, item } => {
+ let mut path = menu_path.to_vec();
+ path.push(*index);
+ let item_rect = MenuRect {
+ x: x + MENU_BORDER_WIDTH as usize,
+ y: cursor_y,
+ width: metrics
+ .width
+ .saturating_sub((MENU_BORDER_WIDTH as usize) * 2),
+ height: metrics.row_height,
+ };
+ let has_submenu = match item {
+ ContextMenuItem::Item { submenu, .. } => {
+ ContextMenuState::has_renderable_items(submenu)
+ }
+ ContextMenuItem::Separator => false,
+ };
+ let hovered = active_path.is_some_and(|active| {
+ active == path.as_slice() || (has_submenu && active.starts_with(&path))
+ });
+
+ if hovered {
+ term.fill_rounded_rectangle(
+ layers,
+ 2,
+ euclid::rect(
+ item_rect.x.saturating_add(MENU_ROW_HOVER_INSET_X) as f32,
+ item_rect.y.saturating_add(MENU_ROW_HOVER_INSET_Y) as f32,
+ item_rect.width.saturating_sub(MENU_ROW_HOVER_INSET_X * 2) as f32,
+ item_rect.height.saturating_sub(MENU_ROW_HOVER_INSET_Y * 2) as f32,
+ ),
+ palette.control_hover_bg,
+ MENU_ROW_RADIUS,
+ )?;
+ }
+
+ if let ContextMenuItem::Item {
+ label,
+ icon,
+ checked,
+ enabled,
+ submenu,
+ ..
+ } = item
+ {
+ let foreground = if !enabled {
+ palette.muted_text
+ } else if hovered {
+ palette.text
+ } else {
+ palette.secondary_text
+ };
+ let icon_y =
+ item_rect.y + (item_rect.height.saturating_sub(MENU_ICON_SIZE)) / 2;
+ let check_x = item_rect.x + MENU_PADDING_X;
+ let icon_x = check_x + MENU_CHECK_SLOT;
+ if *checked {
+ term.paint_sidebar_icon(
+ layers,
+ SvgIcon::Check,
+ check_x,
+ icon_y,
+ MENU_ICON_SIZE,
+ foreground,
+ )?;
+ }
+ if let Some(icon) = icon.as_ref().and_then(|icon| menu_icon(icon)) {
+ term.paint_sidebar_icon(
+ layers,
+ icon,
+ icon_x,
+ icon_y,
+ MENU_ICON_SIZE,
+ foreground,
+ )?;
+ }
+
+ let label_x = item_rect.x
+ + MENU_PADDING_X
+ + MENU_CHECK_SLOT
+ + MENU_ICON_SLOT
+ + MENU_LABEL_GAP;
+ let arrow_width = if ContextMenuState::has_renderable_items(submenu) {
+ MENU_ARROW_SLOT
+ } else {
+ 0
+ };
+ let label_width = item_rect
+ .width
+ .saturating_sub(label_x.saturating_sub(item_rect.x))
+ .saturating_sub(arrow_width)
+ .saturating_sub(MENU_PADDING_X);
+ let text_y = item_rect.y
+ + (item_rect
+ .height
+ .saturating_sub(font_metrics.cell_size.height as usize))
+ / 2;
+ term.paint_sidebar_text(
+ layers,
+ font,
+ font_metrics,
+ label,
+ label_x,
+ text_y,
+ label_width,
+ foreground,
+ )?;
+
+ if ContextMenuState::has_renderable_items(submenu) {
+ let arrow_x = item_rect
+ .x
+ .saturating_add(item_rect.width)
+ .saturating_sub(MENU_PADDING_X)
+ .saturating_sub(MENU_ICON_SIZE);
+ term.paint_sidebar_icon(
+ layers,
+ SvgIcon::ChevronRight,
+ arrow_x,
+ icon_y,
+ MENU_ICON_SIZE,
+ foreground,
+ )?;
+ }
+ }
+
+ term.ui_items.push(UIItem {
+ x: item_rect.x,
+ y: item_rect.y,
+ width: item_rect.width,
+ height: item_rect.height,
+ item_type: UIItemType::ContextMenuItem(path.clone()),
+ });
+ layout.push(ContextMenuLayoutItem {
+ path,
+ rect: item_rect,
+ });
+ cursor_y += metrics.row_height;
+ }
+ }
+ }
+
+ Ok(())
+}
+
+fn context_menu_palette(appearance: Appearance) -> UiPalette {
+ let mut palette = UiPalette::for_appearance(appearance);
+ match appearance {
+ Appearance::Dark | Appearance::DarkHighContrast => {
+ palette.control_bg = LinearRgba::with_srgba(30, 30, 32, 255);
+ palette.control_hover_bg = LinearRgba::with_srgba(255, 255, 255, 255).mul_alpha(0.08);
+ palette.control_border = LinearRgba::with_srgba(118, 118, 128, 255).mul_alpha(0.34);
+ palette.separator = LinearRgba::with_srgba(84, 84, 88, 255).mul_alpha(0.36);
+ palette.text = LinearRgba::with_srgba(242, 242, 247, 255);
+ palette.secondary_text = LinearRgba::with_srgba(226, 226, 232, 255);
+ palette.muted_text = LinearRgba::with_srgba(150, 150, 156, 255);
+ }
+ Appearance::Light | Appearance::LightHighContrast => {
+ palette.control_bg = LinearRgba::with_srgba(246, 246, 248, 255);
+ palette.control_hover_bg = LinearRgba::with_srgba(60, 60, 67, 255).mul_alpha(0.08);
+ palette.control_border = LinearRgba::with_srgba(60, 60, 67, 255).mul_alpha(0.22);
+ palette.separator = LinearRgba::with_srgba(60, 60, 67, 255).mul_alpha(0.20);
+ }
+ }
+ palette
+}
+
+fn menu_icon(icon: &str) -> Option<SvgIcon> {
+ match icon {
+ "arrow.clockwise" => Some(SvgIcon::RotateCcw),
+ "arrow.triangle.2.circlepath" => Some(SvgIcon::RotateCcw),
+ "checkmark.circle" => Some(SvgIcon::CircleCheck),
+ "chevron.right" => Some(SvgIcon::ChevronRight),
+ "doc.on.clipboard" => Some(SvgIcon::ClipboardPaste),
+ "envelope.badge" => Some(SvgIcon::Bell),
+ "exclamationmark.circle" => Some(SvgIcon::CircleAlert),
+ "folder" => Some(SvgIcon::Folder),
+ "folder.badge.minus" => Some(SvgIcon::FolderMinus),
+ "folder.badge.plus" => Some(SvgIcon::FolderPlus),
+ "folder.badge.questionmark" => Some(SvgIcon::FolderOpen),
+ "folder.badge.xmark" => Some(SvgIcon::FolderOpen),
+ "folder.fill" => Some(SvgIcon::Folder),
+ "folder.fill.badge.gearshape" => Some(SvgIcon::Settings),
+ "folder.fill.badge.minus" => Some(SvgIcon::FolderMinus),
+ "folder.fill.badge.plus" => Some(SvgIcon::FolderPlus),
+ "folder.fill.badge.xmark" => Some(SvgIcon::FolderOpen),
+ "house" => Some(SvgIcon::House),
+ "pencil" => Some(SvgIcon::Pencil),
+ "pin" => Some(SvgIcon::Pin),
+ "pin.slash" => Some(SvgIcon::PinOff),
+ "plus" => Some(SvgIcon::Plus),
+ "plus.square" => Some(SvgIcon::SquarePlus),
+ "rectangle.split.1x2" => Some(SvgIcon::SplitHorizontal),
+ "rectangle.split.2x1" => Some(SvgIcon::SplitVertical),
+ "sidebar.leading" => Some(SvgIcon::PanelLeft),
+ "square.and.pencil" => Some(SvgIcon::NotebookTabs),
+ "square.stack" => Some(SvgIcon::SquareStack),
+ "terminal" => Some(SvgIcon::Terminal),
+ "trash" => Some(SvgIcon::Trash2),
+ "trash.fill" => Some(SvgIcon::Trash2),
+ "xmark" => Some(SvgIcon::X),
+ _ => None,
+ }
+}
diff --git a/wezterm-gui/src/termwindow/ui/icons.rs b/wezterm-gui/src/termwindow/ui/icons.rs
index 4462650a..e9db0f88 100644
--- a/wezterm-gui/src/termwindow/ui/icons.rs
+++ b/wezterm-gui/src/termwindow/ui/icons.rs
@@ -8,18 +8,22 @@ pub enum SvgIcon {
ArrowLeft,
Bell,
Braces,
+ Check,
ChevronDown,
ChevronRight,
Cloud,
CodeXml,
+ ClipboardPaste,
Ellipsis,
Expand,
ExternalLink,
Folder,
+ FolderMinus,
FolderOpen,
FolderPlus,
FolderTree,
Globe,
+ House,
Info,
Keyboard,
Layers,
@@ -39,6 +43,7 @@ pub enum SvgIcon {
PanelLeftOpen,
PanelRightClose,
PanelRightOpen,
+ Pencil,
Pin,
PinOff,
Plus,
@@ -52,6 +57,8 @@ pub enum SvgIcon {
CircleAlert,
CircleCheck,
CirclePlus,
+ SquarePlus,
+ SquareStack,
SquareTerminal,
SplitHorizontal,
SplitVertical,
@@ -69,6 +76,7 @@ impl SvgIcon {
}
Self::Bell => include_bytes!("../../../../third_party/lucide/icons/bell.svg"),
Self::Braces => include_bytes!("../../../../third_party/lucide/icons/braces.svg"),
+ Self::Check => include_bytes!("../../../../third_party/lucide/icons/check.svg"),
Self::ChevronDown => {
include_bytes!("../../../../third_party/lucide/icons/chevron-down.svg")
}
@@ -77,12 +85,18 @@ impl SvgIcon {
}
Self::Cloud => include_bytes!("../../../../third_party/lucide/icons/cloud.svg"),
Self::CodeXml => include_bytes!("../../../../third_party/lucide/icons/code-xml.svg"),
+ Self::ClipboardPaste => {
+ include_bytes!("../../../../third_party/lucide/icons/clipboard-paste.svg")
+ }
Self::Ellipsis => include_bytes!("../../../../third_party/lucide/icons/ellipsis.svg"),
Self::Expand => include_bytes!("../../../../third_party/lucide/icons/expand.svg"),
Self::ExternalLink => {
include_bytes!("../../../../third_party/lucide/icons/external-link.svg")
}
Self::Folder => include_bytes!("../../../../third_party/lucide/icons/folder.svg"),
+ Self::FolderMinus => {
+ include_bytes!("../../../../third_party/lucide/icons/folder-minus.svg")
+ }
Self::FolderOpen => {
include_bytes!("../../../../third_party/lucide/icons/folder-open.svg")
}
@@ -93,6 +107,7 @@ impl SvgIcon {
include_bytes!("../../../../third_party/lucide/icons/folder-tree.svg")
}
Self::Globe => include_bytes!("../../../../third_party/lucide/icons/globe.svg"),
+ Self::House => include_bytes!("../../../../third_party/lucide/icons/house.svg"),
Self::Info => include_bytes!("../../../../third_party/lucide/icons/info.svg"),
Self::Keyboard => include_bytes!("../../../../third_party/lucide/icons/keyboard.svg"),
Self::Layers => include_bytes!("../../../../third_party/lucide/icons/layers.svg"),
@@ -136,6 +151,7 @@ impl SvgIcon {
Self::PanelRightOpen => {
include_bytes!("../../../../third_party/lucide/icons/panel-right-open.svg")
}
+ Self::Pencil => include_bytes!("../../../../third_party/lucide/icons/pencil.svg"),
Self::Pin => include_bytes!("../../../../third_party/lucide/icons/pin.svg"),
Self::PinOff => include_bytes!("../../../../third_party/lucide/icons/pin-off.svg"),
Self::Plus => include_bytes!("../../../../third_party/lucide/icons/plus.svg"),
@@ -161,6 +177,12 @@ impl SvgIcon {
Self::CirclePlus => {
include_bytes!("../../../../third_party/lucide/icons/circle-plus.svg")
}
+ Self::SquarePlus => {
+ include_bytes!("../../../../third_party/lucide/icons/square-plus.svg")
+ }
+ Self::SquareStack => {
+ include_bytes!("../../../../third_party/lucide/icons/square-stack.svg")
+ }
Self::SquareTerminal => {
include_bytes!("../../../../third_party/lucide/icons/square-terminal.svg")
}
diff --git a/wezterm-gui/src/termwindow/ui/mod.rs b/wezterm-gui/src/termwindow/ui/mod.rs
index 9f7c42b6..f7c30b7a 100644
--- a/wezterm-gui/src/termwindow/ui/mod.rs
+++ b/wezterm-gui/src/termwindow/ui/mod.rs
@@ -1,3 +1,4 @@
+pub mod context_menu;
pub mod icons;
pub mod platform_chrome;
pub mod right_sidebar;