Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

refactor(ui): let the gradient stop strip report actions to its caller

Skyfuller13030b067c7c68c8
diff --git a/crates/ui-egui/src/gradient_ui.rs b/crates/ui-egui/src/gradient_ui.rs
index be35f6602..b2c703bf4 100644
--- a/crates/ui-egui/src/gradient_ui.rs
+++ b/crates/ui-egui/src/gradient_ui.rs
@@ -805,14 +805,50 @@ 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).with("selection");
- if ui.data(|d| d.get_temp::<Marker>(key)).is_none() {
- ui.data_mut(|d| d.insert_temp(key, Marker::Color(0)));
+ let key = strip_key(app, sink);
+ let selection_key = key.with("selection");
+ if ui.data(|d| d.get_temp::<Marker>(selection_key)).is_none() {
+ ui.data_mut(|d| d.insert_temp(selection_key, Marker::Color(0)));
+ }
+ let pending = match sink {
+ Sink::Layer(id) => app.gradient.panel.clone().filter(|(l, ..)| *l == id).map(|(_, _, p)| p),
+ Sink::Preset => app.gradient.preset_stop.clone(),
+ };
+ let colors = (app.session.tools.foreground, app.session.tools.background);
+ match stop_strip(ui, key, &f, sink.midpoints(), pending.as_ref(), colors) {
+ Some(StopAction::Preview(p)) => match sink {
+ Sink::Layer(id) => app.gradient.panel = Some((id, cmds::STOP, sink.params(p))),
+ Sink::Preset => app.gradient.preset_stop = Some(p),
+ },
+ Some(StopAction::Commit(p)) => {
+ match sink {
+ Sink::Layer(_) => app.gradient.panel = None,
+ Sink::Preset => app.gradient.preset_stop = None,
+ }
+ let selected = ui.data(|d| d.get_temp::<Marker>(selection_key));
+ let selection = selection_after_edit(&f, &p, selected);
+ if app.run(sink.cmd(), sink.params(p)).is_ok()
+ && let Some(marker) = selection
+ {
+ ui.data_mut(|d| d.insert_temp(selection_key, marker));
+ }
+ }
+ Some(StopAction::Color(i)) => match sink {
+ Sink::Layer(id) => edit_stop_color(app, id, i),
+ Sink::Preset => preset_stop_color(app, i),
+ },
+ None => {}
}
- stop_strip(app, ui, sink, f);
properties_panel::stop_fields(app, ui, sink);
}
+/// A shared stop strip keeps previews local; its caller chooses the command or paint target.
+pub(crate) enum StopAction {
+ Preview(Value),
+ Commit(Value),
+ Color(usize),
+}
+
/// Keep selection on a stop after the engine sorts its new position.
fn selection_after_edit(f: &Fill, p: &Value, previous: Option<Marker>) -> Option<Marker> {
let Fill::Gradient { stops, opacity_stops, .. } = f else { return previous };
@@ -840,8 +876,8 @@ fn selection_after_edit(f: &Fill, p: &Value, previous: Option<Marker>) -> Option
Some(if opacity { Marker::Opacity(at) } else { Marker::Color(at) })
}
-fn stop_strip(app: &mut PhotocraftApp, ui: &mut egui::Ui, sink: Sink, f: Fill) {
- let Fill::Gradient { stops, .. } = &f else { return };
+fn stop_strip(ui: &mut egui::Ui, key: egui::Id, f: &Fill, midpoints: bool, pending: Option<&Value>, colors: ([f32; 4], [f32; 4])) -> Option<StopAction> {
+ let Fill::Gradient { stops, .. } = f else { return None };
let t = Tokens::get(ui.ctx());
let w = ui.available_width().max(120.0);
let (outer, resp) = ui.allocate_exact_size(vec2(w, 58.0), Sense::click_and_drag());
@@ -852,12 +888,8 @@ fn stop_strip(app: &mut PhotocraftApp, ui: &mut egui::Ui, sink: Sink, f: Fill) {
let x_of = |loc: f32| bar.left() + loc.clamp(0.0, 1.0) * bar.width();
let loc_of = |x: f32| ((x - bar.left()) / bar.width().max(1.0)).clamp(0.0, 1.0);
// The ramp being shown (with a pending drag's edit applied).
- let pending: Option<Value> = match sink {
- Sink::Layer(id) => app.gradient.panel.clone().filter(|(l, ..)| *l == id).map(|(_, _, p)| p),
- Sink::Preset => app.gradient.preset_stop.clone(),
- };
- let (fg, bg) = (app.session.tools.foreground, app.session.tools.background);
- let shown = pending.as_ref().and_then(|p| cmds::apply_stop(&f, p, fg, bg).ok()).unwrap_or_else(|| f.clone());
+ let (fg, bg) = colors;
+ let shown = pending.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 { reverse, .. } = &mut track_fill {
// All markers address un-reversed positions; Reverse applies to the canvas output.
@@ -875,7 +907,6 @@ fn stop_strip(app: &mut PhotocraftApp, ui: &mut egui::Ui, sink: Sink, f: Fill) {
sorted.sort_by(|a, b| a.0.total_cmp(&b.0));
let mut ops: Vec<(f32, f32)> = if os.is_empty() { vec![(0.0, 1.0), (1.0, 1.0)] } else { os.clone() };
ops.sort_by(|a, b| a.0.total_cmp(&b.0));
- let key = strip_key(app, sink);
let selection_key = key.with("selection");
let selected: Option<Marker> = ui.data(|d| d.get_temp(selection_key));
let dragging: Option<Marker> = ui.data(|d| d.get_temp(key));
@@ -896,7 +927,7 @@ fn stop_strip(app: &mut PhotocraftApp, ui: &mut egui::Ui, sink: Sink, f: Fill) {
p.rect_filled(body, 1.0, color);
p.rect_stroke(body, 1.0, Stroke::new(1.0, if sel { t.accent } else { t.text_dim }), StrokeKind::Outside);
}
- if sink.midpoints() {
+ if midpoints {
for i in 0..sorted.len().saturating_sub(1) {
let (a, b) = (sorted[i].0, sorted[i + 1].0);
if b - a > 1e-3 {
@@ -926,7 +957,7 @@ fn stop_strip(app: &mut PhotocraftApp, ui: &mut egui::Ui, sink: Sink, f: Fill) {
if let Some(i) = sorted.iter().position(|(l, _)| (x_of(*l) - pos.x).abs() <= 6.0 && pos.y >= bar.bottom() + 3.0) {
return Some(Marker::Color(i));
}
- if !sink.midpoints() {
+ if !midpoints {
return None;
}
return (0..sorted.len().saturating_sub(1))
@@ -949,6 +980,7 @@ fn stop_strip(app: &mut PhotocraftApp, ui: &mut egui::Ui, sink: Sink, f: Fill) {
ui.data_mut(|d| d.insert_temp(selection_key, m));
}
let dragging: Option<Marker> = ui.data(|d| d.get_temp(key));
+ let mut preview = None;
if let (Some(m), Some(pos)) = (dragging, resp.interact_pointer_pos()) {
let off = if pos.y > bar.bottom() + 28.0 || pos.y < bar.top() - 26.0 { 1.0 } else { 0.0 };
let loc = loc_of(pos.x);
@@ -963,70 +995,32 @@ fn stop_strip(app: &mut PhotocraftApp, ui: &mut egui::Ui, sink: Sink, f: Fill) {
}
};
if resp.dragged() {
- match sink {
- Sink::Layer(id) => app.gradient.panel = Some((id, cmds::STOP, sink.params(edit))),
- Sink::Preset => app.gradient.preset_stop = Some(edit),
- }
+ preview = Some(edit);
}
}
if resp.drag_stopped() {
ui.data_mut(|d| d.remove::<Marker>(key));
- match sink {
- Sink::Layer(_) => {
- if let Some((_, cmd, p)) = app.gradient.panel.take() {
- let selection = selection_after_edit(&f, &p, selected);
- if app.run(cmd, p).is_ok()
- && let Some(marker) = selection
- {
- ui.data_mut(|d| d.insert_temp(selection_key, marker));
- }
- }
- }
- Sink::Preset => {
- if let Some(p) = app.gradient.preset_stop.take() {
- let selection = selection_after_edit(&f, &p, selected);
- if app.run(sink.cmd(), p).is_ok()
- && let Some(marker) = selection
- {
- ui.data_mut(|d| d.insert_temp(selection_key, marker));
- }
- }
- }
- }
- return;
+ return preview.or_else(|| pending.cloned()).map(StopAction::Commit);
}
if resp.double_clicked()
&& let Some(pos) = resp.interact_pointer_pos()
&& let Some(Marker::Color(i)) = hit(pos)
{
ui.data_mut(|d| d.insert_temp(selection_key, Marker::Color(i)));
- match sink {
- Sink::Layer(id) => edit_stop_color(app, id, i),
- Sink::Preset => preset_stop_color(app, i),
- }
- return;
+ return Some(StopAction::Color(i));
}
if resp.clicked()
&& let Some(pos) = resp.interact_pointer_pos()
{
if let Some(marker) = hit(pos) {
ui.data_mut(|d| d.insert_temp(selection_key, marker));
- return;
+ return None;
}
let loc = loc_of(pos.x);
- let opacity = pos.y < bar.center().y;
- let p = if opacity {
- sink.params(json!({"action": "add", "kind": "opacity", "location": loc}))
- } else {
- sink.params(json!({"action": "add", "location": loc}))
- };
- let selection = selection_after_edit(&f, &p, selected);
- if app.run(sink.cmd(), p).is_ok()
- && let Some(marker) = selection
- {
- ui.data_mut(|d| d.insert_temp(selection_key, marker));
- }
+ let p = if pos.y < bar.center().y { json!({"action": "add", "kind": "opacity", "location": loc}) } else { json!({"action": "add", "location": loc}) };
+ return Some(StopAction::Commit(p));
}
+ preview.map(StopAction::Preview)
}
#[cfg(test)]