From 64d3127fe85cfdab48fd56ce2d888cec24b91676 Mon Sep 17 00:00:00 2001 From: Hamza Date: Sun, 18 Oct 2026 00:03:38 +0500 Subject: [PATCH 1/4] Header & Footer: an automatic date's format comes back on import MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `header_footer()` recorded a Date placeholder's text only when it held no field, and never looked at the field itself, so an automatic date's format was dropped on import: `date_format` stayed empty whatever the deck used. With the dialog of the previous commit that is not only cosmetic. Opening a deck whose dates are `datetime5` showed `datetime1` selected, and pressing Apply then sent that back and reformatted the deck's dates — a silent change to a file the user only opened to look at. The reader now remembers which of the thirteen formats the field is, so the dialog opens on the deck's own choice. An unknown spelling (`datetime`, `datetimeFigureOut`, which both render as the short date) is left as no choice rather than guessed at, since the command only accepts the thirteen and would refuse it. A fixed date still comes back as its text, as before. `crates/pptx` gains a dependency on `crates/text` for `datetime::DATE_KINDS`, which layer 3 may take on layer 2; `cargo xtask layers` passes. --- Cargo.lock | 1 + crates/pptx/Cargo.toml | 1 + crates/pptx/src/read.rs | 27 ++++++++++++--- crates/pptx/tests/header_footer_date.rs | 45 +++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 crates/pptx/tests/header_footer_date.rs diff --git a/Cargo.lock b/Cargo.lock index 01a92e4a5..5aa455924 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1217,6 +1217,7 @@ dependencies = [ "deckcraft-engine", "deckcraft-geom", "deckcraft-model", + "deckcraft-text", "image", "log", "quick-xml 0.41.0", diff --git a/crates/pptx/Cargo.toml b/crates/pptx/Cargo.toml index 0ea79d6b4..8d1e0fc34 100644 --- a/crates/pptx/Cargo.toml +++ b/crates/pptx/Cargo.toml @@ -10,6 +10,7 @@ workspace = true [dependencies] deckcraft-model = { workspace = true } +deckcraft-text = { workspace = true } deckcraft-geom = { workspace = true } deckcraft-color = { workspace = true } quick-xml = { workspace = true } diff --git a/crates/pptx/src/read.rs b/crates/pptx/src/read.rs index 18e8eb92f..412bc8f25 100644 --- a/crates/pptx/src/read.rs +++ b/crates/pptx/src/read.rs @@ -1332,11 +1332,28 @@ fn header_footer(p: &Presentation) -> HeaderFooter { Some(PhType::SlideNum) => hf.slide_number = true, Some(PhType::Date) => { hf.date = true; - if let Some(t) = &sh.text - && !t.paragraphs.iter().flat_map(|p| &p.runs).any(|r| matches!(r.kind, deckcraft_model::text::RunKind::Field { .. })) - && hf.date_text.is_empty() - { - hf.date_text = t.text(); + let field = sh.text.iter().flat_map(|t| t.paragraphs.iter()).flat_map(|p| p.runs.iter()).find_map(|r| match &r.kind { + deckcraft_model::text::RunKind::Field { field } if deckcraft_text::datetime::is_date_field(field) => Some(field.as_str()), + _ => None, + }); + match field { + // An automatic date: remember which of the thirteen formats it is, so the + // Header and Footer dialog opens on the deck's own choice instead of the + // default. An unknown spelling (`datetime`, `datetimeFigureOut`) is left + // as no choice rather than guessed at. + Some(kind) => { + if hf.date_format.is_empty() && deckcraft_text::datetime::DATE_KINDS.contains(&kind) { + hf.date_format = kind.to_string(); + } + } + // A fixed date: its text, as before. + None => { + if hf.date_text.is_empty() + && let Some(t) = &sh.text + { + hf.date_text = t.text(); + } + } } } Some(PhType::Footer) => { diff --git a/crates/pptx/tests/header_footer_date.rs b/crates/pptx/tests/header_footer_date.rs new file mode 100644 index 000000000..99b263082 --- /dev/null +++ b/crates/pptx/tests/header_footer_date.rs @@ -0,0 +1,45 @@ +//! The Header & Footer date through PPTX: an automatic date's format comes back on import, so the +//! dialog opens on the deck's own choice; a fixed date still comes back as its text. + +use deckcraft_model::{Presentation, defaults}; +use serde_json::json; + +/// A deck whose Date placeholders are set up by the engine, exported and imported again. +fn round_trip(args: serde_json::Value) -> Presentation { + let mut s = deckcraft_engine::Session::with_new(); + s.execute("design.headerFooter", &args).expect("headerFooter"); + let p = s.doc().expect("doc").doc.clone(); + let bytes = deckcraft_pptx::export(&p).expect("export"); + deckcraft_pptx::import(&bytes).expect("import") +} + +#[test] +fn an_automatic_dates_format_survives_a_round_trip() { + // A chosen format comes back as the remembered choice. + let q = round_trip(json!({"date": true, "dateFormat": "datetime5", "all": true})); + assert!(q.header_footer.date, "the date placeholder is there"); + assert_eq!(q.header_footer.date_format, "datetime5", "the format is remembered"); + assert_eq!(q.header_footer.date_text, "", "an automatic date has no fixed text"); + + // A different one, so the test cannot pass by always reading datetime5. + let q = round_trip(json!({"date": true, "dateFormat": "datetime11", "all": true})); + assert_eq!(q.header_footer.date_format, "datetime11"); + + // The default: no format asked for, so nothing to remember beyond the field itself. + let q = round_trip(json!({"date": true, "all": true})); + assert_eq!(q.header_footer.date_format, "datetime1", "the placeholder's own field is datetime1"); + + // A fixed date is text, not a field: its text comes back and no format is claimed. + let q = round_trip(json!({"date": true, "dateText": "4 July 1776", "all": true})); + assert_eq!(q.header_footer.date_text, "4 July 1776"); + assert_eq!(q.header_footer.date_format, "", "a fixed date has no format"); +} + +#[test] +fn a_deck_without_a_date_placeholder_claims_no_format() { + let p = defaults::new_presentation(None); + let bytes = deckcraft_pptx::export(&p).expect("export"); + let q = deckcraft_pptx::import(&bytes).expect("import"); + assert!(!q.header_footer.date); + assert_eq!(q.header_footer.date_format, ""); +} From 372c0e6c4445fd6aa029a8f11bfca1ec03124c87 Mon Sep 17 00:00:00 2001 From: Hamza Date: Thu, 29 Oct 2026 16:48:32 +0500 Subject: [PATCH 2/4] Apply only a date format the caller asked for The dialog always sends `dateText`, so an Apply with no format selection reached `set_date` carrying the deck's global format, and rewrote a slide's own date field to it. Measured on two slides, the first on the time format and the second on the short date: before [datetime11, datetime5] apply {"date": true, "dateText": "", "all": true} after [datetime5, datetime5] The first slide's format was never mentioned by the caller, and it was gone. `set_date` already keeps an existing field when the format is empty, so it now gets an empty format unless `dateFormat` was supplied; a format that is supplied still reaches every slide, which the test checks so the guard cannot quietly become a block. --- crates/engine/src/cmd/design/slide_setup.rs | 33 ++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/design/slide_setup.rs b/crates/engine/src/cmd/design/slide_setup.rs index 86d9317b7..ed91d3253 100644 --- a/crates/engine/src/cmd/design/slide_setup.rs +++ b/crates/engine/src/cmd/design/slide_setup.rs @@ -176,7 +176,12 @@ pub(super) fn header_footer(s: &mut Session, p: &Value) -> Result { } (Some(pos), true) if k == PhType::Date && (date_given || format_given) => { if let Some(sh) = sl.shapes.get_mut(pos) { - set_date(sh, &hf.date_text, &hf.date_format); + // Only a format the caller asked for. The dialog always sends + // `dateText`, so an Apply with no format selection reached here and + // rewrote a slide's own `datetime11` field to the deck's global + // format, which the caller never mentioned (noted in review). No + // format means the field keeps the one it has. + set_date(sh, &hf.date_text, if format_given { &hf.date_format } else { "" }); } } _ => {} @@ -298,6 +303,32 @@ mod header_footer_date_tests { got[0].0.clone() } + /// The dialog always sends `dateText`, so an Apply with no format selection reached + /// `set_date` with the deck's global format and rewrote a slide's own date field to it. The + /// format the caller did not mention is not a format the caller asked for (noted in review). + #[test] + fn an_apply_with_no_format_leaves_each_slides_own_format_alone() { + let mut s = Session::with_new(); + s.execute("slide.new", &json!({})).unwrap(); + s.execute("design.headerFooter", &json!({"date": true, "dateFormat": "datetime11", "all": true})).unwrap(); + let kinds = |s: &Session| dates(s).into_iter().map(|(_, k)| k).collect::>(); + assert_eq!(kinds(&s), vec![Some("datetime11".to_string()); 2], "both slides start on the time format"); + + // One slide alone takes a different format, which also becomes the deck's global one. + s.execute("design.headerFooter", &json!({"date": true, "dateFormat": "datetime5", "all": false})).unwrap(); + assert_eq!(kinds(&s), vec![Some("datetime11".to_string()), Some("datetime5".to_string())]); + assert_eq!(s.doc().unwrap().doc.header_footer.date_format, "datetime5"); + + // Apply with no `dateFormat`: the first slide keeps datetime11 rather than being rewritten + // to the global datetime5. + s.execute("design.headerFooter", &json!({"date": true, "dateText": "", "all": true})).unwrap(); + assert_eq!(kinds(&s), vec![Some("datetime11".to_string()), Some("datetime5".to_string())], "neither slide was rewritten"); + + // And a format that *is* given still reaches every slide, so the guard is not a block. + s.execute("design.headerFooter", &json!({"date": true, "dateFormat": "datetime2", "all": true})).unwrap(); + assert_eq!(kinds(&s), vec![Some("datetime2".to_string()); 2], "a format the caller asked for still applies"); + } + /// Header & Footer's automatic date takes the format it is given. `HeaderFooter::date_format` /// existed for this and was never read: the placeholder was always `datetime1`, and a /// `dateFormat` parameter was accepted and silently dropped. From 370b38e69ad007e115da5b0b8168e23659c05b5d Mon Sep 17 00:00:00 2001 From: Hamza Date: Mon, 2 Nov 2026 15:57:14 +0500 Subject: [PATCH 3/4] A date with no field to keep takes the deck's remembered format My previous commit withheld the format from `set_date` whenever the caller had not named one, so a field the slide already had would not be rewritten. That is right where there is a field. A placeholder showing a fixed date holds plain text and no date field at all, so there was no format to preserve and the withheld format left `date_kind("")` to build a `datetime1`: a slide taken from `datetime11` to fixed text and back came away on the short date (noted in review). The deck's format now always reaches `set_date`, and `format_given` is what decides whether it may rewrite an existing field. With no field, there is nothing to preserve and the remembered format is what a new one takes. `a_fixed_date_turned_automatic_again_comes_back_on_the_remembered_format` walks that transition with no `dateFormat` in the call, which is what the dialog sends, and goes on to assert the guard still holds where a field does exist. It is a test of its own rather than more of `the_automatic_date_takes_the_format_it_is_given`, because setting a format for one slide also moves the deck's global one, which that test's later assertions depend on. --- crates/engine/src/cmd/design/slide_setup.rs | 49 +++++++++++++++++---- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/cmd/design/slide_setup.rs b/crates/engine/src/cmd/design/slide_setup.rs index ed91d3253..32534255a 100644 --- a/crates/engine/src/cmd/design/slide_setup.rs +++ b/crates/engine/src/cmd/design/slide_setup.rs @@ -176,12 +176,10 @@ pub(super) fn header_footer(s: &mut Session, p: &Value) -> Result { } (Some(pos), true) if k == PhType::Date && (date_given || format_given) => { if let Some(sh) = sl.shapes.get_mut(pos) { - // Only a format the caller asked for. The dialog always sends - // `dateText`, so an Apply with no format selection reached here and - // rewrote a slide's own `datetime11` field to the deck's global - // format, which the caller never mentioned (noted in review). No - // format means the field keeps the one it has. - set_date(sh, &hf.date_text, if format_given { &hf.date_format } else { "" }); + // The deck's format always travels; whether the caller asked for it + // is what decides if it may rewrite a field the slide already has + // (see `set_date`). + set_date(sh, &hf.date_text, &hf.date_format, format_given); } } _ => {} @@ -205,8 +203,19 @@ fn date_now(format: &str) -> String { } /// Point a slide's date placeholder at a fixed date (its own plain text) or, for an empty -/// `fixed`, at an automatic date field (one it already has keeps its format). -fn set_date(sh: &mut Shape, fixed: &str, format: &str) { +/// `fixed`, at an automatic date field. +/// +/// `format_given` is whether the caller named a format. The dialog always sends `dateText`, so an +/// Apply with no format selection reached here carrying the deck's global format and rewrote a +/// slide's own `datetime11` field to it -- a format the caller never mentioned. A field the slide +/// already has therefore keeps its own format unless one was asked for. +/// +/// That holds only where there *is* a field to keep. A placeholder showing a fixed date has no +/// date field and no format of its own, so clearing `dateText` without naming a format used to +/// build a `datetime1`: a slide that went from `datetime11` to fixed text and back came away on +/// the short date. With nothing to preserve, the deck's remembered format is the right one to +/// take (noted in review). +fn set_date(sh: &mut Shape, fixed: &str, format: &str, format_given: bool) { let runs = || sh.text.iter().flat_map(|t| &t.paragraphs).flat_map(|p| &p.runs); let field_now = runs().find_map(|r| match &r.kind { RunKind::Field { field } if deckcraft_text::datetime::is_date_field(field) => Some(field.clone()), @@ -214,6 +223,7 @@ fn set_date(sh: &mut Shape, fixed: &str, format: &str) { }); let has_field = field_now.is_some(); let props = runs().next().map(|r| r.props.clone()).unwrap_or_default(); + let format = if format_given || !has_field { format } else { "" }; let want = date_kind(format); let (text, kind) = match fixed { // Automatic: keep the field, but follow a format that has changed. @@ -329,6 +339,29 @@ mod header_footer_date_tests { assert_eq!(kinds(&s), vec![Some("datetime2".to_string()); 2], "a format the caller asked for still applies"); } + /// A fixed date is plain text, so the placeholder has no date field and no format of its + /// own. Clearing `dateText` without naming a format therefore has nothing to preserve, and + /// built a `datetime1`: a slide that went from `datetime11` to fixed text and back came away + /// on the short date (noted in review). + #[test] + fn a_fixed_date_turned_automatic_again_comes_back_on_the_remembered_format() { + let mut s = Session::with_new(); + run_automatic(&mut s, json!({"date": true, "dateFormat": "datetime11", "all": true}), "datetime11"); + + s.execute("design.headerFooter", &json!({"date": true, "dateText": "4 July 1776", "all": true})).unwrap(); + assert_eq!(dates(&s), vec![("4 July 1776".to_string(), None)], "fixed: plain text, no field"); + + // No `dateFormat` in this call, which is what the dialog sends on an Apply. + run_automatic(&mut s, json!({"date": true, "dateText": "", "all": true}), "datetime11"); + + // And the guard this must not undo: where there *is* a field, an Apply naming no format + // still leaves the slide's own format alone, although the deck's format now reaches + // `set_date` either way. + s.execute("design.headerFooter", &json!({"date": true, "dateFormat": "datetime5", "all": false})).unwrap(); + s.execute("design.headerFooter", &json!({"date": true, "dateText": "", "all": true})).unwrap(); + assert_eq!(dates(&s)[0].1.as_deref(), Some("datetime5"), "the slide's own format survives"); + } + /// Header & Footer's automatic date takes the format it is given. `HeaderFooter::date_format` /// existed for this and was never read: the placeholder was always `datetime1`, and a /// `dateFormat` parameter was accepted and silently dropped. From 2ba369f5d8fb4d0d17475768a9be03cab7e0102f Mon Sep 17 00:00:00 2001 From: "autocraft-bot[bot]" <354215646+autocraft-bot[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 03:20:42 +0000 Subject: [PATCH 4/4] Apply review fixes - crates/ui-egui/src/dialogs/tests.rs --- crates/ui-egui/src/dialogs/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ui-egui/src/dialogs/tests.rs b/crates/ui-egui/src/dialogs/tests.rs index 7378a6a8b..452ff079c 100644 --- a/crates/ui-egui/src/dialogs/tests.rs +++ b/crates/ui-egui/src/dialogs/tests.rs @@ -28,7 +28,7 @@ fn header_footer_apply_keeps_an_existing_date_format() { use crate::SlideApp; let mut session = deckcraft_engine::Session::with_new(); session.execute("design.headerFooter", &json!({"date": true, "dateText": "", "dateFormat": "datetime4", "all": true})).unwrap(); - // A file whose document-level format is unset, as an imported deck's is. + // A file whose document-level format is unset, as an imported deck's is when its date field is not one of the thirteen kinds. Arc::make_mut(&mut session.doc_mut().unwrap().doc).header_footer.date_format = String::new(); let date_fields = |app: &SlideApp| -> Vec { app.session