From 08ffa67c01f8cc48586b41ca7b1fbca9f03ba87a Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 02:06:18 +0000 Subject: [PATCH] A type's stories from the package that registers it list before other packages' stories of it, so CropOverlay's first story is its own Upright (the old CropFrame card) ahead of draw's Crop tool stories; Crop rect takes the old title Upright --- crates/ui-canvas/src/canvas/crop/stories.rs | 2 +- crates/ui-contract/src/testing/story.rs | 15 ++++++++-- .../tests/it/contract/visited_type.rs | 28 ++++++++++++++++--- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/crates/ui-canvas/src/canvas/crop/stories.rs b/crates/ui-canvas/src/canvas/crop/stories.rs index 9f562ffc1..db2818c93 100644 --- a/crates/ui-canvas/src/canvas/crop/stories.rs +++ b/crates/ui-canvas/src/canvas/crop/stories.rs @@ -69,7 +69,7 @@ fn drag_top_left(fixture: &mut Fixture) { } #[story(CropOverlay<'static>, covers = [Default], drive = Drive::Custom(drag_top_left))] -fn crop_rect(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { +fn upright(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let rect = data.value("crop", crop()); on_canvas(ui, |ui, at| CropOverlay::new("Crop", Bind::bound(&mut *rect)).show_at(ui, at)) } diff --git a/crates/ui-contract/src/testing/story.rs b/crates/ui-contract/src/testing/story.rs index 3d783ec4c..2cf968086 100644 --- a/crates/ui-contract/src/testing/story.rs +++ b/crates/ui-contract/src/testing/story.rs @@ -192,11 +192,22 @@ impl StoryRegistration { inventory::collect!(StoryRegistration); -/// Every story linked into the running binary, by type name, then in the order they are declared in. +/// Every story linked into the running binary, by type name; of each type, the stories of the package that registers +/// it first, then those other packages declare; each in the order they are declared in. #[doc(hidden)] pub fn stories() -> Vec<&'static StoryRegistration> { + let homes: HashMap<&str, &str> = crate::contract::registrations() + .into_iter() + .filter_map(|registration| { + let name = registration.probe().map(|probe| probe.type_name()).or_else(|| registration.pattern_probe().map(|probe| probe.type_name()))?; + Some((name, registration.crate_name())) + }) + .collect(); let mut all: Vec<_> = inventory::iter::().collect(); - all.sort_by_key(|entry| ((entry.type_name)(), entry.file, entry.line)); + all.sort_by_key(|entry| { + let name = (entry.type_name)(); + (name, homes.get(name) != Some(&entry.crate_name), entry.file, entry.line) + }); all } diff --git a/crates/ui-contract/tests/it/contract/visited_type.rs b/crates/ui-contract/tests/it/contract/visited_type.rs index 76ef78f17..c29953156 100644 --- a/crates/ui-contract/tests/it/contract/visited_type.rs +++ b/crates/ui-contract/tests/it/contract/visited_type.rs @@ -1,9 +1,10 @@ //! A package's stories of a type another package registers run that type's per-story cases in the package's suite; -//! the cases about the type as a whole (its required states) stay with the package that registers it. +//! the cases about the type as a whole (its required states) stay with the package that registers it. The type's own +//! stories list before the visitor's. use craft_ui::contract::{Common, Component, Cx, MeasureCx, Props as _, Role, Show as _}; use craft_ui::style::StyleCx; -use craft_ui::testing::{Story, StoryData, StoryOutput, StoryRegistration, trial}; +use craft_ui::testing::{Required, Story, StoryData, StoryOutput, StoryRegistration, trial}; use craft_ui_test_kit::contract::PlainStyle; use egui::{Ui, Vec2}; @@ -39,9 +40,28 @@ fn plain_box(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { Visited { common: Common::new() }.label("Box").show(ui).into() } -// The visitor's story covers no state: a Default story is the registering package's to have. +// The visitor's story covers no state: a Default story is the registering package's to have. Its file sorts before +// the home story's. craft_ui::testing::__private::inventory::submit! { - StoryRegistration::new(VISITOR, "craft_ui_visitor", std::any::type_name::, Story::new("plain_box", plain_box)) + StoryRegistration::new(VISITOR, "craft_ui_visitor", std::any::type_name::, Story::new("plain_box", plain_box)).at("a.rs", 1) +} + +craft_ui::testing::__private::inventory::submit! { + StoryRegistration::new( + env!("CARGO_PKG_NAME"), + "craft_ui_contract", + std::any::type_name::, + Story::new("home_box", plain_box).covers(&[Required::Default]), + ) + .at("z.rs", 1) +} + +#[trial] +fn a_types_own_stories_list_before_another_packages() { + let visited = std::any::type_name::(); + let names: Vec<&str> = + craft_ui::testing::stories().into_iter().filter(|entry| (entry.type_name)() == visited).map(|entry| entry.story.name).collect(); + assert_eq!(names, ["home_box", "plain_box"], "the registering package's story comes first"); } #[trial]