diff --git a/crates/ui-egui/src/panels/color_wheel.rs b/crates/ui-egui/src/panels/color_wheel.rs index 675ca6ad89..f32641a7ac 100644 --- a/crates/ui-egui/src/panels/color_wheel.rs +++ b/crates/ui-egui/src/panels/color_wheel.rs @@ -1324,8 +1324,10 @@ fn hex_row(app: &VectorcraftApp, ui: &mut Ui, tgt: &Target, shown: &str) -> Opti /// Apply the hex field's committed `text`: it goes through `color.parse` as typed, and a text it /// refuses shows its error in the status bar and changes nothing. The text's alpha is not applied /// when `opacity_set` (the opacity row set the opacity in this frame), so a press on that row that -/// commits the text keeps the opacity it set. -fn apply_text(app: &mut VectorcraftApp, ui: &Ui, text: &str, opacity_set: bool) { +/// commits the text keeps the opacity it set. When the text is the one the field shows +/// (`retyped`), its alpha stands for the opacity the field shows, not the value its two digits +/// round to, so retyping it leaves that opacity as it was. +fn apply_text(app: &mut VectorcraftApp, ui: &Ui, text: &str, opacity_set: bool, retyped: bool) { match app.run("color.parse", json!({ "text": text })) { Ok(r) => { let Ok(rgb) = serde_json::from_value::(r["color"].clone()) else { return }; @@ -1334,6 +1336,7 @@ fn apply_text(app: &mut VectorcraftApp, ui: &Ui, text: &str, opacity_set: bool) let cmyk_doc = app.session.active().is_some_and(|d| d.doc.color_mode == ColorMode::Cmyk); let c = stored_rgb(&cms::active(), rgb, replaced(app, &tgt, false).as_ref(), cmyk_doc); let alpha = r["opacity"].as_f64().map(|o| (o / 100.0) as f32).filter(|_| !opacity_set); + let alpha = alpha.map(|a| if retyped { shown_opacity(app, &tgt).0 } else { a }); apply_typed(app, &tgt, c, alpha); } Err(e) => app.status(e), @@ -1448,13 +1451,16 @@ fn show_with(app: &mut VectorcraftApp, panel: &mut WheelUi, ui: &mut Ui) { }; let typed = hex.and_then(|hex| { ui.add_space(4.0); - hex_row(app, ui, &tgt, &hex) + hex_row(app, ui, &tgt, &hex).map(|text| { + let shown = text.strip_prefix('#').unwrap_or(&text).eq_ignore_ascii_case(&hex); + (text, shown) + }) }); // A text committed by a press on the wheel, the box or the opacity row is applied first; the // opacity edit then goes to the target as the text left it, and a pick to the target as both // left it, so a stop keeps all of them. - if let Some(text) = &typed { - apply_text(app, ui, text, opacity_edit.is_some()); + if let Some((text, shown)) = &typed { + apply_text(app, ui, text, opacity_edit.is_some(), *shown); } let tgt = if typed.is_some() { wheel_target(app, ui) } else { tgt }; if let Some(edit) = &opacity_edit { diff --git a/crates/ui-egui/src/panels/tests_color_wheel_hex.rs b/crates/ui-egui/src/panels/tests_color_wheel_hex.rs index e90c0d2953..735df1b0fd 100644 --- a/crates/ui-egui/src/panels/tests_color_wheel_hex.rs +++ b/crates/ui-egui/src/panels/tests_color_wheel_hex.rs @@ -272,6 +272,15 @@ fn retyping_the_shown_color_in_another_form_changes_nothing() { assert_eq!(undo_count(&p.app()), before, "{same}"); assert!(p.app().ui.status.is_empty(), "{same}: {}", p.app().ui.status); } + let text = shown(&p); + assert_eq!(text, "FF000080"); + type_in(&mut p, &text); + assert_eq!(undo_count(&p.app()), before, "the shown text"); + assert_eq!(p.app().session.proxy_opacity(false), Some(0.5), "the shown alpha keeps 50 %"); + type_in(&mut p, "rgb(255 0 0 / 50.4%)"); + assert_eq!(undo_count(&p.app()), before + 1, "an alpha with the same two digits is applied"); + assert!(p.app().session.proxy_opacity(false).is_some_and(|o| (o - 0.504).abs() < 1e-4)); + let before = undo_count(&p.app()); type_in(&mut p, "#00ff00"); assert_eq!(undo_count(&p.app()), before + 1, "another color is applied"); p.run("paint.setFill", json!({"opacity": 99.9})); @@ -282,6 +291,26 @@ fn retyping_the_shown_color_in_another_form_changes_nothing() { assert_eq!(p.app().session.proxy_opacity(false), Some(1.0)); } +/// Retyping the shown text with two objects selected gives the second the first one's opacity, the +/// one the field shows, and leaves the first's as it was. +#[test] +fn retyping_the_shown_alpha_gives_the_selection_the_shown_opacity() { + let mut app = app(); + let first = selected(&app)[0]; + app.run("paint.setFill", json!({"opacity": 50})).unwrap(); + app.run("shape.rectangle", json!({"x": 100, "y": 10, "width": 50, "height": 50})).unwrap(); + let second = selected(&app)[0]; + app.run("paint.setFill", json!({"color": "#ff0000", "opacity": 80})).unwrap(); + app.run("select.set", json!({"ids": [first, second]})).unwrap(); + let mut p = panel(app); + let text = shown(&p); + assert_eq!(text, "FF000080"); + type_in(&mut p, &text); + let opacity = |id| p.app().session.doc().unwrap().doc.node(id).unwrap().appearance.fill_at(None).map(|f| f.opacity); + assert_eq!(opacity(first), Some(0.5), "the first keeps 50 %"); + assert_eq!(opacity(second), Some(0.5), "the second takes 50 %"); +} + /// Retyping a gradient stop's or a freeform point's color, with or without its alpha, records no /// step. #[test]