mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
Merge pull request #10813 from dotCooCoo/fix/new-document-recent-units
New Document: Recent entries of one size keep their own units
This commit is contained in:
1 file changed
+71
-11
@@ -9,7 +9,8 @@
|
||||
//! The dialog's fields, as `ui.dialog.set` reads and writes them, are `file.new`'s params
|
||||
//! (`name` is the document's name, `width`, `height` (pt), `units`, `artboards`, `artboardLayout`,
|
||||
//! `bleed`, `backgroundContents`, `colorMode`, `rasterEffectsPpi`, `previewMode`,
|
||||
//! `alignNewObjectsToPixelGrid`, `profile`), plus `basedOn`, the preset the fields were filled from.
|
||||
//! `alignNewObjectsToPixelGrid`, `profile`), plus `basedOn`, the preset the fields were filled from, and
|
||||
//! `basedOnUnits`, the units of that preset or Recent entry (null when the fields were edited).
|
||||
//! The dialog opens in Preferences › Units › General, also on a Recent entry made in other units
|
||||
//! (one in pixels, or a saved preset in the units it was saved in, keeps its units); units chosen in
|
||||
//! the Units list or set with `ui.dialog.set` stay when a card is picked.
|
||||
@@ -96,9 +97,14 @@ pub(crate) struct VcNewDocument {
|
||||
/// The orientation automation set, which [`DocumentSettings::note_set`] turns the page to: read back from the
|
||||
/// fields, the width and height carry the orientation, so the one listed with them is not applied.
|
||||
turn: Option<Value>,
|
||||
/// The units of the card or Recent entry the settings were filled from, which can differ from the units shown:
|
||||
/// Recent keeps one entry per size and units, so these tell two entries of one size apart. None once a field
|
||||
/// other than the units or the orientation was edited in Preset Details.
|
||||
entry_units: Option<Unit>,
|
||||
}
|
||||
|
||||
/// The same document whatever orientation a set asked for: `turn` is spent by the set that gave it.
|
||||
/// The same document whatever orientation a set asked for: `turn` is spent by the set that gave it, and
|
||||
/// `entry_units` only says which card the settings came from.
|
||||
impl PartialEq for VcNewDocument {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
(&self.title, &self.settings) == (&other.title, &other.settings)
|
||||
@@ -108,7 +114,7 @@ impl PartialEq for VcNewDocument {
|
||||
impl VcNewDocument {
|
||||
/// `settings` as they are, with an empty document name (a card's value).
|
||||
fn of(settings: DocSettings) -> Self {
|
||||
Self { title: String::new(), settings, turn: None }
|
||||
Self { title: String::new(), settings, turn: None, entry_units: None }
|
||||
}
|
||||
|
||||
/// The fields as `ui.dialog.set` and `file.new` name them.
|
||||
@@ -117,6 +123,7 @@ impl VcNewDocument {
|
||||
if let Value::Object(m) = &mut v {
|
||||
m.insert("name".into(), json!(self.title));
|
||||
m.insert("basedOn".into(), json!(self.settings.name));
|
||||
m.insert("basedOnUnits".into(), json!(self.entry_units.map(Unit::label)));
|
||||
}
|
||||
v
|
||||
}
|
||||
@@ -134,7 +141,11 @@ impl VcNewDocument {
|
||||
let mut settings = read(&sizes)?;
|
||||
settings.name = value.get("basedOn").and_then(Value::as_str).unwrap_or(CUSTOM).to_owned();
|
||||
let title = value.get("name").and_then(Value::as_str).unwrap_or_default().to_owned();
|
||||
Ok(Self { title, settings, turn })
|
||||
let entry_units = match value.get("basedOnUnits") {
|
||||
None | Some(Value::Null) => None,
|
||||
Some(u) => Some(u.as_str().and_then(Unit::named).ok_or_else(|| format!("basedOnUnits: unknown units `{u}`"))?),
|
||||
};
|
||||
Ok(Self { title, settings, turn, entry_units })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,8 +256,10 @@ impl DocumentSettings for VcNewDocument {
|
||||
.map(|recent| {
|
||||
let s = &recent.settings;
|
||||
// The entry the fields were filled from shows the units chosen in Preset Details, which
|
||||
// the new document will have (#2078); the others keep their own.
|
||||
let chosen = (s.name.as_str(), s.width, s.height) == (now.name.as_str(), now.width, now.height);
|
||||
// the new document will have (#2078); the others keep their own. Two entries of one size
|
||||
// differ in their units, so only the one in the units the fields came from is that entry.
|
||||
let from = cx.values.entry_units.unwrap_or(now.units);
|
||||
let chosen = (s.name.as_str(), s.width, s.height, s.units) == (now.name.as_str(), now.width, now.height, from);
|
||||
let shown = DocSettings { units: if chosen { now.units } else { s.units }, ..s.clone() };
|
||||
DocumentCard::new(text(preset_name(&s.name)), shown.size_label(), recent.clone(), CardSource::Recent)
|
||||
})
|
||||
@@ -262,6 +275,7 @@ impl DocumentSettings for VcNewDocument {
|
||||
fn apply_card(&mut self, card: &DocumentCard<Self>, _setup: &Setup, memory: &mut Memory) {
|
||||
let units = self.settings.units;
|
||||
self.settings = card.settings.settings.clone();
|
||||
self.entry_units = Some(self.settings.units);
|
||||
if memory.units_chosen {
|
||||
self.settings.units = units;
|
||||
}
|
||||
@@ -377,6 +391,7 @@ impl DocumentSettings for VcNewDocument {
|
||||
after.units = before.units;
|
||||
if !turned && after != before {
|
||||
values.settings.name = CUSTOM.into();
|
||||
values.entry_units = None;
|
||||
cx.drop_card();
|
||||
}
|
||||
if let Some(o) = picked {
|
||||
@@ -401,7 +416,7 @@ fn built_in_card(preset: &DocSettings, values: &VcNewDocument, orientation: Opti
|
||||
std::mem::swap(&mut s.width, &mut s.height);
|
||||
}
|
||||
let line = s.size_label();
|
||||
let value = VcNewDocument { title: values.title.clone(), settings: s, turn: None };
|
||||
let value = VcNewDocument { title: values.title.clone(), settings: s, turn: None, entry_units: None };
|
||||
DocumentCard::new(text(preset_name(&preset.name)), line, value, CardSource::preset(None))
|
||||
}
|
||||
|
||||
@@ -677,7 +692,8 @@ fn first_settings(app: &VectorcraftApp, category: &str) -> Option<VcNewDocument>
|
||||
let as_saved = newdoc::find(&app.session, &first.name).is_some_and(|p| p.units == first.units);
|
||||
let own = !category.eq_ignore_ascii_case("Recent") || first.units == Unit::Pixels || as_saved;
|
||||
let units = if own { first.units } else { app.session.default_units() };
|
||||
Some(VcNewDocument { title: app.session.peek_untitled().to_string(), settings: DocSettings { units, ..first }, turn: None })
|
||||
let entry_units = Some(first.units);
|
||||
Some(VcNewDocument { title: app.session.peek_untitled().to_string(), settings: DocSettings { units, ..first }, turn: None, entry_units })
|
||||
}
|
||||
|
||||
/// The settings New Document opens on.
|
||||
@@ -686,6 +702,7 @@ fn offered(app: &VectorcraftApp) -> VcNewDocument {
|
||||
title: app.session.peek_untitled().to_string(),
|
||||
settings: DocSettings { units: app.session.default_units(), ..DocSettings::default() },
|
||||
turn: None,
|
||||
entry_units: None,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -705,7 +722,7 @@ pub fn open(app: &mut VectorcraftApp) {
|
||||
let mut saved = PresetLibrary::new();
|
||||
for preset in &app.session.prefs.new_doc_presets {
|
||||
// The engine keeps names the library refuses (an empty one); such a preset is not offered here.
|
||||
saved.add(&GroupPath::root(), &preset.name, VcNewDocument { title: preset.name.clone(), settings: preset.clone(), turn: None }).ok();
|
||||
saved.add(&GroupPath::root(), &preset.name, VcNewDocument { title: preset.name.clone(), ..VcNewDocument::of(preset.clone()) }).ok();
|
||||
}
|
||||
let recent: Vec<VcNewDocument> = app.session.prefs.recent_new_docs.iter().cloned().map(VcNewDocument::of).collect();
|
||||
let mut dialog = HostedNewDocument::<VcNewDocument>::new(&ctx, first, move |options| {
|
||||
@@ -1543,8 +1560,7 @@ mod tests {
|
||||
|
||||
let card = |units| DocumentCard::new("Card", "line", VcNewDocument::of(entry(units)), CardSource::Recent);
|
||||
let setup = Setup::default();
|
||||
let mut doc =
|
||||
VcNewDocument { title: "Mine".into(), settings: DocSettings { units: Unit::Millimeters, ..DocSettings::default() }, turn: None };
|
||||
let mut doc = VcNewDocument { title: "Mine".into(), ..VcNewDocument::of(DocSettings { units: Unit::Millimeters, ..DocSettings::default() }) };
|
||||
let mut memory = Memory::default();
|
||||
doc.apply_card(&card(Unit::Inches), &setup, &mut memory);
|
||||
assert_eq!((doc.settings.units, doc.title.as_str()), (Unit::Inches, "Mine"), "a saved preset keeps its own units, the name stays");
|
||||
@@ -1560,6 +1576,50 @@ mod tests {
|
||||
assert_eq!(shown.settings.settings.units, Unit::Millimeters, "the cards follow the chosen units");
|
||||
}
|
||||
|
||||
/// Recent keeps an A4 made in pixels and one made in millimeters as two entries. Only the card the fields came
|
||||
/// from shows the units in Preset Details (the newest entry, opened in the general units unless it is in pixels);
|
||||
/// the other card keeps its own units, also after the units are set from outside the dialog.
|
||||
#[test]
|
||||
fn recent_entries_of_one_size_in_two_units_show_two_sizes() {
|
||||
let a4 = newdoc::find(&Session::new(), "A4").unwrap();
|
||||
let entry = |units| DocSettings { units, ..a4.clone() };
|
||||
let label = |units| entry(units).size_label();
|
||||
let count = |panel: &Panel, units| panel.painted(&label(units)).len();
|
||||
let opened = |newest, older| {
|
||||
let mut app = app();
|
||||
general_units(&mut app, "Points");
|
||||
app.session.prefs.recent_new_docs = vec![entry(newest), entry(older)];
|
||||
let mut panel = Panel::new(app, crate::tests::picks::dialogs);
|
||||
panel.room(egui::vec2(1280.0, 1100.0));
|
||||
open(&mut panel.app());
|
||||
panel
|
||||
};
|
||||
for (newest, older, shown) in [(Unit::Pixels, Unit::Millimeters, Unit::Pixels), (Unit::Millimeters, Unit::Pixels, Unit::Points)] {
|
||||
let mut panel = opened(newest, older);
|
||||
panel.frame();
|
||||
panel.frame();
|
||||
assert_eq!(fields(&panel.app())["units"], json!(shown.label()));
|
||||
assert_eq!(fields(&panel.app())["basedOnUnits"], json!(newest.label()));
|
||||
assert_eq!(count(&panel, shown), 1, "the newest entry shows the units of the fields: {:?}", panel.texts());
|
||||
assert_eq!(count(&panel, older), 1, "the older entry keeps its own units: {:?}", panel.texts());
|
||||
|
||||
set(&panel.app(), "units", json!("Inches")).unwrap();
|
||||
panel.frame();
|
||||
assert_eq!(count(&panel, Unit::Inches), 1, "the chosen units reach the newest entry only: {:?}", panel.texts());
|
||||
assert_eq!(count(&panel, older), 1, "the older entry still keeps its own units: {:?}", panel.texts());
|
||||
|
||||
// The same set before the dialog's first frame.
|
||||
let mut panel = opened(newest, older);
|
||||
set(&panel.app(), "units", json!("Inches")).unwrap();
|
||||
panel.frame();
|
||||
panel.frame();
|
||||
assert_eq!(count(&panel, Unit::Inches), 1, "a set before the first frame reaches the newest entry: {:?}", panel.texts());
|
||||
assert_eq!(count(&panel, older), 1, "and leaves the older entry in its own units: {:?}", panel.texts());
|
||||
}
|
||||
let panel = opened(Unit::Pixels, Unit::Millimeters);
|
||||
assert!(set(&panel.app(), "basedOnUnits", json!("Furlongs")).is_err(), "unknown units are refused");
|
||||
}
|
||||
|
||||
/// Edit opens a profile as a document and closes the dialog, Reset does not; the rows list every
|
||||
/// profile and say which were edited.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user