Color Wheel: a pick in the frame of an opacity edit keeps the stop's opacity

The wheel and the OKLCH rows applied a pick to the gradient stops read
at the start of the frame, so an opacity edit of a stop in the same
frame was written back to its old value. The panel now reads the stops
again after the opacity row sets them.
This commit is contained in:
dotCooCoo committed 2026-11-16 03:50:05 -08:00
1 parent 2bd6094662
commit ca3d617531
2 files changed
+66 -6

No files matched your search

+11 -6
View File
@@ -365,8 +365,9 @@ fn apply_opacity(app: &mut VectorcraftApp, tgt: &Target, o: f32, phase: Live) {
}
/// 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) {
/// clear to solid, and a percent field. Gray when [`shown_opacity`] finds nothing to set. True when
/// it set the opacity.
fn opacity_row(app: &mut VectorcraftApp, ui: &mut Ui, tgt: &Target, color: &Color) -> bool {
let (o, live) = shown_opacity(app, tgt);
let percent = f64::from(o) * 100.0;
let mut edit: Option<(f32, Live)> = None;
@@ -386,9 +387,9 @@ 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);
}
let Some((v, phase)) = edit.filter(|_| live) else { return false };
apply_opacity(app, tgt, v, phase);
true
}
/// The text the hex field shows for `color` and the opacity `o` the row of Show Opacity edits
@@ -732,10 +733,11 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
// The hex field compares its text with what it showed before an opacity edit of this frame, so
// a text committed when a press on the opacity row takes the focus is not dropped.
let hex = app.ui.color_wheel.hex_field.then(|| hex_shown(app, &tgt, color.as_ref()));
let mut opacity_set = false;
if app.ui.color_wheel.opacity {
let shown = color.unwrap_or(app.session.last_solid);
let owner = color.map(|c| Owner::of(app, &tgt, c));
opacity_row(app, ui, &tgt, &shown);
opacity_set = opacity_row(app, ui, &tgt, &shown);
// An opacity edit of a gradient stop or point changes the gradient its owner holds, and the
// HSB and the OKLCH picked last stay with the new gradient.
if let (Some(c), Some(was)) = (color, owner) {
@@ -762,6 +764,9 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
return;
}
}
// A pick or an OKLCH edit read in the frame of an opacity edit goes to the stops as that edit
// left them, so the stop keeps both.
let tgt = if opacity_set { wheel_target(app, ui) } else { tgt };
let behind = super::alt_held(ui);
let cmyk_doc = app.session.active().is_some_and(|d| d.doc.color_mode == ColorMode::Cmyk);
if let Some((lch, phase)) = picked_lch {
@@ -249,6 +249,61 @@ fn an_opacity_edit_keeps_the_oklch_hue_picked_for_a_white_stop() {
assert_eq!(hue_row(&h).1, Some(120.0), "the Hue row keeps the hue after the opacity edit");
}
/// Asks screen readers to set the opacity row to `opacity` and `label`'s slider to `value` in one
/// frame (`Harness::event` would give each request a frame of its own).
fn set_both(h: &mut Harness<'static, VectorcraftApp>, opacity: f64, (label, value): (&str, f64)) {
let (target_node, target_tree) = h.get_by_role_and_label(Role::Slider, "Opacity").accesskit_node().locate();
let data = Some(ActionData::NumericValue(opacity));
let set_opacity = Event::AccessKitActionRequest(ActionRequest { target_node, target_tree, action: Action::SetValue, data });
let node = h.query_all_by_label(label).find(|n| n.accesskit_node().role() == Role::Slider).expect("the slider");
let (target_node, target_tree) = node.accesskit_node().locate();
let data = Some(ActionData::NumericValue(value));
let set_value = Event::AccessKitActionRequest(ActionRequest { target_node, target_tree, action: Action::SetValue, data });
h.input_mut().events.extend([set_opacity, set_value]);
h.run_steps(2);
}
/// A gradient stop with stops red and blue, the first selected.
fn stop_app() -> VectorcraftApp {
let mut app = app();
let stops = json!([{"offset": 0, "color": "#ff0000"}, {"offset": 1, "color": "#0000ff"}]);
app.run("paint.setFill", json!({"gradient": {"stops": stops, "start": [10, 35], "end": [60, 35]}})).unwrap();
app.select_tool("gradient");
app.run("gradient.selectStop", json!({"index": 0})).unwrap();
app
}
/// An opacity edit and a pick on the ring in one frame: the stop keeps both.
#[test]
fn an_opacity_edit_and_a_ring_pick_in_one_frame_keep_both() {
let mut h = harness(stop_app());
set_both(&mut h, 30.0, ("Hue ring", 120.0));
let Paint::Gradient(g) = crate::panels::current_paints(h.state()).0 else { panic!("still a gradient") };
let stop = &g.gradient.stops[0];
assert!((stop.opacity - 0.3).abs() < 1e-6, "the stop keeps the opacity: {:?}", g.gradient.stops);
assert_ne!(super::color::hex_digits(&stop.color), "FF0000", "the stop takes the pick: {:?}", g.gradient.stops);
}
/// An opacity edit and an OKLCH Hue edit in one frame: the stop keeps both.
#[test]
fn an_opacity_edit_and_an_oklch_edit_in_one_frame_keep_both() {
let mut app = stop_app();
app.run("ui.colorWheel", json!({"sliders": "oklch"})).unwrap();
let mut h = Harness::builder().with_size(vec2(260.0, 480.0)).build_ui_state(
|ui, app: &mut VectorcraftApp| {
show(app, ui);
crate::shortcuts::handle(app, ui.ctx());
},
app,
);
h.run_steps(2);
set_both(&mut h, 30.0, ("Hue", 140.0));
let Paint::Gradient(g) = crate::panels::current_paints(h.state()).0 else { panic!("still a gradient") };
let stop = &g.gradient.stops[0];
assert!((stop.opacity - 0.3).abs() < 1e-6, "the stop keeps the opacity: {:?}", g.gradient.stops);
assert_ne!(super::color::hex_digits(&stop.color), "FF0000", "the stop takes the edit: {:?}", g.gradient.stops);
}
/// A drag across the slider previews and ends as one undo step named Opacity.
#[test]
fn a_drag_is_one_undo_step() {