Focus record keeps what moved focus (press or key/request); a dialog host opened in a pass takes that pass's Escape

This commit is contained in:
aiongg
2026-11-17 00:28:28 +00:00
parent f4656014b0
commit 78ac1558e1
5 changed files with 46 additions and 14 deletions
+5 -6
View File
@@ -5,7 +5,7 @@ use crate::contract::Component;
use crate::runtime::focus::{self};
use crate::runtime::keys::{Intents, MenuKey};
use crate::runtime::measure;
use craft_behavior::{MoveMods, NavMove, PartRef, PressPhase};
use craft_behavior::{MoveMods, NavMove, PartRef};
use craft_keymap::Chord;
use egui::{Rect, Vec2};
@@ -23,8 +23,8 @@ pub trait Focus {
/// every pass of the frame; `None` when focus stayed on its part, left the component, or sits on the component
/// itself.
fn focus_arrived(&self) -> Option<PartRef>;
/// [`focus_arrived`](Focus::focus_arrived) when the part is not under a press: focus came by the keyboard or a
/// focus request (Photoshop: Tab into a field selects its text, a click does not). A press on the part reports
/// [`focus_arrived`](Focus::focus_arrived) when the focus record says the keyboard or a
/// focus request moved it (Photoshop: Tab into a field selects its text, a click does not). A press reports
/// none; the press selects by its own rule.
fn focus_arrived_by_key(&self) -> Option<PartRef>;
/// The component holds key focus.
@@ -69,9 +69,8 @@ impl Focus for CxCore<'_> {
}
fn focus_arrived_by_key(&self) -> Option<PartRef> {
self.focus_arrived().filter(|part| {
self.results.get(&(part.key, part.index)).is_some_and(|result| matches!(result.press.phase, PressPhase::Idle | PressPhase::Hover))
})
let facts = if focus::writes() == self.focus.at { self.focus } else { focus::facts(&self.ctx, self.id) };
self.focus_arrived().filter(|_| facts.arrived_origin == Some(focus::FocusOrigin::Request))
}
fn is_focused(&self) -> bool {
+16 -5
View File
@@ -154,7 +154,7 @@ pub(crate) struct State {
/// The frame whose begin hook ran last; a move in the end hook belongs to the next frame.
pub(super) frame: u64,
/// The stop focus moved onto and the frame whose passes see it.
pub(super) arrived: Option<(u64, StopKey)>,
pub(super) arrived: Option<(u64, StopKey, FocusOrigin)>,
/// The running pass's begin hook left a Tab to egui (`NavKeys::tab_to_egui`); its end hook takes it.
pub(super) tab_to_egui: bool,
}
@@ -612,6 +612,15 @@ pub(crate) fn is_focused(ctx: &Context, id: Id) -> bool {
focused_stop(ctx).is_some_and(|(focused, _)| focused == id)
}
/// What moved focus onto a stop.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum FocusOrigin {
/// A pointer press on the stop.
Press,
/// The keyboard (Tab, arrows, Page keys) or a focus request.
Request,
}
/// What a component reads of focus in one pass.
#[derive(Clone, Copy, Default)]
#[doc(hidden)]
@@ -625,6 +634,8 @@ pub struct Facts {
pub(crate) part: Option<PartRef>,
/// The part focus moved onto in this frame, when the component holds focus on it.
pub(crate) arrived: Option<PartRef>,
/// What moved focus onto `arrived`.
pub(crate) arrived_origin: Option<FocusOrigin>,
/// The count of focus writes when the facts were read; see `writes`.
pub(crate) at: u64,
}
@@ -634,11 +645,11 @@ pub(crate) fn facts(ctx: &Context, id: Id) -> Facts {
let at = writes();
match focused_stop(ctx) {
Some((focused, part)) if focused == id => {
let (visible, arrived) = with(ctx, |state| {
let arrived = state.arrived.filter(|(frame, key)| *frame == state.frame && key.0 == id).and_then(|(_, key)| key.1);
(state.visible, arrived)
let (visible, arrived, arrived_origin) = with(ctx, |state| {
let arrival = state.arrived.filter(|(frame, key, _)| *frame == state.frame && key.0 == id);
(state.visible, arrival.and_then(|(_, key, _)| key.1), arrival.map(|(.., origin)| origin))
});
Facts { focused: true, visible, part, arrived, at }
Facts { focused: true, visible, part, arrived, arrived_origin, at }
}
_ => Facts { at, ..Facts::default() },
}
+7 -3
View File
@@ -1,7 +1,7 @@
//! Focus movement: Tab and F6, roving groups, initial focus, restore, programmatic requests and focus after
//! removal. Every movement reads last pass's registrations and returns the egui changes to apply.
use super::{Act, BeginIn, FocusTarget, Initial, PickedIn, State, StopKey, StopRec, TabReach};
use super::{Act, BeginIn, FocusOrigin, FocusTarget, Initial, PickedIn, State, StopKey, StopRec, TabReach};
use crate::runtime::egui::focus as egui_focus;
use crate::runtime::egui::memory::FastMap;
use craft_behavior::focus::after_removal;
@@ -165,7 +165,7 @@ impl State {
/// is recorded under that root.
pub(super) fn focus_stop(&mut self, rec: StopRec) -> Vec<Act> {
let key = rec.stop.key();
let acts = self.focus_stop_in_place(rec);
let acts = self.move_focus(rec, FocusOrigin::Request);
self.revealed = Some(key);
acts
}
@@ -173,6 +173,10 @@ impl State {
/// Moves focus to `rec` without a reveal: a press focuses a stop under the pointer, which is on screen already,
/// and scrolling it in would move the content under the held press.
pub(super) fn focus_stop_in_place(&mut self, rec: StopRec) -> Vec<Act> {
self.move_focus(rec, FocusOrigin::Press)
}
fn move_focus(&mut self, rec: StopRec, origin: FocusOrigin) -> Vec<Act> {
self.note_entry(&rec);
let stop = &rec.stop;
let roving = stop.group.is_some();
@@ -186,7 +190,7 @@ impl State {
);
let moved = self.focus_now != Some(stop.root) || self.focused.get(&stop.root) != Some(&stop.key());
if moved {
self.arrived = Some((self.frame, stop.key()));
self.arrived = Some((self.frame, stop.key(), origin));
}
self.focused.insert(stop.root, stop.key());
self.focus_now = Some(stop.root);
@@ -633,6 +633,13 @@ pub fn stack_place<R>(ctx: &Context, place: usize, dialog: Id, show: impl FnOnce
out
}
/// A dialog host opened a dialog in this pass: it is the top layer from now on, so the Escape that arrived in this
/// pass is its, and the focused stop beneath it gets no Cancel.
#[doc(hidden)]
pub fn take_keys_for_new_dialog(ctx: &Context) {
crate::runtime::keys::take_cancel(ctx);
}
/// A dialog host removed `dialog`: the layers it showed for it (`stack_place`) in the running or the last pass end
/// their shown life at once (`memory::end_shown`), as a container that shows nothing does, so the next open of the
/// dialog is a first pass.
+11
View File
@@ -358,6 +358,17 @@ impl DialogHost {
if self.is_open::<D>() { Err(dialog) } else { Ok(self.open(dialog)) }
}
/// [`open_unless_open`](Self::open_unless_open) from inside a pass: a dialog it opens takes the Escape that arrived
/// in this pass, so the focused component beneath (a canvas's frame part, say) gets no Cancel. Plain `open` has no
/// `Context`, and the dialog shows from the next pass, after the components beneath have read the key.
pub fn open_unless_open_in<D: StackDialog>(&mut self, ctx: &Context, dialog: D) -> Result<DialogId, D> {
let opened = self.open_unless_open(dialog);
if opened.is_ok() {
crate::runtime::overlay::take_keys_for_new_dialog(ctx);
}
opened
}
/// Opens `dialog` as a child of `parent`: shown over it, disabled with it, closed when the
/// parent closes (a contour editor from Layer Style).
pub fn open_child<D: StackDialog>(&mut self, dialog: D, parent: DialogId) -> DialogId {