diff --git a/crates/ui-egui/src/gradient_ui.rs b/crates/ui-egui/src/gradient_ui.rs index ff91c23549e..181dd38a99a 100644 --- a/crates/ui-egui/src/gradient_ui.rs +++ b/crates/ui-egui/src/gradient_ui.rs @@ -26,6 +26,8 @@ use crate::state::Tool; use crate::theme::Tokens; use crate::widgets; +#[cfg(test)] +mod editor_tests; mod properties_panel; /// Id of the temporary layer previewing a new gradient while it is drawn. @@ -599,10 +601,9 @@ fn selected_gradient(app: &PhotocraftApp) -> Option { } /// The Gradient Editor window the options-bar swatch opens, standing in for Photoshop's Gradient -/// Editor. A selected gradient fill layer gets the full editor — the same "Gradient" (stops) and -/// "Gradient Options" sections the Properties panel shows. With no gradient fill layer selected -/// there is nothing to edit yet (a live gradient is created by dragging on the canvas, and a -/// classic one is a preset), so the window says so and offers the Gradients panel. +/// Editor. A selected gradient fill layer gets a combined ramp, with opacity above and colour +/// below, selected-stop fields and Gradient Options. Without a gradient fill layer, the same +/// stop controls edit the tool's current gradient and offer saving it as a preset. pub fn editor_window(app: &mut PhotocraftApp, ctx: &egui::Context) { if !app.ui.panels.gradient_editor { return; @@ -618,7 +619,7 @@ pub fn editor_window(app: &mut PhotocraftApp, ctx: &egui::Context) { .resizable(true) .default_width(330.0) .show(ctx, |ui| match &layer { - Some(l) => properties(app, ui, l), + Some(l) => properties_panel::editor(app, ui, l), None => { // Photoshop's editor edits the gradient the tool paints with, so with no gradient // fill layer around the strip edits the current gradient in place. @@ -715,6 +716,12 @@ enum Sink { } impl Sink { + fn fill(self, app: &PhotocraftApp) -> Option { + match self { + Sink::Layer(id) => fill_of(app.session.active()?.doc.layer(id)?).cloned(), + Sink::Preset => preset_fill(app), + } + } /// Uniqueness for the drag state. fn key(self) -> egui::Id { match self { @@ -783,7 +790,12 @@ fn preset_stop_color(app: &mut PhotocraftApp, i: usize) { } fn stops_editor_for(app: &mut PhotocraftApp, ui: &mut egui::Ui, sink: Sink, f: Fill) { + let key = strip_key(app, sink, Strip::Combined).with("selection"); + if ui.data(|d| d.get_temp::(key)).is_none() { + ui.data_mut(|d| d.insert_temp(key, Marker::Color(0))); + } stop_strip(app, ui, sink, f, Strip::Combined); + properties_panel::stop_fields(app, ui, sink, Strip::Combined); } /// Keep selection on a stop after the engine sorts its new position. @@ -839,10 +851,8 @@ fn stop_strip(app: &mut PhotocraftApp, ui: &mut egui::Ui, sink: Sink, f: Fill, s let shown = pending.as_ref().and_then(|p| cmds::apply_stop(&f, p, fg, bg).ok()).unwrap_or_else(|| f.clone()); let mut track_fill = shown.clone(); if let Fill::Gradient { stops, opacity_stops, reverse, .. } = &mut track_fill { - if !combined { - // Markers address un-reversed stop positions. Each track shows only its own data. - *reverse = false; - } + // All markers address un-reversed positions; Reverse applies to the canvas output. + *reverse = false; match strip { Strip::Color => opacity_stops.clear(), Strip::Opacity => { @@ -1334,7 +1344,7 @@ mod tests { assert!(app.ui.panels.gradient_editor, "the window stays open"); // A committed live gradient fill layer: `selected_gradient` finds it, so the full editor - // (the same sections the Properties panel shows) is what gets built. + // (combined stops and Gradient Options) is what gets built. let mut app = app_with_gradient("linear"); drag(&mut app, [10.0, 10.0], [150.0, 90.0]); assert!(selected_gradient(&app).is_some(), "the drag left a gradient fill layer selected"); diff --git a/crates/ui-egui/src/gradient_ui/editor_tests.rs b/crates/ui-egui/src/gradient_ui/editor_tests.rs new file mode 100644 index 00000000000..43f470c65b7 --- /dev/null +++ b/crates/ui-egui/src/gradient_ui/editor_tests.rs @@ -0,0 +1,146 @@ +//! Exercise the actual editor window, including its layer and tool-preset targets. + +use super::*; +use egui_kittest::{ + Harness, + kittest::{NodeT, Queryable}, +}; + +fn editor(layer: bool) -> (Harness<'static, PhotocraftApp>, Sink) { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 120})).unwrap(); + let mut params = json!({"stops": [[0, "#ff0000"], [0.5, "#00ff00"], [1, "#0000ff"]], "transparency": [[0, 25], [0.5, 60], [1, 100]]}); + let sink = if layer { + params["from"] = json!([20, 60]); + params["to"] = json!([180, 60]); + app.run(cmds::CREATE, params).unwrap(); + Sink::Layer(app.session.active().unwrap().active_layer.unwrap()) + } else { + app.run("gradient.presets.select", params).unwrap(); + Sink::Preset + }; + let mut h = Harness::builder().with_size(vec2(1000.0, 900.0)).build_ui_state(|ui, app: &mut PhotocraftApp| editor_window(app, ui.ctx()), app); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ProMedium); + h.run_steps(2); + h.state_mut().ui.panels.gradient_editor = true; + h.run_steps(4); + (h, sink) +} + +fn click(h: &mut Harness, pos: Pos2) { + h.event(egui::Event::PointerMoved(pos)); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE }); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE }); + h.run_steps(3); +} + +fn select(h: &mut Harness, location: f32, opacity: bool) { + let rect = h.get_by_label("Gradient stops").rect(); + click(h, pos2(rect.left() + 8.0 + location * (rect.width() - 16.0), rect.top() + if opacity { 6.0 } else { 50.0 })); +} + +fn type_number(h: &mut Harness, index: usize, value: &str, commit: bool) { + h.get_all_by_role(egui::accesskit::Role::SpinButton).nth(index).unwrap().click(); + h.run_steps(2); + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.event(egui::Event::Text(value.into())); + h.run_steps(2); + if commit { + h.key_press(egui::Key::Enter); + h.run_steps(3); + } +} + +#[test] +fn top_opacity_and_bottom_color_select_their_own_fields_in_both_editor_modes() { + for layer in [true, false] { + let (mut h, sink) = editor(layer); + let before = sink.fill(h.state()).unwrap(); + assert!(h.query_by_label("Color stops").is_none(), "editor has one combined strip"); + assert!(h.query_by_label("Opacity stops").is_none()); + assert!(h.get_all_by_label("Color").any(|n| n.accesskit_node().role() == egui::accesskit::Role::Button)); + select(&mut h, 0.5, true); + let key = strip_key(h.state(), sink, Strip::Combined).with("selection"); + assert_eq!(h.ctx.data(|d| d.get_temp::(key)), Some(Marker::Opacity(1))); + assert!(h.query_by_label("Color").is_none(), "opacity selection hides color controls"); + select(&mut h, 0.5, false); + assert_eq!(h.ctx.data(|d| d.get_temp::(key)), Some(Marker::Color(1))); + assert!(h.get_all_by_label("Color").any(|n| n.accesskit_node().role() == egui::accesskit::Role::Button)); + assert_eq!(sink.fill(h.state()).unwrap(), before, "selecting stops never edits the gradient"); + } +} + +#[test] +fn selected_editor_opacity_commits_once_and_delete_preserves_minimum() { + let (mut h, sink) = editor(true); + select(&mut h, 0.5, true); + let before = sink.fill(h.state()).unwrap(); + let history = h.state().session.active().unwrap().history.past_len(); + type_number(&mut h, 0, "35", false); + assert_eq!(sink.fill(h.state()).unwrap(), before, "typing previews until committed"); + h.key_press(egui::Key::Enter); + h.run_steps(3); + let after = sink.fill(h.state()).unwrap(); + assert!(matches!(&after, Fill::Gradient { opacity_stops, .. } if (opacity_stops[1].1 - 0.35).abs() < 1e-5)); + assert_eq!(h.state().session.active().unwrap().history.past_len(), history + 1); + h.get_by_label("Delete").click(); + h.run_steps(3); + let Fill::Gradient { stops, opacity_stops, .. } = sink.fill(h.state()).unwrap() else { panic!() }; + assert_eq!(stops.len(), 3); + assert_eq!(opacity_stops.len(), 2); + select(&mut h, 0.0, true); + assert!(h.get_by_label("Delete").accesskit_node().is_disabled()); + h.state_mut().run("edit.undo", json!({})).unwrap(); + assert_eq!(sink.fill(h.state()).unwrap(), after); + h.state_mut().run("edit.undo", json!({})).unwrap(); + assert_eq!(sink.fill(h.state()).unwrap(), before); + h.state_mut().run("edit.redo", json!({})).unwrap(); + assert_eq!(sink.fill(h.state()).unwrap(), after); +} + +#[test] +fn editor_location_follows_reordered_color_and_opens_existing_picker() { + for layer in [true, false] { + let (mut h, sink) = editor(layer); + select(&mut h, 0.0, false); + type_number(&mut h, 0, "70", true); + let key = strip_key(h.state(), sink, Strip::Combined).with("selection"); + assert_eq!(h.ctx.data(|d| d.get_temp::(key)), Some(Marker::Color(1))); + assert!(matches!(sink.fill(h.state()).unwrap(), Fill::Gradient { stops, .. } if (stops[1].0 - 0.7).abs() < 1e-5)); + h.get_all_by_label("Color").find(|n| n.accesskit_node().role() == egui::accesskit::Role::Button).unwrap().click(); + h.run_steps(3); + assert!(!h.state().ui.dialogs.is_empty()); + } +} + +#[test] +fn editor_opacity_location_changes_only_opacity_in_both_modes() { + for layer in [true, false] { + let (mut h, sink) = editor(layer); + let Fill::Gradient { stops: before, .. } = sink.fill(h.state()).unwrap() else { panic!() }; + select(&mut h, 0.0, true); + type_number(&mut h, 1, "70", true); + let Fill::Gradient { stops, opacity_stops, .. } = sink.fill(h.state()).unwrap() else { panic!() }; + assert_eq!(stops, before, "opacity location must not move a color stop"); + assert!((opacity_stops[1].0 - 0.7).abs() < 1e-5); + let key = strip_key(h.state(), sink, Strip::Combined).with("selection"); + assert_eq!(h.ctx.data(|d| d.get_temp::(key)), Some(Marker::Opacity(1))); + } +} + +#[test] +fn preset_editor_opacity_fields_do_not_edit_document_history() { + let (mut h, sink) = editor(false); + let before = h.state().session.active().unwrap().doc.clone(); + let history = h.state().session.active().unwrap().history.past_len(); + select(&mut h, 0.5, true); + type_number(&mut h, 0, "35", true); + assert!(matches!(sink.fill(h.state()).unwrap(), Fill::Gradient { opacity_stops, .. } if (opacity_stops[1].1 - 0.35).abs() < 1e-5)); + h.get_by_label("Delete").click(); + h.run_steps(3); + assert!(matches!(sink.fill(h.state()).unwrap(), Fill::Gradient { stops, opacity_stops, .. } if stops.len() == 3 && opacity_stops.len() == 2)); + assert_eq!(h.state().session.active().unwrap().doc, before); + assert_eq!(h.state().session.active().unwrap().history.past_len(), history); +} diff --git a/crates/ui-egui/src/gradient_ui/properties_panel.rs b/crates/ui-egui/src/gradient_ui/properties_panel.rs index 3627ee33d4b..03817c402e5 100644 --- a/crates/ui-egui/src/gradient_ui/properties_panel.rs +++ b/crates/ui-egui/src/gradient_ui/properties_panel.rs @@ -12,43 +12,9 @@ const STYLES: [(GradientStyle, &str); 5] = [ ]; pub(super) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) { - let Some(Fill::Gradient { angle, scale, style, reverse, dither, align, .. }) = fill_of(layer).cloned() else { return }; let document = app.session.active().map(|s| s.doc.id); ui.push_id(("gradient-properties", document, layer.id.0), |ui| { - if props_layout::section(ui, "gradient-fill-options", "Gradient Options") { - ui.horizontal(|ui| { - row_label(ui, "Style"); - ui.spacing_mut().item_spacing.x = 3.0; - for (value, name) in STYLES { - if style_button(ui, value, value == style, name).clicked() { - let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "style": cmds::style_name(value)})); - } - } - }); - if let Some(value) = numeric_row(ui, "angle", "Angle", angle, -180.0..=180.0, "°") { - let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "angle": value})); - } - if let Some(value) = numeric_row(ui, "scale", "Scale", scale * 100.0, 10.0..=150.0, "%") { - let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "scale": value})); - } - ui.horizontal_wrapped(|ui| { - let mut value = reverse; - if widgets::checkbox(ui, &mut value, "Reverse").changed() { - let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "reverse": value})); - } - let mut value = dither; - if widgets::checkbox(ui, &mut value, "Dither").changed() { - let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "dither": value})); - } - }); - let mut value = align; - if widgets::checkbox(ui, &mut value, "Align with layer").changed() { - let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "align": value})); - } - if widgets::secondary_button(ui, "Reset Alignment", ui.available_width()).on_hover_text(tl!("Centre the gradient (offset 0, 0)")).clicked() { - let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "offset": [0, 0]})); - } - } + options(app, ui, layer); for (strip, title) in [(Strip::Color, "Color stops"), (Strip::Opacity, "Opacity stops")] { let section_id = if strip == Strip::Color { "gradient-fill-colors" } else { "gradient-fill-opacity" }; if props_layout::section(ui, section_id, title) { @@ -59,13 +25,63 @@ pub(super) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) { ui.data_mut(|d| d.insert_temp(key, if strip == Strip::Color { Marker::Color(0) } else { Marker::Opacity(0) })); } stop_strip(app, ui, Sink::Layer(layer.id), fill, strip); - stop_fields(app, ui, layer.id, strip); + stop_fields(app, ui, Sink::Layer(layer.id), strip); } } } }); } +pub(super) fn editor(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) { + let document = app.session.active().map(|s| s.doc.id); + ui.push_id(("gradient-editor-layer", document, layer.id.0), |ui| { + if props_layout::section(ui, "gradient-editor-stops", "Gradient") + && let Some(fill) = current_fill(app, layer.id) + { + stops_editor_for(app, ui, Sink::Layer(layer.id), fill); + } + options(app, ui, layer); + }); +} + +fn options(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) { + let Some(Fill::Gradient { angle, scale, style, reverse, dither, align, .. }) = fill_of(layer).cloned() else { return }; + if props_layout::section(ui, "gradient-fill-options", "Gradient Options") { + ui.horizontal(|ui| { + row_label(ui, "Style"); + ui.spacing_mut().item_spacing.x = 3.0; + for (value, name) in STYLES { + if style_button(ui, value, value == style, name).clicked() { + let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "style": cmds::style_name(value)})); + } + } + }); + if let Some(value) = numeric_row(ui, "angle", "Angle", angle, -180.0..=180.0, "°") { + let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "angle": value})); + } + if let Some(value) = numeric_row(ui, "scale", "Scale", scale * 100.0, 10.0..=150.0, "%") { + let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "scale": value})); + } + ui.horizontal_wrapped(|ui| { + let mut value = reverse; + if widgets::checkbox(ui, &mut value, "Reverse").changed() { + let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "reverse": value})); + } + let mut value = dither; + if widgets::checkbox(ui, &mut value, "Dither").changed() { + let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "dither": value})); + } + }); + let mut value = align; + if widgets::checkbox(ui, &mut value, "Align with layer").changed() { + let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "align": value})); + } + if widgets::secondary_button(ui, "Reset Alignment", ui.available_width()).on_hover_text(tl!("Centre the gradient (offset 0, 0)")).clicked() { + let _ = app.run(cmds::SET, json!({"layer": layer.id.0, "offset": [0, 0]})); + } + } +} + fn current_fill(app: &PhotocraftApp, id: LayerId) -> Option { fill_of(app.session.active()?.doc.layer(id)?).cloned() } @@ -127,19 +143,18 @@ fn style_button(ui: &mut egui::Ui, style: GradientStyle, selected: bool, name: & response.on_hover_text(tl!(name)) } -fn stop_fields(app: &mut PhotocraftApp, ui: &mut egui::Ui, id: LayerId, strip: Strip) { - let Some(Fill::Gradient { mut stops, mut opacity_stops, midpoints, .. }) = current_fill(app, id) else { return }; +pub(super) fn stop_fields(app: &mut PhotocraftApp, ui: &mut egui::Ui, sink: Sink, strip: Strip) { + let Some(Fill::Gradient { mut stops, mut opacity_stops, midpoints, .. }) = sink.fill(app) else { return }; stops.sort_by(|a, b| a.0.total_cmp(&b.0)); if opacity_stops.is_empty() { opacity_stops = vec![(0.0, 1.0), (1.0, 1.0)]; } opacity_stops.sort_by(|a, b| a.0.total_cmp(&b.0)); - let sink = Sink::Layer(id); let key = strip_key(app, sink, strip).with("selection"); - let default = if strip == Strip::Color { Marker::Color(0) } else { Marker::Opacity(0) }; + let default = if strip == Strip::Opacity { Marker::Opacity(0) } else { Marker::Color(0) }; let selected = ui.data(|d| d.get_temp::(key)).unwrap_or(default); let (index, location, count) = match selected { - Marker::Color(i) if strip == Strip::Color => { + Marker::Color(i) if strip != Strip::Opacity => { let Some((location, color)) = stops.get(i) else { ui.data_mut(|d| d.remove::(key)); return; @@ -152,38 +167,41 @@ fn stop_fields(app: &mut PhotocraftApp, ui: &mut egui::Ui, id: LayerId, strip: S ui.painter().rect_stroke(rect, Tokens::get(ui.ctx()).radius_sm, Stroke::new(1.0, Tokens::get(ui.ctx()).field_border), StrokeKind::Inside); response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, ui.is_enabled(), tl!("Color"))); if response.on_hover_text(tl!("Color")).clicked() { - edit_stop_color(app, id, i); + match sink { + Sink::Layer(id) => edit_stop_color(app, id, i), + Sink::Preset => preset_stop_color(app, i), + } } }); (i, *location, stops.len()) } - Marker::Opacity(i) if strip == Strip::Opacity => { + Marker::Opacity(i) if strip != Strip::Color => { let Some((location, opacity)) = opacity_stops.get(i) else { ui.data_mut(|d| d.remove::(key)); return; }; if let Some(value) = numeric_row(ui, &format!("opacity-{i}"), "Opacity", *opacity * 100.0, 0.0..=100.0, "%") { - let _ = app.run(cmds::STOP, sink.params(json!({"action": "opacity", "kind": "opacity", "index": i, "opacity": value}))); + let _ = app.run(sink.cmd(), sink.params(json!({"action": "opacity", "kind": "opacity", "index": i, "opacity": value}))); } (i, *location, opacity_stops.len()) } - Marker::Mid(i) if strip == Strip::Color => { + Marker::Mid(i) if strip != Strip::Opacity => { if stops.get(i).is_none() || stops.get(i.saturating_add(1)).is_none() { ui.data_mut(|d| d.remove::(key)); return; } if let Some(value) = numeric_row(ui, &format!("midpoint-{i}"), "Midpoint", midpoints.get(i).copied().unwrap_or(0.5) * 100.0, 5.0..=95.0, "%") { - let _ = app.run(cmds::STOP, sink.params(json!({"action": "midpoint", "index": i, "location": value / 100.0}))); + let _ = app.run(sink.cmd(), sink.params(json!({"action": "midpoint", "index": i, "location": value / 100.0}))); } return; } _ => return, }; - let kind = if strip == Strip::Opacity { "opacity" } else { "color" }; + let kind = if matches!(selected, Marker::Opacity(_)) { "opacity" } else { "color" }; if let Some(value) = numeric_row(ui, &format!("{kind}-location-{index}"), "Location", location * 100.0, 0.0..=100.0, "%") { let p = sink.params(json!({"action": "move", "kind": kind, "index": index, "location": value / 100.0})); - let selection = current_fill(app, id).and_then(|f| selection_after_edit(&f, &p, Some(selected))); - if app.run(cmds::STOP, p).is_ok() { + let selection = sink.fill(app).and_then(|f| selection_after_edit(&f, &p, Some(selected))); + if app.run(sink.cmd(), p).is_ok() { if let Some(marker) = selection { ui.data_mut(|d| d.insert_temp(key, marker)); } @@ -197,7 +215,7 @@ fn stop_fields(app: &mut PhotocraftApp, ui: &mut egui::Ui, id: LayerId, strip: S let response = icons::button(ui, "trash", 24.0, false, "Delete"); response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, ui.is_enabled(), tl!("Delete"))); if response.clicked() { - let _ = app.run(cmds::STOP, sink.params(json!({"action": "delete", "kind": kind, "index": index}))); + let _ = app.run(sink.cmd(), sink.params(json!({"action": "delete", "kind": kind, "index": index}))); ui.data_mut(|d| d.remove::(key)); } });