Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions apps/gpui_components/lib/gpui/ui/overlay.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
"""
Expand Down
5 changes: 1 addition & 4 deletions apps/gpui_native/native/src/element/component/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
19 changes: 15 additions & 4 deletions apps/gpui_native/native/src/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 61 additions & 0 deletions apps/gpui_native/test/test/gpui/test/native/navigation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
<div class="flex flex-col gap-2">
<GPUI.UI.input id="first" label="First" value="" focus_request={1}
phx-change="first_changed" />
<GPUI.UI.input id="second" label="Second" value="" focus_request={assigns.focus}
phx-change="second_changed" />
<Overlay.dialog id="palette" open={assigns.open} title="Palette" width={240}
phx-change="palette_changed">
<:content>
<GPUI.UI.input id="query" label="Query" value={assigns.query} focus_request={1}
phx-change="query_changed" phx-submit="query_submitted" />
</:content>
</Overlay.dialog>
</div>
"""
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, query: "")
settle(ui)
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, query: "")
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

# 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)

Expand Down