Merge remote-tracking branch 'upstream/next' into memo-board

This commit is contained in:
aiongg
2026-11-19 16:25:01 +00:00
9 changed files with 223 additions and 37 deletions
@@ -24,8 +24,10 @@ const PATH_FS: [&str; 2] = ["std", "fs"];
const PLAIN_DATA: &str = "contract/no-global-state:plain-data";
const PROJECTION: &str = "contract/no-global-state:projection";
/// The hidden `Tracked::__split_for_bind`, called only by `bind_tracked!`; the lint reads source before macro expansion, so every textual hit is a direct call.
/// The hidden `Tracked::__split_for_bind`, called only by `bind_tracked!`; the lint reads source before macro expansion, so every textual hit outside its definition is a direct call.
const SPLIT_FOR_BIND: &str = "__split_for_bind";
/// The macro that calls [`SPLIT_FOR_BIND`]; its definition is not a call site.
const BIND_TRACKED: &str = "bind_tracked";
struct Hits {
found: Vec<(proc_macro2::Span, String, String)>,
@@ -110,6 +112,10 @@ impl Visit<'_> for Hits {
}
fn visit_item_macro(&mut self, node: &ItemMacro) {
// `bind_tracked!`'s own definition is the one place that names the hidden call.
if node.mac.path.is_ident("macro_rules") && node.ident.as_ref().is_some_and(|i| i == BIND_TRACKED) {
return;
}
self.macro_path(&node.mac);
visit::visit_item_macro(self, node);
}
@@ -52,10 +52,6 @@ impl Decls {
&self.parts
}
pub(crate) fn is_empty(&self) -> bool {
self.parts.is_empty()
}
pub(crate) fn iter(&self) -> std::iter::Zip<std::slice::Iter<'_, PartDecl>, std::slice::Iter<'_, PartExtras>> {
self.parts.iter().zip(&self.extras)
}
+10 -4
View File
@@ -939,16 +939,22 @@ fn sense_steps(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option<ShowAt>, e
if !inert && facts.text() {
parts.text.commit_now = focus::commit_now(&ctx) == Some(id);
}
// A component that declares no parts skips the step; a debug build runs it anyway to check the declaration.
let declares_parts = facts.parts || e.declares_parts();
if !inert && (declares_parts || cfg!(debug_assertions)) {
#[cfg(feature = "testing")]
let checking_parts = !inert && !declares_parts && crate::testing::contract::checking_parts();
#[cfg(not(feature = "testing"))]
let checking_parts = false;
if !inert && (declares_parts || checking_parts) {
let input = MeasureIn { ui: &*ui, env, proposal: Proposal::Ideal, room: p.room };
e.parts(rect, flow, content_rect, &mut parts, &input, rtl);
}
if facts.canvas_control && p.trace.recording() {
#[cfg(feature = "testing")]
if checking_parts && !declares_parts && !steps::declares_none(&parts) {
crate::testing::contract::bad_parts(facts.name);
}
if facts.canvas_control && !ui.is_sizing_pass() {
inspect::canvas_control(&ctx, facts.name);
}
debug_assert!(declares_parts || steps::declares_none(&parts), "`{}` sets `PARTS = false` and declares a part", facts.name);
steps::check_parts(&ctx, facts.name, &parts);
let keys = e.keys();
let (holder_widget, text_keys) = text_holder(&ctx, id, &parts, keys, &mut *e.core().memory);
+3 -1
View File
@@ -31,7 +31,9 @@ pub(crate) use id::{claim_id, draft, id, keeping_ids};
pub(crate) use label::scrub_by_label;
pub(crate) use measure::{MeasureIn, guard, measure, measure_content, measure_cx, padding, whole};
pub(crate) use paint::{Fade, UnderVeil, fade, next_shape, paint_focus, paint_part_layers, paint_surface, surface_ink, unfade};
pub(crate) use parts::{PartsStep, check_parts, declares_none};
#[cfg(feature = "testing")]
pub(crate) use parts::declares_none;
pub(crate) use parts::{PartsStep, check_parts};
pub(crate) use place::place;
pub(crate) use report::{ReportIn, report, report_inert, report_parts, validity_of};
#[cfg(feature = "testing")]
@@ -14,8 +14,9 @@ pub(crate) struct PartsStep {
}
/// Whether step 7 declared no parts, no roving group, no scroll part and no text, editor or selection part.
#[cfg(feature = "testing")]
pub(crate) fn declares_none(parts: &PartsStep) -> bool {
parts.decls.is_empty()
parts.decls.parts().is_empty()
&& parts.rovings.is_empty()
&& parts.scrolls.is_empty()
&& parts.text.decl.is_none()
+28 -20
View File
@@ -1,5 +1,5 @@
//! Decision records and findings: what each pipeline step did, kept in frames a test fixture drives and inside an
//! inspected region ([`record`]). The region's record is what the debug inspector draws: every component shown in it
//! Decision records and findings. Inspected regions ([`record`]) collect pipeline steps. Fixture frames also collect
//! contract findings. The region's record is what the debug inspector draws: every component shown in it
//! with its box and access facts, the hit areas the sensing core registered and the focus stops in Tab order.
use crate::runtime::egui::areas;
@@ -35,9 +35,9 @@ pub(crate) struct Log {
kept: IdMap<Kept>,
}
/// Whether records are kept: in frames a test fixture drives, and inside an inspected region in any build.
/// Whether an explicit inspector region is collecting elements and their pipeline steps.
pub(crate) fn recording(ctx: &Context) -> bool {
fixture_frame() || pass_state::with::<Log, _>(ctx, |log| !log.open.is_empty())
pass_state::with::<Log, _>(ctx, |log| !log.open.is_empty())
}
#[cfg(feature = "testing")]
@@ -50,16 +50,12 @@ fn fixture_frame() -> bool {
/// Starts the step record of `id` for a new show.
pub(crate) fn begin(ctx: &Context, id: Id) {
if recording(ctx) {
pass_state::with::<Log, _>(ctx, |log| log.steps.insert(id, Vec::new()));
}
pass_state::with::<Log, _>(ctx, |log| log.steps.insert(id, Vec::new()));
}
/// Records that `step` ran for `id`.
pub(crate) fn step(ctx: &Context, id: Id, step: &'static str) {
if recording(ctx) {
pass_state::with::<Log, _>(ctx, |log| log.steps.entry(id).or_default().push(step));
}
pass_state::with::<Log, _>(ctx, |log| log.steps.entry(id).or_default().push(step));
}
/// The steps the latest show of `id` ran, in order.
@@ -70,7 +66,7 @@ pub(crate) fn steps_of(ctx: &Context, id: Id) -> Vec<&'static str> {
/// Reports a finding.
pub(crate) fn finding(ctx: &Context, finding: Finding) {
if !recording(ctx) {
if !fixture_frame() && !recording(ctx) {
return;
}
pass_state::with::<Log, _>(ctx, |log| {
@@ -89,7 +85,7 @@ pub(crate) fn findings(ctx: &Context) -> Vec<Finding> {
/// Records that the canvas control `type_name` showed in this pass, so the canvas-control cases can check that a
/// press away from its parts reaches the tool.
pub(crate) fn canvas_control(ctx: &Context, type_name: &'static str) {
if !recording(ctx) {
if !fixture_frame() && !recording(ctx) {
return;
}
pass_state::with::<Log, _>(ctx, |log| {
@@ -307,8 +303,13 @@ fn finish(ctx: &Context, open: Open) -> Recorded {
.into_iter()
.map(|(owner, part, rect)| HitArea { owner, part, rect: from_screen * rect })
.collect();
// Keep the first occurrence, matching the tree-order lookup when an id appears more than once.
let mut index = std::collections::HashMap::new();
for (at, element) in elements.iter().enumerate() {
index.entry((element.id, element.part)).or_insert(at);
}
for (node, label) in labels {
let facts = elements.iter_mut().find(|element| element.id == node && element.part.is_none()).and_then(|element| element.facts.as_mut());
let facts = index.get(&(node, None)).and_then(|&at| elements[at].facts.as_mut());
if let Some(facts) = facts {
facts.labelled_by.push(label);
}
@@ -320,20 +321,27 @@ fn finish(ctx: &Context, open: Open) -> Recorded {
let key = |hit: &HitArea| (place.get(&hit.owner).copied(), hit.rect.min.y, hit.rect.min.x);
key(a).partial_cmp(&key(b)).unwrap_or(std::cmp::Ordering::Equal)
});
for (at, (id, part)) in focus::tab_order(ctx, |id| ids.contains(&id)).into_iter().enumerate() {
let index = match elements.iter().position(|element| element.id == id && element.part == part) {
Some(index) => index,
for (stop, (id, part)) in focus::tab_order(ctx, |id| ids.contains(&id)).into_iter().enumerate() {
let at = match index.get(&(id, part)).copied() {
Some(at) => at,
None => {
// A part with no node of its own: listed after its component, where its hit area is.
let Some(owner) = elements.iter().position(|element| element.id == id && element.part.is_none()) else { continue };
let Some(owner) = index.get(&(id, None)).copied() else { continue };
let rect = hits.iter().find(|hit| hit.owner == id && hit.part == part).map_or(elements[owner].rect, |hit| hit.rect);
let depth = elements[owner].depth + 1;
let element = Element { id, part, component: elements[owner].component, depth, rect, content: rect, facts: None, stop: None };
elements.insert(owner + 1, element);
owner + 1
let at = owner + 1;
elements.insert(at, element);
for old in index.values_mut() {
if *old >= at {
*old += 1;
}
}
index.insert((id, part), at);
at
}
};
elements[index].stop = Some(at + 1);
elements[at].stop = Some(stop + 1);
}
Recorded { elements, hits }
}
+32 -1
View File
@@ -3,7 +3,8 @@
use crate::runtime::inspect::{self, Finding};
use egui::{Context, Id};
/// The steps the latest show of `id` ran, in order.
/// The steps the latest inspected show of `id` ran, in order. Wrap the show in
/// [`crate::runtime::inspect::record`] to collect its steps.
pub fn steps(ctx: &Context, id: Id) -> Vec<&'static str> {
inspect::steps_of(ctx, id)
}
@@ -48,3 +49,33 @@ pub fn raw_widget(ui: &mut egui::Ui, rect: egui::Rect) {
pub fn allocate(ui: &mut egui::Ui, size: egui::Vec2) {
crate::runtime::egui::fixture::allocate_blocker(ui, size);
}
thread_local! {
static PARTS_CHECK: std::cell::RefCell<Option<Vec<&'static str>>> = const { std::cell::RefCell::new(None) };
}
/// Run a story with the parts declaration check enabled, and return any offending component types.
pub fn check_parts_declarations(run: impl FnOnce()) -> Vec<&'static str> {
struct Reset(Option<Vec<&'static str>>);
impl Drop for Reset {
fn drop(&mut self) {
PARTS_CHECK.with(|check| *check.borrow_mut() = self.0.take());
}
}
let previous = PARTS_CHECK.with(|check| check.replace(Some(Vec::new())));
let _reset = Reset(previous);
run();
PARTS_CHECK.with(|check| check.borrow_mut().take().unwrap_or_default())
}
pub(crate) fn checking_parts() -> bool {
PARTS_CHECK.with(|check| check.borrow().is_some())
}
pub(crate) fn bad_parts(component: &'static str) {
PARTS_CHECK.with(|check| {
if let Some(bad) = check.borrow_mut().as_mut() {
bad.push(component);
}
});
}
+14 -1
View File
@@ -4,7 +4,20 @@ use super::{ANY, Case, Subject};
use crate::testing::fixture::Fixture;
use egui::{Rect, Vec2};
pub(super) const CASES: &[Case] = &[Case::new("paints-inside", ANY, small_rooms_paint_inside_the_shown_rect)];
pub(super) const CASES: &[Case] =
&[Case::new("paints-inside", ANY, small_rooms_paint_inside_the_shown_rect), Case::new("parts-declaration", ANY, parts_declaration)];
fn parts_declaration(subject: &Subject) -> Result<(), String> {
for story in &subject.stories {
let bad = crate::testing::contract::check_parts_declarations(|| {
Fixture::story(story).frame(Vec::new());
});
if !bad.is_empty() {
return Err(format!("{}: components with PARTS = false declared parts: {bad:?}", story.name));
}
}
Ok(())
}
/// The slack between a painted bound and the shown rect: the rounding of the layout, as the layout cases allow. A
/// shape's own slack adds half its stroke width.
+127 -4
View File
@@ -216,17 +216,20 @@ fn a_style_value_set_in_content_env_changes_a_body_childs_style() {
assert!(inside > outside, "the body's child read the body's value: {outside} -> {inside}");
}
/// The step log, a record only trials and the debug inspector read, is kept in frames a test fixture drives; a frame
/// an app drives itself, such as the gallery's, keeps none.
/// A fixture pays for pipeline step records only when the caller inspects a region.
#[trial]
fn the_step_log_is_kept_in_fixture_frames_only() {
fn the_step_log_is_kept_only_for_an_inspected_region() {
let mut fixture = Frames::new();
let seen = RefCell::new(Seen::default());
fixture.frame(Vec::new(), |ui| {
Probe::new(&seen).show(ui);
});
let id = seen.borrow().id.expect("the probe ran");
assert!(!steps(&fixture.ctx, id).is_empty(), "a fixture frame records the probe's steps");
assert!(steps(&fixture.ctx, id).is_empty(), "an ordinary fixture frame has no step log");
fixture.frame(Vec::new(), |ui| {
craft_ui::runtime::inspect::record(ui, egui::Id::unique("inspect probe"), |ui| Probe::new(&seen).show(ui));
});
assert!(!steps(&fixture.ctx, seen.borrow().id.expect("the inspected probe ran")).is_empty());
let app = Frames::new();
let seen = RefCell::new(Seen::default());
@@ -237,3 +240,123 @@ fn the_step_log_is_kept_in_fixture_frames_only() {
let id = seen.borrow().id.expect("the probe ran");
assert!(steps(&app.ctx, id).is_empty(), "an app frame records no steps");
}
/// A component whose declaration says the pipeline need not run its parts hook.
#[derive(Component)]
struct NoParts<'a> {
#[common]
common: Common,
calls: &'a std::cell::Cell<usize>,
declare: bool,
}
impl Component for NoParts<'_> {
const ROLE: Role = Role::None;
const PARTS: bool = false;
type Style = PlainStyle;
type Output = Response;
fn style(_: &StyleCx<'_>, _: NoPartsStyleKey) -> PlainStyle {
PlainStyle::min(Vec2::splat(20.0))
}
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
self.calls.set(self.calls.get() + 1);
if self.declare {
cx.part(craft_behavior::PartKey::new("unexpected"), rect);
}
}
fn paint(&mut self, _: &mut PaintUi<'_>, _: &Cx<'_, Self>) {}
fn finish(self, cx: Cx<'_, Self>) -> Response {
cx.response.clone()
}
}
#[trial]
fn a_component_declaring_no_parts_skips_the_parts_hook() {
let mut frames = Frames::new();
let calls = std::cell::Cell::new(0);
frames.frame(vec![], |ui| {
NoParts { common: Common::new(), calls: &calls, declare: false }.show(ui);
});
assert_eq!(calls.get(), 0, "the parts hook is skipped on ordinary runs");
}
#[trial]
fn parts_declaration_checks_report_bad_components_and_restore_nested_checks() {
use craft_ui::testing::contract::check_parts_declarations;
let calls = std::cell::Cell::new(0);
let show = || {
Frames::new().frame(vec![], |ui| {
NoParts { common: Common::new(), calls: &calls, declare: true }.show(ui);
});
};
let outer = check_parts_declarations(|| {
show();
let inner = check_parts_declarations(show);
assert!(inner.iter().any(|name| name.contains("NoParts")), "{inner:?}");
show();
});
assert!(outer.len() >= 2 && outer.iter().all(|name| name.contains("NoParts")), "{outer:?}");
let panic = std::panic::catch_unwind(|| check_parts_declarations(|| panic!("guard test")));
assert!(panic.is_err());
let before = calls.get();
show();
assert_eq!(calls.get(), before, "the check is disabled after unwinding");
}
#[derive(Component)]
struct FocusParts {
#[common]
common: Common,
}
impl Component for FocusParts {
const ROLE: Role = Role::None;
type Style = PlainStyle;
type Output = Response;
fn style(_: &StyleCx<'_>, _: FocusPartsStyleKey) -> PlainStyle {
PlainStyle::min(Vec2::splat(20.0))
}
fn sense(&self) -> Sense {
Sense::empty()
}
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
cx.part(craft_behavior::PartKey::new("first"), rect).focusable();
cx.part(craft_behavior::PartKey::new("second"), rect).focusable();
}
fn finish(self, cx: Cx<'_, Self>) -> Response {
cx.response.clone()
}
}
#[trial]
fn inspector_places_synthetic_focus_parts_after_each_owner() {
let mut frames = Frames::new();
let mut recorded = None;
let mut ids = Vec::new();
frames.frame(vec![], |ui| {
let (shown, record) = craft_ui::runtime::inspect::record(ui, named_id("focus-region"), |ui| {
["one", "two"].map(|salt| FocusParts { common: Common::new() }.id_salt(salt).show(ui).id)
});
ids = shown.to_vec();
recorded = record;
});
let record = recorded.expect("real pass was recorded");
let mut stops = Vec::new();
for id in ids {
let owner = record.elements.iter().position(|element| element.id == id && element.part.is_none()).unwrap();
let parts = &record.elements[owner + 1..owner + 3];
assert!(parts.iter().all(|part| part.id == id && part.part.is_some() && part.facts.is_none()), "{parts:?}");
stops.extend(parts.iter().map(|part| part.stop.expect("part is a focus stop")));
}
stops.sort_unstable();
assert_eq!(stops, [1, 2, 3, 4]);
}