mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-03 15:21:17 -05:00
Color Wheel: Show Opacity edge cases in the opacity commands and the row
- paint.setFill and paint.setStroke give a graph's series marks and axis lines the opacity they write even when the object already shows it, so another mark's kept opacity no longer comes back after the graph is drawn again. - The proxies answer no opacity whenever the Live Paint Bucket paints, which includes a Cmd+click and an Eyedropper pick armed for it. - A swatch that is not a name is an error with an opacity instead of an opacity alone. - Hiding the Show Opacity row, or the row turning gray, during a drag keeps the previewed opacity as one undo step and ends the interaction. - The help text and docs/mcp.md name the picked mesh points case, the grayscale image colorize exception and the New Art Has Basic Appearance off case.
This commit is contained in:
@@ -1184,6 +1184,24 @@ pub(crate) fn capture_series_paint_writes(
|
||||
super::graph::capture_series_top_paints(d, &rows)
|
||||
}
|
||||
|
||||
/// Store on the graph the opacity and blend mode of each row an edit wrote (`written`: an object with the index of the
|
||||
/// row) on the series marks among them, when that row is the topmost visible one of its kind, also when the mark
|
||||
/// already showed that opacity: it replaces the opacity another mark of the series left on the graph. When the edit
|
||||
/// reaches several marks of one series, the frontmost one sets what the graph stores. Returns whether the stored
|
||||
/// opacity or blend mode changed.
|
||||
pub(crate) fn capture_series_opacity_writes(d: &mut vectorcraft_doc::Document, written: &[(NodeId, usize)]) -> bool {
|
||||
let ids: Vec<NodeId> = written.iter().map(|(id, _)| *id).collect();
|
||||
// A later mark's capture replaces an earlier one's: take them in paint order.
|
||||
let rows: Vec<(NodeId, bool)> = series_children_in_paint_order(d, &ids)
|
||||
.into_iter()
|
||||
.flat_map(|id| {
|
||||
let shown = written.iter().filter(move |(w, _)| *w == id).filter_map(|(_, at)| Some((id, top_row_kind(&d.node(id)?.appearance, *at)?)));
|
||||
shown.collect::<Vec<_>>()
|
||||
})
|
||||
.collect();
|
||||
super::graph::capture_series_opacity(d, &rows)
|
||||
}
|
||||
|
||||
/// The kind of the row at `at` (fill `true`, stroke `false`) when it is the topmost visible row of that kind in `ap`.
|
||||
fn top_row_kind(ap: &Appearance, at: usize) -> Option<bool> {
|
||||
let fill = ap.items.get(at)?.is_fill();
|
||||
|
||||
@@ -352,6 +352,47 @@ fn a_paint_opacity_on_a_series_mark_survives_regeneration() {
|
||||
assert!(opacities.iter().all(|o| *o == (0.4, 0.6)), "{opacities:?}");
|
||||
}
|
||||
|
||||
/// `paint.setFill {opacity}` and `paint.setStroke {opacity}` with the opacity a mark already shows replace the opacity
|
||||
/// another mark of the series left on the graph, as an explicit 100% does.
|
||||
#[test]
|
||||
fn a_paint_opacity_a_series_mark_already_shows_replaces_the_kept_opacity() {
|
||||
let (mut s, id, _, marks) = column_marks();
|
||||
s.execute("paint.setFill", &json!({"ids": [marks[0].0], "opacity": 40})).unwrap();
|
||||
s.execute("paint.setStroke", &json!({"ids": [marks[0].0], "opacity": 60})).unwrap();
|
||||
let kept = spec(&s, id).series_paints[0].clone();
|
||||
assert_eq!((kept.fill_opacity, kept.stroke_opacity), (Some(0.4), Some(0.6)));
|
||||
// The second mark is opaque already: its row comes out as it was.
|
||||
s.execute("paint.setFill", &json!({"ids": [marks[1].0], "color": "#ff0000", "opacity": 100})).unwrap();
|
||||
let kept = spec(&s, id).series_paints[0].clone();
|
||||
assert_eq!((kept.fill_opacity, kept.stroke_opacity), (None, Some(0.6)));
|
||||
s.execute("paint.setStroke", &json!({"ids": [marks[1].0], "opacity": 100})).unwrap();
|
||||
let kept = spec(&s, id).series_paints[0].clone();
|
||||
assert_eq!((kept.fill_opacity, kept.stroke_opacity), (None, None));
|
||||
s.execute("graph.setData", &json!({"rows": [[1, 2], [3, 4]]})).unwrap();
|
||||
let n = s.doc().unwrap().doc.node(id).unwrap().clone();
|
||||
let opacities: Vec<(f32, f32)> =
|
||||
series(&n, 0).children().unwrap().iter().map(|m| (m.appearance.fill().unwrap().opacity, m.appearance.stroke().unwrap().opacity)).collect();
|
||||
assert!(opacities.iter().all(|o| *o == (1.0, 1.0)), "{opacities:?}");
|
||||
}
|
||||
|
||||
/// The same for a graph's axis lines: a stroke opacity set on an opaque line replaces the one its kind keeps from another
|
||||
/// line.
|
||||
#[test]
|
||||
fn a_stroke_opacity_an_axis_line_already_shows_replaces_the_kept_opacity() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
|
||||
let id = graph(&mut s, "column");
|
||||
let lines = line_nodes(&s, id);
|
||||
s.execute("paint.setStroke", &json!({"ids": [lines[0].id.0], "opacity": 40})).unwrap();
|
||||
let kept = |s: &Session| spec(s, id).label_styles.axis_rows.and_then(|r| r.paint.stroke_opacity);
|
||||
assert_eq!(kept(&s), Some(0.4));
|
||||
s.execute("paint.setStroke", &json!({"ids": [lines[1].id.0], "opacity": 100})).unwrap();
|
||||
assert_eq!(kept(&s), None);
|
||||
s.execute("graph.setData", &json!({"id": id.0, "csv": ",2024,2025\nQ1,3,2\nQ2,5,4\nQ3,-1,6"})).unwrap();
|
||||
let opacities: Vec<f32> = line_nodes(&s, id).iter().map(|n| n.appearance.stroke().map_or(1.0, |st| st.opacity)).collect();
|
||||
assert!(opacities.iter().all(|o| *o == 1.0), "{opacities:?}");
|
||||
}
|
||||
|
||||
/// Clear Appearance and Reduce to Basic Appearance on a series mark take its fill and stroke opacity away, and the
|
||||
/// graph keeps none, so the series stays opaque when it is drawn again.
|
||||
#[test]
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -449,3 +449,37 @@ fn the_live_paint_bucket_answers_no_opacity() {
|
||||
assert!(s.execute("paint.setFill", &json!({"opacity": 40})).is_err());
|
||||
assert_eq!(*s.doc().unwrap().doc, *doc);
|
||||
}
|
||||
|
||||
/// The Eyedropper armed to sample into the Live Paint Bucket's gradient works for the bucket, so
|
||||
/// the proxies answer no opacity for the selection then too, as they show no paint of its own.
|
||||
#[test]
|
||||
fn the_eyedropper_armed_for_the_bucket_answers_no_opacity() {
|
||||
let mut s = session();
|
||||
let id = rect(&mut s, 10.0);
|
||||
select(&mut s, &[id]);
|
||||
s.select_tool("eyedropper", ViewInfo::default()).unwrap();
|
||||
assert_eq!(proxies(&mut s)["fillOpacity"], 100.0, "the Eyedropper alone shows the selection's");
|
||||
s.set_tool_option("stop", &json!("livePaintBucket"));
|
||||
assert!(s.bucket_paints());
|
||||
assert_eq!(proxies(&mut s)["fillOpacity"], Value::Null);
|
||||
assert_eq!(proxies(&mut s)["strokeOpacity"], Value::Null);
|
||||
}
|
||||
|
||||
/// A `swatch` that is no name does not turn a request into an opacity alone: it is an error and
|
||||
/// leaves the document as it was.
|
||||
#[test]
|
||||
fn a_swatch_that_is_no_name_is_refused_with_an_opacity() {
|
||||
let mut s = session();
|
||||
let id = rect(&mut s, 10.0);
|
||||
select(&mut s, &[id]);
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
for cmd in ["paint.setFill", "paint.setStroke"] {
|
||||
for swatch in [json!(5), json!(true), json!(["a"]), json!({})] {
|
||||
assert!(s.execute(cmd, &json!({"swatch": swatch, "opacity": 40})).is_err(), "{cmd} {swatch}");
|
||||
}
|
||||
// A null swatch names none, so the opacity goes alone.
|
||||
s.execute(cmd, &json!({"swatch": null, "opacity": 40})).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
}
|
||||
assert_eq!(*s.doc().unwrap().doc, *doc);
|
||||
}
|
||||
|
||||
@@ -658,11 +658,13 @@ fn show_box(ui: &mut Ui, ch: Channel, hsb: [f32; 3], marked: bool, size: f32, ga
|
||||
/// picks it), else that of the fill or stroke item
|
||||
/// ([`vectorcraft_engine::Session::proxy_opacity`]), which nothing selected and type characters
|
||||
/// do not have. A stop or point of a gradient on type characters belongs to the gradient, so the
|
||||
/// row sets it. With nothing selected the stop or point belongs to the defaults for new art, which
|
||||
/// the row leaves alone, and an object selected in a symbol instance has no opacity the row can set.
|
||||
/// row sets it. With nothing selected, or while the Live Paint Bucket paints
|
||||
/// ([`vectorcraft_engine::Session::bucket_paints`]), the stop or point belongs to the defaults for
|
||||
/// new art, which the row leaves alone, and an object selected in a symbol instance has no opacity
|
||||
/// the row can set.
|
||||
fn shown_opacity(app: &VectorcraftApp, tgt: &Target) -> (f32, bool) {
|
||||
let stroke = !app.session.fill_active;
|
||||
let selected = app.session.active().is_some_and(|d| !d.selection.is_empty() && d.selection.symbol_part.is_none());
|
||||
let selected = !app.session.bucket_paints() && app.session.active().is_some_and(|d| !d.selection.is_empty() && d.selection.symbol_part.is_none());
|
||||
let point = |index: usize| match super::active_paint(app) {
|
||||
Paint::Gradient(g) => super::gradient::shown_points(&g).points.get(index).map(|p| p.opacity),
|
||||
_ => None,
|
||||
@@ -700,6 +702,25 @@ fn apply_opacity(app: &mut VectorcraftApp, tgt: &Target, o: f32, phase: Live) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Panel state: whether a drag of the Show Opacity row has previewed on an open interaction and not
|
||||
/// been released.
|
||||
const OPACITY_LIVE: &str = "color-wheel-opacity-live";
|
||||
|
||||
/// End the drag of the Show Opacity row when its row is hidden (`ui.colorWheel {opacity: false}`) or
|
||||
/// turned gray before the release: the preview is kept as one undo step and the interaction ends.
|
||||
fn end_opacity_drag(app: &mut VectorcraftApp, ui: &Ui) {
|
||||
if !pstate::<bool>(ui.ctx(), OPACITY_LIVE) {
|
||||
return;
|
||||
}
|
||||
if app.session.in_interaction()
|
||||
&& let Err(e) = app.session.commit_interaction()
|
||||
{
|
||||
app.status(e.to_string());
|
||||
}
|
||||
app.sync_views();
|
||||
set_pstate(ui.ctx(), OPACITY_LIVE, false);
|
||||
}
|
||||
|
||||
/// The Show Opacity row: the Transparency panel's icon, a slider whose track shows `color` from
|
||||
/// clear to solid, and a percent field. Gray when [`shown_opacity`] finds nothing to set.
|
||||
fn opacity_row(app: &mut VectorcraftApp, ui: &mut Ui, tgt: &Target, color: &Color) {
|
||||
@@ -724,6 +745,11 @@ fn opacity_row(app: &mut VectorcraftApp, ui: &mut Ui, tgt: &Target, color: &Colo
|
||||
});
|
||||
if let Some((v, phase)) = edit.filter(|_| live) {
|
||||
apply_opacity(app, tgt, v, phase);
|
||||
set_pstate(ui.ctx(), OPACITY_LIVE, phase == Live::Dragging);
|
||||
}
|
||||
// A drag whose row turned gray reports no release.
|
||||
if !live {
|
||||
end_opacity_drag(app, ui);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1200,6 +1226,8 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
set_pstate(ui.ctx(), MEMORY, Some((now, rgb, hsb)));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
end_opacity_drag(app, ui);
|
||||
}
|
||||
let (mut picked_lch, mut picked_hsl) = (None, None);
|
||||
match app.ui.color_wheel.sliders {
|
||||
|
||||
@@ -137,6 +137,34 @@ fn the_slider_sets_the_fill_opacity_and_takes_the_arrow_keys() {
|
||||
assert_eq!((fill_opacity(h.state()) * 100.0).round(), before + 1.0);
|
||||
}
|
||||
|
||||
/// Hiding the row during a drag on its slider keeps the opacity the drag previewed as one undo step
|
||||
/// and leaves no interaction open.
|
||||
#[test]
|
||||
fn hiding_the_row_during_a_drag_keeps_its_opacity_as_one_undo_step() {
|
||||
let mut h = harness(app());
|
||||
let undo = undo_labels(h.state()).len();
|
||||
let r = slider_rect(&h);
|
||||
let at = |f: f32| Pos2::new(r.left() + 4.0 + f * (r.width() - 8.0), r.top() + 7.0);
|
||||
let press = |h: &mut Harness<'static, VectorcraftApp>, pos: Pos2, pressed: bool| {
|
||||
h.event(Event::PointerMoved(pos));
|
||||
h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE });
|
||||
h.run_steps(1);
|
||||
};
|
||||
press(&mut h, at(0.5), true);
|
||||
for _ in 0..2 {
|
||||
h.event(Event::PointerMoved(at(0.25)));
|
||||
h.run_steps(1);
|
||||
}
|
||||
assert!(h.state().session.in_interaction(), "the drag previews");
|
||||
let previewed = fill_opacity(h.state());
|
||||
assert!((previewed - 0.25).abs() < 0.05, "{previewed}");
|
||||
h.state_mut().run("ui.colorWheel", json!({"opacity": false})).unwrap();
|
||||
h.run_steps(2);
|
||||
assert!(!h.state().session.in_interaction(), "the interaction ended");
|
||||
assert!((fill_opacity(h.state()) - previewed).abs() < 1e-6);
|
||||
assert_eq!(undo_labels(h.state()).len(), undo + 1);
|
||||
}
|
||||
|
||||
/// Screen readers get a slider named Opacity with its percentage and its range, and can step it and
|
||||
/// set it.
|
||||
#[test]
|
||||
@@ -322,6 +350,16 @@ fn the_row_is_gray_with_nothing_selected_and_on_type() {
|
||||
action(&mut h, Action::SetValue, Some(ActionData::NumericValue(25.0)));
|
||||
assert_eq!(h.state().session.paint.fill, defaults);
|
||||
|
||||
// The Live Paint Bucket painting a gradient with an object selected: the stop is the bucket's.
|
||||
let mut app = self::app();
|
||||
app.session.select_tool("livePaintBucket", Default::default()).unwrap();
|
||||
app.run("paint.setFill", json!({"gradient": {}})).unwrap();
|
||||
let mut h = harness(app);
|
||||
assert!(h.get_by_role_and_label(Role::Slider, "Opacity").accesskit_node().is_disabled());
|
||||
let defaults = h.state().session.paint.fill.clone();
|
||||
action(&mut h, Action::SetValue, Some(ActionData::NumericValue(25.0)));
|
||||
assert_eq!(h.state().session.paint.fill, defaults);
|
||||
|
||||
// An object with a gradient fill selected in a dynamic symbol instance.
|
||||
let mut app = self::app();
|
||||
app.run("paint.setFill", json!({"gradient": {}})).unwrap();
|
||||
|
||||
+6
-3
@@ -912,10 +912,13 @@ its kind (`paint.proxies`) and the Gradient tool's annotator then show that row.
|
||||
`inspect_document` reports it as `paint.appearanceItem`. `paint.setFill` and `paint.setStroke` also take `opacity`
|
||||
(0 to 100, a percentage as in `transparency.set`), which sets the opacity of the fill or stroke row they paint, with
|
||||
or without a new paint, as one undo step. An opacity that no target takes is an error and also leaves the paint
|
||||
undone. Without a new paint, `objects` counts only the targets that took the opacity. `paint.proxies` answers it as `fillOpacity` and `strokeOpacity`, from the first selected object that has
|
||||
undone. A `swatch` that is not a name does not make the request an opacity alone: it is an error without a paint of
|
||||
its own. A color for a grayscale or 1-bit image's drawing color creates no fill row, so an opacity given with it goes
|
||||
to a fill row the image has. With New Art Has Basic Appearance off, the next shape copies the first selected object's
|
||||
appearance after any command, its opacity included. Without a new paint, `objects` counts only the targets that took the opacity. `paint.proxies` answers it as `fillOpacity` and `strokeOpacity`, from the first selected object that has
|
||||
that row (a selected path of a compound path answers with its compound path's row, which the commands edit, and a
|
||||
selected Live Paint group answers with its faces' fills and its edges' strokes). It answers null with nothing selected, for an object selected in a symbol instance, while the Live Paint Bucket is the
|
||||
active tool, while the Type tool has characters selected, and when only type characters (which have no opacity of their own) or objects without that
|
||||
selected Live Paint group answers with its faces' fills and its edges' strokes). It answers null with nothing selected, for an object selected in a symbol instance, while the Live Paint Bucket paints (it is the active tool, or a Cmd+click or an Eyedropper pick works for it),
|
||||
while the Type tool has characters selected, and when only type characters (which have no opacity of their own) or objects without that
|
||||
row are selected. Live effects take the same `item` to apply to one fill or
|
||||
stroke instead of the whole object: `run_command effect.apply {"effect": "path.offsetPath", "item": 0}`, and
|
||||
`effect.remove`, `effect.setParams` (`visible` toggles one) and `effect.duplicate` address that item's effects;
|
||||
|
||||
Reference in New Issue
Block a user