craft-ui: a focus scope keeps the opener of the pass it first appeared in, in a run and a replay alike

A replayed scope takes its opener by the same rule as a run (the opener is focus-service state, not view output), and
transcripts compare without it. Transcript::first_difference names the first record two transcripts differ in.
This commit is contained in:
aiongg
2026-11-19 20:32:54 +00:00
parent ff64465e3a
commit fe40ab109b
2 changed files with 64 additions and 8 deletions
+39 -7
View File
@@ -19,6 +19,7 @@ use crate::runtime::egui::memory::FastMap;
use crate::runtime::frame::PerViewport;
use craft_behavior::{DocKey, OwnerClass, PartRef};
use egui::{Context, Id, ViewportId};
use std::fmt;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
@@ -279,7 +280,8 @@ impl Transcript {
}
/// The transcript as a replay registers it with `outer` the innermost open scope: a record under the scope that
/// was open around the run goes under `outer`, and a replayed scope is no new scope (`entering` is false).
/// was open around the run goes under `outer`, a replayed scope is no new scope (`entering` is false), and its
/// opener is the focus service's, not the run's (`opener`).
fn placed(&self, outer: Option<Id>) -> Transcript {
let place = |scope: Option<Id>| if scope == self.outer { outer } else { scope };
Transcript {
@@ -287,7 +289,9 @@ impl Transcript {
scopes: self
.scopes
.iter()
.map(|(id, scope)| (*id, ScopeRec { parent: place(scope.parent), entering: scope.entering.map(|_| false), ..scope.clone() }))
.map(|(id, scope)| {
(*id, ScopeRec { parent: place(scope.parent), entering: scope.entering.map(|_| false), opener: None, ..scope.clone() })
})
.collect(),
groups: self.groups.clone(),
row_sets: self.row_sets.clone(),
@@ -296,10 +300,25 @@ impl Transcript {
}
}
impl Transcript {
/// The first record in which the replays of `self` and `other` differ (`placed`): the kind, its index and both
/// sides, `None` on a side that has fewer records. `None` when they register the same.
pub(crate) fn first_difference(&self, other: &Self) -> Option<String> {
fn first<T: PartialEq + fmt::Debug>(kind: &str, left: &[T], right: &[T]) -> Option<String> {
let index = left.iter().zip(right).position(|(a, b)| a != b).or_else(|| (left.len() != right.len()).then(|| left.len().min(right.len())))?;
Some(format!("{kind} {index}: left {:?} / right {:?}", left.get(index), right.get(index)))
}
let [a, b] = [self.placed(None), other.placed(None)];
first("stop", &a.stops, &b.stops)
.or_else(|| first("scope", &a.scopes, &b.scopes))
.or_else(|| first("roving group", &a.groups, &b.groups))
.or_else(|| first("row groups", &a.row_sets, &b.row_sets))
}
}
impl PartialEq for Transcript {
fn eq(&self, other: &Self) -> bool {
let [a, b] = [self.placed(None), other.placed(None)];
(a.stops, a.scopes, a.groups, a.row_sets) == (b.stops, b.scopes, b.groups, b.row_sets)
self.first_difference(other).is_none()
}
}
@@ -336,9 +355,12 @@ pub(crate) fn transcript(ctx: &Context, mark: Mark) -> Transcript {
/// as the run would have. A replayed scope is no new scope: it moves no focus when it shows again.
pub(crate) fn replay(ctx: &Context, transcript: &Transcript) {
let frame = crate::runtime::egui::pass::frame_nr(ctx, viewport(ctx));
let focused = egui_focus::focused(ctx);
with(ctx, |state| {
let placed = transcript.placed(state.open.last().map(|(id, _)| *id));
for (id, scope) in placed.scopes {
for (id, mut scope) in placed.scopes {
// No replayed scope has a noted opener: an overlay, which notes one, blocks its record.
scope.opener = opener(state, id, None, focused);
if scope.entering.is_none() {
state.seen.insert(id, frame);
}
@@ -427,8 +449,7 @@ pub(crate) fn push_scope_from(
let focused = egui_focus::focused(ctx);
with(ctx, |state| {
let parent = state.open.last().map(|(parent, _)| *parent);
let opener = noted.or_else(|| focused.and_then(|root| state.focused.get(&root).copied().or(Some((root, None)))));
let opener = state.last_scopes.get(&id).map_or(opener, |last| last.opener.or(opener));
let opener = opener(state, id, noted, focused);
state.scopes.insert(id, ScopeRec { policy, parent, label, opener, closing, entering });
state.scope_order.push(id);
let first = state.stops.len();
@@ -436,6 +457,17 @@ pub(crate) fn push_scope_from(
});
}
/// The opener of the scope `id` registered in the running pass, by a run or a replay alike: taken once, when the scope
/// first appears, as the stop `noted` or else the stop of the focused widget `focused`; then kept from the pass before,
/// with what focus entering it noted there (`note_entry`). It is never taken again from the focus of a later pass,
/// which may be a stop inside the scope itself.
fn opener(state: &State, id: Id, noted: Option<StopKey>, focused: Option<Id>) -> Option<StopKey> {
match state.last_scopes.get(&id) {
Some(last) => last.opener,
None => noted.or_else(|| focused.map(|root| state.focused.get(&root).copied().unwrap_or((root, None)))),
}
}
/// The innermost scope open in the running pass: the scope whose body is being shown.
#[doc(hidden)]
pub fn open_scope(ctx: &Context) -> Option<Id> {
@@ -102,7 +102,7 @@ fn layer_scope_record(frames: &mut Frames, outer: Option<egui::Id>, entering: bo
}
replay::open(&ctx);
focus::register_stop(&ctx, Stop::new(fixture::named_id("beside"), KeyUse::CODE));
focus::push_scope_from(&ctx, scope, FocusPolicy::NONE, None, None, false, Some(entering));
focus::push_scope_from(&ctx, scope, FocusPolicy::POPUP, None, None, false, Some(entering));
focus::register_stop(&ctx, Stop::new(stop, KeyUse::CODE));
focus::pop_scope(&ctx);
record = Some(replay::close(&ctx, fixture::layer_of(ui)));
@@ -174,3 +174,27 @@ fn a_replayed_member_stacks_above_the_members_before_it() {
});
assert_eq!(hovered, Some([true, false]), "the replayed child is above the member that covers it");
}
// A popup scope that appears with nothing focused has no opener, and keeps none once focus moved into it: the opener is
// taken when the scope first appears, never from a later pass's focus, which is a stop inside the scope itself. A
// recorded first pass, the next real run and a replay then register the same.
#[trial]
fn a_scope_keeps_the_opener_of_the_pass_it_appeared_in() {
let mut frames = Frames::new();
let [scope, stop] = [fixture::named_id("popup"), fixture::named_id("inside")];
let mut openers = Vec::new();
let mut focused = Vec::new();
for first in [true, false, false] {
frames.frame(Vec::new(), |ui| {
let ctx = ui.ctx().clone();
focused.push(focus::focused_stop(&ctx));
focus::register_stop(&ctx, Stop::new(fixture::named_id("beside"), KeyUse::CODE));
focus::push_scope_from(&ctx, scope, FocusPolicy::POPUP, None, None, false, Some(first));
focus::register_stop(&ctx, Stop::new(stop, KeyUse::CODE));
focus::pop_scope(&ctx);
openers.push(focus::with(&ctx, |state| state.scopes.get(&scope).map(|rec| rec.opener)));
});
}
assert_eq!(focused[1], Some((stop, None)), "focus moved into the popup after its first pass");
assert_eq!(openers, vec![Some(None); 3], "the popup keeps the opener of its first pass");
}