From 42e34e2acf5232928bc5f3fe092733d41b09cfe3 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 08:50:16 +0000 Subject: [PATCH] egui: Memory::set_focus_surrender, a focused widget's own rule for a press or click elsewhere --- PATCHES.md | 1 + crates/egui/src/context.rs | 2 +- crates/egui/src/memory/mod.rs | 30 ++++++++++++- crates/egui/tests/focus_surrender.rs | 66 ++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 crates/egui/tests/focus_surrender.rs diff --git a/PATCHES.md b/PATCHES.md index b8bfef7a..9a741ae6 100644 --- a/PATCHES.md +++ b/PATCHES.md @@ -36,6 +36,7 @@ Drop a patch when an egui release in use contains it. | E28 | on `craft/framework-patches` | egui | `Context::set_layer_order(&[LayerId])`: layers the caller lists end each pass above every unlisted layer of the same `Order`, in list order, each with its unlisted sublayers directly above it, after egui's own sort and sublayer splice; `move_to_top` and `Area` raise-on-press then reorder only unlisted layers; `order_map`, `compare_order` and the modal gate follow the final order | upstreamable | craft-ui overlay layer order | | E29 | on `craft/framework-patches` | egui | `Context::register_accesskit_parent` is public: a member with no egui widget registers its id and its parent `Ui`'s unique id before it creates its node | upstreamable | craft-ui arena members | | E30 | on `craft/kittest-input-hook` | egui_kittest | the eframe harness (`HarnessBuilder::build_eframe`) runs `App::raw_input_hook` on each pass's input before the pass, as eframe does every frame, so an app's before-input work runs in its tests; the `Ui` harnesses are unchanged | upstreamable | craft-ui `embed::before_input` in an app's `raw_input_hook` (PhotoCraft) | +| E31 | on `craft/focus-surrender` | egui | `Memory::set_focus_surrender(id, SurrenderFocusOn)`: the focused widget's own rule for a press or click elsewhere, in place of `InputOptions::surrender_focus_on`, while it keeps focus; egui's click-away reads it first | upstreamable | craft-ui: the runtime's focus moves only by its press rule, the app's own widgets keep egui's rule with the app's option | E1 changes from `e7e37ad`: the hooks are installed in `init_native`, so `create_native` uses them too, and a unit test covers installing and replacing them. E2+E3 is the same change as `b02321d` with a `surface_fit` diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index e2beb076..73c948fc 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -1904,7 +1904,7 @@ impl Context { } if apply_click_away && !res.hovered() { - let triggered = match memory.options.input_options.surrender_focus_on { + let triggered = match memory.focus_surrender() { SurrenderFocusOn::Presses => input.pointer.any_pressed(), SurrenderFocusOn::Clicks => input.pointer.any_click(), SurrenderFocusOn::Never => false, diff --git a/crates/egui/src/memory/mod.rs b/crates/egui/src/memory/mod.rs index 0903da64..4bc949bb 100644 --- a/crates/egui/src/memory/mod.rs +++ b/crates/egui/src/memory/mod.rs @@ -8,7 +8,7 @@ use epaint::emath::TSTransform; use crate::{ EventFilter, Id, IdMap, LayerId, Order, PointerButton, Pos2, Rangef, RawInput, Rect, Style, - Vec2, ViewportId, ViewportIdMap, ViewportIdSet, area, vec2, + SurrenderFocusOn, Vec2, ViewportId, ViewportIdMap, ViewportIdSet, area, vec2, }; mod theme; @@ -582,6 +582,10 @@ pub(crate) struct Focus { struct FocusWidget { pub id: Id, pub filter: EventFilter, + + /// The widget's own rule for a press or click elsewhere, in place of + /// [`crate::InputOptions::surrender_focus_on`]; set with [`Memory::set_focus_surrender`]. + pub surrender: Option, } impl FocusWidget { @@ -589,6 +593,7 @@ impl FocusWidget { Self { id, filter: Default::default(), + surrender: None, } } } @@ -996,9 +1001,32 @@ impl Memory { self.focus_mut().focused_widget = Some(FocusWidget { id, filter: event_filter, + surrender: None, }); } + /// Set the rule by which the focused widget `id` surrenders focus when the user presses or + /// clicks somewhere else, in place of [`crate::InputOptions::surrender_focus_on`]. + /// + /// Takes effect at once and lasts while `id` keeps focus: any focus change, a request + /// for `id` itself included, goes back to the option. Does nothing if `id` has no focus. + pub fn set_focus_surrender(&mut self, id: Id, surrender: SurrenderFocusOn) { + if let Some(focused) = &mut self.focus_mut().focused_widget + && focused.id == id + { + focused.surrender = Some(surrender); + } + } + + /// The rule by which the focused widget surrenders focus on a press or click elsewhere: its + /// own ([`Self::set_focus_surrender`]), or [`crate::InputOptions::surrender_focus_on`]. + pub(crate) fn focus_surrender(&self) -> SurrenderFocusOn { + self.focus() + .and_then(|focus| focus.focused_widget) + .and_then(|focused| focused.surrender) + .unwrap_or(self.options.input_options.surrender_focus_on) + } + /// Defer focus until the next pass so an already rendered widget sees `gained_focus`. pub(crate) fn request_focus_next_frame(&mut self, id: Id) { if self.focused() != Some(id) { diff --git a/crates/egui/tests/focus_surrender.rs b/crates/egui/tests/focus_surrender.rs new file mode 100644 index 00000000..48b045f7 --- /dev/null +++ b/crates/egui/tests/focus_surrender.rs @@ -0,0 +1,66 @@ +use egui::{ + Context, Event, Id, Modifiers, PointerButton, RawInput, Rect, Sense, SurrenderFocusOn, pos2, + vec2, +}; + +const FIELD: Rect = Rect::from_min_max(pos2(10.0, 10.0), pos2(110.0, 40.0)); + +/// Runs one pass with `events`, showing a focusable widget `id` at `FIELD`; `before` runs first. +fn pass(ctx: &Context, id: Id, events: Vec, before: impl Fn(&Context)) { + let mut output = ctx.run_ui( + RawInput { + screen_rect: Some(Rect::from_min_size(pos2(0.0, 0.0), vec2(400.0, 300.0))), + events, + ..Default::default() + }, + |ui| { + before(ui.ctx()); + ui.interact(FIELD, id, Sense::click()); + }, + ); + output.textures_delta.clear(); +} + +/// Whether `id`, focused (with its own rule `own`, if any), keeps focus through a click +/// outside it. +fn keeps_focus_through_a_click_elsewhere(own: Option) -> bool { + let ctx = Context::default(); + let id = Id::unique("field"); + pass(&ctx, id, Vec::new(), |_| {}); + pass(&ctx, id, Vec::new(), |ctx| { + ctx.memory_mut(|memory| { + memory.request_focus(id); + if let Some(own) = own { + memory.set_focus_surrender(id, own); + } + }); + }); + let outside = pos2(300.0, 200.0); + let button = |pressed| Event::PointerButton { + pos: outside, + button: PointerButton::Primary, + pressed, + modifiers: Modifiers::NONE, + }; + pass(&ctx, id, vec![Event::PointerMoved(outside)], |_| {}); + pass(&ctx, id, vec![button(true)], |_| {}); + pass(&ctx, id, vec![button(false)], |_| {}); + pass(&ctx, id, Vec::new(), |_| {}); + ctx.memory(|memory| memory.has_focus(id)) +} + +#[test] +fn a_focused_widget_s_own_surrender_rule_wins_over_the_option() { + assert_eq!( + Context::default().options(|options| options.input_options.surrender_focus_on), + SurrenderFocusOn::Clicks + ); + assert!( + !keeps_focus_through_a_click_elsewhere(None), + "the option surrenders on a click elsewhere" + ); + assert!( + keeps_focus_through_a_click_elsewhere(Some(SurrenderFocusOn::Never)), + "the widget's own rule keeps it" + ); +}