mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
Gradient: a stop's picked swatch follows the stop, and unchanged stops keep the gradient swatch
When the stop popover moves a stop past its neighbor, the swatch picked for that stop moves to the stop's new index, so the Swatches view keeps showing it. Writing a stop list equal to a paint's current stops no longer clears that paint's gradient swatch link.
This commit is contained in:
1 parent
e8068cb1e0
commit
14b5a5a4ae
2 files changed
+65
-3
No files matched your search
@@ -529,8 +529,12 @@ fn apply_gradient_edit_in_model(
|
||||
}
|
||||
let recolor = p.get("stops").is_some() || bool_or(p, "reverse", false);
|
||||
if let Some(stops) = p.get("stops") {
|
||||
gp.gradient.stops = parse_stops(stops)?;
|
||||
gp.swatch = None;
|
||||
let stops = parse_stops(stops)?;
|
||||
// A paint whose stops stay as they are keeps its gradient swatch.
|
||||
if stops != gp.gradient.stops {
|
||||
gp.gradient.stops = stops;
|
||||
gp.swatch = None;
|
||||
}
|
||||
}
|
||||
if bool_or(p, "reverse", false) {
|
||||
// Midpoints mirrored with their segments.
|
||||
@@ -901,3 +905,32 @@ fn set_gradient_geom(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
})?;
|
||||
Ok(json!({ "ids": targets.iter().map(|i| i.0).collect::<Vec<_>>() }))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use vectorcraft_color::Color;
|
||||
|
||||
fn linked(stops: &[(f32, &str)]) -> Paint {
|
||||
let stops = stops.iter().map(|(offset, hex)| GradientStop::new(*offset, Color::from_hex(hex).unwrap())).collect();
|
||||
let mut paint = GradientPaint::new(Gradient::new(GradientKind::Linear, stops));
|
||||
paint.swatch = Some("Linked".into());
|
||||
Paint::Gradient(Box::new(paint))
|
||||
}
|
||||
|
||||
fn swatch(paint: &Paint) -> Option<&str> {
|
||||
match paint {
|
||||
Paint::Gradient(g) => g.swatch.as_deref(),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writing_the_stops_a_paint_already_has_keeps_its_gradient_swatch() {
|
||||
let paint = linked(&[(0.0, "#ffffff"), (1.0, "#000000")]);
|
||||
let same = json!({"stops": [{"offset": 0, "color": "#ffffff"}, {"offset": 1, "color": "#000000"}]});
|
||||
assert_eq!(swatch(&apply_gradient_edit(&paint, &same, None).unwrap()), Some("Linked"));
|
||||
let other = json!({"stops": [{"offset": 0, "color": "#ff0000"}, {"offset": 1, "color": "#000000"}]});
|
||||
assert_eq!(swatch(&apply_gradient_edit(&paint, &other, None).unwrap()), None, "new stops unlink the swatch");
|
||||
}
|
||||
}
|
||||
@@ -786,8 +786,17 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
&& !owner.key.point
|
||||
&& let Some(index) = app.session.selected_stop()
|
||||
{
|
||||
// The stop can cross its neighbor when its location changes.
|
||||
// The stop can cross its neighbor when its location changes. A swatch picked for it moves with it.
|
||||
let moved = owner.key.clone();
|
||||
owner.key.index = index;
|
||||
dialogs.with(|host| {
|
||||
if let Some(popover) = host.get_mut_by_id::<GradientStopPopover>(id)
|
||||
&& let Some(picked) = &mut popover.picked_swatch
|
||||
&& picked.target == moved
|
||||
{
|
||||
picked.target.index = index;
|
||||
}
|
||||
});
|
||||
}
|
||||
if let Some((name, color)) = filed.swatch
|
||||
&& let Some(target) = selected(app)
|
||||
@@ -1063,6 +1072,26 @@ mod tests {
|
||||
assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal"), "the picked swatch moved before the other stays the one shown");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_swatches_view_keeps_the_picked_swatch_when_the_popover_moves_its_stop_past_a_neighbor() {
|
||||
let mut app = app();
|
||||
let stops = json!([{"offset": 0, "color": "#ffffff"}, {"offset": 0.5, "color": "#008080"}, {"offset": 0.7, "color": "#000000"}]);
|
||||
app.run("paint.setFill", json!({"gradient": {"stops": stops, "start": [100, 150], "end": [200, 150]}})).unwrap();
|
||||
app.run("gradient.selectStop", json!({"index": 1})).unwrap();
|
||||
for name in ["First teal", "Second teal"] {
|
||||
app.run("swatch.new", json!({"name": name, "color": "#008080"})).unwrap();
|
||||
}
|
||||
let mut panel = swatches_view(app);
|
||||
panel.click("Second teal");
|
||||
assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal"));
|
||||
panel.app().dialogs().unwrap().with(|host| {
|
||||
host.get_mut_by_id::<GradientStopPopover>(dialog_id(KIND)).expect("the popover").filed.fields.location = Some(90.0);
|
||||
});
|
||||
panel.frame();
|
||||
assert_eq!(panel.app().session.selected_stop(), Some(2), "the stop crossed its neighbor");
|
||||
assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal"), "the pick moved with its stop");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_swatches_view_keeps_the_picked_swatch_through_a_rename_in_a_batch() {
|
||||
let mut app = app();
|
||||
|
||||
Reference in new issue
Block a user