diff --git a/crates/ui-egui/src/dialogs/layer_style.rs b/crates/ui-egui/src/dialogs/layer_style.rs index 19c8bc83a6..1f78120417 100644 --- a/crates/ui-egui/src/dialogs/layer_style.rs +++ b/crates/ui-egui/src/dialogs/layer_style.rs @@ -589,17 +589,14 @@ fn picker_patterns(app: &PhotocraftApp) -> Vec<&photocraft_doc::Pattern> { doc_pats.iter().chain(app.session.patterns.items.iter()).filter(|p| seen.insert(&p.id)).collect() } -/// The patterns a style can use (document's, then the library's), as (id, name). -pub fn pattern_list(app: &PhotocraftApp) -> Vec<(String, String)> { - picker_patterns(app).into_iter().map(|p| (p.id.clone(), p.display_name().to_string())).collect() -} - /// What the Pattern field's picker shows: the library's folders with their patterns, the patterns in no folder after -/// them, and the swatch each pattern paints. +/// them, and the swatch each pattern paints. Taken when the dialog opens, like the rest of what it opens with: a +/// library edited while the dialog is open shows at the next opening. #[derive(craft::ui::contract::MemoProps)] pub(crate) struct PatternPresets { folders: Vec, loose: Vec<(String, String)>, + /// The pattern's id, the key of its face and the texture of its swatch. #[memo_props(hash = "an id, the key of its picture and the texture it paints")] swatches: Vec<(String, u64, egui::TextureId)>, #[memo_props( @@ -610,9 +607,9 @@ pub(crate) struct PatternPresets { } impl PatternPresets { - /// The picture key and the texture of the pattern `id`, as [`pattern_presets`] loaded them. + /// The face key and the texture of the pattern `id`, as [`pattern_presets`] loaded them. fn swatch(&self, id: &str) -> Option<(u64, egui::TextureId)> { - self.swatches.iter().find(|(key, _, _)| key == id).map(|(_, face, texture)| (*face, *texture)) + self.swatches.iter().find(|(swatch, _, _)| *swatch == id).map(|(_, face, texture)| (*face, *texture)) } } @@ -653,22 +650,25 @@ pub(crate) fn pattern_presets(app: &PhotocraftApp, ctx: &egui::Context) -> Patte PatternPresets { folders, loose, swatches, _textures: textures } } -/// The picker's rows: each folder, its patterns below it, and the patterns in no folder after the folders. A folder -/// row is keyed apart from any pattern id by its `folder/` prefix, as the Styles page's groups use `group/`: a pattern -/// id is a group of hex fields (`photocraft_doc::Pattern::new`), so it never reads as a folder key. A pattern listed -/// in two folders is a row once, under the first of them, so no key is ever a row twice. +/// The picker's rows: each folder, its patterns below it, and the patterns in no folder after the folders. This order +/// is also what the page reads an empty Pattern value from - it takes the first row that is not a folder - so a new +/// Pattern Overlay starts on the first pattern of the first folder. +/// +/// A folder row's key carries its place in the folder list, so it cannot read as a pattern id and two folders that +/// carry the same name (the preset store does not refuse that) keep their chevrons apart. A pattern listed in two +/// folders is a row once, under the first of them, so no pattern id is ever a row twice. fn pattern_rows(inputs: &PatternPresets, _: &Env) -> Vec> { let mut rows = Vec::new(); let mut placed = HashSet::new(); - for folder in &inputs.folders { + for (at, folder) in inputs.folders.iter().enumerate() { let items: Vec<&(String, String)> = folder.items.iter().filter(|(id, _)| placed.insert(id.clone())).collect(); if items.is_empty() { continue; // every pattern of this folder stands in an earlier one: no empty header } - rows.push(PresetRow::group(format!("folder/{}", folder.name), folder.name.clone(), 0)); + rows.push(PresetRow::group(format!("folder/{at}/{}", folder.name), folder.name.clone(), 0)); rows.extend(items.into_iter().map(|(id, name)| pattern_row(id, name, 1, inputs))); } - // The loose ones are in no folder at all, so they were none of them placed above. + // The loose ones are in no folder at all, so none of them was placed above. rows.extend(inputs.loose.iter().map(|(id, name)| pattern_row(id, name, 0, inputs))); rows } diff --git a/crates/ui-egui/src/dialogs/layer_style/pattern_overlay_picker_tests.rs b/crates/ui-egui/src/dialogs/layer_style/pattern_overlay_picker_tests.rs index e1c0231ef2..10e468e23c 100644 --- a/crates/ui-egui/src/dialogs/layer_style/pattern_overlay_picker_tests.rs +++ b/crates/ui-egui/src/dialogs/layer_style/pattern_overlay_picker_tests.rs @@ -7,31 +7,43 @@ use craft::ui::patterns::presets::PresetRow; use craft::ui::runtime::env::Env; use photocraft_doc::Effect; -use photocraft_engine::presets::PresetsExt; +use photocraft_engine::presets::{Group, PresetsExt}; use serde_json::json; use super::tests::{app_with_layer, cancel, edit, first_of, layer_effects, ok}; use super::{EffectKind, PatternPresets, open, pattern_presets, pattern_rows}; use crate::PhotocraftApp; -/// A built-in pattern, taken under an id and a name of its own. -fn renamed(builtins: &mut Vec, name: &str, id: &str) -> photocraft_doc::Pattern { +/// The six patterns the tests use, by shown name. Built-in patterns only serve as the pixel source: each is taken +/// under a name and an id of its own, so nothing here depends on what the shipped library happens to hold. +const TILES: [&str; 6] = ["Plain", "Stripes", "Dots", "Linen", "Canvas", "Unfiled"]; + +/// A built-in pattern, renamed to `name` under the id `tile-`. +fn renamed(builtins: &mut Vec, name: &str) -> photocraft_doc::Pattern { + let index = TILES.iter().position(|tile| *tile == name).expect("a test pattern name"); let mut pattern = builtins.remove(0); - pattern.id = id.to_string(); + pattern.id = format!("tile-{index}"); pattern.name = name.to_string(); pattern } -/// An app with a Pattern Overlay layer and two patterns the folders do not hold. The overlay command copies the -/// pattern it uses into the document, so `loose-library` is a pattern of the document and of the library at once. +/// An app with a Pattern Overlay layer and a library of our own: "Basics" holds the first three patterns, "Cloth" the +/// next two, and `Unfiled` belongs to no folder. The overlay command copies the pattern it uses into the document, so +/// `Unfiled` is a pattern of the document and of the library at once. fn app_with_patterns() -> Box { let mut app = app_with_layer(16); let mut builtins = photocraft_engine::pattern_cmds::builtin(); - let (document_tile, loose_library) = (renamed(&mut builtins, "Document Tile", "document-tile"), renamed(&mut builtins, "Loose Library", "loose-library")); - let shown = loose_library.id.clone(); - app.session.patterns.upsert(document_tile); - app.session.patterns.upsert(loose_library); - app.run("layer.layerStyle.patternOverlay", json!({"pattern": shown})).expect("a pattern overlay"); + let tiles: Vec = TILES.into_iter().map(|name| renamed(&mut builtins, name)).collect(); + app.session.patterns.items.clear(); + for tile in tiles { + app.session.patterns.upsert(tile); + } + let presets = app.session.presets_mut(); + presets.pattern_groups = vec![ + Group::new("Basics", vec!["tile-0".to_string(), "tile-1".to_string(), "tile-2".to_string()]), + Group::new("Cloth", vec!["tile-3".to_string(), "tile-4".to_string()]), + ]; + app.run("layer.layerStyle.patternOverlay", json!({"pattern": "tile-5"})).expect("a pattern overlay"); app } @@ -47,7 +59,7 @@ fn groups(rows: &[PresetRow]) -> Vec { rows.iter().filter(|row| row.is_group()).map(|row| row.label.clone()).collect() } -/// The rows below the group named `group`: its children. +/// The rows below the first group row named `group`: its children. fn children(rows: &[PresetRow], group: &str) -> Vec<(String, usize)> { let at = rows.iter().position(|row| row.is_group() && row.label == group).expect("the folder is a row"); rows[at + 1..].iter().take_while(|row| row.depth > rows[at].depth).map(|row| (row.label.clone(), row.depth)).collect() @@ -58,33 +70,38 @@ fn loose(rows: &[PresetRow]) -> Vec { rows.iter().filter(|row| !row.is_group() && row.depth == 0).map(|row| row.label.clone()).collect() } +/// The id of the pattern the document's Pattern Overlay uses: the one the field holds when it opens. +fn shown_pattern(app: &PhotocraftApp) -> String { + app.session.active().expect("a document").doc.patterns[0].id.clone() +} + /// The library's folders are rows of their own, in the library's order. #[test] fn the_pattern_field_offers_the_library_folders_as_groups() { - assert_eq!(groups(&rows(&app_with_patterns())), ["Geometric", "Textures"], "the library's folders, in the library's order"); + assert_eq!(groups(&rows(&app_with_patterns())), ["Basics", "Cloth"], "the library's folders, in the library's order"); } /// A folder's patterns are rows below it, one level deeper, in the folder's order. #[test] fn a_folder_holds_its_patterns_as_rows_below_it() { let rows = rows(&app_with_patterns()); - let at = rows.iter().position(|row| row.is_group() && row.label == "Geometric").expect("the folder is a row"); + let at = rows.iter().position(|row| row.is_group() && row.label == "Basics").expect("the folder is a row"); assert_eq!(rows[at].depth, 0, "a top-level folder"); assert_eq!( - children(&rows, "Geometric"), - [("Checkerboard".to_string(), 1), ("Diagonal Lines".to_string(), 1), ("Dots".to_string(), 1), ("Grid".to_string(), 1)], + children(&rows, "Basics"), + [("Plain".to_string(), 1), ("Stripes".to_string(), 1), ("Dots".to_string(), 1)], "the folder's patterns, nested under it" ); } -/// Patterns in no folder come after every folder: the document's first, then the library's. +/// The pattern in no folder comes after every folder and its patterns, and is still a top-level row. #[test] fn the_patterns_in_no_folder_come_after_the_folders() { let rows = rows(&app_with_patterns()); - assert_eq!(loose(&rows), ["Loose Library", "Document Tile"], "the loose ones, at the top level, the document's before the library's as today"); + assert_eq!(loose(&rows), ["Unfiled"], "the pattern in no folder, at the top level"); let last_nested = rows.iter().rposition(|row| row.depth > 0).expect("a folder's pattern"); let first_loose = rows.iter().position(|row| !row.is_group() && row.depth == 0).expect("a loose pattern"); - assert!(first_loose > last_nested, "the loose patterns come after the folders and their patterns"); + assert!(first_loose > last_nested, "the loose pattern comes after the folders and their patterns"); } /// Every pattern paints its swatch, so the field shows the current pattern as a thumbnail and the popup shows tiles; @@ -97,12 +114,13 @@ fn every_pattern_row_carries_the_swatch_it_paints() { assert!(rows.iter().filter(|row| row.is_group()).all(|row| row.preview.is_none()), "a folder is a header, not a swatch"); } -/// A pattern the document and the library both hold is one row, and every pattern of the list is a row once: the -/// picker never gets two rows with one key. +/// Every offered pattern is a row once: the pattern the document and the library both hold stands once, and the count +/// is what the session offers rather than a number the test carries. #[test] -fn a_pattern_the_document_and_the_library_both_have_is_one_row() { - let rows = rows(&app_with_patterns()); - let shared: Vec<&PresetRow> = rows.iter().filter(|row| row.key == "loose-library").collect(); +fn every_offered_pattern_is_exactly_one_row() { + let app = app_with_patterns(); + let rows = rows(&app); + let shared: Vec<&PresetRow> = rows.iter().filter(|row| row.key == "tile-5").collect(); assert_eq!(shared.len(), 1, "the overlay copied it into the document and the library still has it"); assert_eq!(shared[0].depth, 0, "and it is in no folder, so it is a loose row"); let keys: Vec<&str> = rows.iter().filter(|row| !row.is_group()).map(|row| row.key.as_str()).collect(); @@ -111,7 +129,39 @@ fn a_pattern_the_document_and_the_library_both_have_is_one_row() { keys.iter().filter(|key| seen.insert(*key)).copied().collect() }; assert_eq!(keys, unique, "no pattern is a row twice"); - assert_eq!(keys.len(), 9, "the library's seven built-in patterns and the two added ones"); + let mut offered = std::collections::HashSet::new(); + offered.extend(app.session.patterns.items.iter().map(|pattern| pattern.id.clone())); + offered.extend(app.session.active().expect("a document").doc.patterns.iter().map(|pattern| pattern.id.clone())); + assert_eq!(keys.len(), offered.len(), "one row per offered pattern"); +} + +/// The page takes an empty Pattern value from the first non-group row, so with the measured order - folders before +/// the loose patterns - an overlay that names no pattern starts on the first pattern of the first folder, which is +/// not the document's own loose pattern the flat list used to start on. Pinned because the order is what the issue +/// measured. +#[test] +fn an_empty_pattern_value_resolves_to_the_first_folder_pattern() { + let rows = rows(&app_with_patterns()); + let first = rows.iter().find(|row| !row.is_group()).expect("a pattern row"); + assert_eq!(first.key, "tile-0", "the first pattern of the first folder"); + assert_eq!(first.label, "Plain"); + assert_eq!(shown_pattern(&app_with_patterns()), "tile-5", "which is not the pattern the document's overlay carries"); +} + +/// Two library folders may carry the same name, and their chevrons are their own: their rows need keys of their own. +#[test] +fn two_folders_of_the_same_name_toggle_apart() { + let mut app = app_with_patterns(); + app.session.presets_mut().pattern_groups.push(Group::new("Basics", vec!["tile-5".to_string()])); + let rows = rows(&app); + let named: Vec<&PresetRow> = rows.iter().filter(|row| row.is_group() && row.label == "Basics").collect(); + assert_eq!(named.len(), 2, "both folders show"); + assert_ne!(named[0].key, named[1].key, "and each chevron belongs to one of them"); + assert_eq!( + children(&rows, "Basics"), + [("Plain".to_string(), 1), ("Stripes".to_string(), 1), ("Dots".to_string(), 1)], + "the first of them keeps its patterns" + ); } /// A folder that holds none of the picker's patterns gets no header, and a pattern two folders both claim is a row @@ -119,13 +169,12 @@ fn a_pattern_the_document_and_the_library_both_have_is_one_row() { #[test] fn an_empty_folder_shows_no_header_and_a_shared_pattern_is_a_row_once() { let mut app = app_with_patterns(); - let first = app.session.patterns.items[0].id.clone(); let presets = app.session.presets_mut(); - presets.pattern_groups.push(photocraft_engine::presets::Group::new("Emptied", vec!["no-such-pattern".to_string()])); - presets.pattern_groups.push(photocraft_engine::presets::Group::new("Also Geometric", vec![first.clone()])); + presets.pattern_groups.push(Group::new("Emptied", vec!["no-such-pattern".to_string()])); + presets.pattern_groups.push(Group::new("Also Basics", vec!["tile-0".to_string()])); let rows = rows(&app); - assert_eq!(groups(&rows), ["Geometric", "Textures"], "the emptied folder shows no header and the shared one adds none"); - let twice = rows.iter().filter(|row| row.key == first).collect::>(); + assert_eq!(groups(&rows), ["Basics", "Cloth"], "the emptied folder shows no header and the shared one adds none"); + let twice: Vec<&PresetRow> = rows.iter().filter(|row| row.key == "tile-0").collect(); assert_eq!(twice.len(), 1, "the pattern two folders claim is one row"); assert_eq!(twice[0].depth, 1, "under the folder that came first"); } @@ -142,16 +191,11 @@ fn the_pattern_the_field_holds_is_a_row_with_a_swatch() { cancel(&mut app, id); } -/// The id of the pattern the open document's Pattern Overlay uses: the one the field holds when it opens. -fn shown_pattern(app: &PhotocraftApp) -> String { - app.session.active().expect("a document").doc.patterns[0].id.clone() -} - /// Cancel on the dialog: the layer keeps the pattern the overlay had. #[test] fn cancel_keeps_the_pattern_the_overlay_had() { let mut app = app_with_patterns(); - let (opened, picked) = (shown_pattern(&app), app.session.patterns.items[0].id.clone()); + let (opened, picked) = (shown_pattern(&app), "tile-0".to_string()); let id = open(&mut app, Some(EffectKind::PatternOverlay.key())).expect("the dialog opens"); let overlay = first_of(&app, id, EffectKind::PatternOverlay); edit(&mut app, id, |draft| draft.set_param(overlay, "pattern", super::StyleValue::Text(picked.clone()))); @@ -166,7 +210,7 @@ fn cancel_keeps_the_pattern_the_overlay_had() { #[test] fn ok_applies_the_pattern_the_field_picked() { let mut app = app_with_patterns(); - let (opened, picked) = (shown_pattern(&app), app.session.patterns.items[0].id.clone()); + let (opened, picked) = (shown_pattern(&app), "tile-0".to_string()); let id = open(&mut app, Some(EffectKind::PatternOverlay.key())).expect("the dialog opens"); let overlay = first_of(&app, id, EffectKind::PatternOverlay); assert_ne!(picked, opened, "the picked pattern is another one than the overlay had");