Guard prepared leaf and text memo inputs

This commit is contained in:
FredApps
2026-11-20 03:56:04 +01:00
parent 859f3b0fce
commit e61b412dd3
3 changed files with 94 additions and 2 deletions
+3 -1
View File
@@ -168,7 +168,9 @@ impl PaintUi<'_> {
let mut control = placed.control;
let mut prepared = None;
if child.fills() != Some(craft_behavior::Axis::Y) {
let ideal = if self.fade.is_none() {
// The minimum measure above may have changed the child's enabled state. In that
// case `under` can differ from what the child inherits when it is shown.
let ideal = if self.fade.is_none() && under.enabled == self.enabled {
let (ideal, kept) = run::measure_under_prepared(&mut child, &*self.ui, id, craft_behavior::Proposal::Ideal, &under);
prepared = kept;
ideal
+13 -1
View File
@@ -231,6 +231,7 @@ pub(crate) fn line_baseline(ctx: &Context, env: &TextEnv<'_>, role: TypeRole, ce
struct LayoutKey {
run: TextRun<'static>,
tokens: u64,
token_values: Arc<craft_theme::Tokens>,
direction: Direction,
ink: Color32,
fonts: StackFacts,
@@ -242,6 +243,7 @@ struct LayoutKey {
struct LayoutQuery<'a> {
run: &'a TextRun<'a>,
tokens: u64,
token_values: &'a Arc<craft_theme::Tokens>,
direction: Direction,
ink: Color32,
fonts: StackFacts,
@@ -296,6 +298,7 @@ impl LayoutQuery<'_> {
fn matches(&self, key: &LayoutKey) -> bool {
key.run == *self.run
&& key.tokens == self.tokens
&& (Arc::ptr_eq(&key.token_values, self.token_values) || key.token_values.as_ref() == self.token_values.as_ref())
&& key.direction == self.direction
&& key.ink == self.ink
&& key.fonts == self.fonts
@@ -331,6 +334,7 @@ impl LayoutQuery<'_> {
rotation: self.run.rotation,
},
tokens: self.tokens,
token_values: Arc::clone(self.token_values),
direction: self.direction,
ink: self.ink,
fonts: self.fonts,
@@ -377,10 +381,11 @@ impl TextMemo {
impl TextRun<'_> {
/// All inputs read by `job`, the cutting and row-alignment steps.
fn layout_key<'a>(&'a self, ctx: &Context, env: &TextEnv<'_>, max_width: f32) -> LayoutQuery<'a> {
fn layout_key<'a>(&'a self, ctx: &Context, env: &'a TextEnv<'_>, max_width: f32) -> LayoutQuery<'a> {
LayoutQuery {
run: self,
tokens: env.tokens.key(),
token_values: env.tokens.tokens(),
direction: env.direction,
ink: env.ink,
fonts: env.fonts,
@@ -1041,9 +1046,16 @@ mod memo_tests {
let fonts = TextEnv { fonts: StackFacts { line_ratio: 1.25, ..env.fonts }, ..env };
let _ = run.layout(ctx, &fonts, 80.0);
let mut changed_tokens = (**tokens.tokens()).clone();
let equal_tokens = UiTokens::new(Arc::new(changed_tokens.clone()), Contrast::Standard, Size::Compact);
let equal_env = TextEnv { tokens: &equal_tokens, ..env };
let equal_key = run.layout_key(ctx, &equal_env, 80.0);
with_memo(ctx, |memo| assert!(memo.get(&equal_key).is_some(), "equal token values from distinct Arcs may reuse the entry"));
changed_tokens.color.text.muted = craft_theme::Rgba([12, 34, 56, 255]);
let changed_tokens = UiTokens::new(Arc::new(changed_tokens), Contrast::Standard, Size::Compact);
let styled = TextEnv { tokens: &changed_tokens, ..env };
let mut collision = run.layout_key(ctx, &styled, 80.0);
collision.tokens = tokens.key();
with_memo(ctx, |memo| assert!(memo.get(&collision).is_none(), "a token-hash collision must not reuse a different token set"));
let _ = run.layout(ctx, &styled, 80.0);
let before = with_memo(ctx, |memo| memo.order.len());
assert_eq!(before, 25);
@@ -97,3 +97,81 @@ fn adjacent_leaf_reuses_steps_only_while_its_style_key_is_unchanged() {
assert_eq!(reused, 0, "a changed style key uses the ordinary path");
assert_eq!(changed, 40.0, "the shown child receives its changed style");
}
#[derive(Component)]
struct EnablesOnMinimum {
#[common]
common: Common,
seen: Rc<Cell<Option<bool>>>,
}
impl Component for EnablesOnMinimum {
const ROLE: Role = Role::GROUP;
type Style = PlainStyle;
type Output = Response;
fn style(_: &StyleCx<'_>, _: EnablesOnMinimumStyleKey) -> PlainStyle {
PlainStyle::default()
}
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
if cx.proposal == Proposal::Min {
self.common = Common::new();
}
Vec2::new(10.0, 20.0)
}
fn sense(&self) -> Sense {
Sense::empty()
}
fn paint(&mut self, _: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
self.seen.set(Some(cx.core.resolved.enabled));
}
fn finish(self, cx: Cx<'_, Self>) -> Response {
cx.response.clone()
}
}
#[derive(Component)]
struct EnabledOwner {
#[common]
common: Common,
seen: Rc<Cell<Option<bool>>>,
}
impl Component for EnabledOwner {
const ROLE: Role = Role::GROUP;
type Style = PlainStyle;
type Output = Response;
fn style(_: &StyleCx<'_>, _: EnabledOwnerStyleKey) -> PlainStyle {
PlainStyle::min(Vec2::new(200.0, 50.0))
}
fn sense(&self) -> Sense {
Sense::empty()
}
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
let child = EnablesOnMinimum { common: Common::new(), seen: self.seen.clone() }.enabled(false);
ui.labelled("child", cx.rect, "Caption".into(), SideLabel::new(), child);
}
fn finish(self, cx: Cx<'_, Self>) -> Response {
cx.response.clone()
}
}
#[trial]
fn labelled_child_enabling_itself_in_minimum_measure_uses_the_current_enabled_state() {
let seen = Rc::new(Cell::new(None));
let output = seen.clone();
let mut fixture = Fixture::new(move |ui| {
EnabledOwner { common: Common::new(), seen: output.clone() }.id_salt("owner").show(ui);
});
fixture.frame(Vec::new());
assert_eq!(seen.get(), Some(true), "the shown child uses its enabled state after the minimum measure");
assert_eq!(last_pass_prepared_leaves(fixture.ctx()), 0, "different inherited enabled states use the ordinary path");
}