mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #14083 from ljkx/fix/14029-new-document-history-state
New Document: the first History state is New, not Open (#14029)
This commit is contained in:
10 files changed
+116
-13
No files matched your search
@@ -127,6 +127,8 @@ pub mod mask_view_cmds;
|
||||
pub(super) mod migrate_cmds;
|
||||
pub mod mode_cmds;
|
||||
pub mod multichannel_cmds;
|
||||
#[cfg(test)]
|
||||
mod new_document_history_name_tests;
|
||||
pub mod notes_cmds;
|
||||
#[cfg(test)]
|
||||
mod notes_stored_values_tests;
|
||||
@@ -333,6 +335,10 @@ fn build() -> Vec<CommandSpec> {
|
||||
doc.icc_profile = Some(prof.to_bytes());
|
||||
}
|
||||
let i = s.add_document(doc, None);
|
||||
// A new document's first History state is "New"; opened files keep "Open" (#14029).
|
||||
if let Some(st) = s.active_mut() {
|
||||
st.history.set_current_label("New");
|
||||
}
|
||||
Ok(json!({ "document": i }))
|
||||
}
|
||||
),
|
||||
|
||||
@@ -372,7 +372,7 @@ mod tests {
|
||||
let mut s = session();
|
||||
let first = s.active().unwrap().history.state(0).unwrap();
|
||||
s.execute("history.deleteState", json!({"state": 1})).unwrap();
|
||||
assert_eq!(entries(&s), vec!["Open".to_string()]);
|
||||
assert_eq!(entries(&s), vec!["New".to_string()]);
|
||||
assert!(std::sync::Arc::ptr_eq(&s.active().unwrap().doc, &first));
|
||||
assert!(!s.active().unwrap().history.can_redo());
|
||||
// Redo states after an undo go too.
|
||||
|
||||
@@ -145,9 +145,9 @@ mod tests {
|
||||
while s.is_enabled("history.stepBackward") {
|
||||
s.execute("history.stepBackward", json!({})).unwrap();
|
||||
}
|
||||
assert_eq!(entries(&s), ["Open"]);
|
||||
assert_eq!(entries(&s), ["New"]);
|
||||
assert!(s.execute("history.stepBackward", json!({})).is_err());
|
||||
assert_eq!(entries(&s), ["Open"], "a refused step changes nothing");
|
||||
assert_eq!(entries(&s), ["New"], "a refused step changes nothing");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -241,7 +241,7 @@ mod tests {
|
||||
for p in [json!({}), json!(null), json!([1]), json!({"state": -1}), json!("x")] {
|
||||
assert!(s.execute(id, p.clone()).is_err(), "{id} {p}");
|
||||
}
|
||||
assert_eq!(entries(&s), ["Open"]);
|
||||
assert_eq!(entries(&s), ["New"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -203,7 +203,7 @@ mod tests {
|
||||
#[test]
|
||||
fn default_source_is_the_oldest_state() {
|
||||
let mut s = session();
|
||||
assert_eq!(crate::inspect::document(s.active().unwrap())["historySource"], json!({"state": 0, "label": "Open"}));
|
||||
assert_eq!(crate::inspect::document(s.active().unwrap())["historySource"], json!({"state": 0, "label": "New"}));
|
||||
brush(&mut s, json!({}));
|
||||
assert!(close(rgb(&s), [1.0, 1.0, 1.0]), "{:?}", rgb(&s));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
//! #14029: a document made with File › New starts with the History state "New", as in the
|
||||
//! reference application; a file that opens keeps "Open".
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::Session;
|
||||
|
||||
/// The History states and snapshots of the active document, as `document.history` and
|
||||
/// `document.snapshots` report them.
|
||||
fn history(s: &Session) -> (Value, Value) {
|
||||
let d = crate::inspect::document(s.active().unwrap());
|
||||
(d["history"].clone(), d["snapshots"].clone())
|
||||
}
|
||||
|
||||
/// Open an 8 × 8 PNG from memory as `name`, the way File › Open admits a file.
|
||||
fn open_png(s: &mut Session, name: &str) {
|
||||
let img = photocraft_codecs::Image::from_u8(8, 8, photocraft_codecs::ChannelLayout::Rgb, vec![128; 8 * 8 * 3]).unwrap();
|
||||
let bytes = photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &Default::default()).unwrap();
|
||||
crate::file_cmds::open_bytes_as(s, name, &bytes, None, None).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_new_document_dialogs_document_has_the_state_new_under_its_snapshot() {
|
||||
// The measured case: White, 13 x 400 px at 1 ppi, created as Untitled-1.
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 13, "height": 400, "resolution": 1, "background": "white", "name": "Untitled-1"})).unwrap();
|
||||
assert_eq!(history(&s), (json!(["New"]), json!([{"id": 0, "name": "Untitled-1"}])));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_artboard_document_has_the_state_new_too() {
|
||||
// The measured case: Artboards on, 11 x 8 in at 300 ppi, White.
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 3300, "height": 2400, "resolution": 300, "artboard": true, "name": "Untitled-1"})).unwrap();
|
||||
assert_eq!(history(&s), (json!(["New"]), json!([{"id": 0, "name": "Untitled-1"}])));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_background_and_mode_of_file_new_gives_new() {
|
||||
for p in [
|
||||
json!({"width": 8, "height": 8, "background": "transparent"}),
|
||||
json!({"width": 8, "height": 8, "background": "backgroundColor"}),
|
||||
json!({"width": 8, "height": 8, "background": "black", "mode": "cmyk", "depth": 16}),
|
||||
json!({"width": 8, "height": 8, "background": "#336699", "mode": "lab", "depth": 32}),
|
||||
] {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", p.clone()).unwrap();
|
||||
assert_eq!(history(&s).0, json!(["New"]), "{p}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_first_state_stays_new_through_undo_and_redo_and_is_the_default_brush_source() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 8, "height": 8})).unwrap();
|
||||
let source = |s: &Session| crate::inspect::document(s.active().unwrap())["historySource"].clone();
|
||||
assert_eq!(source(&s), json!({"state": 0, "label": "New"}));
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
assert_eq!(history(&s).0, json!(["New", "New Layer"]));
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
assert_eq!(history(&s).0, json!(["New"]));
|
||||
s.execute("edit.redo", json!({})).unwrap();
|
||||
assert_eq!(history(&s).0, json!(["New", "New Layer"]));
|
||||
assert_eq!(source(&s), json!({"state": 0, "label": "New"}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_opened_file_keeps_open_next_to_a_new_document() {
|
||||
let mut s = Session::new();
|
||||
open_png(&mut s, "before.png");
|
||||
s.execute("file.new", json!({"width": 8, "height": 8, "name": "Untitled-1"})).unwrap();
|
||||
open_png(&mut s, "after.png");
|
||||
let states: Vec<(String, Vec<String>)> = s.documents().iter().map(|d| (d.doc.name.clone(), d.history.entries())).collect();
|
||||
assert_eq!(
|
||||
states,
|
||||
[
|
||||
("before.png".to_string(), vec!["Open".to_string()]),
|
||||
("Untitled-1".to_string(), vec!["New".to_string()]),
|
||||
("after.png".to_string(), vec!["Open".to_string()]),
|
||||
]
|
||||
);
|
||||
}
|
||||
@@ -668,7 +668,7 @@ mod history_transform_tests {
|
||||
.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!(cells.len(), 3, "New, 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();
|
||||
@@ -896,7 +896,7 @@ mod snapshot_row_tests {
|
||||
let st = h.state().session.active().unwrap();
|
||||
(st.history.past_len(), st.history.redo_labels().count())
|
||||
};
|
||||
assert_eq!(history(&h), (0, 3), "Open current, New Layer, Fill, Fill dimmed");
|
||||
assert_eq!(history(&h), (0, 3), "New current, New Layer, Fill, Fill dimmed");
|
||||
assert!(h.state().session.is_enabled("history.deleteState"), "the engine still offers a later state to delete");
|
||||
let enabled = |h: &Harness<'static, PhotocraftApp>| {
|
||||
let p = trash(h);
|
||||
|
||||
@@ -198,7 +198,8 @@ pub(crate) fn step_icon(step: &str) -> &'static str {
|
||||
// The selections the Patch tool draws (select.lasso's historyName).
|
||||
"Patch Tool" => "square-dashed",
|
||||
s if s.contains("Fill") => "paint-bucket",
|
||||
"Open" => "image",
|
||||
// The opening state: "Open" for a file, "New" for a document from File › New (#14029).
|
||||
"Open" | "New" => "image",
|
||||
_ => "sliders-horizontal",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Open, then New Layer, Fill, New Layer, Fill.
|
||||
/// New (the opening state of File › New), then New Layer, Fill, New Layer, Fill.
|
||||
fn harness() -> Harness<'static, PhotocraftApp> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 64, "height": 48})).unwrap();
|
||||
@@ -59,6 +59,19 @@ fn clicking_a_state_jumps_there_with_one_command() {
|
||||
assert_eq!(state(&h), (4, 0, rev + 2));
|
||||
}
|
||||
|
||||
/// #14029: a document from File › New lists its opening state as "New" (an opened file's reads
|
||||
/// "Open"), at the top of the states, with the opening state's icon.
|
||||
#[test]
|
||||
fn a_new_documents_opening_state_reads_new_with_the_opening_icon() {
|
||||
let h = harness();
|
||||
assert!(h.query_by_label("Open").is_none(), "no state reads Open");
|
||||
let new = h.get_by_label("New").rect();
|
||||
let first_step = h.get_all_by_label("New Layer").next().expect("the first New Layer row").rect();
|
||||
assert!(new.bottom() <= first_step.top() + 0.5, "New is the row above the first step: {new:?} {first_step:?}");
|
||||
assert_eq!(history_step_icon("New"), "image");
|
||||
assert_eq!(history_step_icon("New"), history_step_icon("Open"));
|
||||
}
|
||||
|
||||
/// The ghost label that follows a History row being dragged to the trash (the text painted at
|
||||
/// the pointer, `layer_transfer::ghost`), if one is drawn.
|
||||
fn ghost_label(h: &Harness<'_, PhotocraftApp>, p: egui::Pos2) -> Option<String> {
|
||||
@@ -77,7 +90,7 @@ fn dragging_a_later_state_shows_its_label_at_the_pointer() {
|
||||
h.state_mut().run("history.options", json!({"allowNonLinearHistory": true})).unwrap();
|
||||
h.state_mut().run("edit.undo", json!({})).unwrap();
|
||||
h.run_steps(2);
|
||||
// Rows: Open, New Layer, Fill, New Layer (current), the dimmed Fill.
|
||||
// Rows: New, New Layer, Fill, New Layer (current), the dimmed Fill.
|
||||
for (label, row) in [("New Layer", 1), ("Fill", 1)] {
|
||||
let from = h.get_all_by_label(label).nth(row).expect("the row").rect().center();
|
||||
let to = from + vec2(-60.0, 40.0);
|
||||
@@ -122,7 +135,7 @@ fn new_history_steps_scroll_the_current_state_into_view() {
|
||||
assert!(view.contains_rect(row), "the current history row must scroll into view: row {row:?} view {view:?}",);
|
||||
}
|
||||
|
||||
/// Open plus 18 New Layer states in a dock that shows only a few rows.
|
||||
/// New plus 18 New Layer states in a dock that shows only a few rows.
|
||||
fn long_harness() -> Harness<'static, PhotocraftApp> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 64, "height": 48})).unwrap();
|
||||
|
||||
@@ -1115,7 +1115,8 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo
|
||||
let entries = st.history.entries();
|
||||
let name =
|
||||
if idx < entries.len() { entries.get(idx).cloned() } else { st.history.redo_labels().nth(idx - entries.len()).map(str::to_string) }?;
|
||||
if idx == 0 && name == "Open" {
|
||||
// The opening state is "Open" for a file and "New" for a new document (#14029).
|
||||
if idx == 0 && matches!(name.as_str(), "Open" | "New") {
|
||||
Some(tl!("Paints from the document's opening state").to_string())
|
||||
} else {
|
||||
Some(crate::i18n::fmt(tl!("Source: {name}"), &[("name", &name)]))
|
||||
|
||||
@@ -156,7 +156,7 @@ fn click_creates_placeholder_selected_and_typing_replaces_it() {
|
||||
assert_eq!(layer_text(&app), "Héllo world");
|
||||
// Creating and typing share the session key: one step after the initial snapshot.
|
||||
let steps = app.session.active().unwrap().history.entries();
|
||||
assert_eq!(steps, ["Open", "New Type Layer"]);
|
||||
assert_eq!(steps, ["New", "New Type Layer"]);
|
||||
commit(&mut app);
|
||||
let doc = &app.session.active().unwrap().doc;
|
||||
assert_eq!(doc.layers.last().unwrap().name, "Héllo world");
|
||||
|
||||
Reference in new issue
Block a user