From 331a2a3ab2c0c2a31bb21df92b527ace3eb65679 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sun, 15 Nov 2026 04:46:08 +0000 Subject: [PATCH] tools-w11: net change of 4 commits - tools-w11: written from its sheet - tools-w11: repair 1 - Integration: gradient overlay builds its non_exhaustive StopColor with StopColor::new - tools-w11: review fixes (review-1) --- crates/draw/src/ui/gradient/fill.rs | 9 +- crates/draw/src/ui/gradient/fill_editor.rs | 141 ++++++++++++++++-- .../src/ui/gradient/fill_editor/stories.rs | 50 +++++++ .../draw/src/ui/gradient/gradient_line/mod.rs | 16 +- .../src/ui/gradient/gradient_overlay/keys.rs | 5 +- .../src/ui/gradient/gradient_overlay/mod.rs | 36 ++--- .../src/ui/gradient/gradient_overlay/parts.rs | 3 +- .../ui/gradient/gradient_overlay/pointer.rs | 5 +- .../ui/gradient/gradient_overlay/stories.rs | 16 +- crates/draw/tests/it/gradient_fill.rs | 112 ++++++++++++++ crates/draw/tests/it/gradient_overlay.rs | 3 +- crates/draw/tests/it/main.rs | 1 + migration/api-map/draw-canvas-controls.toml | 10 +- 13 files changed, 345 insertions(+), 62 deletions(-) create mode 100644 crates/draw/src/ui/gradient/fill_editor/stories.rs create mode 100644 crates/draw/tests/it/gradient_fill.rs diff --git a/crates/draw/src/ui/gradient/fill.rs b/crates/draw/src/ui/gradient/fill.rs index 9299240d6..7ab677932 100644 --- a/crates/draw/src/ui/gradient/fill.rs +++ b/crates/draw/src/ui/gradient/fill.rs @@ -31,8 +31,13 @@ impl GradientStyle { /// The style's name. pub fn label(self, words: &GradientFillWords) -> Label { - let _ = (self, words); - todo!("tl.N2") + match self { + GradientStyle::Linear => words.linear.clone().into(), + GradientStyle::Radial => words.radial.clone().into(), + GradientStyle::Angle => words.angle_style.clone().into(), + GradientStyle::Reflected => words.reflected.clone().into(), + GradientStyle::Diamond => words.diamond.clone().into(), + } } /// The style's icon: `gradient_linear`, `gradient_radial`, `gradient_angle`, `gradient_reflected` diff --git a/crates/draw/src/ui/gradient/fill_editor.rs b/crates/draw/src/ui/gradient/fill_editor.rs index 0c3522f54..8437c3be2 100644 --- a/crates/draw/src/ui/gradient/fill_editor.rs +++ b/crates/draw/src/ui/gradient/fill_editor.rs @@ -1,15 +1,29 @@ //! [`GradientFillEditor`]: the gradient fill rows of a shape or a Gradient Fill layer. -use craft_behavior::EditEvents; +use std::sync::Arc; + +use craft_behavior::{EditEvent, EditEvents, Mixed}; use craft_color::ui::ScreenPickGesture; -use craft_color::ui::gradient::{GradientStops, StopColor}; +use craft_color::ui::gradient::{GradientEditor, GradientStops, StopColor}; use craft_theme::Srgba; -use craft_ui::contract::{Bind, NoValue, Outcome}; +use craft_ui::buttons::Button; +use craft_ui::choice::{Checkbox, Segmented}; +use craft_ui::contract::{Bind, Entry, Outcome, Props as _, Show as _, ShowContainer as _}; +use craft_ui::display::Text; +use craft_ui::layout_components::{Row, Stack}; use craft_ui::patterns::presets::PresetState; -use craft_ui::runtime::env::Label; +use craft_ui::runtime::env::{Env, Label, UnitKind}; +use craft_ui::text::NumberField; +use craft_ui::value_drag::AngleDial; use egui::{Response, Ui}; -use super::{GradientFill, GradientGroup, GradientRef}; +use super::palette::stops_of; +use super::{GradientFill, GradientGroup, GradientPalette, GradientRef, GradientStyle}; + +/// The range of the Angle rows, in degrees. +const ANGLE_RANGE: std::ops::RangeInclusive = -180.0..=180.0; +/// The range of the Scale field, in percent (Photoshop's). +const SCALE_RANGE: std::ops::RangeInclusive = 10.0..=150.0; craft_ui::words! { /// The words of the gradient fill rows. @@ -19,6 +33,7 @@ craft_ui::words! { angle_style: "gradientFill.angleStyle" = "Angle"; reflected: "gradientFill.reflected" = "Reflected"; diamond: "gradientFill.diamond" = "Diamond"; + fill: "gradientFill.fill" = "Gradient Fill"; style: "gradientFill.style" = "Style"; angle: "gradientFill.angle" = "Angle"; scale: "gradientFill.scale" = "Scale"; @@ -32,7 +47,8 @@ craft_ui::words! { /// The shape gradient tab and the fill layer's Gradient Fill rows: the palette, the stop editor, the /// style, angle, scale, reverse, dither and align rows, and Reset Alignment. Each child's events -/// become events of the whole value; the editor keeps no state between passes. +/// become events of the whole value; the editor keeps no state between passes, so a second pass in +/// one frame applies only the input of its own pass. #[must_use = "show the editor with `show`"] #[derive(craft_ui::contract::Pattern)] pub struct GradientFillEditor<'a> { @@ -62,13 +78,41 @@ pub struct GradientFillEditorOutput { } impl Outcome for GradientFillEditorOutput { - type Value = NoValue; + type Value = GradientFill; fn response(&self) -> &Response { &self.response } } +/// `events` of one field as events of the whole fill: each carried value is `base` with the field set by `set`. +fn lift(events: EditEvents, base: &GradientFill, set: impl Fn(&mut GradientFill, T)) -> EditEvents { + events.map(|field| { + let mut fill = base.clone(); + set(&mut fill, field); + fill + }) +} + +/// Adds `lifted` to the pass's `events`. +fn absorb(events: &mut EditEvents, lifted: EditEvents) { + for event in lifted.iter() { + events.push(event.clone()); + } +} + +/// `stops` with every midpoint centred, as a preset gives them. +fn centred(stops: &GradientStops) -> GradientStops { + let mut stops = stops.clone(); + for stop in &mut stops.color { + stop.midpoint = 0.5; + } + for stop in &mut stops.opacity { + stop.midpoint = 0.5; + } + stops +} + impl<'a> GradientFillEditor<'a> { /// The editor of `value`, offering the preset `groups` with their palette `state`. pub fn new( @@ -94,8 +138,85 @@ impl<'a> GradientFillEditor<'a> { /// Shows the editor. pub fn show(self, ui: &mut Ui) -> GradientFillEditorOutput { - let GradientFillEditor { label, value, groups, state, recent, sampled } = self; - let _ = (ui, label, value, groups, state, recent, sampled); - todo!("tl.T35") + let GradientFillEditor { label, mut value, groups, mut state, recent, sampled } = self; + let env = Env::of(ui); + let words = env.words::(); + let label_text: Arc = Arc::from(words.fill.as_str()); + let shown = Stack::new().fill_width().show(ui, |ui, cx| { + let mut events = EditEvents::::new(); + let mut picked = None; + let mut color_requested = None; + let mut screen_pick = None; + let Some(now) = value.reborrow().read(cx).and_then(|held| held.same().cloned()) else { + return (events, picked, color_requested, screen_pick); + }; + if !groups.is_empty() || !recent.is_empty() { + let palette = GradientPalette::new(label.clone(), groups, state.reborrowed()).recent(recent).show(ui); + picked = palette.picked; + if let Some(key) = picked + && let Some(stops) = stops_of(groups, recent, key) + { + let mut next = now.clone(); + next.stops = if matches!(key, GradientRef::Recent(_)) { stops.clone() } else { centred(stops) }; + if next != now { + events.push(EditEvent::set(Mixed::Same(now.clone()), next, label_text.clone(), None)); + } + } + } + let mut editor = GradientEditor::new(label.clone(), Bind::given(now.stops.clone())); + if let Some(color) = sampled { + editor = editor.sampled(color); + } + let stops = editor.show(ui); + color_requested = stops.color_requested; + screen_pick = stops.screen_pick; + absorb(&mut events, lift(stops.edit.events, &now, |fill, stops| fill.stops = stops)); + + Text::new(words.options.clone()).show(ui); + Row::new().show(ui, |ui, _| { + Text::new(words.style.clone()).show(ui); + let entries: Vec> = + GradientStyle::ALL.iter().map(|style| Entry::new(*style, style.label(&words)).icon(style.icon())).collect(); + let pick = Segmented::new(words.style.clone(), Bind::given(now.style), &entries).show(ui); + absorb(&mut events, lift(pick.events, &now, |fill, style| fill.style = style)); + }); + Row::new().show(ui, |ui, _| { + Text::new(words.angle.clone()).show(ui); + let dial = AngleDial::new(Bind::given(now.angle)).label(words.angle.clone()).range(ANGLE_RANGE).show(ui); + absorb(&mut events, lift(dial.events, &now, |fill, angle| fill.angle = angle)); + let field = + NumberField::new(words.angle.clone(), Bind::given(Some(now.angle))).unit_kind(UnitKind::Degrees).range(ANGLE_RANGE).show(ui); + absorb(&mut events, lift(field.edit.events.filter_map(|angle| angle), &now, |fill, angle| fill.angle = angle)); + }); + Row::new().show(ui, |ui, _| { + Text::new(words.scale.clone()).show(ui); + let field = NumberField::new(words.scale.clone(), Bind::given(Some(now.scale * 100.0))).range(SCALE_RANGE).unit("%").show(ui); + absorb(&mut events, lift(field.edit.events.filter_map(|percent| percent), &now, |fill, percent| fill.scale = percent / 100.0)); + }); + Row::new().show(ui, |ui, _| { + let reverse = Checkbox::new(Bind::given(now.reverse)).label(words.reverse.clone()).show(ui); + absorb(&mut events, lift(reverse.events, &now, |fill, on| fill.reverse = on)); + let dither = Checkbox::new(Bind::given(now.dither)).label(words.dither.clone()).show(ui); + absorb(&mut events, lift(dither.events, &now, |fill, on| fill.dither = on)); + }); + let align = Checkbox::new(Bind::given(now.align)).label(words.align.clone()).show(ui); + absorb(&mut events, lift(align.events, &now, |fill, on| fill.align = on)); + let reset = Button::new(words.reset_alignment.clone()).enabled(now.offset != kurbo::Vec2::ZERO).show(ui); + if reset.clicked() { + let mut centred = now.clone(); + centred.offset = kurbo::Vec2::ZERO; + events.push(EditEvent::set(Mixed::Same(now.clone()), centred, Arc::from(words.reset_alignment.as_str()), None)); + } + + for event in events.iter() { + value.reborrow().write(cx, event); + } + (events, picked, color_requested, screen_pick) + }); + let (events, picked, color_requested, screen_pick) = shown.inner.unwrap_or_default(); + GradientFillEditorOutput { response: shown.output, events, picked, color_requested, screen_pick } } } + +#[cfg(feature = "screen-testing")] +mod stories; diff --git a/crates/draw/src/ui/gradient/fill_editor/stories.rs b/crates/draw/src/ui/gradient/fill_editor/stories.rs new file mode 100644 index 000000000..10caa0a53 --- /dev/null +++ b/crates/draw/src/ui/gradient/fill_editor/stories.rs @@ -0,0 +1,50 @@ +//! Stories of the gradient fill editor. + +use craft_color::ui::gradient::{ColorStop, GradientStops}; +use craft_theme::Srgba; +use craft_ui::contract::Bind; +use craft_ui::patterns::presets::PresetState; +use craft_ui::testing::{StoryData, StoryOutput, story}; +use egui::Ui; + +use super::GradientFillEditor; +use crate::ui::gradient::{GradientFill, GradientGroup, GradientItem, GradientRef, GradientStyle}; + +fn pair(from: [f32; 3], to: [f32; 3]) -> GradientStops { + GradientStops::new(vec![ColorStop::new(0.0, Srgba::rgb(from[0], from[1], from[2])), ColorStop::new(1.0, Srgba::rgb(to[0], to[1], to[2]))]) +} + +fn groups() -> Vec { + vec![GradientGroup::new( + "Basics", + vec![ + GradientItem::new("Black to white", pair([0.0; 3], [1.0; 3])), + GradientItem::new("Red to blue", pair([0.9, 0.1, 0.1], [0.1, 0.1, 0.9])), + ], + )] +} + +/// The story's fill and the palette's state. +fn state(data: &mut StoryData, fill: GradientFill) -> (&mut GradientFill, &mut PresetState) { + let both = data.value("state", (fill, PresetState::new())); + (&mut both.0, &mut both.1) +} + +#[story(GradientFillEditor<'static>, covers = [Default])] +fn gradient_fill_editor(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let groups = groups(); + let recent = [pair([0.2, 0.8, 0.4], [0.1, 0.2, 0.6])]; + let (fill, palette) = state(data, GradientFill::new(pair([0.13, 0.35, 0.72], [0.95, 0.75, 0.2]))); + let shown = GradientFillEditor::new("Gradient", Bind::bound(fill), &groups, Bind::bound(palette)).recent(&recent).show(ui); + StoryOutput::from(shown) +} + +#[story(GradientFillEditor<'static>, covers = [Default])] +fn gradient_fill_editor_without_presets(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let mut initial = GradientFill::new(pair([0.13, 0.35, 0.72], [0.95, 0.75, 0.2])); + initial.style = GradientStyle::Radial; + initial.reverse = true; + let (fill, palette) = state(data, initial); + let shown = GradientFillEditor::new("Gradient", Bind::bound(fill), &[], Bind::bound(palette)).show(ui); + StoryOutput::from(shown) +} diff --git a/crates/draw/src/ui/gradient/gradient_line/mod.rs b/crates/draw/src/ui/gradient/gradient_line/mod.rs index 0cf54c8be..7fa9afa33 100644 --- a/crates/draw/src/ui/gradient/gradient_line/mod.rs +++ b/crates/draw/src/ui/gradient/gradient_line/mod.rs @@ -9,7 +9,7 @@ use egui::Pos2; use kurbo::Point; -use crate::ui::gradient::{GradientStops, StopRef, StopRow}; +use craft_color::ui::gradient::{GradientStops, StopRef, StopRow}; /// Midpoint diamonds show only between stops farther apart than this, as the stop bar's do. pub const MIN_MIDPOINT_GAP: f32 = 0.001; @@ -68,20 +68,6 @@ impl LineGradient { } } -/// The shape a gradient's style adds round the line. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -#[non_exhaustive] -pub enum GradientExtent { - /// Only the line (linear). - #[default] - None, - /// A circle of the line's length round the start (radial, angle). - Circle, - /// A square with the end as one corner, centred on the start (diamond). - Square, - /// The line mirrored through the start, dashed (reflected). - Mirror, -} /// A part of the line a pointer or key takes. Indices are into the colour row's stops. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] diff --git a/crates/draw/src/ui/gradient/gradient_overlay/keys.rs b/crates/draw/src/ui/gradient/gradient_overlay/keys.rs index 0cf0d52b4..427efa441 100644 --- a/crates/draw/src/ui/gradient/gradient_overlay/keys.rs +++ b/crates/draw/src/ui/gradient/gradient_overlay/keys.rs @@ -8,8 +8,9 @@ use craft_ui::contract::{Cx, Edit as _, Focus as _}; use super::parts::line_part; use super::parts::{part_ref, selection_of}; use super::{CMD_ENTER, ENTER, GradientOverlay, INSERT, SPACE}; -use crate::ui::gradient::stops::{self, STOP_STEP, STOP_STEP_FAR, round_location}; -use crate::ui::gradient::{GradientLine, LineGradient, LinePart, Sampler, StopRow, default_sampler}; +use crate::ui::gradient::{GradientLine, LineGradient, LinePart}; +use craft_color::ui::gradient::stops::{self, STOP_STEP, STOP_STEP_FAR, round_location}; +use craft_color::ui::gradient::{Sampler, StopRow, default_sampler}; impl GradientOverlay<'_> { /// Selection follows the focus. While focused: the keys of the picked part (the line when none is picked). diff --git a/crates/draw/src/ui/gradient/gradient_overlay/mod.rs b/crates/draw/src/ui/gradient/gradient_overlay/mod.rs index 66502203e..d4eda663c 100644 --- a/crates/draw/src/ui/gradient/gradient_overlay/mod.rs +++ b/crates/draw/src/ui/gradient/gradient_overlay/mod.rs @@ -25,8 +25,8 @@ use craft_ui::style::{ComponentStyle, HandleLook, MarkerLook, StyleColor, StyleC use egui::{Color32, Pos2, Rect, Response, Vec2, vec2}; use super::gradient_line::{at_screen, midpoint_location}; -use super::marker_texts; -use super::{GradientExtent, GradientWords, LineGradient, LinePart, Sampler, StopColor, StopRef, StopRow, StopRows, StopRules}; +use super::{GradientStyle, LineGradient, LinePart}; +use craft_color::ui::gradient::{GradientWords, Sampler, StopColor, StopRef, StopRow, StopRows, StopRules, marker_texts}; use parts::{END, LINE, MIDPOINT, START, STOP, line_part}; craft_ui::words! { @@ -65,15 +65,15 @@ pub struct GradientOverlayStyle { handle: HandleLook, /// The end rings and the colour stops. marker: MarkerLook, - /// The line and the extent. + /// The line and the style shape. line: StyleColor, - /// The outline beside the line and the extent, and round the diamonds. + /// The outline beside the line and the style shape, and round the diamonds. halo: StyleColor, /// The selected diamond's fill. selected: StyleColor, - /// Length of a dash of the mirrored extent. + /// Length of a dash of the mirrored style shape. dash: f32, - /// Length of the gap between dashes of the mirrored extent. + /// Length of the gap between dashes of the mirrored style shape. gap: f32, /// Radius of a midpoint's diamond. diamond_radius: f32, @@ -88,7 +88,7 @@ const TEAR_OFF: f32 = 36.0; /// and the colour stops and midpoint diamonds on it, as the live Gradient tool draws them. The host holds the /// gradient and passes the line and stops as the value. /// -/// - Look: the line over its halo, the extent in the line's style (the mirrored half dashed, no halo), rings at the ends, +/// - Look: the line over its halo, the style shape in the line's style (the mirrored half dashed, no halo), rings at the ends, /// a disc in each stop's colour with a ring round it, and a diamond between stops. Points go through the canvas's /// view, so a rotated, flipped or stretched view draws the line where the document has it. Under /// [`StopRows::Split`] and [`StopRows::ColorOnly`] a stop's disc is opaque; under [`StopRows::Combined`] its colour @@ -108,7 +108,7 @@ const TEAR_OFF: f32 = 36.0; /// or the line by 1 document unit, 10 with Shift. Delete and Backspace remove a stop, Insert and Cmd/Ctrl+Enter add one /// halfway after the picked stop. Enter and Space on a colour stop or an end set `color_requested`. Each change is one /// Set. -/// - With [`shared_selection`](Self::shared_selection) the overlay and a [`GradientEditor`](super::GradientEditor) share one selected stop. +/// - With [`shared_selection`](Self::shared_selection) the overlay and a [`GradientEditor`](craft_color::ui::gradient::GradientEditor) share one selected stop. #[derive(Component, Props)] #[component(canvas_control)] pub struct GradientOverlay<'a> { @@ -127,12 +127,9 @@ pub struct GradientOverlay<'a> { /// Whether the gradient runs from the end to the start. #[prop(flag)] reverse: bool, - /// The shape the gradient's style adds round the line; decoration only. - #[prop] - extent: GradientExtent, /// The gradient's style, which draws its shape round the line (SG-66). #[prop] - style: super::GradientStyle, + style: GradientStyle, /// Whether an Alt-drag on a stop drags a copy of it; off, Alt is the tool's. #[prop(flag)] alt_copies: bool, @@ -170,8 +167,7 @@ impl<'a> GradientOverlay<'a> { sampler: None, midpoints: true, reverse: false, - extent: GradientExtent::None, - style: super::GradientStyle::Linear, // P3(tools-w11): tl.T36 replaces `extent`. + style: GradientStyle::Linear, alt_copies: false, location_decimals: 0, shared_selection: None, @@ -226,7 +222,7 @@ impl<'a> GradientOverlay<'a> { }; let stop = value.stops.color.get(index)?; let request = craft_ui::runtime::requests::ColorRequest::new("gradient.stop", stop.color); - Some(StopColor { index, tag: stop.tag, request }) + Some(StopColor::new(index, stop.tag, request)) } /// The group's accessible value. @@ -382,14 +378,14 @@ impl GradientOverlay<'_> { draw.stroke(shape, StrokeWidth::Medium, style.line); }; let d = end - start; - match self.extent { - GradientExtent::None => {} - GradientExtent::Circle => outlined(&PaintShape::Circle(start, d.length())), - GradientExtent::Square => { + match self.style { + GradientStyle::Linear => {} + GradientStyle::Radial | GradientStyle::Angle => outlined(&PaintShape::Circle(start, d.length())), + GradientStyle::Diamond => { let turned = vec2(-d.y, d.x); outlined(&PaintShape::Polygon(vec![start + d, start + turned, start - d, start - turned])); } - GradientExtent::Mirror => draw.dashed(&[start, start - d], Dashes::new(style.dash, style.gap), StrokeWidth::Medium, style.line), + GradientStyle::Reflected => draw.dashed(&[start, start - d], Dashes::new(style.dash, style.gap), StrokeWidth::Medium, style.line), } outlined(&PaintShape::Path(vec![start, end])); if self.midpoints { diff --git a/crates/draw/src/ui/gradient/gradient_overlay/parts.rs b/crates/draw/src/ui/gradient/gradient_overlay/parts.rs index a046b0537..520e0c8f0 100644 --- a/crates/draw/src/ui/gradient/gradient_overlay/parts.rs +++ b/crates/draw/src/ui/gradient/gradient_overlay/parts.rs @@ -8,7 +8,8 @@ use egui::Pos2; use super::{GradientOverlay, screen_line}; use crate::ui::gradient::gradient_line::{at_screen, midpoint_location}; -use crate::ui::gradient::{LineGradient, LinePart, StopRef, StopRow}; +use crate::ui::gradient::{LineGradient, LinePart}; +use craft_color::ui::gradient::{StopRef, StopRow}; /// The line between the handles. pub(super) const LINE: PartKey = PartKey::new("line"); diff --git a/crates/draw/src/ui/gradient/gradient_overlay/pointer.rs b/crates/draw/src/ui/gradient/gradient_overlay/pointer.rs index 57345adf3..70b91dd24 100644 --- a/crates/draw/src/ui/gradient/gradient_overlay/pointer.rs +++ b/crates/draw/src/ui/gradient/gradient_overlay/pointer.rs @@ -12,8 +12,9 @@ use egui::Pos2; use super::GradientOverlay; use super::parts::{keyboard_order, part_ref, selection_of}; use crate::ui::gradient::gradient_line::{MIN_MIDPOINT_GAP, project}; -use crate::ui::gradient::stops::{self, round_location}; -use crate::ui::gradient::{GradientLine, LineGradient, LinePart, Sampler, StopRef, StopRow, default_sampler}; +use crate::ui::gradient::{GradientLine, LineGradient, LinePart}; +use craft_color::ui::gradient::stops::{self, round_location}; +use craft_color::ui::gradient::{Sampler, StopRef, StopRow, default_sampler}; /// The press a drag edits from: the part it took and the gradient at the press. Kept for the length of the press. #[derive(Clone, Default)] diff --git a/crates/draw/src/ui/gradient/gradient_overlay/stories.rs b/crates/draw/src/ui/gradient/gradient_overlay/stories.rs index 4b5db3c37..f170711c8 100644 --- a/crates/draw/src/ui/gradient/gradient_overlay/stories.rs +++ b/crates/draw/src/ui/gradient/gradient_overlay/stories.rs @@ -11,8 +11,9 @@ use std::hash::{DefaultHasher, Hash as _, Hasher as _}; use super::GradientOverlay; use super::parts::END; -use crate::ui::gradient::stops::sample; -use crate::ui::gradient::{ColorStop, GradientEditor, GradientExtent, GradientLine, GradientStops, LineGradient, StopRef, StopRows, gradient_line}; +use crate::ui::gradient::{GradientLine, GradientStyle, LineGradient, gradient_line}; +use craft_color::ui::gradient::stops::sample; +use craft_color::ui::gradient::{ColorStop, GradientEditor, GradientStops, StopRef, StopRows}; fn gradient() -> LineGradient { LineGradient { @@ -85,7 +86,7 @@ fn gradient_overlay_extent(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let value = data.value("gradient", gradient()); let picture = ramp(value, true); on_canvas(ui, &picture, |ui, at| { - GradientOverlay::new("Gradient", Bind::bound(&mut *value)).extent(GradientExtent::Circle).reverse().show_at(ui, at).edit + GradientOverlay::new("Gradient", Bind::bound(&mut *value)).style(GradientStyle::Radial).reverse().show_at(ui, at).edit }) } @@ -93,7 +94,14 @@ fn gradient_overlay_extent(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { fn gradient_overlay_mirror(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let value = data.value("gradient", gradient()); let picture = ramp(value, false); - on_canvas(ui, &picture, |ui, at| GradientOverlay::new("Gradient", Bind::bound(&mut *value)).extent(GradientExtent::Mirror).show_at(ui, at).edit) + on_canvas(ui, &picture, |ui, at| GradientOverlay::new("Gradient", Bind::bound(&mut *value)).style(GradientStyle::Reflected).show_at(ui, at).edit) +} + +#[story(GradientOverlay<'static>, covers = [Default], drive = Drive::Drag(Grip::new(END).toward(vec2(0.0, 1.0))))] +fn gradient_overlay_diamond(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let value = data.value("gradient", gradient()); + let picture = ramp(value, false); + on_canvas(ui, &picture, |ui, at| GradientOverlay::new("Gradient", Bind::bound(&mut *value)).style(GradientStyle::Diamond).show_at(ui, at).edit) } #[story(GradientOverlay<'static>, covers = [Default], drive = Drive::Drag(Grip::new(END).toward(vec2(0.0, 1.0))))] diff --git a/crates/draw/tests/it/gradient_fill.rs b/crates/draw/tests/it/gradient_fill.rs new file mode 100644 index 000000000..3a666a805 --- /dev/null +++ b/crates/draw/tests/it/gradient_fill.rs @@ -0,0 +1,112 @@ +//! `GradientFillEditor`: each row reports its edit as one gesture of the whole fill. + +use std::cell::RefCell; +use std::rc::Rc; + +use craft_behavior::EditEvent; +use craft_color::ui::gradient::{ColorStop, GradientStops}; +use craft_draw::ui::gradient::{GradientFill, GradientFillEditor, GradientGroup, GradientItem, GradientRef, GradientStyle}; +use craft_theme::Srgba; +use craft_ui::contract::{Bind, Role}; +use craft_ui::patterns::presets::PresetState; +use craft_ui::testing::{Fixture, trial}; +use egui::vec2; + +fn pair(from: f32, to: f32) -> GradientStops { + GradientStops::new(vec![ColorStop::new(0.0, Srgba::rgb(from, from, from)), ColorStop::new(1.0, Srgba::rgb(to, to, to))]) +} + +fn preset() -> GradientStops { + pair(0.9, 0.1) +} + +struct Shown { + fixture: Fixture, + held: Rc>, + log: Rc>>>, +} + +impl Shown { + /// The events reported since the last call. + fn take(&mut self) -> Vec> { + self.fixture.settle(); + std::mem::take(&mut *self.log.borrow_mut()) + } +} + +fn shown() -> Shown { + let mut fill = GradientFill::new(GradientStops::new(vec![ + ColorStop::new(0.0, Srgba::rgb(0.0, 0.0, 0.0)).with_midpoint(0.3), + ColorStop::new(1.0, Srgba::rgb(1.0, 1.0, 1.0)), + ])); + fill.dither = true; + let held = Rc::new(RefCell::new(fill)); + let log = Rc::new(RefCell::new(Vec::new())); + let state = Rc::new(RefCell::new(PresetState::::new())); + let (value, events, palette) = (Rc::clone(&held), Rc::clone(&log), state); + let mut fixture = Fixture::new(move |ui| { + let groups = [GradientGroup::new("Basics", vec![GradientItem::new("Red to blue", preset())])]; + let given = value.borrow().clone(); + let mut palette = palette.borrow_mut(); + let out = GradientFillEditor::new("Fill", Bind::given(given), &groups, Bind::bound(&mut *palette)).show(ui); + for event in out.events.iter() { + event.apply(&mut *value.borrow_mut()); + events.borrow_mut().push(event.clone()); + } + }); + fixture.settle(); + Shown { fixture, held, log } +} + +/// Asserts the events are one gesture (one `Commit` or `Set`, at most one `Begin`) in which every carried value +/// equals `start` once `neutral` copies the edited part back; returns the last value. +fn one_gesture(events: &[EditEvent], start: &GradientFill, neutral: impl Fn(&mut GradientFill, &GradientFill)) -> GradientFill { + let ended = events.iter().filter(|e| matches!(e, EditEvent::Commit { .. } | EditEvent::Set { .. })).count(); + let begun = events.iter().filter(|e| matches!(e, EditEvent::Begin { .. })).count(); + assert_eq!((ended, begun <= 1), (1, true), "one gesture, got {events:?}"); + for value in events.iter().filter_map(EditEvent::value) { + let mut rest = value.clone(); + neutral(&mut rest, start); + assert_eq!(&rest, start, "only the edited part differs"); + } + let last = events.iter().rev().find_map(EditEvent::value).expect("a value").clone(); + assert_ne!(&last, start, "the edit changed the fill"); + last +} + +#[trial] +fn each_row_reports_one_gesture_of_the_whole_fill() { + let mut s = shown(); + + let start = s.held.borrow().clone(); + let stop = s.fixture.by_label("Color stop 1 of 2").expect("the bar shows its stops"); + s.fixture.drag(&stop, vec2(30.0, 0.0)); + let events = s.take(); + one_gesture(&events, &start, |fill, from| fill.stops = from.stops.clone()); + + let start = s.held.borrow().clone(); + let mut dials = s.fixture.by_role(Role::SLIDER); + assert_eq!(dials.len(), 1, "the angle dial is the only slider"); + let dial = dials.remove(0); + s.fixture.drag(&dial, vec2(30.0, 0.0)); + let events = s.take(); + one_gesture(&events, &start, |fill, from| fill.angle = from.angle); + + let start = s.held.borrow().clone(); + let radial = s.fixture.by_label("Radial").expect("the style entries"); + s.fixture.click(&radial); + let events = s.take(); + let after = one_gesture(&events, &start, |fill, from| fill.style = from.style); + assert_eq!(after.style, GradientStyle::Radial); + + let start = s.held.borrow().clone(); + let item = s.fixture.by_label("Red to blue").expect("the preset"); + s.fixture.click(&item); + let events = s.take(); + assert!(!events.is_empty(), "the pick reports an edit"); + let mut expected = start.clone(); + expected.stops = preset(); + for value in events.iter().filter_map(EditEvent::value) { + assert_eq!(value, &expected, "the preset replaces the stops with centred midpoints and keeps the rest"); + } +} diff --git a/crates/draw/tests/it/gradient_overlay.rs b/crates/draw/tests/it/gradient_overlay.rs index 9f2821bc8..a0e25b4db 100644 --- a/crates/draw/tests/it/gradient_overlay.rs +++ b/crates/draw/tests/it/gradient_overlay.rs @@ -5,8 +5,9 @@ use std::rc::Rc; use craft_behavior::CanvasView; use craft_behavior::kurbo::Point; +use craft_color::ui::gradient::{ColorStop, GradientStops, StopRef}; +use craft_draw::ui::gradient::GradientOverlay; use craft_draw::ui::gradient::gradient_line::{GradientLine, LineGradient}; -use craft_draw::ui::gradient::{ColorStop, GradientOverlay, GradientStops, StopRef}; use craft_theme::Srgba; use craft_ui::canvas::Canvas; use craft_ui::contract::{Bind, Show as _, ShowContainer as _}; diff --git a/crates/draw/tests/it/main.rs b/crates/draw/tests/it/main.rs index 3bc60110e..0a553f16b 100644 --- a/crates/draw/tests/it/main.rs +++ b/crates/draw/tests/it/main.rs @@ -4,6 +4,7 @@ mod add_beside; // draw-gradient-b +mod gradient_fill; mod gradient_overlay; // draw-connector diff --git a/migration/api-map/draw-canvas-controls.toml b/migration/api-map/draw-canvas-controls.toml index 84519ca60..95df97dc8 100644 --- a/migration/api-map/draw-canvas-controls.toml +++ b/migration/api-map/draw-canvas-controls.toml @@ -30,14 +30,14 @@ note = "A canvas control: `new(label, Bind)` over the canvas's vie [[type]] name = "STOP_STEP" old = "" -new = "craft_draw::ui::gradient::stops::STOP_STEP" +new = "craft_color::ui::gradient::stops::STOP_STEP" status = "replaced" note = "New documented const: the share of the bar or line one Left or Right press moves a stop or midpoint." [[type]] name = "STOP_STEP_FAR" old = "" -new = "craft_draw::ui::gradient::stops::STOP_STEP_FAR" +new = "craft_color::ui::gradient::stops::STOP_STEP_FAR" status = "replaced" note = "New documented const: the share one Shift+arrow press moves a stop or midpoint." @@ -509,9 +509,9 @@ note = "`#[non_exhaustive]`: match with a wildcard arm." [[type]] name = "GradientExtent" old = "craft_draw::ui::GradientExtent" -new = "craft_draw::ui::gradient::GradientExtent" -status = "moved" -note = "`#[non_exhaustive]`: match with a wildcard arm." +new = "" +status = "removed" +note = "Replaced by `craft_draw::ui::gradient::GradientStyle` and `GradientOverlay::style`: the overlay draws each style's shape from the style." [[setter]] type = "GradientOverlay"