Skip to content

Overlays: dismiss causes and focus_on_open on owned chains #326

Open

Description

@samitbasu

Gaps found by a consumer:

  1. A popover can't be dismissed on "outside press and Escape" only. DismissOn offers Default (which includes focus leaving and window blur), EscOnly and Never, and on_dismiss isn't told why it fired. A popover opened on a screenshot run's first frame closes at once. Proposal: DismissOn::Outside (outside press + Escape), or on_dismiss_with(|app, cause|).
  2. focus_on_open exists only on NodeHandle, so owned controls can't be the one focused when the overlay opens. Proposal: .focus_on_open() on the owned chains (see One options layer for owned controls: Options, Configure/Interactive traits, refused(reason), when/when_some #315's Focus trait).

Activity

  1. added
    consumer
    on Nov 17, 2026
  2. samitbasu commented on Nov 18, 2026

    @samitbasu
    OwnerAuthor

    Design note: overlays (two items)

    No code yet. This is written against main 584e09e. The places in core:

    • src/overlay.rs: DismissOn and its outside/esc/focus_out predicates;
    • src/dismiss.rs: dismissals, which finds each dismissal with a crate-internal Cause;
    • src/overlay_input.rs: judge, which runs the on_dismiss handlers, and opening_focus;
    • src/overlay_chain.rs: dismiss_on and on_dismiss;
    • src/builder.rs: NodeHandle::focus_on_open;
    • src/pipeline.rs: OpenFocus.

    1. Dismiss causes

    Today: DismissOn has three variants:

    • Default: a press outside, Esc, focus leaving, and the window losing focus;
    • EscOnly;
    • Never.

    The library already knows why it dismisses. dismiss::Cause is Outside | Esc | Cancel | FocusOut | WindowBlur | Exclusive | AnchorGone, but it's crate-internal, and judge drops it before calling the handler.

    Probable root cause of the consumer's report. A screenshot run's window often starts without OS focus, so egui-winit sends WindowFocused(false) early. The WindowBlur rule then dismisses any popover already drawn: "a popover opened on the first frame closes at once". I still need to confirm this with a test. If it holds, it's a bug worth fixing whatever the API: a window that never had focus while the overlay was open hasn't lost it. The fix is for WindowBlur to dismiss only an overlay that was drawn while the window had focus. It's small, in dismissals plus one per-overlay flag kept on the overlay stack.

    API: both proposals, since they answer different needs.

    pub enum DismissOn {
        #[default] Default,   // outside press, Esc, focus out, window blur
        Outside,              // NEW: outside press and Esc only
        EscOnly,
        Never,
    }
    
    #[non_exhaustive]
    pub enum Dismissed {      // public: the crate-internal Cause, less Cancel
        Outside, Esc, FocusOut, WindowBlur,
        Exclusive,            // another overlay that takes the screen opened
        AnchorGone,           // the overlay's anchor isn't in the frame
    }
    
    impl OverlayChain {
        pub fn on_dismiss(self, then: impl FnOnce(&mut A) + 'static) -> Self;                // unchanged
        pub fn on_dismiss_with(self, then: impl FnOnce(&mut A, Dismissed) + 'static) -> Self; // NEW
    }
    • DismissOn::Outside is a policy: what may dismiss at all. It belongs in the declaration, so an app doesn't have to re-open an overlay it didn't want closed (which would also flash its motion and move focus).
    • on_dismiss_with is information: an app that closes on every cause but logs or animates differently, or one that wants Escape to revert edits and outside presses to keep them. Only one of the two handlers is kept, the last one set.
    • Cancel isn't public. When Escape is answered by the overlay's cancel button, the button's handler runs instead of on_dismiss, as today.
    • Exclusive stays only for Default. The rule in exclusive() that a menu closes when another overlay opens keeps its dismiss_on == DismissOn::Default guard.
    • Changes in core:
      • the predicates in overlay.rs gain Outside: outside() and esc() true, focus_out() false;
      • blur's dismisses_on_focus_loss follows focus_out;
      • DismissFn<A> becomes Box<dyn FnOnce(&mut A, Dismissed)>, with on_dismiss wrapping its handler to ignore the cause. That keeps one handler type, so mounts' lift and the owned chains' plumbing change in one place;
      • judge passes found.cause through, as does nav::navigate's dismissal, which closes a menu from its navigation keys (a new Dismissed::Keys; that's a question below).
    • Owned chains that wrap an overlay (select, dropdown menu, popover, dialog, combobox) gain dismiss_on and on_dismiss_with where they already expose on_dismiss or an open/close handler. Exactly which chains is listed in the PR.
    • Tests, through the real draw path:
      • each cause reaches on_dismiss_with;
      • Outside ignores focus leaving and a window blur, and still closes on an outside press and on Escape;
      • a popover open on the first frame of a window without focus stays open (the blur fix).

    2. .focus_on_open() on the owned chains

    Today:

    • NodeHandle::focus_on_open marks a node (OpenFocus::Here).
    • When its overlay first enters focus, opening_focus picks the first marked node, if it registered a focusable widget, and otherwise the overlay's first focusable. Two marked nodes report FocusOnOpenTwice.
    • The menu item chain has its own focus_on_open.
    • The other owned chains have none, because the focusable widget is an inner node the caller can't reach: a stepper's field leaf, a slider's thumb, a text field's leaf.

    Why focus_on_appear doesn't stand in for it: inside an overlay, a control's focus_on_appear requests focus during the draw. But judge runs at the end of the same draw and requests the overlay's opening focus, and the later request wins. So today an owned control inside an overlay can't take focus as the overlay opens. That's the consumer's gap.

    API: one method with the same name and meaning as NodeHandle's, on every owned chain whose control takes focus:

    b.stepper("qty", qty, 1.0..=99.0).focus_on_open().on_change(..);
    b.text_field("name", &name).focus_on_open().on_change(..);

    Each chain marks the node that holds its focusable widget: the button's own node, the stepper's field leaf, the slider's thumb, the text field's leaf, the select's trigger, and so on. That node is the one each chain's Autofocus already targets, so the mapping is known per control, and it's encoded once per control.

    How it fits #315, and whether to wait:

    Questions for the user, with my recommendations

    1. The blur fix: should a window blur dismiss only an overlay that was shown while the window had focus? This would fix the first-frame close whatever else is chosen. Recommend yes, as its own commit, with a test first.
    2. Both DismissOn::Outside and on_dismiss_with, or one? Recommend both: one is policy, the other information.
    3. The cause type's name: Dismissed or DismissCause? Recommend Dismissed (|app, why: Dismissed|).
    4. A menu its navigation keys close (nav::navigate returns overlays to dismiss; the PR will list exactly which keys): its own cause, Dismissed::Keys, or folded into Esc? Recommend its own cause, so an app can tell keyboard navigation out of a menu from a cancel.
    5. focus_on_open now, or after One options layer for owned controls: Options, Configure/Interactive traits, refused(reason), when/when_some #315? Recommend now, built so One options layer for owned controls: Options, Configure/Interactive traits, refused(reason), when/when_some #315 only moves it.
    6. One enum (Autofocus::OnOverlayOpen) instead of a separate flag? Recommend the enum.
    7. Which chains first: every focusable owned control, or just those the consumer needs? Recommend all of them, since each is a one-liner over the shared piece and leaving gaps is the drift One options layer for owned controls: Options, Configure/Interactive traits, refused(reason), when/when_some #315 complains about.

    Suggested PRs

    1. The blur fix (test first).
    2. DismissOn::Outside plus Dismissed/on_dismiss_with.
    3. focus_on_open on the owned chains.
  3. added a commit that references this issue on Nov 18, 2026
  4. samitbasu commented on Nov 18, 2026

    @samitbasu
    OwnerAuthor

    Decisions (user, 2026-11-18):

    • Dismissal: both DismissOn::Outside (an outside press or Escape only) and on_dismiss_with(|app, why|), with a public #[non_exhaustive] Dismissed { Outside, Esc, FocusOut, WindowBlur, Exclusive, AnchorGone, Keys }. on_dismiss wraps it and ignores the cause.
    • Focus on open: .focus_on_open() on every focusable owned chain NOW, as Autofocus::OnOverlayOpen (so it can't be combined with focus_on_appear); One options layer for owned controls: Options, Configure/Interactive traits, refused(reason), when/when_some #315's Focus trait absorbs it later.
    • Names: Dismissed, with its own Keys cause.

    The blur fix already merged (#365).

  5. samitbasu commented on Nov 19, 2026

    @samitbasu
    OwnerAuthor

    Coordination note from the #372/#21 dialog work (core C1), so the two dismissal changes meet cleanly:

    • C1 adds ButtonChain::dismisses(): after its own handler, the button dismisses its overlay through on_dismiss. In dismiss.rs this is a new crate-internal Cause::Closed, found in dismissals like the others (a per-Layer flag set from the buttons pressed this pass), so the dismissal path stays single. judge treats it like any non-Cancel cause.
    • For Dismissed: that cause needs a public variant when on_dismiss_with lands. I suggest Dismissed::Closed ("a button marked dismisses() was pressed: the dialog's × or a Cancel with no handler"). Dismissed is #[non_exhaustive], so whichever PR lands second adds the variant and the mapping; it's one match arm.
    • focus_on_open on owned chains: alert_dialog wants first focus on Cancel. Until ButtonChain::focus_on_open() lands from this issue, the dialog relies on build order (Cancel is the first focusable of a field-less alert dialog, and the × is never built in one). When yours lands I'll switch the footer's Cancel to it in a one-line follow-up; I'm not adding a focus_on_open to ButtonChain myself, so nothing collides.

    The PR will be linked here when it's up.

  6. samitbasu commented on Nov 19, 2026

    @samitbasu
    OwnerAuthor

    The C1 PR is up: #409 (ButtonChain::dismisses, crate-internal Cause::Closed in dismissals). When Dismissed lands, it needs a Closed variant for it.

  7. added a commit that references this issue on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    consumer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions