diff --git a/CHANGELOG.md b/CHANGELOG.md index 5920c7b1..417670ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +- Dialog Enter no longer requests automatic closure; content handles submission + and the app controls `open`. Explicit focus requests made while closing a + dialog take precedence over restoring its previous focus. + ## 0.2.0 - 2026-09-08 ### Breaking changes diff --git a/apps/gpui_components/lib/gpui/ui/overlay.ex b/apps/gpui_components/lib/gpui/ui/overlay.ex index c9fcca5e..ab150745 100644 --- a/apps/gpui_components/lib/gpui/ui/overlay.ex +++ b/apps/gpui_components/lib/gpui/ui/overlay.ex @@ -78,8 +78,11 @@ defmodule GPUI.UI.Overlay do A non-empty `title` supplies the dialog heading and names its optional trigger. Changes to `open` are emitted through `phx-change`. The native dialog traps - focus while open and restores the previous focus when it closes. Escape and - overlay clicks request closure when enabled. + focus while open and restores the previous focus when it closes, unless another + element requests focus in the same render. Escape and overlay clicks request + closure when enabled. Enter belongs to the content (for example, an input's + `phx-submit`); it does not request closure. Set `open: false` after a successful + submission to close the dialog. #{ComponentsSchema.component_options_doc(:ui_dialog)} """ diff --git a/apps/gpui_native/native/src/element/component/form.rs b/apps/gpui_native/native/src/element/component/form.rs index 43c4ce0f..afa98393 100644 --- a/apps/gpui_native/native/src/element/component/form.rs +++ b/apps/gpui_native/native/src/element/component/form.rs @@ -267,10 +267,7 @@ pub(crate) fn render_input_component( .map(|_value| input.state.read(context.cx).value().to_string()); let focus_handle = input.state.focus_handle(context.cx); if request_focus && node.focus_request > 0 { - let requested_focus = focus_handle.clone(); - context.window.defer(context.cx, move |window, cx| { - requested_focus.focus(window, cx) - }); + crate::element::focus_after_frame(focus_handle.clone(), context.window, context.cx); } let mut element = Input::new(&input.state) .role(gpui::Role::GenericContainer) diff --git a/apps/gpui_native/native/src/element/component/overlay/dialog.rs b/apps/gpui_native/native/src/element/component/overlay/dialog.rs index e7a137a9..e6469b47 100644 --- a/apps/gpui_native/native/src/element/component/overlay/dialog.rs +++ b/apps/gpui_native/native/src/element/component/overlay/dialog.rs @@ -214,6 +214,10 @@ pub(crate) fn render_dialog( .overlay_closable(config.closable) .keyboard(config.keyboard && config.closable) .close_button(config.close_button) + // Enter belongs to the content, such as an input's submit; + // the app decides when the dialog closes. Escape and the + // close button still dismiss it. + .on_ok(|_, _, _| false) .on_close({ let runtime = callback_runtime.clone(); let binding = callback_binding.clone(); diff --git a/apps/gpui_native/native/src/element/component/text_surface.rs b/apps/gpui_native/native/src/element/component/text_surface.rs index 6afad6b9..a0b0d881 100644 --- a/apps/gpui_native/native/src/element/component/text_surface.rs +++ b/apps/gpui_native/native/src/element/component/text_surface.rs @@ -457,10 +457,7 @@ pub(crate) fn render( surface.focus_request = node.focus_request; let focus = surface.state.focus_handle(context.cx); if request_focus && node.focus_request > 0 { - let focus = focus.clone(); - context - .window - .defer(context.cx, move |window, cx| focus.focus(window, cx)); + crate::element::focus_after_frame(focus.clone(), context.window, context.cx); } let decoration_quads = diff --git a/apps/gpui_native/native/src/element/mod.rs b/apps/gpui_native/native/src/element/mod.rs index e726b861..1e67876d 100644 --- a/apps/gpui_native/native/src/element/mod.rs +++ b/apps/gpui_native/native/src/element/mod.rs @@ -202,13 +202,24 @@ fn request_native_focus( }) .unwrap_or(false); if request { - let requested_focus = focus_handle.clone(); - context.window.defer(context.cx, move |window, cx| { - requested_focus.focus(window, cx) - }); + focus_after_frame(focus_handle.clone(), context.window, context.cx); } } +/// Focuses `handle` once the frame's other deferred work has run. A dialog +/// closing in the same render returns focus to where it opened from; an +/// element that asked for focus in that render must keep it. +#[cfg(feature = "real-gpui")] +pub(crate) fn focus_after_frame( + handle: gpui::FocusHandle, + window: &mut gpui::Window, + cx: &mut gpui::App, +) { + window.defer(cx, move |window, cx| { + window.defer(cx, move |window, cx| handle.focus(window, cx)); + }); +} + #[cfg(feature = "real-gpui")] pub(crate) fn apply_focus_contract( mut element: gpui::Div, diff --git a/apps/gpui_native/test/test/gpui/test/native/navigation_test.exs b/apps/gpui_native/test/test/gpui/test/native/navigation_test.exs index 4d2260ba..2036e7f5 100644 --- a/apps/gpui_native/test/test/gpui/test/native/navigation_test.exs +++ b/apps/gpui_native/test/test/gpui/test/native/navigation_test.exs @@ -132,6 +132,67 @@ defmodule GPUI.Test.Native.NavigationTest do assert_receive {:gpui, ^ui, {:event, %{type: :change, event: "dialog_changed", value: true}}} end + defmodule PaletteView do + use GPUI.View + + alias GPUI.UI.Overlay + + @impl GPUI.View + def render(assigns) do + ~GPUI""" +