gpui: Improve window.prompt to support ESC with non-English cancel text on macOS (#29538)

Release Notes:

- N/A

----

The before version GPUI used `Cancel` for cancel text, if we use
non-English text (e.g.: "取消" in Chinese), then the press `Esc` to cancel
will not work.

So this PR to change it by use `PromptButton` to instead the `&str`,
then we can use `PromptButton::cancel("取消")` for the `Cancel` button.

Run `cargo run -p gpui --example window` to test.

---

Platform Test:

- [x] macOS
- [x] Windows
- [x] Linux (x11 and Wayland)

---------

Co-authored-by: Mikayla Maki <mikayla@zed.dev>
Co-authored-by: Mikayla Maki <mikayla.c.maki@gmail.com>
This commit is contained in:
Jason Lee 2025-05-30 23:26:27 +08:00 committed by GitHub
parent 1d5d3de85c
commit 047e7eacec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 174 additions and 66 deletions

View file

@ -9,13 +9,13 @@ use crate::{
KeyDownEvent, KeyEvent, Keystroke, KeystrokeEvent, LayoutId, LineLayoutIndex, Modifiers,
ModifiersChangedEvent, MonochromeSprite, MouseButton, MouseEvent, MouseMoveEvent, MouseUpEvent,
Path, Pixels, PlatformAtlas, PlatformDisplay, PlatformInput, PlatformInputHandler,
PlatformWindow, Point, PolychromeSprite, PromptLevel, Quad, Render, RenderGlyphParams,
RenderImage, RenderImageParams, RenderSvgParams, Replay, ResizeEdge, SMOOTH_SVG_SCALE_FACTOR,
SUBPIXEL_VARIANTS, ScaledPixels, Scene, Shadow, SharedString, Size, StrikethroughStyle, Style,
SubscriberSet, Subscription, TaffyLayoutEngine, Task, TextStyle, TextStyleRefinement,
TransformationMatrix, Underline, UnderlineStyle, WindowAppearance, WindowBackgroundAppearance,
WindowBounds, WindowControls, WindowDecorations, WindowOptions, WindowParams, WindowTextSystem,
point, prelude::*, px, rems, size, transparent_black,
PlatformWindow, Point, PolychromeSprite, PromptButton, PromptLevel, Quad, Render,
RenderGlyphParams, RenderImage, RenderImageParams, RenderSvgParams, Replay, ResizeEdge,
SMOOTH_SVG_SCALE_FACTOR, SUBPIXEL_VARIANTS, ScaledPixels, Scene, Shadow, SharedString, Size,
StrikethroughStyle, Style, SubscriberSet, Subscription, TaffyLayoutEngine, Task, TextStyle,
TextStyleRefinement, TransformationMatrix, Underline, UnderlineStyle, WindowAppearance,
WindowBackgroundAppearance, WindowBounds, WindowControls, WindowDecorations, WindowOptions,
WindowParams, WindowTextSystem, point, prelude::*, px, rems, size, transparent_black,
};
use anyhow::{Context as _, Result, anyhow};
use collections::{FxHashMap, FxHashSet};
@ -3821,28 +3821,36 @@ impl Window {
/// Present a platform dialog.
/// The provided message will be presented, along with buttons for each answer.
/// When a button is clicked, the returned Receiver will receive the index of the clicked button.
pub fn prompt(
pub fn prompt<T>(
&mut self,
level: PromptLevel,
message: &str,
detail: Option<&str>,
answers: &[&str],
answers: &[T],
cx: &mut App,
) -> oneshot::Receiver<usize> {
) -> oneshot::Receiver<usize>
where
T: Clone + Into<PromptButton>,
{
let prompt_builder = cx.prompt_builder.take();
let Some(prompt_builder) = prompt_builder else {
unreachable!("Re-entrant window prompting is not supported by GPUI");
};
let answers = answers
.iter()
.map(|answer| answer.clone().into())
.collect::<Vec<_>>();
let receiver = match &prompt_builder {
PromptBuilder::Default => self
.platform_window
.prompt(level, message, detail, answers)
.prompt(level, message, detail, &answers)
.unwrap_or_else(|| {
self.build_custom_prompt(&prompt_builder, level, message, detail, answers, cx)
self.build_custom_prompt(&prompt_builder, level, message, detail, &answers, cx)
}),
PromptBuilder::Custom(_) => {
self.build_custom_prompt(&prompt_builder, level, message, detail, answers, cx)
self.build_custom_prompt(&prompt_builder, level, message, detail, &answers, cx)
}
};
@ -3857,7 +3865,7 @@ impl Window {
level: PromptLevel,
message: &str,
detail: Option<&str>,
answers: &[&str],
answers: &[PromptButton],
cx: &mut App,
) -> oneshot::Receiver<usize> {
let (sender, receiver) = oneshot::channel();