mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
New Document: Web and Mobile presets turn Artboards on (#14042)
A preset picked on the Web or Mobile tab of File > New ticks Artboards, so Create makes the document as an artboard; a preset of the Photo, Print, Art & Illustration or Film & Video tab unticks it. Recent's built-in cards, the Clipboard preset and Recent/Saved cards keep today's behaviour.
This commit is contained in:
1 parent
5bdbcc1181
commit
5697cdfc48
3 files changed
+188
-1
No files matched your search
@@ -218,17 +218,34 @@ pub fn at_pref_resolutions(p: &Preset, units: &UnitsAndRulers) -> Preset {
|
||||
(p.0, px(p.1), px(p.2), ppi)
|
||||
}
|
||||
|
||||
/// The category tabs whose presets start the document as an artboard, as in Photoshop (#14042).
|
||||
const ARTBOARD_CATEGORIES: &[&str] = &["Web", "Mobile"];
|
||||
|
||||
/// Apply a built-in preset (at the preference resolutions, see [`at_pref_resolutions`]) to the
|
||||
/// dialog fields.
|
||||
/// dialog fields. A preset of the category tab picked in `__category` also sets Artboards: on for
|
||||
/// Web and Mobile, off for the other tabs (#14042). Recent's built-in cards and the Clipboard
|
||||
/// preset leave it as it is.
|
||||
pub fn apply_preset(f: &mut Map<String, Value>, p: &Preset, units: &UnitsAndRulers) {
|
||||
let (name, w, h, ppi) = at_pref_resolutions(p, units);
|
||||
let unit = preset_unit(p, f);
|
||||
let cat = get_s(f, "__category", CATEGORIES[0].0);
|
||||
let tab = CATEGORIES.iter().find(|c| c.0 == cat && c.0 != CATEGORIES[0].0 && c.1.iter().any(|q| q.0 == name));
|
||||
f.remove("__savedPreset");
|
||||
f.insert("width".into(), json!(w));
|
||||
f.insert("height".into(), json!(h));
|
||||
f.insert("resolution".into(), json!(ppi));
|
||||
f.insert("__preset".into(), json!(name));
|
||||
f.insert("__unit".into(), json!(unit));
|
||||
match tab {
|
||||
Some((tab, _)) if ARTBOARD_CATEGORIES.contains(tab) => {
|
||||
f.insert("artboard".into(), json!(true));
|
||||
}
|
||||
// Off is the checkbox's and `file.new`'s default.
|
||||
Some(_) => {
|
||||
f.remove("artboard");
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// The colour mode of the dialog's `mode` value.
|
||||
|
||||
@@ -987,6 +987,9 @@ mod blend_dropdown_keys_tests;
|
||||
#[cfg(test)]
|
||||
mod warp_text_dialog_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod new_doc_web_artboards_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod blend_dropdown_wheel_tests;
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
//! #14042: a Web or Mobile preset in File › New turns Artboards on, as in Photoshop; a preset of
|
||||
//! the Photo, Print, Art & Illustration or Film & Video tab turns it off. Recent and Saved cards keep
|
||||
//! restoring their own settings.
|
||||
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
use serde_json::{Map, Value, json};
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::new_doc_ui::{CATEGORIES, CLIPBOARD, Preset, apply_preset, apply_settings, command_params};
|
||||
use crate::state::{DialogKind, UiState};
|
||||
|
||||
fn presets(tab: &str) -> &'static [Preset] {
|
||||
CATEGORIES.iter().find(|c| c.0 == tab).unwrap().1
|
||||
}
|
||||
|
||||
/// The dialog's fields on `tab`, with Artboards ticked by hand or not.
|
||||
fn on_tab(tab: &str, artboard: bool) -> Map<String, Value> {
|
||||
let mut f = UiState::new_document_fields();
|
||||
f.insert("__category".into(), json!(tab));
|
||||
if artboard {
|
||||
f.insert("artboard".into(), json!(true));
|
||||
}
|
||||
f
|
||||
}
|
||||
|
||||
fn artboard(f: &Map<String, Value>) -> bool {
|
||||
f.get("artboard").and_then(Value::as_bool).unwrap_or(false)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn web_and_mobile_presets_turn_artboards_on() {
|
||||
for tab in ["Web", "Mobile"] {
|
||||
assert!(!presets(tab).is_empty(), "{tab}");
|
||||
for p in presets(tab) {
|
||||
for ticked in [false, true] {
|
||||
let mut f = on_tab(tab, ticked);
|
||||
apply_preset(&mut f, p, &Default::default());
|
||||
assert!(artboard(&f), "{tab} › {}", p.0);
|
||||
assert_eq!(command_params(&f)["artboard"], json!(true), "Create sends it: {tab} › {}", p.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn presets_of_the_other_tabs_turn_artboards_off() {
|
||||
for tab in ["Photo", "Print", "Art & Illustration", "Film & Video"] {
|
||||
assert!(!presets(tab).is_empty(), "{tab}");
|
||||
for p in presets(tab) {
|
||||
let mut f = on_tab(tab, true);
|
||||
apply_preset(&mut f, p, &Default::default());
|
||||
assert!(!artboard(&f), "{tab} › {}", p.0);
|
||||
assert!(command_params(&f).get("artboard").is_none(), "Create makes a plain document: {tab} › {}", p.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Recent's built-in cards and the Clipboard preset weren't measured: they keep today's behaviour.
|
||||
#[test]
|
||||
fn recent_presets_and_the_clipboard_leave_artboards_as_it_is() {
|
||||
for ticked in [false, true] {
|
||||
for p in presets(CATEGORIES[0].0) {
|
||||
let mut f = on_tab(CATEGORIES[0].0, ticked);
|
||||
apply_preset(&mut f, p, &Default::default());
|
||||
assert_eq!(artboard(&f), ticked, "Recent › {}", p.0);
|
||||
}
|
||||
let mut f = on_tab(CATEGORIES[0].0, ticked);
|
||||
crate::new_doc_ui::set_clipboard(&mut f, 640, 360);
|
||||
assert_eq!(artboard(&f), ticked, "{CLIPBOARD}");
|
||||
// Applied without a tab (a script, or a preset no tab lists) changes nothing either.
|
||||
let mut f = on_tab("Web", ticked);
|
||||
apply_preset(&mut f, &("Not a Web preset", 640, 360, 72.0), &Default::default());
|
||||
assert_eq!(artboard(&f), ticked);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recent_cards_keep_restoring_their_own_settings() {
|
||||
let mut f = on_tab("Web", false);
|
||||
apply_preset(&mut f, &presets("Web")[0], &Default::default());
|
||||
let mut settings_from = f.clone();
|
||||
settings_from.remove("artboard");
|
||||
let settings = serde_json::from_value(crate::new_doc_ui::document_settings(&settings_from)).unwrap();
|
||||
let before = f.get("artboard").cloned();
|
||||
apply_settings(&mut f, &settings);
|
||||
assert_eq!(f.get("artboard").cloned(), before, "the stored settings hold no Artboards value");
|
||||
}
|
||||
|
||||
mod dialog {
|
||||
use super::*;
|
||||
|
||||
fn harness() -> Harness<'static, PhotocraftApp> {
|
||||
let app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
let mut h =
|
||||
Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 60.0).build_ui_state(|ui, app| crate::dialogs::show(app, ui.ctx()), app);
|
||||
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
|
||||
h.state_mut().ui.open_dialog(DialogKind::NewDocument, UiState::new_document_fields());
|
||||
h.run_steps(3);
|
||||
h
|
||||
}
|
||||
|
||||
fn fields(h: &Harness<'static, PhotocraftApp>) -> Map<String, Value> {
|
||||
h.state().ui.dialogs.first().map(|d| d.fields.clone()).expect("the dialog is open")
|
||||
}
|
||||
|
||||
fn open_tab(h: &mut Harness<'static, PhotocraftApp>, tab: &str) {
|
||||
h.get_by_label(tab).click();
|
||||
h.run_steps(2);
|
||||
}
|
||||
|
||||
/// Preset cards paint their titles directly, without an accessibility label.
|
||||
fn click_preset(h: &mut Harness<'static, PhotocraftApp>, title: &str) {
|
||||
fn find(shape: &egui::Shape, title: &str) -> Option<egui::Pos2> {
|
||||
match shape {
|
||||
egui::Shape::Text(text) if text.galley.job.text == title => Some(text.visual_bounding_rect().center()),
|
||||
egui::Shape::Vec(shapes) => shapes.iter().find_map(|shape| find(shape, title)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
let at = h.output().shapes.iter().find_map(|shape| find(&shape.shape, title)).expect("visible preset title");
|
||||
// Let a previous card click age past the double-click interval, so this one only selects.
|
||||
h.run_steps(60);
|
||||
h.hover_at(at);
|
||||
h.run_steps(1);
|
||||
h.drag_at(at);
|
||||
h.run_steps(1);
|
||||
h.drop_at(at);
|
||||
h.run_steps(2);
|
||||
}
|
||||
|
||||
fn artboards_ticked(h: &Harness<'static, PhotocraftApp>) -> bool {
|
||||
h.get_by_label("Artboards").accesskit_node().toggled() == Some(egui::accesskit::Toggled::True)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_web_preset_ticks_artboards_and_create_makes_an_artboard() {
|
||||
let mut h = harness();
|
||||
assert!(!artboards_ticked(&h));
|
||||
open_tab(&mut h, "Web");
|
||||
click_preset(&mut h, "Web Most Common");
|
||||
assert_eq!(fields(&h)["__preset"], "Web Most Common");
|
||||
assert!(artboards_ticked(&h), "the checkbox shows it");
|
||||
h.get_by_label("Create").click();
|
||||
h.run_steps(3);
|
||||
assert!(h.state().ui.dialogs.is_empty(), "the dialog closed");
|
||||
let doc = &h.state().session.active().expect("a new document").doc;
|
||||
assert_eq!((doc.size.width, doc.size.height), (1366, 768));
|
||||
assert_eq!(doc.artboards().len(), 1, "made as an artboard");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_photo_preset_after_a_mobile_one_unticks_artboards() {
|
||||
let mut h = harness();
|
||||
open_tab(&mut h, "Mobile");
|
||||
click_preset(&mut h, presets("Mobile")[0].0);
|
||||
assert!(artboards_ticked(&h));
|
||||
open_tab(&mut h, "Photo");
|
||||
click_preset(&mut h, presets("Photo")[0].0);
|
||||
assert_eq!(h.state().ui.dialogs.len(), 1, "a single click only selects");
|
||||
assert!(!artboards_ticked(&h));
|
||||
h.get_by_label("Create").click();
|
||||
h.run_steps(3);
|
||||
let doc = &h.state().session.active().expect("a new document").doc;
|
||||
assert!(doc.artboards().is_empty(), "a plain document");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user