PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(gradient): options-bar Opacity updates the live gradient layer (#5012) (#5045)
Alon77d3478df150
diff --git a/crates/ui-egui/src/gradient_ui.rs b/crates/ui-egui/src/gradient_ui.rsindex 5590c13f2..be35f6602 100644--- a/crates/ui-egui/src/gradient_ui.rs+++ b/crates/ui-egui/src/gradient_ui.rs@@ -678,6 +678,25 @@ pub fn options_changed(app: &mut PhotocraftApp, before: &crate::state::ToolOptiolet _ = app.run(cmds::SET, p);}+/// Live mode: the options bar's Opacity sets a selected gradient fill layer's opacity, as a+/// drag's `opacity` does, live while it is scrubbed; `drag` numbers the scrub so it is one undo+/// step, as in the Layers panel (#5012).+pub fn opacity_changed(app: &mut PhotocraftApp, drag: Option<u64>) {+ let o = &app.ui.tool_options;+ if app.ui.tool != Tool::Gradient || o.gradient_classic {+ return;+ }+ let opacity = o.fill_opacity / 100.0;+ let Some((l, ..)) = active_gradient(app) else { return };+ if (l.opacity - opacity).abs() > 1e-6 {+ let mut p = json!({"layer": l.id.0, "opacity": opacity});+ if let Some(n) = drag {+ p["coalesce"] = json!(format!("gradient-tool-opacity:{n}"));+ }+ let _ = app.run("layer.setProps", p);+ }+}+// ---------------------------------------------------------------- Properties panel/// Properties panel for a live gradient. Every change uses the existing gradient commands.@@ -1257,6 +1276,27 @@ mod tests {assert_eq!(layer.blend, BlendMode::Difference);}+ #[test]+ fn changing_live_gradient_opacity_updates_selected_fill_layer() {+ let mut app = app_with_gradient("linear");+ drag(&mut app, [20.0, 60.0], [180.0, 60.0]);+ let past = app.session.active().unwrap().history.past_len();+ // A scrub is live on every frame and one undo step.+ for pct in [70.0, 55.0, 40.0] {+ app.ui.tool_options.fill_opacity = pct;+ opacity_changed(&mut app, Some(7));+ }+ opacity_changed(&mut app, None);+ let st = app.session.active().unwrap();+ assert!((st.doc.layers.last().unwrap().opacity - 0.4).abs() < 1e-6);+ assert_eq!(st.history.past_len(), past + 1, "one undo step, none for an unchanged value");+ // Classic mode paints pixels: the options bar leaves the layer alone.+ app.ui.tool_options.gradient_classic = true;+ app.ui.tool_options.fill_opacity = 80.0;+ opacity_changed(&mut app, None);+ assert!((app.session.active().unwrap().doc.layers.last().unwrap().opacity - 0.4).abs() < 1e-6);+ }+#[test]fn preview_shows_the_drag_before_commit() {let mut app = app_with_gradient("radial");diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex 601f049ee..11d5ecbed 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -1264,7 +1264,17 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {// The wheel over it steps the mode, as over the Layers panel's (#2061).widgets::dropdown_wheel_hovered(ui, "gradient-blend-mode", &mut app.ui.tool_options.gradient_blend_mode, &opts, 96.0);opt_label(ui, tl!("Opacity"));- widgets::value_field(ui, &mut app.ui.tool_options.fill_opacity, 1.0..=100.0, "%", 62.0);+ let r = widgets::value_field(ui, &mut app.ui.tool_options.fill_opacity, 1.0..=100.0, "%", 62.0);+ // Live while scrubbed, one undo step per scrub (#5012).+ let drag_key = r.id.with("gradient-opacity-drag");+ if r.drag_started() {+ let n = ui.ctx().cumulative_pass_nr();+ ui.data_mut(|d| d.insert_temp(drag_key, n));+ }+ if r.changed() {+ let drag = if r.dragged() { ui.data(|d| d.get_temp::<u64>(drag_key)) } else { None };+ crate::gradient_ui::opacity_changed(app, drag);+ }widgets::checkbox(ui, &mut app.ui.tool_options.gradient_reverse, tl!("Reverse"));widgets::checkbox(ui, &mut app.ui.tool_options.gradient_dither, tl!("Dither"));// Live mode: the options also change a selected gradient fill layer.