Skip to content

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

Merge remote-tracking branch 'origin/main' into fix/history-brush-source

David Mann27f001c3fa98
diff --cc crates/engine/src/fill_cmds.rs
index b1e1e8ad3,a7350dafc..b33a20008
--- a/crates/engine/src/fill_cmds.rs
+++ b/crates/engine/src/fill_cmds.rs
@@@ -121,11 -121,10 +121,10 @@@ pub fn fill(s: &mut Session, p: &Value
let source_doc = match explicit {
Some(i) if i == past => st.doc.clone(),
Some(i) => st.history.state(i).ok_or_else(|| bad(format!("no history state {i} (0..={past})")))?,
- // The History Brush's default source: the oldest state still held.
- None => st.history.state(0).unwrap_or_else(|| st.doc.clone()),
+ // The History Brush's source (History panel), by default the oldest state held.
+ None => crate::history_source_cmds::source_doc(st)?,
};
- let mut source_doc = (*source_doc).clone();
- let (src, _) = crate::channel_cmds::target_surface(&mut source_doc, id, p)
+ let src = crate::channel_cmds::read_target_surface(source_doc.as_ref(), id, p)
.map_err(|_| EngineError::Other("the target did not exist (as pixels, a mask or a channel) in that history state".into()))?;
Source::Pixels(area, read_rgba(src, area, "reading Fill's history source")?)
}
diff --cc crates/engine/src/inspect.rs
index 576b19d08,d0b2a72e9..eb35f487c
--- a/crates/engine/src/inspect.rs
+++ b/crates/engine/src/inspect.rs
@@@ -39,7 -40,7 +40,8 @@@ pub fn document(d: &DocState) -> Value
"selectionBounds": doc.selection.as_ref().map(|s| { let r = s.content_bounds(); [r.x0, r.y0, r.width() as i32, r.height() as i32] }),
"layers": doc.layers.iter().rev().map(|l| layer_sel(l, &selected)).collect::<Vec<_>>(),
"history": d.history.entries(),
+ "historySource": crate::history_source_cmds::source_json(d),
+ "snapshots": d.snapshots.iter().map(|snap| json!({"id": snap.id, "name": snap.name})).collect::<Vec<_>>(),
"canUndo": d.history.can_undo(),
"canRedo": d.history.can_redo(),
"revision": d.revision,
diff --cc crates/engine/src/lib.rs
index 44181eef0,21f3c8adb..f6f6b4b3b
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@@ -51,9 -52,9 +52,10 @@@ pub mod group_view_cmds
pub mod guide_layout;
pub mod hidden_target;
pub mod history_cmds;
+pub mod history_source_cmds;
pub mod image_cmds;
pub mod inspect;
+ pub mod installed_profiles;
pub mod jobs;
pub mod kys;
pub mod layer_copy_cmds;
diff --cc crates/engine/src/retouch_cmds.rs
index e92306589,9923f7a3d..e469349a6
--- a/crates/engine/src/retouch_cmds.rs
+++ b/crates/engine/src/retouch_cmds.rs
@@@ -509,11 -508,10 +509,10 @@@ fn history_source(s: &Session, id: Opti
let source_doc = match explicit {
Some(i) if i == st.history.past_len() => st.doc.clone(),
Some(i) => st.history.state(i).ok_or_else(|| bad(cmd, format!("no history state {i} (0..={})", st.history.past_len())))?,
- None => st.history.state(0).unwrap_or_else(|| st.doc.clone()),
+ None => crate::history_source_cmds::source_doc(st)?,
};
- let mut source_doc = (*source_doc).clone();
- crate::channel_cmds::target_surface(&mut source_doc, id, p)
- .map(|(surf, _)| surf.clone())
+ crate::channel_cmds::read_target_surface(source_doc.as_ref(), id, p)
+ .cloned()
.map_err(|_| EngineError::Other("the target did not exist (as pixels, a mask or a channel) in that history state".into()))
}
diff --cc crates/ui-egui/src/panels.rs
index 4ea94ee21,696e5b38c..c1e57c2f0
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@@ -2814,8 -2889,8 +2890,9 @@@ fn history(app: &mut PhotocraftApp, ui
ui.painter().text(pos2(thumb.right() + 8.0, rect.center().y), Align2::LEFT_CENTER, &doc.name, egui::FontId::proportional(12.0), t.text);
ui.painter().line_segment([rect.left_bottom(), rect.right_bottom()], Stroke::new(1.0, t.separator));
}
+ let mut snap_cmd = None;
let mut target: Option<isize> = None;
+ let mut set_source: Option<usize> = None;
let all = entries.iter().map(|e| (e.clone(), false)).chain(redo.iter().map(|e| (e.clone(), true)));
// The dock gives History a fixed height: the rows scroll above the footer.
let footer = if t.pro { widgets::footer_height(ui) + ui.spacing().item_spacing.y } else { 0.0 };
@@@ -2892,21 -2965,79 +2976,94 @@@
}
}
+/// The History panel's source column at the left of a state row: the History Brush icon on the
+/// source state; a click makes the row the source (`history.setSource`). It sits above the row, so
+/// the click doesn't also step to the state.
+fn history_source_column(ui: &mut egui::Ui, row: Rect, i: usize, is_source: bool) -> bool {
+ let t = Tokens::get(ui.ctx());
+ let cell = Rect::from_min_size(row.min, vec2(24.0, row.height()));
+ let resp = ui.interact(cell, ui.id().with(("history-source", i)), Sense::click()).on_hover_text(tl!("Sets the source for the history brush"));
+ if is_source {
+ icons::paint(ui, cell.shrink(4.0), icons::tool_icon(Tool::HistoryBrush), 12.0, t.icon);
+ } else if resp.hovered() {
+ icons::paint(ui, cell.shrink(4.0), icons::tool_icon(Tool::HistoryBrush), 12.0, t.text_faint);
+ }
+ resp.clicked()
+}
+
+ /// The History panel's snapshot rows (#2966), under the opening row, each with its thumbnail and
+ /// name: a click returns to the snapshot (a new state), a double-click or right-click › Rename…
+ /// edits the name in place (Enter or a click elsewhere keeps it, Escape cancels), and
+ /// right-click › Delete removes it. Returns the command a row asks for.
+ fn snapshot_rows(app: &mut PhotocraftApp, ui: &mut egui::Ui) -> Option<(&'static str, Value)> {
+ let t = Tokens::get(ui.ctx());
+ let st = app.session.active()?;
+ let (doc_id, snaps) = (st.doc.id, st.snapshots.clone());
+ let live: Vec<u64> = snaps.iter().map(|s| s.id).collect();
+ // Per document: snapshot ids restart in each, and a tab switch must not carry the edit over.
+ let rename_id = egui::Id::new(("history-snapshot-rename", doc_id));
+ let mut renaming: Option<(u64, String)> = ui.data(|d| d.get_temp(rename_id)).flatten();
+ let mut cmd = None;
+ let ctx = ui.ctx().clone();
+ for snap in &snaps {
+ let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 38.0), Sense::click());
+ resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, &snap.name));
+ if resp.hovered() {
+ ui.painter().rect_filled(rect, 0.0, t.hover.gamma_multiply(0.5));
+ }
+ let thumb = Rect::from_min_size(pos2(rect.left() + 28.0, rect.center().y - 14.0), vec2(28.0, 28.0));
+ widgets::checker(ui.painter(), thumb, 4.0);
+ let tex = app.snapshot_thumb(&ctx, doc_id, snap, &live);
+ ui.painter().image(tex, thumb, Rect::from_min_max(egui::Pos2::ZERO, pos2(1.0, 1.0)), Color32::WHITE);
+ ui.painter().rect_stroke(thumb, 0.0, Stroke::new(1.0, t.separator), StrokeKind::Outside);
+ match renaming.as_mut().filter(|(id, _)| *id == snap.id) {
+ Some((_, text)) => {
+ let field = Rect::from_min_max(pos2(thumb.right() + 6.0, rect.center().y - 10.0), pos2(rect.right() - 6.0, rect.center().y + 10.0));
+ let r = ui.put(field, egui::TextEdit::singleline(text));
+ if r.lost_focus() {
+ if !ui.input(|i| i.key_pressed(egui::Key::Escape)) {
+ cmd = Some(("history.renameSnapshot", json!({"snapshot": snap.id, "name": text.clone()})));
+ }
+ renaming = None;
+ } else if !r.has_focus() {
+ // Starting to rename: focus the field with the whole name selected, so typing
+ // replaces it (Photoshop).
+ r.request_focus();
+ if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), r.id) {
+ let all = egui::text::CCursorRange::two(egui::text::CCursor::new(0), egui::text::CCursor::new(text.chars().count()));
+ state.cursor.set_char_range(Some(all));
+ state.store(ui.ctx(), r.id);
+ }
+ }
+ }
+ None => {
+ ui.painter().text(pos2(thumb.right() + 8.0, rect.center().y), Align2::LEFT_CENTER, &snap.name, egui::FontId::proportional(12.0), t.text);
+ }
+ }
+ if resp.double_clicked() {
+ renaming = Some((snap.id, snap.name.clone()));
+ } else if resp.clicked() {
+ cmd = Some(("history.selectSnapshot", json!({"snapshot": snap.id})));
+ }
+ resp.context_menu(|ui| {
+ if ui.button(tl!("Rename…")).clicked() {
+ renaming = Some((snap.id, snap.name.clone()));
+ ui.close();
+ }
+ if ui.button(tl!("Delete")).clicked() {
+ cmd = Some(("history.deleteSnapshot", json!({"snapshot": snap.id})));
+ ui.close();
+ }
+ });
+ }
+ if !snaps.is_empty() {
+ let y = ui.cursor().top();
+ ui.painter().hline(ui.max_rect().x_range(), y, Stroke::new(1.0, t.separator));
+ }
+ ui.data_mut(|d| d.insert_temp(rename_id, renaming));
+ cmd
+ }
+
// ----------------------------------------------------------------------------- properties
/// Floating Properties card anchored to the canvas' top-right corner.
@@@ -4108,32 -4410,125 +4436,149 @@@ mod history_transform_tests
assert_eq!(st.history.entries().len(), steps - 1, "trash deletes the current state");
assert!(!st.history.can_redo(), "and it can't be redone");
}
+
+ /// The source column at the left of each state (#2966): a click makes that state the History
+ /// Brush's source through `history.setSource`, without stepping to it.
+ #[test]
+ fn history_source_column_sets_the_history_brush_source() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 200, "height": 150})).unwrap();
+ app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ app.run("edit.fill", json!({"color": "#00ff00"})).unwrap();
+ let mut h = Harness::builder().with_size(vec2(300.0, 400.0)).build_ui_state(|ui, app: &mut PhotocraftApp| history(app, ui), app);
+ h.run_steps(2);
+ // The column cells are the only 24 pt squares, one per state, top to bottom.
+ let mut cells: Vec<Rect> = h
+ .ctx
+ .viewport(|v| v.prev_pass.widgets.layers().flat_map(|(_, w)| w.iter()).filter(|w| w.rect.size() == vec2(24.0, 24.0)).map(|w| w.rect).collect());
+ cells.sort_by(|a, b| a.center().y.total_cmp(&b.center().y));
+ cells.dedup();
+ assert_eq!(cells.len(), 3, "Open, Fill, Fill: {cells:?}");
+ assert_eq!(h.state().session.active().unwrap().history.brush_source(), Some(0), "the default source");
+ click(&mut h, cells[1].center());
+ let st = h.state().session.active().unwrap();
+ assert_eq!(st.history.brush_source(), Some(1), "the clicked state is the source");
+ assert_eq!((st.history.entries().len(), st.history.can_redo()), (3, false), "and the document did not step back");
+ }
}
+ #[cfg(test)]
+ mod snapshot_row_tests {
+ use egui::vec2;
+ use egui_kittest::Harness;
+ use egui_kittest::kittest::Queryable;
+ use serde_json::json;
+
+ use super::*;
+
+ fn top_pixel(h: &Harness<'static, PhotocraftApp>) -> [f32; 4] {
+ h.state().session.active().unwrap().doc.layers.last().unwrap().surface().unwrap().rgba(1, 1)
+ }
+
+ fn snapshot_names(h: &Harness<'static, PhotocraftApp>) -> Vec<String> {
+ h.state().session.active().unwrap().snapshots.iter().map(|s| s.name.clone()).collect()
+ }
+
+ /// Snapshot rows (#2966) sit under the opening row: clicking one returns to it as a new
+ /// state, and its right-click menu renames or deletes it.
+ #[test]
+ fn snapshot_rows_select_rename_and_delete() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 200, "height": 150})).unwrap();
+ app.run("layer.new.layer", json!({})).unwrap();
+ app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ app.run("history.newSnapshot", json!({})).unwrap();
+ app.run("edit.fill", json!({"color": "#0000ff"})).unwrap();
+ // 60 fps steps, so two clicks a frame apart count as a double-click.
+ let mut h =
+ Harness::builder().with_size(vec2(300.0, 400.0)).with_step_dt(1.0 / 60.0).build_ui_state(|ui, app: &mut PhotocraftApp| history(app, ui), app);
+ h.run_steps(3);
+ let steps = h.state().session.active().unwrap().history.entries().len();
+ h.get_by_label("Snapshot 1").click();
+ h.run_steps(2);
+ let st = h.state().session.active().unwrap();
+ assert_eq!(st.history.entries().len(), steps + 1);
+ assert_eq!(st.history.entries().last().map(String::as_str), Some("Snapshot 1"));
+ assert_eq!(top_pixel(&h), [1.0, 0.0, 0.0, 1.0], "back to red");
+ // Right-click › Rename…: type a name, Enter.
+ h.get_by_label("Snapshot 1").click_secondary();
+ h.run_steps(2);
+ h.get_by_label("Rename…").click();
+ h.run_steps(2);
+ h.get_by_role(egui::accesskit::Role::TextInput).type_text("Lesson start");
+ h.key_press(egui::Key::Enter);
+ h.run_steps(2);
+ assert_eq!(snapshot_names(&h), ["Lesson start"]);
+ // A double-click renames too; Escape keeps the old name.
+ let steps_now = h.state().session.active().unwrap().history.entries().len();
+ // A real double-click (accessibility clicks are single clicks).
+ let p = h.get_by_label("Lesson start").rect().center();
+ let p = egui::pos2(p.x as f32, p.y as f32);
+ h.event(egui::Event::PointerMoved(p));
+ // Wait out the double-click window of the earlier clicks.
+ h.run_steps(30);
+ for pressed in [true, false, true, false] {
+ h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE });
+ h.step();
+ }
+ h.run_steps(3);
+ h.get_by_role(egui::accesskit::Role::TextInput).type_text("Oops");
+ h.key_press(egui::Key::Escape);
+ h.run_steps(2);
+ assert_eq!(snapshot_names(&h), ["Lesson start"], "Escape cancels");
+ assert_eq!(h.state().session.active().unwrap().history.entries().len(), steps_now + 1, "the click selected it once");
+ // Right-click › Delete.
+ h.get_by_label("Lesson start").click_secondary();
+ h.run_steps(2);
+ h.get_by_label("Delete").click();
+ h.run_steps(2);
+ assert!(snapshot_names(&h).is_empty());
+ assert_eq!(h.state().session.active().unwrap().history.entries().len(), steps + 2, "deleting leaves the history alone");
+ }
+
+ /// Snapshot rows scroll with the states: many snapshots in a short dock leave the footer and
+ /// its camera button in view.
+ #[test]
+ fn many_snapshots_keep_the_footer_in_a_short_panel() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 200, "height": 150})).unwrap();
+ for _ in 0..6 {
+ app.run("history.newSnapshot", json!({})).unwrap();
+ }
+ let mut h = Harness::builder().with_size(vec2(300.0, 250.0)).build_ui_state(|ui, app: &mut PhotocraftApp| history(app, ui), app);
+ PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ProMedium);
+ h.state_mut().ui.theme = crate::theme::ThemeKind::ProMedium;
+ h.run_steps(3);
+ let view = h.ctx.input(|i| i.viewport_rect());
+ let buttons: Vec<Rect> = h
+ .ctx
+ .viewport(|v| v.prev_pass.widgets.layers().flat_map(|(_, w)| w.iter()).filter(|w| w.rect.size() == vec2(26.0, 26.0)).map(|w| w.rect).collect());
+ assert_eq!(buttons.len(), 3, "{buttons:?}");
+ assert!(buttons.iter().all(|b| view.contains_rect(*b)), "footer in view: {buttons:?} in {view:?}");
+ }
+
+ /// Renaming belongs to one document: snapshot ids restart in each, so switching tabs must not
+ /// carry the edit over to the other document's snapshot with the same id.
+ #[test]
+ fn a_rename_in_progress_stays_with_its_document() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ for _ in 0..2 {
+ app.run("file.new", json!({"width": 200, "height": 150})).unwrap();
+ app.run("history.newSnapshot", json!({})).unwrap();
+ }
+ let mut h = Harness::builder().with_size(vec2(300.0, 400.0)).build_ui_state(|ui, app: &mut PhotocraftApp| history(app, ui), app);
+ h.run_steps(3);
+ h.get_by_label("Snapshot 1").click_secondary();
+ h.run_steps(2);
+ h.get_by_label("Rename…").click();
+ h.run_steps(2);
+ assert!(h.query_by_role(egui::accesskit::Role::TextInput).is_some(), "renaming in document 2");
+ h.state_mut().session.set_active(0);
+ h.run_steps(2);
+ assert!(h.query_by_role(egui::accesskit::Role::TextInput).is_none(), "not in document 1");
+ }
+ }
+
#[cfg(test)]
#[path = "layer_pct_slider_tests.rs"]
mod layer_pct_slider_tests;