From 217eeffde49f9cfba7faa447d74a25a73370c53a Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Sun, 18 Oct 2026 16:29:00 -0400 Subject: [PATCH 1/3] Keep focus an element asks for while a dialog closes Closing a dialog returns focus to where it opened from. A focus request made in the same render, such as a palette command focusing an editor, ran before that and lost. Inputs, text surfaces, terminals and focusable elements now apply focus requests after the frame's other deferred work. --- .../native/src/element/component/form.rs | 5 +-- .../src/element/component/text_surface.rs | 5 +-- apps/gpui_native/native/src/element/mod.rs | 19 +++++++-- .../test/gpui/test/native/navigation_test.exs | 41 +++++++++++++++++++ 4 files changed, 58 insertions(+), 12 deletions(-) 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/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..a6965602 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,47 @@ 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""" +
+ + + + <:content> + + + +
+ """ + end + end + + # A command picked in a palette can ask for focus as the palette closes; the + # dialog returning focus to where it opened from must not undo that. + test "focus asked for while a dialog closes stays there", %{ui: ui} do + render(ui, PaletteView, open: false, focus: 0) + settle(ui) + render(ui, PaletteView, open: true, focus: 0) + settle(ui) + + render(ui, PaletteView, open: false, focus: 1) + settle(ui) + type(ui, "x") + + assert_receive {:gpui, ^ui, {:event, %{type: :change, event: "second_changed", value: "x"}}} + refute_received {:gpui, ^ui, {:event, %{event: "first_changed"}}} + end + test "accordion items expose stable targets and controlled pointer changes", %{ui: ui} do render_accordion(ui) From 2483ec2ef6dd68f94f4b71fcf1a6679d795d6341 Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Sun, 18 Oct 2026 16:33:54 -0400 Subject: [PATCH 2/3] Leave dialogs open on Enter; the app closes them Enter in a dialog ran gpui-component's confirm action, which closed the dialog and, after its close animation, put focus back where the dialog opened from. A palette command that focused an editor lost focus, and Enter in a form field inside a dialog closed the dialog and discarded the form. GPUI dialogs have no confirm action: the app owns their open state, and Escape and the close button still dismiss them. --- .../src/element/component/overlay/dialog.rs | 4 +++ .../test/gpui/test/native/navigation_test.exs | 30 +++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) 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/test/test/gpui/test/native/navigation_test.exs b/apps/gpui_native/test/test/gpui/test/native/navigation_test.exs index a6965602..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 @@ -148,8 +148,8 @@ defmodule GPUI.Test.Native.NavigationTest do <:content> - + @@ -160,12 +160,17 @@ defmodule GPUI.Test.Native.NavigationTest do # A command picked in a palette can ask for focus as the palette closes; the # dialog returning focus to where it opened from must not undo that. test "focus asked for while a dialog closes stays there", %{ui: ui} do - render(ui, PaletteView, open: false, focus: 0) + render(ui, PaletteView, open: false, focus: 0, query: "") settle(ui) - render(ui, PaletteView, open: true, focus: 0) + render(ui, PaletteView, open: true, focus: 0, query: "") settle(ui) + type(ui, "q") + assert_receive {:gpui, ^ui, {:event, %{type: :change, event: "query_changed", value: "q"}}} + render(ui, PaletteView, open: true, focus: 0, query: "q") + press(ui, :enter) + assert_receive {:gpui, ^ui, {:event, %{type: :submit, event: "query_submitted"}}} - render(ui, PaletteView, open: false, focus: 1) + render(ui, PaletteView, open: false, focus: 1, query: "") settle(ui) type(ui, "x") @@ -173,6 +178,21 @@ defmodule GPUI.Test.Native.NavigationTest do refute_received {:gpui, ^ui, {:event, %{event: "first_changed"}}} end + # The app owns a dialog's open state; Enter in its content is the content's. + test "Enter in a dialog's input submits it and leaves the dialog open", %{ui: ui} do + render(ui, PaletteView, open: true, focus: 0, query: "") + settle(ui) + type(ui, "q") + assert_receive {:gpui, ^ui, {:event, %{type: :change, event: "query_changed", value: "q"}}} + render(ui, PaletteView, open: true, focus: 0, query: "q") + press(ui, :enter) + + assert_receive {:gpui, ^ui, {:event, %{type: :submit, event: "query_submitted"}}} + refute_receive {:gpui, ^ui, {:event, %{event: "palette_changed", value: false}}} + type(ui, "r") + assert_receive {:gpui, ^ui, {:event, %{type: :change, event: "query_changed", value: "qr"}}} + end + test "accordion items expose stable targets and controlled pointer changes", %{ui: ui} do render_accordion(ui) From 5c78ef3437dd4a6e60bec457b84bdfcf469fdfdb Mon Sep 17 00:00:00 2001 From: Jason Stiebs Date: Mon, 19 Oct 2026 08:44:30 -0500 Subject: [PATCH 3/3] Document controlled dialog submission and focus precedence --- CHANGELOG.md | 4 ++++ apps/gpui_components/lib/gpui/ui/overlay.ex | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) 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)} """