mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Document action history ownership and continuation contracts
This commit is contained in:
1 parent
fb34385723
commit
838dca6f5d
12 files changed
+51
No files matched your search
@@ -580,6 +580,7 @@ pub fn begin_playback(s: &mut Session, action: &Action) -> Result<Option<(usize,
|
||||
Ok(recording)
|
||||
}
|
||||
|
||||
/// Unwind the playback stack and finish grouping only at the outer boundary, then restore suspended recording.
|
||||
pub fn finish_playback(s: &mut Session, recording: Option<(usize, usize)>, action: &Action, from: usize) {
|
||||
s.actions.playback_stack.pop();
|
||||
s.actions.playing = s.actions.playing.saturating_sub(1);
|
||||
|
||||
@@ -179,10 +179,12 @@ pub(crate) fn begin_document_history(s: &mut Session, name: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Finalize the per-document scope explicitly owned by Batch, including a completed prefix on failure.
|
||||
pub(crate) fn finish_document_history(s: &mut Session) {
|
||||
finish(s);
|
||||
}
|
||||
|
||||
/// Finish an ordinary outer playback without closing a scope owned by the surrounding Batch.
|
||||
pub(super) fn finish_playback(s: &mut Session) {
|
||||
if s.action_history.as_ref().is_none_or(|group| !group.external) {
|
||||
finish(s);
|
||||
@@ -326,6 +328,7 @@ mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
/// Keep the policy fixed until every clone of a paused continuation releases its lease, in either history mode.
|
||||
fn history_policy_stays_fixed_until_all_paused_continuations_are_released() {
|
||||
for single in [false, true] {
|
||||
let mut s = Session::new();
|
||||
@@ -346,6 +349,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Generic preference set/reset routes must preserve the same paused-policy guard as actions.setHistory.
|
||||
fn generic_preference_updates_cannot_change_a_paused_history_policy() {
|
||||
for single in [false, true] {
|
||||
let mut s = Session::new();
|
||||
@@ -377,6 +381,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Rebinding a dialog retains its first edit while respecting an accepted Clear History boundary.
|
||||
fn idle_dialog_rebind_keeps_first_edit_and_clear_boundaries() {
|
||||
for single in [false, true] {
|
||||
for clear in [false, true] {
|
||||
@@ -413,6 +418,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// A dialog continuation cannot adopt another caller's active grouping scope.
|
||||
fn idle_dialog_rebind_never_takes_an_unrelated_parent_scope() {
|
||||
for single in [false, true] {
|
||||
let mut s = Session::new();
|
||||
|
||||
@@ -544,6 +544,7 @@ fn record_keeps_only_replayable_steps() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Playback on a different document must match native edits at every depth, independent of history grouping.
|
||||
fn play_on_another_document_matches_pixels_at_each_depth() {
|
||||
for depth in [8, 16, 32] {
|
||||
let mut s = Session::new();
|
||||
@@ -1240,6 +1241,7 @@ fn button_color_is_one_of_the_photoshop_swatches() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// An arrangement that cannot move the layer must not create a grouped history entry.
|
||||
fn arrange_that_cannot_move_is_a_noop_in_playback() {
|
||||
// #8453: imported actions make an adjustment layer, then Bring it to Front, where it already is.
|
||||
let mut s = Session::new();
|
||||
|
||||
@@ -17,6 +17,7 @@ fn clip_bytes(s: &Session) -> usize {
|
||||
s.clipboard.as_ref().map_or(0, |c| c.surface.tiles().map(|(_, t)| t.len()).sum())
|
||||
}
|
||||
|
||||
/// Apply the requested irreversible purge and record its boundary so action finalization cannot resurrect undo states.
|
||||
pub(super) fn purge(s: &mut Session, what: &str) -> Result<Value> {
|
||||
let mut freed = 0usize;
|
||||
let mut items: Vec<&str> = Vec::new();
|
||||
|
||||
@@ -179,6 +179,7 @@ fn same_format_takes_the_file_names_extension() {
|
||||
assert_eq!(same_format("/in/.hidden"), "png");
|
||||
}
|
||||
|
||||
/// Run one source through its action and output pipeline while finalizing only that document's owned history scope.
|
||||
fn process_outputs<'a>(
|
||||
s: &Session,
|
||||
outputs: impl IntoIterator<Item = (&'a str, String)>,
|
||||
@@ -483,6 +484,7 @@ pub(crate) fn batch_keeps_step(id: &str) -> bool {
|
||||
(batch_save_point(id) || !crate::actions_cmds::shell_save_command(id)) && !crate::actions_cmds::shell_view_command(id)
|
||||
}
|
||||
|
||||
/// Execute the selected source set with per-document action scopes and restore the caller's library on every exit.
|
||||
pub(super) fn batch(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "file.automate.batch";
|
||||
let action_name = s
|
||||
@@ -887,6 +889,7 @@ mod action_history_tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Batch over open documents must preserve an enclosing action scope and finish its own document scopes.
|
||||
fn opened_document_batch_keeps_top_level_and_parent_history_boundaries() {
|
||||
for single in [false, true] {
|
||||
for parent in [false, true] {
|
||||
@@ -939,6 +942,7 @@ mod action_history_tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// A Batch failure restores the library and leaves the caller's active history scope owned by the caller.
|
||||
fn opened_document_batch_error_restores_library_without_finishing_parent() {
|
||||
for parent in [false, true] {
|
||||
let mut s = Session::new();
|
||||
@@ -989,6 +993,7 @@ mod action_history_tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Cancellation after a completed edit retains an undoable prefix without leaving a Batch-owned scope open.
|
||||
fn cancelled_folder_batch_finalizes_its_completed_prefix() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let image = photocraft_codecs::Image::from_u8(4, 4, photocraft_codecs::ChannelLayout::Rgb, vec![255; 48]).unwrap();
|
||||
|
||||
@@ -56,6 +56,7 @@ fn prefs_set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(Value::Object(out))
|
||||
}
|
||||
|
||||
/// Validate policy-changing resets before mutating either preferences or color settings.
|
||||
fn prefs_reset(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let path = p.get("path").or_else(|| p.get("section")).and_then(Value::as_str);
|
||||
if !path.is_some_and(|path| path == "colorSettings" || path.starts_with("colorSettings.")) {
|
||||
|
||||
@@ -53,6 +53,7 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
/// Restore the next document state and its selection/visibility bookkeeping through the ordinary history path.
|
||||
pub fn redo(&mut self) -> bool {
|
||||
if self.active_job().is_some() {
|
||||
return false;
|
||||
|
||||
@@ -127,6 +127,7 @@ impl Default for History {
|
||||
}
|
||||
|
||||
impl History {
|
||||
/// Initialize history with the supplied document as its starting state and no active action visibility capture.
|
||||
pub fn new(max_states: usize) -> Self {
|
||||
Self {
|
||||
undo: VecDeque::new(),
|
||||
@@ -250,6 +251,7 @@ impl History {
|
||||
self.visibility_capture = Some(Arc::new(prefix.to_vec()));
|
||||
}
|
||||
|
||||
/// Return the visibility prefix accumulated for the current grouped action, if capture is active.
|
||||
pub fn captured_visibility(&self) -> Option<&[LayerId]> {
|
||||
self.visibility_capture.as_deref().map(Vec::as_slice)
|
||||
}
|
||||
@@ -627,6 +629,7 @@ impl History {
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply an accepted Clear to an empty captured history as well as populated histories, preserving its boundary.
|
||||
fn clear_states_including_empty(&mut self) {
|
||||
let undo = std::mem::take(&mut self.undo);
|
||||
let redo = std::mem::take(&mut self.redo);
|
||||
|
||||
@@ -1158,6 +1158,7 @@ fn transferred_tiles_cannot_make_the_budget_check_underestimate_current() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Clear History changes the state list without changing pixels and remains undoable where the operation permits it.
|
||||
fn clear_states_keeps_the_document_and_undo_brings_the_list_back() {
|
||||
let mut h = History::new(10);
|
||||
let mut cur = Arc::new(base());
|
||||
@@ -1182,6 +1183,7 @@ fn clear_states_keeps_the_document_and_undo_brings_the_list_back() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Finalizing a grouped action must not discard an accepted Clear merely because its captured history was empty.
|
||||
fn an_action_preserves_an_accepted_clear_over_an_empty_capture() {
|
||||
let mut captured = History::new(50);
|
||||
assert!(!captured.clear_states(), "ordinary empty Clear still refuses");
|
||||
|
||||
@@ -888,6 +888,7 @@ pub(crate) struct PausedPlaybacks {
|
||||
}
|
||||
|
||||
impl PausedPlaybacks {
|
||||
/// Allocate an opaque token retaining the action, resume position and captured authorization gate.
|
||||
fn pause(&mut self, action: String, from: usize, gate: Option<Gate>) -> u64 {
|
||||
self.last = self.last.wrapping_add(1);
|
||||
self.paused
|
||||
@@ -895,6 +896,7 @@ impl PausedPlaybacks {
|
||||
self.last
|
||||
}
|
||||
|
||||
/// Consume the continuation named by a numeric dialog token; malformed or expired tokens identify no playback.
|
||||
fn take(&mut self, token: &Value) -> Option<PausedPlayback> {
|
||||
token.as_u64().and_then(|token| self.paused.remove(&token))
|
||||
}
|
||||
|
||||
@@ -100,6 +100,7 @@ fn single_history_dialog_refuses_intervening_edit_or_document_switch() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Prepare a real paused spelling step with a known document and history boundary, under either grouping policy.
|
||||
fn paused_history_spelling(single: bool, gate: Option<Gate>) -> (PhotocraftApp, std::sync::Arc<photocraft_doc::Document>, usize) {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width":300,"height":100})).unwrap();
|
||||
@@ -124,6 +125,7 @@ fn paused_history_spelling(single: bool, gate: Option<Gate>) -> (PhotocraftApp,
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Repeated corrections share one owned history entry; a rejected correction cannot lose earlier admitted edits.
|
||||
fn paused_spelling_repeated_changes_and_errors_keep_one_owned_history() {
|
||||
for single in [false, true] {
|
||||
let (mut app, before, past) = paused_history_spelling(single, None);
|
||||
@@ -177,6 +179,7 @@ fn deny_second_spelling_change(id: &str, params: &Value) -> photocraft_engine::R
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Reject a spelling continuation after document/history drift or captured-policy denial before any correction.
|
||||
fn paused_spelling_rejects_external_edit_switch_and_original_gate_before_change() {
|
||||
for single in [false, true] {
|
||||
for scenario in ["edit", "switch", "gate"] {
|
||||
@@ -2243,6 +2246,7 @@ fn deny_blur(id: &str, _: &Value) -> photocraft_engine::Result<()> {
|
||||
if id == "filter.blur.gaussianBlur" { Err(photocraft_engine::EngineError::Other("blur denied".into())) } else { Ok(()) }
|
||||
}
|
||||
|
||||
/// Permit the recorded small blur but reject an edited radius, so admission is tested against final dialog parameters.
|
||||
fn deny_wide_blur(id: &str, params: &Value) -> photocraft_engine::Result<()> {
|
||||
if id == "filter.blur.gaussianBlur" && params["radius"].as_f64().is_some_and(|radius| radius > 1.0) {
|
||||
Err(photocraft_engine::EngineError::Other("edited blur radius denied".into()))
|
||||
@@ -2251,6 +2255,7 @@ fn deny_wide_blur(id: &str, params: &Value) -> photocraft_engine::Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a two-tone raster whose blur changes pixels, with a real action step configured to open its dialog.
|
||||
fn gated_blur_fixture(single: bool, background: bool) -> PhotocraftApp {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 16, "height": 16, "background": "#000000"})).unwrap();
|
||||
@@ -2265,6 +2270,7 @@ fn gated_blur_fixture(single: bool, background: bool) -> PhotocraftApp {
|
||||
app
|
||||
}
|
||||
|
||||
/// Simulate a temporary control gate: capture it at the pause and release the session gate before later confirmation.
|
||||
fn pause_gated_blur(app: &mut PhotocraftApp, gate: Option<Gate>) -> u64 {
|
||||
app.session.authorize = gate;
|
||||
let paused = play(app, &json!({"action": "Gated blur"})).unwrap();
|
||||
@@ -2273,6 +2279,7 @@ fn pause_gated_blur(app: &mut PhotocraftApp, gate: Option<Gate>) -> u64 {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Denied edited parameters must change neither pixels nor history and must admit no background worker.
|
||||
fn a_paused_dialog_authorizes_edited_params_before_history_or_worker_admission() {
|
||||
for (single, background) in [(true, false), (true, true), (false, false)] {
|
||||
let mut app = gated_blur_fixture(single, background);
|
||||
@@ -2297,6 +2304,7 @@ fn a_paused_dialog_authorizes_edited_params_before_history_or_worker_admission()
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Allowed captured edits and unrelated manual dialogs retain their normal synchronous and worker behavior.
|
||||
fn an_allowed_paused_edit_and_an_ordinary_manual_dialog_still_run() {
|
||||
for (paused, background) in [(true, false), (true, true), (false, false)] {
|
||||
let mut app = gated_blur_fixture(true, background);
|
||||
@@ -2318,6 +2326,7 @@ fn an_allowed_paused_edit_and_an_ordinary_manual_dialog_still_run() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// A paused action's captured permission cannot bypass a stricter live policy on confirmation.
|
||||
fn a_stricter_current_gate_still_denies_the_captured_dialog_edit() {
|
||||
for service in [false, true] {
|
||||
let mut app = gated_blur_fixture(true, true);
|
||||
@@ -2342,6 +2351,7 @@ fn a_stricter_current_gate_still_denies_the_captured_dialog_edit() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Spelling corrections consult live Session and automation-service policies before resuming the owned history.
|
||||
fn paused_spelling_checks_the_live_gate_before_resuming_history() {
|
||||
fn deny_correction(id: &str, p: &Value) -> photocraft_engine::Result<()> {
|
||||
if id == "edit.checkSpelling" && p["action"] == "changeAll" { Err(photocraft_engine::EngineError::Other("live spelling gate".into())) } else { Ok(()) }
|
||||
@@ -2377,6 +2387,7 @@ fn paused_spelling_checks_the_live_gate_before_resuming_history() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// A changed authority context cancels both direct and nested tails while keeping the admitted prefix undoable.
|
||||
fn a_changed_live_gate_cannot_be_replaced_for_the_remaining_action() {
|
||||
fn deny_tail(id: &str, _: &Value) -> photocraft_engine::Result<()> {
|
||||
if id == "layer.renameLayer" { Err(photocraft_engine::EngineError::Other("live tail denied".into())) } else { Ok(()) }
|
||||
@@ -2409,6 +2420,7 @@ fn a_changed_live_gate_cannot_be_replaced_for_the_remaining_action() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Distinct captured/live gates fail before applying the dialog or starting a worker, even if the edit is allowed.
|
||||
fn a_changed_context_is_rejected_before_dialog_or_worker_admission() {
|
||||
fn allow_all(_: &str, _: &Value) -> photocraft_engine::Result<()> {
|
||||
Ok(())
|
||||
@@ -2427,6 +2439,7 @@ fn a_changed_context_is_rejected_before_dialog_or_worker_admission() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// The same gate, a released temporary gate and a newly installed gate permit supported continuation paths.
|
||||
fn unchanged_and_released_gates_allow_the_action_tail() {
|
||||
for scenario in ["released", "same", "new"] {
|
||||
let mut app = gated_blur_fixture(true, false);
|
||||
@@ -2443,6 +2456,8 @@ fn unchanged_and_released_gates_allow_the_action_tail() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Distinct configured policies permit manual confirmation but cannot be combined for automated dispatch.
|
||||
/// The identical-gate automated control proves the supported composed-policy path still completes and undoes once.
|
||||
fn configured_gates_require_one_composed_policy_for_automated_resume() {
|
||||
fn deny_file_open(id: &str, _: &Value) -> photocraft_engine::Result<()> {
|
||||
if id == "file.open" { Err(photocraft_engine::EngineError::Other("file open denied".into())) } else { Ok(()) }
|
||||
@@ -2486,6 +2501,7 @@ fn configured_gates_require_one_composed_policy_for_automated_resume() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// A policy change after the final spelling step must not discard completed edits when no tail remains to dispatch.
|
||||
fn a_changed_gate_after_the_last_spelling_step_keeps_completed_edits() {
|
||||
let (mut app, before, past) = paused_history_spelling(true, Some(deny_wide_blur));
|
||||
app.session.actions.list[0].steps.truncate(2);
|
||||
@@ -2501,6 +2517,8 @@ fn a_changed_gate_after_the_last_spelling_step_keeps_completed_edits() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Implicit layer-owned adjustments reject a receiver switch; explicit receivers change only their intended surface.
|
||||
/// An omitted target separately inherits the mask captured when its dialog opens.
|
||||
fn paused_adjustments_bind_implicit_pixel_and_mask_receivers() {
|
||||
for targeting in ["implicit", "null", "explicit"] {
|
||||
for mask in [false, true] {
|
||||
@@ -2568,6 +2586,7 @@ fn paused_adjustments_bind_implicit_pixel_and_mask_receivers() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Document-owned alpha channels remain valid targets when the active layer changes during the pause.
|
||||
fn a_document_channel_adjustment_survives_an_active_layer_change() {
|
||||
let mut app = gated_blur_fixture(true, false);
|
||||
let a = app.session.active().unwrap().active_layer.unwrap();
|
||||
@@ -2586,6 +2605,7 @@ fn a_document_channel_adjustment_survives_an_active_layer_change() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// A public Session layer switch invalidates an implicit receiver while explicit-layer playback remains usable.
|
||||
fn an_implicit_paused_filter_refuses_a_public_api_receiver_change() {
|
||||
for single in [false, true] {
|
||||
for explicit in [false, true] {
|
||||
@@ -2620,6 +2640,7 @@ fn an_implicit_paused_filter_refuses_a_public_api_receiver_change() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Changes to other selected layers do not invalidate the unchanged active receiver captured by a filter dialog.
|
||||
fn an_implicit_filter_keeps_its_receiver_when_only_other_selected_layers_change() {
|
||||
let mut app = gated_blur_fixture(true, false);
|
||||
let a = app.session.active().unwrap().active_layer.unwrap();
|
||||
@@ -2645,6 +2666,7 @@ fn deny_displace_file(id: &str, params: &Value) -> photocraft_engine::Result<()>
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Authorize a deferred displacement-map path before file access, worker admission or continuation history changes.
|
||||
fn a_deferred_displace_map_is_authorized_before_reading_or_resuming_history() {
|
||||
let mut app = recording_with_displace_child();
|
||||
app.background_jobs = true;
|
||||
|
||||
@@ -766,6 +766,7 @@ pub fn glyphs_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
|
||||
|
||||
// ------------------------------------------------------------------ spelling
|
||||
|
||||
/// Refresh the opening layer when checking one layer; an absent opening receiver must not adopt the newly active layer.
|
||||
fn refresh_spelling(app: &mut PhotocraftApp) {
|
||||
let Some(d) = app.ui.type_panels.spell.clone() else { return };
|
||||
let mut p = json!({ "action": "list", "allLayers": d.all_layers, "ignore": d.ignore, "suggestions": 8 });
|
||||
@@ -817,6 +818,7 @@ pub fn close_spelling(app: &mut PhotocraftApp) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Bind single-layer Change All to the listed item; all-layer corrections intentionally omit a layer restriction.
|
||||
fn spelling_change_all_params(item: &Value, word: &str, replace: &str, all_layers: bool) -> Value {
|
||||
let mut params = json!({"action":"changeAll", "word":word, "replace":replace, "allLayers":all_layers});
|
||||
if !all_layers {
|
||||
@@ -825,6 +827,7 @@ fn spelling_change_all_params(item: &Value, word: &str, replace: &str, all_layer
|
||||
params
|
||||
}
|
||||
|
||||
/// Render spelling navigation and corrections using the retained opening receiver and paused-action continuation.
|
||||
fn spelling_window(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
let t = Tokens::get(ctx);
|
||||
let Some(d) = app.ui.type_panels.spell.clone() else { return };
|
||||
@@ -995,6 +998,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Single-layer Change All edits the listed text layer even after the host selects another layer.
|
||||
fn change_all_keeps_the_listed_layer_after_a_session_selection_change() {
|
||||
for all_layers in [false, true] {
|
||||
let mut app = app_with_text("Teh cat");
|
||||
@@ -1023,6 +1027,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Refreshing and toggling Check All Layers must retain the original single-layer receiver, including the absent-layer case.
|
||||
fn spelling_refresh_and_all_layers_toggles_keep_the_opening_layer() {
|
||||
let mut app = app_with_text("Teh cat");
|
||||
let original = app.session.active().unwrap().active_layer.unwrap();
|
||||
|
||||
Reference in new issue
Block a user