diff --git a/crates/browse/src/ui/bin/stories.rs b/crates/browse/src/ui/bin/stories.rs index c68b30996..3e763f9c2 100644 --- a/crates/browse/src/ui/bin/stories.rs +++ b/crates/browse/src/ui/bin/stories.rs @@ -138,7 +138,12 @@ fn show_bin(ui: &mut Ui, data: &mut StoryData, view: BinView, cards: &[FreeformC .show(ui); *data.value(key, BinState::::default()) = state; *data.value("selection", Selection::::default()) = selection; - out.into() + let last = data.note("last", None::); + if let Some(event) = out.events.last() { + *last = Some(format!("{event:?}")); + } + let readout = last.as_ref().map_or_else(|| "No event yet".to_owned(), |event| format!("Last event: {event}")); + StoryOutput::from(out).readout(readout) } #[story(MediaBin<'static, u32, dyn BinModel>, covers = [], drive = Drive::Never)] diff --git a/crates/color/src/ui/choice/stories.rs b/crates/color/src/ui/choice/stories.rs index f97ce234d..0ca8894e0 100644 --- a/crates/color/src/ui/choice/stories.rs +++ b/crates/color/src/ui/choice/stories.rs @@ -14,9 +14,15 @@ fn color_choice(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { NamedColor::new("black", "Black", Some(Srgba::rgb(0.0, 0.0, 0.0))), NamedColor::new("gray", "Medium Gray", Some(Srgba::rgb(0.5, 0.5, 0.5))), ]; - let out = ColorChoice::new("Pasteboard color", &chosen).named(named).custom(Some(Srgba::rgb(0.8, 0.3, 0.1))).show(ui); + let custom = Srgba::rgb(0.8, 0.3, 0.1); + let out = ColorChoice::new("Pasteboard color", &chosen).named(named).custom(Some(custom)).show(ui); + let held = data.value("chosen", ChosenColor::Named("black".into())); if let Some(next) = out.chosen.clone() { - *data.value("chosen", ChosenColor::Named("black".into())) = next; + *held = next; } - out.into() + let readout = match &*held { + ChosenColor::Named(id) => id.clone(), + ChosenColor::Custom => custom.to_hex().to_uppercase(), + }; + StoryOutput::from(out).readout(readout) } diff --git a/crates/color/src/ui/lut_picker/stories.rs b/crates/color/src/ui/lut_picker/stories.rs index c4524e6da..4ebef50f1 100644 --- a/crates/color/src/ui/lut_picker/stories.rs +++ b/crates/color/src/ui/lut_picker/stories.rs @@ -19,12 +19,16 @@ fn pick_second(fixture: &mut Fixture) { #[story(LutPicker<'static>, covers = [Default], drive = Drive::Custom(pick_second))] fn lut_picker(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { - LutPicker::new(Bind::bound(data.value("lut", LutChoice::Builtin("warm")))).label("3D LUT File").show(ui).into() + let shown = LutPicker::new(Bind::bound(data.value("lut", LutChoice::Builtin("warm")))).label("3D LUT File").show(ui); + let chosen = format!("Chosen: {:?}", data.value("lut", LutChoice::Builtin("warm"))); + StoryOutput::from(shown).readout(chosen) } #[story(LutPicker<'static>, covers = [], drive = Drive::Never)] fn lut_picker_loaded(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { - LutPicker::new(Bind::bound(data.value("lut", LutChoice::loaded("Film", 33)))).label("3D LUT File").show(ui).into() + let shown = LutPicker::new(Bind::bound(data.value("lut", LutChoice::loaded("Film", 33)))).label("3D LUT File").show(ui); + let chosen = format!("Chosen: {:?}", data.value("lut", LutChoice::loaded("Film", 33))); + StoryOutput::from(shown).readout(chosen) } #[story(LutPicker<'static>, covers = [Invalid], drive = Drive::Never)] @@ -33,5 +37,7 @@ fn lut_picker_invalid(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { LutChoice::Builtin(_) => Validity::Refuse(Message::new("Load a file")), _ => Validity::Ok, }; - LutPicker::new(Bind::bound(data.value("lut", LutChoice::Builtin("warm"))).check(refuse_builtin)).label("3D LUT File").show(ui).into() + let shown = LutPicker::new(Bind::bound(data.value("lut", LutChoice::Builtin("warm"))).check(refuse_builtin)).label("3D LUT File").show(ui); + let chosen = format!("Chosen: {:?}", data.value("lut", LutChoice::Builtin("warm"))); + StoryOutput::from(shown).readout(chosen) } diff --git a/crates/color/src/ui/palette/stories.rs b/crates/color/src/ui/palette/stories.rs index 22e28651e..14d95d541 100644 --- a/crates/color/src/ui/palette/stories.rs +++ b/crates/color/src/ui/palette/stories.rs @@ -1,6 +1,6 @@ //! Stories of `SwatchPalette` and `SwatchTable`. -use super::{SwatchCell, SwatchPalette, SwatchTable}; +use super::{SwatchCell, SwatchPalette, SwatchTable, SwatchTableOutput}; use craft_behavior::Selection; use craft_theme::Srgba; use craft_ui::contract::Bind; @@ -36,27 +36,46 @@ fn table_entries() -> Vec<(Label, Srgba)> { })) } +/// The table's output with the readout of its latest select or open, kept in the story's data across passes. +fn with_readout(data: &mut StoryData, shown: SwatchTableOutput) -> StoryOutput { + let last = data.note("last", String::new()); + if let Some(index) = shown.selected { + *last = format!("select {index}"); + } + if let Some(index) = shown.open { + *last = format!("open {index}"); + } + let readout = format!("Last event: {last}"); + StoryOutput::from(shown).readout(readout) +} + #[story(SwatchPalette<'static>, covers = [], drive = Drive::Never)] fn swatch_palette(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let swatches = named(PALETTE.iter().map(|(name, color)| ((*name).to_owned(), *color))); - SwatchPalette::new("Swatches", Bind::bound(data.value("color", PALETTE[8].1)), &swatches).show(ui).into() + let shown = SwatchPalette::new("Swatches", Bind::bound(data.value("color", PALETTE[8].1)), &swatches).show(ui); + let last = data.note("last", String::new()); + if let Some(event) = &shown.edit { + *last = format!("{event:?}"); + } + let readout = format!("Last event: {last}"); + StoryOutput::from(shown).readout(readout) } #[story(SwatchTable<'static>, covers = [Default, Many], drive = Drive::Never)] fn swatch_table(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let entries = table_entries(); - SwatchTable::new("Color Table", &entries) - .selection(Bind::bound(data.value("selected", Selection::single(5usize)))) - .per_row(16) - .show_all(ui) - .into() + let shown = + SwatchTable::new("Color Table", &entries).selection(Bind::bound(data.value("selected", Selection::single(5usize)))).per_row(16).show_all(ui); + with_readout(data, shown) } #[story(SwatchTable<'static>, covers = [], drive = Drive::Never)] fn swatch_table_multiple(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let entries = table_entries(); let start = Selection::new(vec![2usize, 3, 18], Some(18), Some(2)); - SwatchTable::new("Color Table", &entries).multiple(true).selection(Bind::bound(data.value("selected", start))).per_row(16).show_all(ui).into() + let shown = + SwatchTable::new("Color Table", &entries).multiple(true).selection(Bind::bound(data.value("selected", start))).per_row(16).show_all(ui); + with_readout(data, shown) } #[story(SwatchTable<'static>, covers = [Empty], drive = Drive::Never)] @@ -67,7 +86,8 @@ fn swatch_table_empty(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { #[story(SwatchTable<'static>, covers = [One], drive = Drive::Never)] fn swatch_table_one(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let entries = table_entries().into_iter().take(1).collect::>(); - SwatchTable::new("Color Table", &entries).selection(Bind::bound(data.value("selected", Selection::single(0usize)))).show_all(ui).into() + let shown = SwatchTable::new("Color Table", &entries).selection(Bind::bound(data.value("selected", Selection::single(0usize)))).show_all(ui); + with_readout(data, shown) } #[story(SwatchCell, covers = [Default], drive = Drive::Never)] diff --git a/crates/color/src/ui/picker/stories.rs b/crates/color/src/ui/picker/stories.rs index f49443313..e72e9c8b3 100644 --- a/crates/color/src/ui/picker/stories.rs +++ b/crates/color/src/ui/picker/stories.rs @@ -8,7 +8,7 @@ use egui::Ui; use super::channel::{ChannelRow, ChannelRule, TrackRamp}; use super::screen_pick::ScreenPickButton; -use super::{ColorPicker, ColorPickerLayout, ColorWarning, ColorWarningChip, PickedColor}; +use super::{ColorPicker, ColorPickerLayout, ColorPickerOutput, ColorWarning, ColorWarningChip, PickedColor}; use crate::ui::AreaMode; /// A naive CMYK in percent, without a working space: a host's model for the stories. A real host converts through its @@ -53,6 +53,16 @@ impl ColorModel for NaiveCmyk { static CMYK_MODEL: NaiveCmyk = NaiveCmyk; +/// The picker's output with the readout of its latest event, kept in the story's data across passes. +fn with_readout(data: &mut StoryData, shown: ColorPickerOutput) -> StoryOutput { + let last = data.note("last", String::new()); + if let Some(event) = shown.events.last() { + *last = format!("{event:?}"); + } + let readout = format!("Last event: {last}"); + StoryOutput::from(shown).readout(readout) +} + fn start() -> PickedColor { PickedColor::rgb(Srgba::rgb(0.2, 0.5, 0.8)) } @@ -60,7 +70,8 @@ fn start() -> PickedColor { #[story(ColorPicker<'static>, covers = [Default], drive = Drive::Never)] fn color_picker_compact(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let color = data.value("color", start()); - ColorPicker::new("Foreground color", Bind::bound(color)).alpha(true).eyedropper(true).show(ui).into() + let shown = ColorPicker::new("Foreground color", Bind::bound(color)).alpha(true).eyedropper(true).show(ui); + with_readout(data, shown) } #[story(ColorPicker<'static>, covers = [], drive = Drive::Never)] @@ -68,7 +79,8 @@ fn color_picker_web_colors(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let web = *data.value("web", true); let mut web = web; let color = data.value("color", start()); - ColorPicker::new("Foreground color", Bind::bound(color)).web_colors(Bind::bound(&mut web)).show(ui).into() + let shown = ColorPicker::new("Foreground color", Bind::bound(color)).web_colors(Bind::bound(&mut web)).show(ui); + with_readout(data, shown) } #[story(ColorPicker<'static>, covers = [], drive = Drive::Never)] @@ -76,21 +88,22 @@ fn color_picker_full(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let mut mode = *data.value("mode", AreaMode::default()); let current = Srgba::rgb(0.9, 0.2, 0.1); let color = data.value("color", PickedColor::rgb(Srgba::rgb(1.0, 0.0, 1.0))); - ColorPicker::new("Foreground color", Bind::bound(color)) + let shown = ColorPicker::new("Foreground color", Bind::bound(color)) .layout(ColorPickerLayout::Full) .mode(Bind::bound(&mut mode)) .compare(current) .warnings([ColorWarning::new("Out of gamut for printing", Srgba::rgb(0.9, 0.1, 0.9))]) .eyedropper(true) - .show(ui) - .into() + .show(ui); + with_readout(data, shown) } #[story(ColorPicker<'static>, covers = [Many], drive = Drive::Never)] fn color_picker_full_models(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let color = data.value("color", start()); let models: [&'static dyn ColorModel; 5] = [&HsbModel, &RgbModel, &LabModel, &CMYK_MODEL, &OklchModel]; - ColorPicker::new("Foreground color", Bind::bound(color)).layout(ColorPickerLayout::Full).models(models).alpha(true).show(ui).into() + let shown = ColorPicker::new("Foreground color", Bind::bound(color)).layout(ColorPickerLayout::Full).models(models).alpha(true).show(ui); + with_readout(data, shown) } #[story(ColorWarningChip<'static>, covers = [Default], drive = Drive::Never)] diff --git a/crates/color/src/ui/wheel/stories.rs b/crates/color/src/ui/wheel/stories.rs index aa2ca787d..3053caa23 100644 --- a/crates/color/src/ui/wheel/stories.rs +++ b/crates/color/src/ui/wheel/stories.rs @@ -10,7 +10,13 @@ use egui::Ui; #[story(ColorWheel<'static>, covers = [Default], drive = Drive::Drag(Grip::new(super::RING)))] fn color_wheel(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let values = data.value("values", HsbModel.from_srgb(Srgba::rgb(0.2, 0.5, 0.8))); - ColorWheel::new("Color wheel", Bind::bound(values)).show(ui).into() + let shown = ColorWheel::new("Color wheel", Bind::bound(values)).show(ui); + let last = data.note("last", String::new()); + if let Some(event) = shown.events.last() { + *last = format!("{event:?}"); + } + let readout = format!("Last event: {last}"); + StoryOutput::from(shown).readout(readout) } #[story(ColorWheel<'static>, covers = [Invalid], drive = Drive::Never)] diff --git a/crates/draw/src/ui/brush_settings/stories.rs b/crates/draw/src/ui/brush_settings/stories.rs index 645d89295..255a5ee7e 100644 --- a/crates/draw/src/ui/brush_settings/stories.rs +++ b/crates/draw/src/ui/brush_settings/stories.rs @@ -89,9 +89,16 @@ fn section_list(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { BrushSectionList::new("Brush settings sections", §ions, Bind::given(shown)).link_row().show(ui).into() } +/// How the story words the angle and roundness it shows. +fn angle_note(angle: TipAngle) -> String { + format!("Angle {:.0}°, roundness {:.0} %", angle.degrees, (angle.roundness * 100.0).round()) +} + #[story(TipEllipse<'static>, covers = [Default], drive = Drive::Drag(Grip::new(super::ellipse::ARROW)))] fn tip_ellipse(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { - TipEllipse::new("Tip angle and roundness", Bind::bound(data.value("angle", ANGLE))).show(ui).into() + let shown = TipEllipse::new("Tip angle and roundness", Bind::bound(data.value("angle", ANGLE))).show(ui); + let note = angle_note(*data.value("angle", ANGLE)); + StoryOutput::from(shown).readout(note) } #[story(TipEllipse<'static>, covers = [Mixed], drive = Drive::Never)] @@ -102,7 +109,9 @@ fn tip_ellipse_mixed(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { #[story(TipEllipse<'static>, covers = [Invalid], drive = Drive::Never)] fn tip_ellipse_invalid(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let refuse_flat = |angle: &TipAngle| if angle.roundness < 0.5 { Validity::Refuse(Message::new("Too flat")) } else { Validity::Ok }; - TipEllipse::new("Tip angle and roundness", Bind::bound(data.value("flat", ANGLE)).check(refuse_flat)).show(ui).into() + let shown = TipEllipse::new("Tip angle and roundness", Bind::bound(data.value("flat", ANGLE)).check(refuse_flat)).show(ui); + let note = angle_note(*data.value("flat", ANGLE)); + StoryOutput::from(shown).readout(note) } #[story(DynamicControlRow<'static, u8>, covers = [Default], drive = Drive::Never)] diff --git a/crates/draw/src/ui/gradient/palette/stories.rs b/crates/draw/src/ui/gradient/palette/stories.rs index 21081029c..2f458ed06 100644 --- a/crates/draw/src/ui/gradient/palette/stories.rs +++ b/crates/draw/src/ui/gradient/palette/stories.rs @@ -31,11 +31,16 @@ fn palette(ui: &mut Ui, data: &mut StoryData, view: PresetView) -> StoryOutput { let mut initial = PresetState::new(); initial.view = view; let state = data.value("state", initial); - let _ = GradientPalette::new("Gradients", &groups, Bind::bound(&mut *state)) + let shown = GradientPalette::new("Gradients", &groups, Bind::bound(&mut *state)) .recent(&recent) .selected(GradientRef::Item { group: 0, item: 1 }) .show(ui); - StoryOutput::default() + let last = data.note("last", String::new()); + if let Some(picked) = shown.picked { + *last = format!("{picked:?}"); + } + let readout = format!("Last event: {last}"); + StoryOutput::from(shown).readout(readout) } #[story(GradientPalette<'static>, covers = [Default])] diff --git a/crates/draw/src/ui/stroke/stories.rs b/crates/draw/src/ui/stroke/stories.rs index 1c2f732e1..85a4332bc 100644 --- a/crates/draw/src/ui/stroke/stories.rs +++ b/crates/draw/src/ui/stroke/stories.rs @@ -41,28 +41,46 @@ fn other_than(base: &StrokeStyle) -> StrokeStyle { } } +/// `output` with the readout of the latest event, kept in the story's data across passes. +fn with_readout(data: &mut StoryData, output: impl Into, latest: Option) -> StoryOutput { + let last = data.note("last", None::); + if latest.is_some() { + *last = latest; + } + let readout = last.as_ref().map_or_else(|| "No event yet".to_owned(), |event| format!("Last event: {event}")); + output.into().readout(readout) +} + #[story(StrokeEditor<'static>, covers = [], drive = Drive::Never)] fn stroke_editor_popover(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let style = data.value("popover", StrokeStyle::default()); - StrokeEditor::new("Stroke", Bind::bound(style)).sections(StrokeSections::POPOVER).show(ui).into() + let out = StrokeEditor::new("Stroke", Bind::bound(style)).sections(StrokeSections::POPOVER).show(ui); + let latest = out.events.last().map(|event| format!("{event:?}")); + with_readout(data, out, latest) } #[story(StrokeEditor<'static>, covers = [], drive = Drive::Never)] fn stroke_editor_panel(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let style = data.value("panel", StrokeStyle::default()); - StrokeEditor::new("Stroke", Bind::bound(style)).sections(StrokeSections::PANEL).show(ui).into() + let out = StrokeEditor::new("Stroke", Bind::bound(style)).sections(StrokeSections::PANEL).show(ui); + let latest = out.events.last().map(|event| format!("{event:?}")); + with_readout(data, out, latest) } #[story(StrokeEditor<'static>, covers = [], drive = Drive::Never)] fn stroke_editor_open_path(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let style = data.value("open", StrokeStyle::default()); - StrokeEditor::new("Stroke", Bind::bound(style)).sections(StrokeSections::POPOVER).closed(false).show(ui).into() + let out = StrokeEditor::new("Stroke", Bind::bound(style)).sections(StrokeSections::POPOVER).closed(false).show(ui); + let latest = out.events.last().map(|event| format!("{event:?}")); + with_readout(data, out, latest) } #[story(StrokeEditor<'static>, covers = [], drive = Drive::Never)] fn stroke_editor_dashed(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let style = data.value("dashed", StrokeStyle::default().with_line_style(super::LineStyle::Dashed)); - StrokeEditor::new("Stroke", Bind::bound(style)).sections(StrokeSections::POPOVER).show(ui).into() + let out = StrokeEditor::new("Stroke", Bind::bound(style)).sections(StrokeSections::POPOVER).show(ui); + let latest = out.events.last().map(|event| format!("{event:?}")); + with_readout(data, out, latest) } #[story(StrokeEditor<'static>, covers = [Mixed], drive = Drive::Never)] @@ -71,7 +89,9 @@ fn stroke_editor_mixed(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let base = StrokeStyle::default(); StrokeMixed::of([&base, &other_than(&base)]).expect("two styles") }); - StrokeEditor::new("Stroke", Bind::bound(style)).sections(StrokeSections::POPOVER).mixed(mixed).show(ui).into() + let out = StrokeEditor::new("Stroke", Bind::bound(style)).sections(StrokeSections::POPOVER).mixed(mixed).show(ui); + let latest = out.events.last().map(|event| format!("{event:?}")); + with_readout(data, out, latest) } /// The arrowhead `id` at one end of the line through `rect`. @@ -124,11 +144,13 @@ struct Picks { start: String, end: String, profile: String, + last: Option, } #[story(StrokeEditor<'static>, covers = [], drive = Drive::Never)] fn stroke_editor_choice_rows(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { - let picks = data.value("rows", Picks { style: StrokeStyle::default(), start: "none".into(), end: "arrow".into(), profile: "uniform".into() }); + let picks = + data.value("rows", Picks { style: StrokeStyle::default(), start: "none".into(), end: "arrow".into(), profile: "uniform".into(), last: None }); let heads = [ StrokeChoice::new("none", "None"), StrokeChoice::new("arrow", "Arrow"), @@ -151,7 +173,18 @@ fn stroke_editor_choice_rows(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let out = StrokeEditor::new("Stroke", Bind::bound(&mut picks.style)).rows(&rows).show(ui); let picked = out.choice.clone(); let story: StoryOutput = out.into(); + if let Some((row, choice)) = &picked { + let row_name = match row.as_str() { + START => "Start", + END => "End", + _ => "Profile", + }; + let choice_name = heads.iter().chain(&profiles).find(|c| c.id == *choice).map_or(choice.as_str(), |c| c.label.as_str()); + picks.last = Some(format!("{row_name}: {choice_name}")); + } + let readout = picks.last.as_ref().map_or_else(|| "No event yet".to_owned(), |event| format!("Last event: {event}")); drop(rows); + let story = story.readout(readout); if let Some((row, choice)) = picked { match row.as_str() { START => picks.start = choice, @@ -165,20 +198,26 @@ fn stroke_editor_choice_rows(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { #[story(StrokeButton<'static>, covers = [Default], drive = Drive::Never)] fn stroke_button(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let (style, open) = data.value("button", (StrokeStyle::default(), false)); - StrokeButton::new("Stroke Options", Bind::bound(style), Bind::bound(open)).show(ui).into() + let out = StrokeButton::new("Stroke Options", Bind::bound(style), Bind::bound(open)).show(ui); + let latest = out.events.last().map(|event| format!("{event:?}")); + with_readout(data, out, latest) } #[story(StrokeButton<'static>, covers = [Mixed], drive = Drive::Never)] fn stroke_button_mixed(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let (style, open) = data.value("button mixed", (Mixed::::Mixed, false)); - StrokeButton::new("Stroke Options", Bind::bound_mixed(style), Bind::bound(open)).show(ui).into() + let out = StrokeButton::new("Stroke Options", Bind::bound_mixed(style), Bind::bound(open)).show(ui); + let latest = out.events.last().map(|event| format!("{event:?}")); + with_readout(data, out, latest) } #[story(StrokeButton<'static>, covers = [Invalid], drive = Drive::Never)] fn stroke_button_refused(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let (style, open) = data.value("button refused", (StrokeStyle::default().with_line_style(super::LineStyle::Dashed), false)); let refuse_dashes = |style: &StrokeStyle| if style.dashes.is_empty() { Validity::Ok } else { Validity::Refuse(Message::new("Solid lines only")) }; - StrokeButton::new("Stroke Options", Bind::bound(style).check(refuse_dashes), Bind::bound(open)).show(ui).into() + let out = StrokeButton::new("Stroke Options", Bind::bound(style).check(refuse_dashes), Bind::bound(open)).show(ui); + let latest = out.events.last().map(|event| format!("{event:?}")); + with_readout(data, out, latest) } /// Opens the popover with a click on the button, then picks the Dashed preset. @@ -196,5 +235,7 @@ fn pick_dashed(fixture: &mut Fixture) { #[story(StrokeButton<'static>, covers = [Open], drive = Drive::Custom(pick_dashed))] fn stroke_button_open(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let style = data.value("button open", StrokeStyle::default()); - StrokeButton::new("Stroke Options", Bind::bound(style), Bind::given(false)).show(ui).into() + let out = StrokeButton::new("Stroke Options", Bind::bound(style), Bind::given(false)).show(ui); + let latest = out.events.last().map(|event| format!("{event:?}")); + with_readout(data, out, latest) } diff --git a/crates/draw/src/ui/symmetry/stories.rs b/crates/draw/src/ui/symmetry/stories.rs index 6c6a2cb3a..e8271f151 100644 --- a/crates/draw/src/ui/symmetry/stories.rs +++ b/crates/draw/src/ui/symmetry/stories.rs @@ -19,6 +19,11 @@ fn sample(mode: SymmetryMode) -> Symmetry { Symmetry { mode, center: SHOWN.centre(), rotation: 20.0 } } +/// The count of axes the symmetry draws over the page. +fn axes(symmetry: &Symmetry) -> String { + format!("{} axes on the page", symmetry.segments(PAGE).len()) +} + /// A view that shows the page at 100 %, centred. fn view(ui: &Ui, room: egui::Rect) -> CanvasView { SHOWN.view(room, ui.pixels_per_point(), 100.0) @@ -48,7 +53,7 @@ fn dual_axis(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { craft_ui::testing::canvas_story::show_document(ui, body.rect, &SHOWN); SymmetryOverlay::new("Symmetry", &symmetry, PAGE).show_at(ui, body.rect); }); - StoryOutput::default() + StoryOutput::default().readout(axes(&symmetry)) } #[story(SymmetryOverlay<'static>, covers = [Default], drive = Drive::Never)] @@ -60,7 +65,7 @@ fn editing_guides(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { craft_ui::testing::canvas_story::show_document(ui, body.rect, &SHOWN); SymmetryOverlay::new("Symmetry", &symmetry, PAGE).editing(true).show_at(ui, body.rect); }); - StoryOutput::default() + StoryOutput::default().readout(axes(&symmetry)) } #[story(SymmetryHandles<'static>, covers = [Default], drive = Drive::Drag(Grip::part(SYMMETRY.at(CENTRE))))] @@ -72,5 +77,6 @@ fn handles_on_a_canvas(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { craft_ui::testing::canvas_story::show_document(ui, body.rect, &SHOWN); SymmetryHandles::new("Symmetry", Bind::bound(&mut *symmetry), PAGE).show_at(ui, body.rect) }); - shown.inner.map(|output| StoryOutput::from(output.edit)).unwrap_or_default() + let output = shown.inner.map(|output| StoryOutput::from(output.edit)).unwrap_or_default(); + output.readout(axes(symmetry)) } diff --git a/crates/imaging/src/ui/channels/stories.rs b/crates/imaging/src/ui/channels/stories.rs index e41dde78a..4dbb3c820 100644 --- a/crates/imaging/src/ui/channels/stories.rs +++ b/crates/imaging/src/ui/channels/stories.rs @@ -4,7 +4,7 @@ use craft_theme::Srgba; use craft_ui::testing::{Drive, StoryData, StoryOutput, story}; use egui::Ui; -use super::{ChannelKind, ChannelRow, ChannelsPanel, ColorIndicates}; +use super::{ChannelKind, ChannelRow, ChannelsOutput, ChannelsPanel, ColorIndicates}; type Rows = Vec>; @@ -44,20 +44,33 @@ fn multichannel_rows() -> Rows { rows } +/// The panel's output with the readout of its latest event, kept in the story's data across passes. +fn with_readout(data: &mut StoryData, shown: ChannelsOutput) -> StoryOutput { + let last = data.note("last", None::); + if let Some(event) = shown.events.last() { + *last = Some(format!("{event:?}")); + } + let readout = last.as_ref().map_or_else(|| "No event yet".to_owned(), |event| format!("Last event: {event}")); + StoryOutput::from(shown).readout(readout) +} + #[story(ChannelsPanel<'static, u32>, covers = [Default], drive = Drive::Never)] fn channels_panel_alpha_and_spot(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let rows = data.value("rows", alpha_and_spot_rows()).clone(); - ChannelsPanel::new(&rows).show(ui).into() + let shown = ChannelsPanel::new(&rows).show(ui); + with_readout(data, shown) } #[story(ChannelsPanel<'static, u32>, covers = [], drive = Drive::Never)] fn channels_panel_quick_mask_and_layer_mask(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let rows = data.value("rows", mask_rows()).clone(); - ChannelsPanel::new(&rows).show(ui).into() + let shown = ChannelsPanel::new(&rows).show(ui); + with_readout(data, shown) } #[story(ChannelsPanel<'static, u32>, covers = [], drive = Drive::Never)] fn channels_panel_multichannel(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let rows = data.value("rows", multichannel_rows()).clone(); - ChannelsPanel::new(&rows).show(ui).into() + let shown = ChannelsPanel::new(&rows).show(ui); + with_readout(data, shown) } diff --git a/crates/layers/src/ui/panel/stories.rs b/crates/layers/src/ui/panel/stories.rs index bb8b27e2d..07c30da01 100644 --- a/crates/layers/src/ui/panel/stories.rs +++ b/crates/layers/src/ui/panel/stories.rs @@ -5,7 +5,7 @@ use craft_ui::contract::Bind; use craft_ui::testing::{Drive, StoryData, StoryOutput, story}; use egui::Ui; -use super::LayersPanel; +use super::{LayersOutput, LayersPanel}; use crate::ui::filter::{FilterBy, LayerFilter}; use crate::ui::mock::FakeLayers; use crate::ui::tree::{LayerPart, LayerSelection}; @@ -17,27 +17,41 @@ fn selected() -> FakeLayers { tree } +/// The panel's output with the readout of its latest event, kept in the story's data across passes. +fn with_readout(data: &mut StoryData, shown: LayersOutput) -> StoryOutput { + let last = data.note("last", None::); + if let Some(event) = shown.events.last() { + *last = Some(format!("{event:?}")); + } + let readout = last.as_ref().map_or_else(|| "No event yet".to_owned(), |event| format!("Last event: {event}")); + StoryOutput::from(shown).readout(readout) +} + #[story(LayersPanel<'static, FakeLayers>, covers = [Default], drive = Drive::Never)] -fn layers_panel_tree(ui: &mut Ui, _data: &mut StoryData) -> StoryOutput { +fn layers_panel_tree(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let tree = selected(); - LayersPanel::new(&tree).show(ui).into() + let shown = LayersPanel::new(&tree).show(ui); + with_readout(data, shown) } #[story(LayersPanel<'static, FakeLayers>, covers = [], drive = Drive::Never)] -fn layers_panel_no_active_layer(ui: &mut Ui, _data: &mut StoryData) -> StoryOutput { +fn layers_panel_no_active_layer(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let tree = FakeLayers::sample(); - LayersPanel::new(&tree).show(ui).into() + let shown = LayersPanel::new(&tree).show(ui); + with_readout(data, shown) } #[story(LayersPanel<'static, FakeLayers>, covers = [], drive = Drive::Never)] fn layers_panel_filtered(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let tree = selected(); let filter = data.value("filter", LayerFilter { by: FilterBy::Name, name: String::from("o"), ..LayerFilter::default() }); - LayersPanel::new(&tree).filter(Bind::bound(filter)).show(ui).into() + let shown = LayersPanel::new(&tree).filter(Bind::bound(filter)).show(ui); + with_readout(data, shown) } #[story(LayersPanel<'static, FakeLayers>, covers = [], drive = Drive::Never)] -fn layers_panel_extra_columns(ui: &mut Ui, _data: &mut StoryData) -> StoryOutput { +fn layers_panel_extra_columns(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let tree = selected(); - LayersPanel::new(&tree).lock_column(true).show(ui).into() + let shown = LayersPanel::new(&tree).lock_column(true).show(ui); + with_readout(data, shown) } diff --git a/crates/ui-contract/src/testing/story.rs b/crates/ui-contract/src/testing/story.rs index 01ef3a2a6..020310fe7 100644 --- a/crates/ui-contract/src/testing/story.rs +++ b/crates/ui-contract/src/testing/story.rs @@ -208,6 +208,8 @@ pub struct StoryData { /// What a story said about how a value is edited: a range or a list of choices. #[doc(hidden)] pub how: HashMap<(&'static str, TypeId), Edit>, + /// What the story keeps about what happened in it ([`StoryData::note`]). + notes: HashMap<(&'static str, TypeId), Box>, /// The story's `disabled_text`. disabled_text: Option<&'static str>, } @@ -292,6 +294,19 @@ impl StoryData { } } + /// The note named `name` of type `T`, created from `init` on first use: what the story keeps about what happened in + /// it for its readout ([`StoryOutput::readout`]), such as the last event or a click count. A note is not a value: + /// the runner's drives and a viewer's Reset compare the values only, so a cancelled drag that the readout reports + /// leaves the values as they were. + #[allow(clippy::unreachable, reason = "the slot key holds the type, so the downcast cannot fail")] + pub fn note(&mut self, name: &'static str, init: T) -> &mut T { + let slot = self.notes.entry((name, TypeId::of::())).or_insert_with(|| Box::new(init)); + match slot.downcast_mut::() { + Some(note) => note, + None => unreachable!("a slot's note has the type its key names"), + } + } + /// Like [`StoryData::value`] for a number the Properties editor keeps inside `range`. pub fn ranged(&mut self, name: &'static str, init: T, range: RangeInclusive) -> &mut T { self.how.entry((name, TypeId::of::())).or_insert(Edit::Range(range)); @@ -459,9 +474,19 @@ pub struct StoryOutput { pub visible: Option>, /// The shown value as the component words it ([`crate::contract::Outcome::value_text`]). pub value_text: Option, + /// The story's readout: a line in the story's own words about what it shows or what last happened in it ("Last + /// event: …", "Clicked 2 times"), which a viewer shows under the story. + pub readout: Option, } impl StoryOutput { + /// The same output with `text` as its readout. + #[must_use] + pub fn readout(mut self, text: impl Into) -> Self { + self.readout = Some(text.into()); + self + } + pub(crate) fn from_response(response: &Response) -> Self { StoryOutput { id: Some(response.id), clicked: super::contract::clicked(response), ..StoryOutput::default() } } diff --git a/crates/ui-gallery/src/chrome/card.rs b/crates/ui-gallery/src/chrome/card.rs index 11b3f32b9..de0f3ff8b 100644 --- a/crates/ui-gallery/src/chrome/card.rs +++ b/crates/ui-gallery/src/chrome/card.rs @@ -1,8 +1,8 @@ //! One card: stories live on their surface under a header with the component's name and, while a story differs from //! its start, Reset; the Open command of an Overview card opens the component's tab. A card of several stories lays -//! them out as the component's catalog entry says, each under its caption. A story runs in the shell of the view the card -//! is in, so its dialogs open over that view, as an app's do. [`StoryBody`] is the story part alone, which the story -//! renderer (`story-render`) shows too. +//! them out as the component's catalog entry says, each under its caption and over its readout. A story runs in the +//! shell of the view the card is in, so its dialogs open over that view, as an app's do. [`StoryBody`] is the story +//! part alone, which the story renderer (`story-render`) shows too. use super::inspect::{Inspected, region_key}; use crate::viewer::{CardState, Cells, DialogState}; @@ -313,6 +313,9 @@ impl StoryBody { let before = shell.as_ref().map_or(0, |shell| shell.dialogs_shown()); let (output, record) = host.content(ui, show, data, self.inspect.map(|(key, _)| key)); recorded = record.or(recorded.take()); + if let Some(readout) = &output.readout { + Text::new(readout.clone()).shade(Shade::Muted).show(ui); + } if !ui.is_sizing_pass() { ran.set(Some(shell.is_some_and(|shell| shell.dialogs_shown() > before))); } diff --git a/crates/ui-gallery/tests/it/overview.rs b/crates/ui-gallery/tests/it/overview.rs index 713b50298..a6794a36e 100644 --- a/crates/ui-gallery/tests/it/overview.rs +++ b/crates/ui-gallery/tests/it/overview.rs @@ -218,3 +218,33 @@ fn a_card_at_rest_shows_no_reset() { assert!(fixture.by_label("Reset").is_none(), "{name}'s card shows Reset with no input"); } } + +/// A story's readout shows under the story and follows what happens in it: the LayersPanel card says that nothing +/// happened yet, and after a click on a layer row it names the event. +#[trial] +fn a_card_shows_its_storys_readout_under_the_story_and_follows_it() { + let mut gallery = gallery_filtered(level_of("LayersPanel"), "LayersPanel"); + let mut fixture = Fixture::new(move |ui| gallery.ui(ui)); + fixture.room(Vec2::new(1400.0, 900.0)).settle(); + let readout = |fixture: &Fixture| { + fixture.texts().iter().find(|run| run.text == "No event yet" || run.text.starts_with("Last event: ")).map(|run| (run.text.clone(), run.rect)) + }; + let (before, at) = readout(&fixture).expect("the card shows its story's readout"); + assert_eq!(before, "No event yet"); + let card = fixture + .by_role(Role::GROUP) + .into_iter() + .find(|group| fixture.tree().name_of(group.node) == Some("LayersPanel")) + .expect("the LayersPanel card shows") + .rect; + let rows: Vec<_> = [Role::TREE_ITEM, Role::LIST_ITEM, Role::ROW] + .into_iter() + .flat_map(|role| fixture.by_role(role)) + .filter(|row| card.contains_rect(row.rect)) + .collect(); + let row = *rows.last().expect("the panel shows layer rows"); + assert!(rows.iter().all(|row| row.rect.bottom() <= at.top()), "the readout lies under the story: {at:?}"); + fixture.click(&row).settle(); + let (after, _) = readout(&fixture).expect("the readout stays"); + assert!(after.starts_with("Last event: "), "the readout names the click's event: {after}"); +} diff --git a/crates/ui-panels/src/rails/tool/stories.rs b/crates/ui-panels/src/rails/tool/stories.rs index 7bed45378..2f02c7cdc 100644 --- a/crates/ui-panels/src/rails/tool/stories.rs +++ b/crates/ui-panels/src/rails/tool/stories.rs @@ -53,5 +53,17 @@ fn tool_rail(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { .column_toggle("Toolbar") .edit_toolbar("Edit Toolbar") .show_rail(ui, |_, _| {}); - out.into() + let note = data.note("note", "Nothing yet".to_owned()); + if let Some(tool) = out.select { + *note = format!("selected {tool}"); + } else if let Some(tool) = out.double_clicked { + *note = format!("double clicked {tool}"); + } else if out.toggle_columns { + *note = "toggle columns".to_owned(); + } else if out.edit_toolbar { + *note = "edit toolbar".to_owned(); + } + let flyout = out.flyout.map_or_else(|| "closed".to_owned(), |slot| format!("of slot {slot} open")); + let readout = format!("{note}; flyout {flyout}"); + StoryOutput::from(out).readout(readout) } diff --git a/crates/ui-widgets/src/choice/swatch/stories.rs b/crates/ui-widgets/src/choice/swatch/stories.rs index b69eb9dfa..6f7b6c143 100644 --- a/crates/ui-widgets/src/choice/swatch/stories.rs +++ b/crates/ui-widgets/src/choice/swatch/stories.rs @@ -3,7 +3,6 @@ use super::Swatch; use crate::choice::RadioGroup; use crate::contract::{Bind, Gap, Props as _, Show as _, ShowContainer as _}; -use crate::display::Text; use crate::layout_components::{Row, Stack}; use crate::testing::{Drive, StoryData, StoryOutput, show_as_only_item, story}; use craft_behavior::Mixed; @@ -35,15 +34,16 @@ fn swatch(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { /// Opaque, translucent, clear, a colour equal to the panel, mixed and selected, each named by its label. #[story(Swatch, covers = [], drive = Drive::Never)] -fn swatch_states(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { +fn swatch_states(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let clicks = data.note("clicks", 0u32); Row::new().show(ui, |ui, _| { - Swatch::new("Opaque", BLUE).show(ui); - Swatch::new("Translucent", Srgba { r: 0.90, g: 0.28, b: 0.30, a: 0.43 }).show(ui); - Swatch::new("Clear", Srgba { r: 0.0, g: 0.0, b: 0.0, a: 0.0 }).show(ui); - Swatch::new("Mixed", Mixed::::Mixed).show(ui); - Swatch::new("Selected", PALETTE[5]).selected(true).show(ui); + *clicks += u32::from(Swatch::new("Opaque", BLUE).show(ui).clicked); + *clicks += u32::from(Swatch::new("Translucent", Srgba { r: 0.90, g: 0.28, b: 0.30, a: 0.43 }).show(ui).clicked); + *clicks += u32::from(Swatch::new("Clear", Srgba { r: 0.0, g: 0.0, b: 0.0, a: 0.0 }).show(ui).clicked); + *clicks += u32::from(Swatch::new("Mixed", Mixed::::Mixed).show(ui).clicked); + *clicks += u32::from(Swatch::new("Selected", PALETTE[5]).selected(true).show(ui).clicked); }); - StoryOutput::default() + StoryOutput::default().readout(format!("Clicked {clicks} times")) } /// Twelve swatches in a `RadioGroup`: one Tab stop, arrows move, Space picks. @@ -77,12 +77,11 @@ fn swatch_table_cells(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { /// A well and a line that counts its clicks. #[story(Swatch, covers = [], drive = Drive::Never)] fn swatch_clicks(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { - let clicks = data.value("clicks", 0u32); + let clicks = data.note("clicks", 0u32); Stack::new().show(ui, |ui, _| { if Swatch::new("Fill", BLUE).show(ui).clicked { *clicks += 1; } - Text::new(format!("{clicks} clicks")).show(ui); }); - StoryOutput::default() + StoryOutput::default().readout(format!("Clicked {clicks} times")) }