mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Route Edit Undo to the most recent document or Actions change
This commit is contained in:
1 parent
c460b695d1
commit
d1985f586a
11 files changed
+59
-8
No files matched your search
@@ -16,9 +16,11 @@ mod tests;
|
||||
type Shortcuts = BTreeMap<String, String>;
|
||||
type Alternates = BTreeMap<String, Vec<String>>;
|
||||
|
||||
/// Snapshot of the definitions and shortcut bindings changed by one library edit.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct LibraryUndo {
|
||||
label: &'static str,
|
||||
document_history: Vec<(photocraft_doc::DocId, u64, bool)>,
|
||||
list: Vec<Action>,
|
||||
sets: Vec<String>,
|
||||
rows: Vec<String>,
|
||||
@@ -97,8 +99,14 @@ pub(crate) fn publish_library(s: &mut Session, list: Vec<Action>, sets: Vec<Stri
|
||||
}
|
||||
None => (shortcuts.clone(), alternates.clone()),
|
||||
};
|
||||
let document_history = s.documents().iter().map(|st| (st.doc.id, st.history.row_ids().max().unwrap_or(0), st.history.is_cleared())).collect();
|
||||
// A document edit after a library command must start a new undo step.
|
||||
for st in &mut s.docs {
|
||||
st.coalesce = None;
|
||||
}
|
||||
let undo = LibraryUndo {
|
||||
label,
|
||||
document_history,
|
||||
list: std::mem::replace(&mut s.actions.list, list),
|
||||
sets: std::mem::replace(&mut s.actions.sets, sets),
|
||||
rows: std::mem::replace(&mut s.actions.set_rows, rows),
|
||||
@@ -153,9 +161,17 @@ fn undo_state(s: &Session) -> Result<&LibraryUndo> {
|
||||
Ok(undo)
|
||||
}
|
||||
|
||||
/// Label for Edit Undo when the latest library change precedes no undoable document edit.
|
||||
pub fn undo_library_label(s: &Session) -> Option<&'static str> {
|
||||
s.actions.library_undo.as_ref()?;
|
||||
undo_state(s).ok().map(|undo| undo.label)
|
||||
let undo = undo_state(s).ok()?;
|
||||
if let Some(st) = s.active().filter(|st| st.history.can_undo()) {
|
||||
let (_, last_id, was_cleared) = undo.document_history.iter().find(|(id, _, _)| *id == st.doc.id).copied().unwrap_or((st.doc.id, 0, false));
|
||||
let current_id = st.history.row_id(st.history.past_len()).unwrap_or(0);
|
||||
if current_id > last_id || (st.history.is_cleared() && !was_cleared) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Some(undo.label)
|
||||
}
|
||||
|
||||
pub(super) fn undo_enabled(s: &Session) -> std::result::Result<(), String> {
|
||||
|
||||
@@ -157,6 +157,7 @@ fn pen_custom_undo_binding_and_menu_retract_pending_points() {
|
||||
crate::vector_ui::pen_down(&mut app, x, y, false);
|
||||
crate::vector_ui::pen_up(&mut app);
|
||||
}
|
||||
app.run("actions.reset", serde_json::json!({"confirm":true,"mode":"append"})).unwrap();
|
||||
app.session.prefs.edit(|p| p.shortcuts.insert("edit.undo".into(), "Cmd+Shift+Y".into()));
|
||||
let ctx = egui::Context::default();
|
||||
let press = |app: &mut PhotocraftApp, key: Key, modifiers: Modifiers| {
|
||||
@@ -173,8 +174,11 @@ fn pen_custom_undo_binding_and_menu_retract_pending_points() {
|
||||
press(&mut app, Key::Y, Modifiers::COMMAND | Modifiers::SHIFT);
|
||||
assert_eq!(app.ui.pen.as_ref().unwrap().knots.len(), 1);
|
||||
assert!(crate::menus::is_enabled(&app, "edit.undo"));
|
||||
let undo_label = |app: &PhotocraftApp| crate::menus::menu_items(app).into_iter().find(|item| item.id == "edit.undo").unwrap().label;
|
||||
assert_ne!(undo_label(&app), "Undo Reset Actions");
|
||||
crate::menus::invoke(&mut app, &ctx, "edit.undo", serde_json::json!({})).unwrap();
|
||||
assert!(app.ui.pen.is_none());
|
||||
assert_eq!(undo_label(&app), "Undo Reset Actions");
|
||||
}
|
||||
|
||||
/// A 200×200 document for the ⌘⇧↩ / ⌘⌥↩ tests.
|
||||
|
||||
@@ -100,7 +100,7 @@ fn invoke_early_commands(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str,
|
||||
if id == "edit.undo" && crate::vector_ui::pen_undo_last_point(app) {
|
||||
return Some(Ok(Value::Null));
|
||||
}
|
||||
if id == "edit.undo" && photocraft_engine::actions_edit_cmds::undo_library_label(&app.session).is_some() {
|
||||
if id == "edit.undo" && crate::actions::library::edit_undo_label(app).is_some() {
|
||||
return Some(crate::actions::library::undo(app));
|
||||
}
|
||||
// Image › Crop while the Crop tool has a pending frame commits that frame (#1918).
|
||||
|
||||
@@ -241,7 +241,7 @@ pub fn menu_items(app: &PhotocraftApp) -> Vec<MenuItem> {
|
||||
item.label = "Release Clipping Mask".into();
|
||||
item.enabled = is_enabled(app, &item.id);
|
||||
}
|
||||
if let Some(label) = photocraft_engine::actions_edit_cmds::undo_library_label(&app.session)
|
||||
if let Some(label) = crate::actions::library::edit_undo_label(app)
|
||||
&& let Some(item) = items.iter_mut().find(|item| item.id == "edit.undo")
|
||||
{
|
||||
item.label = label.into();
|
||||
|
||||
@@ -755,7 +755,7 @@ pub(crate) fn state_hash(app: &PhotocraftApp, input: Option<u64>) -> u64 {
|
||||
s.clipboard.is_some().hash(&mut h);
|
||||
// A running job greys the commands that would change its document.
|
||||
s.has_jobs().hash(&mut h);
|
||||
photocraft_engine::actions_edit_cmds::undo_library_label(s).hash(&mut h);
|
||||
(crate::actions::library::edit_undo_label(app), crate::vector_ui::pen_can_undo(app)).hash(&mut h);
|
||||
if let Some(d) = s.active() {
|
||||
(d.doc.id.0, d.revision, d.active_layer.map(|l| l.0), d.selected_layers.len(), d.isolated_layers.len()).hash(&mut h);
|
||||
}
|
||||
|
||||
@@ -13,10 +13,10 @@ pub fn is_enabled(app: &PhotocraftApp, id: &str) -> bool {
|
||||
return crate::symmetry_ui::can_transform(app);
|
||||
}
|
||||
// Draft Pen points can be undone even when the document has no History entries.
|
||||
if id == "edit.undo" && app.ui.pen.as_ref().is_some_and(|pen| !pen.knots.is_empty()) {
|
||||
if id == "edit.undo" && crate::vector_ui::pen_can_undo(app) {
|
||||
return true;
|
||||
}
|
||||
if id == "edit.undo" && photocraft_engine::actions_edit_cmds::undo_library_label(&app.session).is_some() {
|
||||
if id == "edit.undo" && crate::actions::library::edit_undo_label(app).is_some() {
|
||||
return true;
|
||||
}
|
||||
// Photoshop greys these for the Background layer, other layer kinds or single-layer documents.
|
||||
|
||||
@@ -237,6 +237,19 @@ pub(crate) fn cancel(app: &mut PhotocraftApp, id: u64) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The library label is shown only when Edit Undo will reach the library.
|
||||
pub(crate) fn edit_undo_label(app: &PhotocraftApp) -> Option<&'static str> {
|
||||
if crate::vector_ui::pen_can_undo(app)
|
||||
|| crate::transform_tool::is_enabled(app, "edit.undo").is_some()
|
||||
|| crate::crop_ui::is_enabled(app, "edit.undo").is_some()
|
||||
|| idle(app, "actions.undoLibrary").is_err()
|
||||
{
|
||||
return None;
|
||||
}
|
||||
photocraft_engine::actions_edit_cmds::undo_library_label(&app.session)
|
||||
}
|
||||
|
||||
/// Route an eligible Edit Undo through normal command authorization.
|
||||
pub(crate) fn undo(app: &mut PhotocraftApp) -> Result<Value, String> {
|
||||
app.run("actions.undoLibrary", json!({}))
|
||||
}
|
||||
@@ -19,6 +19,13 @@ fn library_dialog_cancel_stale_and_undo_leave_document_history_untouched() {
|
||||
crate::dialogs::confirm(&mut app, id).unwrap();
|
||||
assert!(app.session.actions.sets.is_empty());
|
||||
let ctx = egui::Context::default();
|
||||
assert_eq!(library::edit_undo_label(&app), Some("Undo Clear All Actions"));
|
||||
app.run("layer.new.layer", json!({})).unwrap();
|
||||
assert!(library::edit_undo_label(&app).is_none());
|
||||
crate::menus::invoke(&mut app, &ctx, "edit.undo", json!({})).unwrap();
|
||||
assert_eq!(app.session.active().unwrap().history.past_len(), history);
|
||||
assert!(app.session.actions.sets.is_empty(), "the newer document edit was undone first");
|
||||
assert_eq!(library::edit_undo_label(&app), Some("Undo Clear All Actions"));
|
||||
crate::menus::invoke(&mut app, &ctx, "edit.undo", json!({})).unwrap();
|
||||
assert_eq!(app.session.actions.sets, ["Keep", "Newer"]);
|
||||
assert_eq!(app.session.active().unwrap().history.past_len(), history);
|
||||
|
||||
@@ -522,6 +522,7 @@ fn a_click_on_the_frame_or_a_handle_never_snaps_it() {
|
||||
#[test]
|
||||
fn edit_undo_steps_the_crop_box_not_the_document() {
|
||||
let mut app = app(SampleType::U8);
|
||||
app.run("actions.reset", json!({"confirm":true,"mode":"append"})).unwrap();
|
||||
app.ui.crop_rect = Some([40.0, 30.0, 100.0, 70.0]);
|
||||
app.crop.default_frame = false;
|
||||
let before = app.ui.crop_rect.unwrap();
|
||||
@@ -529,7 +530,8 @@ fn edit_undo_steps_the_crop_box_not_the_document() {
|
||||
drag(&mut app, &[[70.0, 50.0], [90.0, 50.0]], NONE); // move the frame right 20
|
||||
let moved = app.ui.crop_rect.unwrap();
|
||||
assert!((moved[0] - 60.0).abs() < 1e-3, "{moved:?}");
|
||||
app.run("edit.undo", json!({})).unwrap();
|
||||
crate::menus::invoke(&mut app, &egui::Context::default(), "edit.undo", json!({})).unwrap();
|
||||
assert_eq!(app.session.actions.set_rows.len(), 1, "the library edit remains available");
|
||||
assert_eq!(app.ui.crop_rect, Some(before), "undo restores the box");
|
||||
assert_eq!(app.session.active().unwrap().history.past_len(), doc_steps, "the document is untouched");
|
||||
app.run("edit.redo", json!({})).unwrap();
|
||||
|
||||
@@ -297,6 +297,11 @@ use path_ops::{selected_document_geometry, targeted_vector_mask_id};
|
||||
pub(crate) use pen::{finish_pen_if_left, freeform_finish_owned};
|
||||
use shapes::{fill_param, stroke_param};
|
||||
|
||||
/// Whether Edit Undo currently belongs to a live Pen draft.
|
||||
pub(crate) fn pen_can_undo(app: &PhotocraftApp) -> bool {
|
||||
app.ui.pen.as_ref().is_some_and(|pen| pen.owner.as_ref().is_some_and(|owner| owner.valid(app)) && (!pen.knots.is_empty() || pen.resumes.is_some()))
|
||||
}
|
||||
|
||||
/// Commit a valid source-owned draft on a tab switch; otherwise cancel with an actionable diagnostic.
|
||||
pub(crate) fn cancel_stale_pen(app: &mut PhotocraftApp) -> bool {
|
||||
let Some(pen) = app.ui.pen.as_ref() else { return false };
|
||||
|
||||
@@ -117,6 +117,7 @@ fn free_transform_starts_where_the_selected_pixels_were_moved() {
|
||||
#[test]
|
||||
fn undo_steps_back_through_the_transform_not_the_document() {
|
||||
let mut h = harness();
|
||||
h.state_mut().run("actions.reset", json!({"confirm":true,"mode":"append"})).unwrap();
|
||||
let steps = history(&h);
|
||||
begin(&mut h);
|
||||
let q0 = quad(&h);
|
||||
@@ -153,6 +154,9 @@ fn undo_steps_back_through_the_transform_not_the_document() {
|
||||
// Outside the transform, ⌘Z is the document's Undo again.
|
||||
key(&mut h, Modifiers::COMMAND, Key::Z);
|
||||
assert_eq!(width(&h), 100, "the committed transform is undone");
|
||||
assert_eq!(h.state().session.actions.set_rows.len(), 1);
|
||||
key(&mut h, Modifiers::COMMAND, Key::Z);
|
||||
assert!(h.state().session.actions.set_rows.is_empty(), "the preceding library edit is next");
|
||||
}
|
||||
|
||||
/// `engine.execute` over the control channel (and MCP, which goes through it) as a programmatic
|
||||
|
||||
Reference in new issue
Block a user