From 197d70c249be5dc51162e8bd2578eeaf60c54797 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 08:25:23 +0000 Subject: [PATCH] Gradient stories: swatches with names and full-width bars, click count; editor colour-request readout --- crates/draw/src/ui/gradient/editor/stories.rs | 37 ++++++++++++++-- crates/draw/src/ui/gradient/swatch/stories.rs | 43 ++++++++++++++++--- 2 files changed, 69 insertions(+), 11 deletions(-) diff --git a/crates/draw/src/ui/gradient/editor/stories.rs b/crates/draw/src/ui/gradient/editor/stories.rs index 9863e04a1..5a1762ced 100644 --- a/crates/draw/src/ui/gradient/editor/stories.rs +++ b/crates/draw/src/ui/gradient/editor/stories.rs @@ -1,12 +1,38 @@ //! Stories of `GradientEditor`. -use super::GradientEditor; +use super::{GradientEditor, GradientEditorOutput}; use crate::ui::gradient::{ColorStop, GradientStops, OpacityStop, StopRef, StopRows, StopRules}; use craft_theme::Srgba; use craft_ui::contract::Bind; +use craft_ui::runtime::env::Env; use craft_ui::testing::{Drive, StoryData, StoryOutput, story}; use egui::Ui; +craft_ui::words! { + /// The status line of the gradient editor stories. + #[non_exhaustive] + pub(crate) struct GradientEditorStoryWords { + /// How to ask for a stop's colour. + hint: "gradientEditorStory.hint" = "Double-click a color stop, or press Enter on it, to edit its color"; + /// The editor asked the host for a stop's colour: `{number}` from 1. + requested: template "gradientEditorStory.requested" = "Color requested for stop {number}"; + } +} + +/// The story's output from the editor's, with the last colour request as its readout, or the hint before one. +fn hinted

(ui: &Ui, data: &mut StoryData, output: GradientEditorOutput

) -> StoryOutput { + let words = Env::of(ui).words::(); + let requested = data.note("requested", None::); + if let Some(stop) = &output.color_requested { + *requested = Some(stop.index); + } + let readout = match *requested { + Some(index) => craft_i18n::fmt(words.requested.as_str(), &[("number", (index + 1).to_string().as_str())]), + None => words.hint.as_str().to_owned(), + }; + StoryOutput::from(output).readout(readout) +} + fn color(at: f32, [r, g, b]: [f32; 3]) -> ColorStop { ColorStop::new(at, Srgba::new(r, g, b, 1.0)) } @@ -31,13 +57,15 @@ fn combined() -> GradientStops { #[story(GradientEditor<'static, u8>, covers = [Default], drive = Drive::Never)] fn gradient_editor(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let stops = data.value("stops", split()); - GradientEditor::new("Gradient", Bind::bound(stops)).show(ui).into() + let output = GradientEditor::new("Gradient", Bind::bound(stops)).show(ui); + hinted(ui, data, output) } #[story(GradientEditor<'static, u8>, covers = [], drive = Drive::Never)] fn gradient_editor_combined(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let stops = data.value("stops", combined()); - GradientEditor::new("Gradient", Bind::bound(stops)).rules(StopRules::new(StopRows::Combined)).show(ui).into() + let output = GradientEditor::new("Gradient", Bind::bound(stops)).rules(StopRules::new(StopRows::Combined)).show(ui); + hinted(ui, data, output) } #[story(GradientEditor<'static, u8>, covers = [], drive = Drive::Never)] @@ -50,5 +78,6 @@ fn gradient_editor_color_only(ui: &mut Ui, data: &mut StoryData) -> StoryOutput #[story(GradientEditor<'static, u8>, covers = [], drive = Drive::Never)] fn gradient_editor_popover(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let (stops, selected) = data.value("state", (split(), Some(StopRef::Color(0)))); - GradientEditor::new("Gradient", Bind::bound(stops)).color_popover(true).shared_selection(Bind::bound(selected)).show(ui).into() + let output = GradientEditor::new("Gradient", Bind::bound(stops)).color_popover(true).shared_selection(Bind::bound(selected)).show(ui); + hinted(ui, data, output) } diff --git a/crates/draw/src/ui/gradient/swatch/stories.rs b/crates/draw/src/ui/gradient/swatch/stories.rs index 4c9155c30..42ecbfe87 100644 --- a/crates/draw/src/ui/gradient/swatch/stories.rs +++ b/crates/draw/src/ui/gradient/swatch/stories.rs @@ -4,17 +4,30 @@ use super::GradientSwatch; use crate::ui::gradient::{ColorStop, GradientStops, OpacityStop, StopRows}; use craft_theme::Srgba; use craft_ui::contract::{Show as _, ShowContainer as _}; -use craft_ui::layout_components::Stack; +use craft_ui::display::{Shade, Text}; +use craft_ui::layout_components::{Row, Stack}; +use craft_ui::runtime::env::Env; use craft_ui::testing::{Drive, StoryData, StoryOutput, story}; use egui::Ui; +craft_ui::words! { + /// The status line of the gradient swatch story. + #[non_exhaustive] + pub(crate) struct GradientSwatchStoryWords { + /// How many times a swatch was clicked. + clicked: template "gradientSwatchStory.clicked" = "Clicked {count} times"; + } +} + fn stop(at: f32, [r, g, b]: [f32; 3]) -> ColorStop { ColorStop::new(at, Srgba::new(r, g, b, 1.0)) } -/// Three gradients: fading in, with a skewed midpoint, and with the opacity in the colour. +/// Three gradients: fading in, with a skewed midpoint, and with the opacity in the colour. Each shows as a swatch +/// with its name, then filling the width. #[story(GradientSwatch, covers = [Default], drive = Drive::Never)] -fn gradient_swatch(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { +fn gradient_swatch(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let words = Env::of(ui).words::(); let mut fading = GradientStops::new(vec![stop(0.0, [0.96, 0.65, 0.14]), stop(1.0, [0.24, 0.39, 0.87])]); let mut skewed = fading.clone(); skewed.color[0].midpoint = 0.15; @@ -24,10 +37,26 @@ fn gradient_swatch(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { ColorStop::new(0.5, Srgba::new(0.9, 0.28, 0.3, 0.0)), ColorStop::new(1.0, Srgba::new(0.19, 0.64, 0.42, 1.0)), ]); + let gradients = [ + ("Fading in", &fading, StopRows::Split), + ("Midpoint at 15 %", &skewed, StopRows::Split), + ("Opacity in the colour", &combined, StopRows::Combined), + ]; + let clicks = data.note("clicks", 0_u32); + let mut clicked = false; Stack::new().show(ui, |ui, _| { - GradientSwatch::new("Fading in", &fading).fill().has_popup().show(ui); - GradientSwatch::new("Midpoint at 15 %", &skewed).fill().show(ui); - GradientSwatch::new("Opacity in the colour", &combined).rows(StopRows::Combined).fill().show(ui); + for (name, stops, rows) in gradients { + Row::new().show(ui, |ui, _| { + clicked |= GradientSwatch::new(name, stops).rows(rows).has_popup().show(ui).clicked; + Text::new(name).shade(Shade::Muted).show(ui); + }); + clicked |= GradientSwatch::new(name, stops).rows(rows).fill().show(ui).clicked; + } }); - StoryOutput::default() + if clicked { + *clicks += 1; + } + let mut output = StoryOutput::default(); + output.clicked = clicked; + output.readout(craft_i18n::fmt(words.clicked.as_str(), &[("count", clicks.to_string().as_str())])) }