From 12ba7abdd29d5bd44cffdb9583d13c49e18093d8 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Tue, 17 Nov 2026 14:48:30 -0800 Subject: [PATCH] Integrate macOS window controls and fix palette text rendering Center native window controls in the draggable tab bar. Preserve full text runs during GPUI layout and let AppKit compose text in palette and rename fields. --- Cargo.toml | 2 +- THIRD_PARTY_NOTICES.md | 2 +- src/chrome.rs | 16 ++++++++-- src/main.rs | 5 +++- src/palette.rs | 11 ++++--- src/pane_name.rs | 41 +++++++++++++++++++++++--- src/terminal.rs | 1 + vendor/gpui/PATCH.md | 25 ++++++++++++---- vendor/gpui/src/elements/text.rs | 26 ++++++++++------ vendor/gpui/src/platform/mac/window.rs | 11 +++++++ 10 files changed, 112 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f836699..3e34f46 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,7 +42,7 @@ nix = { version = "0.29", features = ["event", "poll", "signal", "user"] } # GPUI's zed-async-tar uses xattr 0.2.3, whose Linux ENOATTR alias was removed # from libc. Keep the compatibility fix local; see vendor/xattr/PATCH.md. xattr = { path = "vendor/xattr" } -# Wake Wayland presentation after idle without continuously rendering. +# Local window and text layout fixes are documented in vendor/gpui/PATCH.md. gpui = { path = "vendor/gpui" } [profile.dev] diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index 3a9203d..670cb5c 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -40,4 +40,4 @@ stack transitions use this curve; text scrolling has a separate spring in Neovid `vendor/gpui` contains the published GPUI 0.2.2 crate, licensed under Apache-2.0. The original [license](vendor/gpui/LICENSE-APACHE) and source metadata are retained. -Changes for Wayland idle redraw are documented in [PATCH.md](vendor/gpui/PATCH.md). +Local patches are documented in [PATCH.md](vendor/gpui/PATCH.md). diff --git a/src/chrome.rs b/src/chrome.rs index 187ff4a..b83b216 100644 --- a/src/chrome.rs +++ b/src/chrome.rs @@ -43,6 +43,7 @@ pub struct ChromeTab { pub fn header( connection: SharedString, tabs: Vec, + reserve_traffic_lights: bool, on_tab: impl Fn(u64, &mut Window, &mut App) + 'static, on_new: impl Fn(&mut Window, &mut App) + 'static, on_close: impl Fn(&mut Window, &mut App) + 'static, @@ -128,9 +129,17 @@ pub fn header( .items_center() .gap(px(10.)) .px(px(12.)) + // In fullscreen AppKit reveals the buttons in its own overlay. + .when(reserve_traffic_lights, |row| row.pl(px(86.))) .flex_1() .min_h_0() - .on_mouse_down(MouseButton::Left, |_, window, _| window.start_window_move()) + .on_mouse_down(MouseButton::Left, |event, window, _| { + if cfg!(target_os = "macos") && event.click_count == 2 { + window.titlebar_double_click(); + } else { + window.start_window_move(); + } + }) .child(tab_strip) .child( div() @@ -145,7 +154,9 @@ pub fn header( .child(div().text_ellipsis().child(connection)), ) .child(control("new-tab", "+", false, on_new)) - .child(control("detach-window", "×", true, on_close)), + .when(!cfg!(target_os = "macos"), |row| { + row.child(control("detach-window", "×", true, on_close)) + }), ) } @@ -198,6 +209,7 @@ mod tests { active: true, editor: None, }], + cfg!(target_os = "macos"), |_, _, _| {}, |_, _| {}, |_, _| {}, diff --git a/src/main.rs b/src/main.rs index affc865..c321984 100644 --- a/src/main.rs +++ b/src/main.rs @@ -180,7 +180,10 @@ pub(crate) fn run_gui(socket: PathBuf, remote: Option) -> Result<()> { window_bounds: Some(WindowBounds::Windowed(bounds)), titlebar: Some(TitlebarOptions { title: Some("psyTerm".into()), - ..Default::default() + appears_transparent: cfg!(target_os = "macos"), + // GPUI positions the top-left of the 14-point close button. + traffic_light_position: cfg!(target_os = "macos") + .then(|| point(px(12.), px((chrome::HEADER_HEIGHT - 14.) / 2.))), }), window_decorations: Some(WindowDecorations::Client), app_id: Some("dev.buzzert.psyterm".into()), diff --git a/src/palette.rs b/src/palette.rs index bdd8485..0ceaece 100644 --- a/src/palette.rs +++ b/src/palette.rs @@ -12,7 +12,7 @@ use gpui::{ use crate::{ chrome, commands::{CommandAction, PaletteItem, PaletteItemKind}, - pane_name::NameEditor, + pane_name::{NameEditor, use_native_text_input}, }; #[derive(Clone)] @@ -306,9 +306,12 @@ impl CommandPalette { } fn key_down(&mut self, event: &KeyDownEvent, window: &mut Window, cx: &mut Context) { - cx.stop_propagation(); let key = &event.keystroke; - let primary = key.modifiers.control || key.modifiers.platform; + if use_native_text_input(key) { + return; + } + cx.stop_propagation(); + let primary = key.modifiers.platform || key.modifiers.secondary(); match key.key.as_str() { "escape" => { self.back(window, cx); @@ -371,7 +374,7 @@ impl CommandPalette { } _ => {} }, - _ if !primary && !key.modifiers.alt => { + _ if !primary && !key.modifiers.alt && !key.modifiers.control => { if let Some(text) = &key.key_char { self.editor.replace(None, text); } diff --git a/src/pane_name.rs b/src/pane_name.rs index 7c47706..5338216 100644 --- a/src/pane_name.rs +++ b/src/pane_name.rs @@ -2,6 +2,19 @@ //! methods use UTF-16 ranges. Editing never sends bytes to the terminal process. use std::ops::Range; +/// AppKit's input context combines Option/dead keys and delivers composed text +/// through the focused field's EntityInputHandler. +pub fn use_native_text_input(key: &gpui::Keystroke) -> bool { + cfg!(target_os = "macos") + && !key.modifiers.platform + && !key.modifiers.control + && !key.modifiers.function + && key + .key_char + .as_ref() + .is_some_and(|text| !text.chars().any(char::is_control)) +} + const MAX_CHARACTERS: usize = 512; #[derive(Debug, Clone)] @@ -255,10 +268,12 @@ impl RenameField { } fn key_down(&mut self, event: &KeyDownEvent, window: &mut Window, cx: &mut Context) { - // Parent terminal key handlers must never see keys typed into this field. - cx.stop_propagation(); let key = &event.keystroke; - let command = key.modifiers.control || key.modifiers.platform; + if use_native_text_input(key) { + return; + } + cx.stop_propagation(); + let command = key.modifiers.platform || key.modifiers.secondary(); match key.key.as_str() { "enter" => { // Enter first accepts active IME composition; a subsequent Enter saves. @@ -299,7 +314,7 @@ impl RenameField { } _ => {} }, - _ if !command && !key.modifiers.alt => { + _ if !command && !key.modifiers.alt && !key.modifiers.control => { if let Some(text) = &key.key_char { self.editor.replace(None, text); } @@ -612,6 +627,24 @@ mod tests { assert_eq!(editor.cursor(), 1); } + #[cfg(target_os = "macos")] + #[test] + fn appkit_composes_option_text_while_editors_handle_shortcuts() { + for (binding, text, native) in [ + ("a", "a", true), + ("alt-2", "€", true), + ("alt-e", "´", true), + ("cmd-c", "c", false), + ("ctrl-c", "c", false), + ("enter", "\n", false), + ("tab", "\t", false), + ] { + let mut key = gpui::Keystroke::parse(binding).unwrap(); + key.key_char = Some(text.into()); + assert_eq!(use_native_text_input(&key), native, "{binding}"); + } + } + #[test] fn composition_replaces_previous_marked_text_and_commits_once() { let mut editor = NameEditor::new("old"); diff --git a/src/terminal.rs b/src/terminal.rs index b5f31f6..7584b1e 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -480,6 +480,7 @@ impl Render for Workspace { let header = chrome::header( self.connection.clone(), tabs, + cfg!(target_os = "macos") && !window.is_fullscreen(), move |tab, _, _| { let _ = client.send(ClientMessage::FocusTab { tab }); }, diff --git a/vendor/gpui/PATCH.md b/vendor/gpui/PATCH.md index fdf5a37..19e0b1b 100644 --- a/vendor/gpui/PATCH.md +++ b/vendor/gpui/PATCH.md @@ -1,8 +1,9 @@ -# GPUI 0.2.2 Wayland idle redraw patch +# Local GPUI 0.2.2 patches -This directory vendors the published `gpui` 0.2.2 crate unchanged except for -`src/window.rs`, `src/app.rs`, and this note. Upstream source and Apache-2.0 -license are retained; see `LICENSE-APACHE` and `.cargo_vcs_info.json`. +This directory vendors the published `gpui` 0.2.2 crate with the patches below. +The upstream license and revision are in `LICENSE-APACHE` and `.cargo_vcs_info.json`. + +## Wayland idle redraw Wayland compositors can withhold a pending frame callback after GPUI skips presenting a clean frame. Later asynchronous view notifications make the scene @@ -14,9 +15,21 @@ of an app effect cycle, an invalidated idle window draws and presents once, clearing the idle flag. Normal compositor callbacks then resume. Dirty active windows remain callback-paced; clean idle windows submit no frames. The flag starts false, avoiding presentation before the initial Wayland configure. -Other platform backends are unchanged. No foreign Wayland connection or raw -surface manipulation is used. The optional `scripts/check-wayland-idle.py` exercises an actual headless Weston window and verifies output repaints after an idle interval without pointer input or screenshots waking the compositor. + +## macOS titlebar dragging + +The macOS implementation of `start_window_move` passes the current mouse +event to AppKit's `performWindowDragWithEvent:`. This lets applications +initiate native dragging from a custom titlebar. The implementation follows +[upstream GPUI](https://github.com/zed-industries/zed/blob/main/crates/gpui_macos/src/window.rs). + +## Text truncation during layout + +Flex layout can measure a label at a narrow width before allocating its final +width. Each truncation now starts from the original styled text runs, and the +measurement cache includes the width used for truncation. This preserves the +full command titles in the palette when there is room to display them. diff --git a/vendor/gpui/src/elements/text.rs b/vendor/gpui/src/elements/text.rs index 5d34ccf..7357dd7 100644 --- a/vendor/gpui/src/elements/text.rs +++ b/vendor/gpui/src/elements/text.rs @@ -316,6 +316,7 @@ struct TextLayoutInner { lines: SmallVec<[WrappedLine; 1]>, line_height: Pixels, wrap_width: Option, + truncate_width: Option, size: Option>, bounds: Option>, } @@ -334,7 +335,7 @@ impl TextLayout { .line_height .to_pixels(font_size.into(), window.rem_size()); - let mut runs = if let Some(runs) = runs { + let runs = if let Some(runs) = runs { runs } else { vec![text_style.to_run(text.len())] @@ -372,19 +373,24 @@ impl TextLayout { if let Some(text_layout) = element_state.0.borrow().as_ref() && text_layout.size.is_some() - && (wrap_width.is_none() || wrap_width == text_layout.wrap_width) + && wrap_width == text_layout.wrap_width + && truncate_width == text_layout.truncate_width { return text_layout.size.unwrap(); } - let mut line_wrapper = cx.text_system().line_wrapper(text_style.font(), font_size); + // Flex layout may measure the same element at several widths. Each + // measurement needs the original runs, including their full lengths. + let mut runs = std::borrow::Cow::Borrowed(&runs); let text = if let Some(truncate_width) = truncate_width { - line_wrapper.truncate_line( - text.clone(), - truncate_width, - &truncation_suffix, - &mut runs, - ) + cx.text_system() + .line_wrapper(text_style.font(), font_size) + .truncate_line( + text.clone(), + truncate_width, + &truncation_suffix, + runs.to_mut(), + ) } else { text.clone() }; @@ -406,6 +412,7 @@ impl TextLayout { len: 0, line_height, wrap_width, + truncate_width, size: Some(Size::default()), bounds: None, }); @@ -424,6 +431,7 @@ impl TextLayout { len, line_height, wrap_width, + truncate_width, size: Some(size), bounds: None, }); diff --git a/vendor/gpui/src/platform/mac/window.rs b/vendor/gpui/src/platform/mac/window.rs index 95efffa..963676b 100644 --- a/vendor/gpui/src/platform/mac/window.rs +++ b/vendor/gpui/src/platform/mac/window.rs @@ -1498,6 +1498,17 @@ impl PlatformWindow for MacWindow { .detach() } + fn start_window_move(&self) { + let window = self.0.lock().native_window; + unsafe { + let app = NSApplication::sharedApplication(nil); + let event: id = msg_send![app, currentEvent]; + if !event.is_null() { + let _: () = msg_send![window, performWindowDragWithEvent: event]; + } + } + } + fn titlebar_double_click(&self) { let this = self.0.lock(); let window = this.native_window;