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

@ -418,7 +418,7 @@ pub(crate) trait PlatformWindow: HasWindowHandle + HasDisplayHandle {
level: PromptLevel,
msg: &str,
detail: Option<&str>,
answers: &[&str],
answers: &[PromptButton],
) -> Option<oneshot::Receiver<usize>>;
fn activate(&self);
fn is_active(&self) -> bool;
@ -1244,6 +1244,58 @@ pub enum PromptLevel {
Critical,
}
/// Prompt Button
#[derive(Clone, Debug, PartialEq)]
pub enum PromptButton {
/// Ok button
Ok(SharedString),
/// Cancel button
Cancel(SharedString),
/// Other button
Other(SharedString),
}
impl PromptButton {
/// Create a button with label
pub fn new(label: impl Into<SharedString>) -> Self {
PromptButton::Other(label.into())
}
/// Create an Ok button
pub fn ok(label: impl Into<SharedString>) -> Self {
PromptButton::Ok(label.into())
}
/// Create a Cancel button
pub fn cancel(label: impl Into<SharedString>) -> Self {
PromptButton::Cancel(label.into())
}
#[allow(dead_code)]
pub(crate) fn is_cancel(&self) -> bool {
matches!(self, PromptButton::Cancel(_))
}
/// Returns the label of the button
pub fn label(&self) -> &SharedString {
match self {
PromptButton::Ok(label) => label,
PromptButton::Cancel(label) => label,
PromptButton::Other(label) => label,
}
}
}
impl From<&str> for PromptButton {
fn from(value: &str) -> Self {
match value.to_lowercase().as_str() {
"ok" => PromptButton::Ok("Ok".into()),
"cancel" => PromptButton::Cancel("Cancel".into()),
_ => PromptButton::Other(SharedString::from(value.to_owned())),
}
}
}
/// The style of the cursor (pointer)
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum CursorStyle {