Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Layers panel options: document the functions and tests the PR adds

abc2cb7a671f1486bc
diff --git a/crates/engine/src/artboard_cmds/tests.rs b/crates/engine/src/artboard_cmds/tests.rs
index f225f1244..28e8d0587 100644
--- a/crates/engine/src/artboard_cmds/tests.rs
+++ b/crates/engine/src/artboard_cmds/tests.rs
@@ -586,6 +586,7 @@ fn add_beside_places_a_board_on_each_side_and_alt_duplicates_contents() {
assert!(s.execute("layer.artboard.addBeside", json!({"side": "diagonal"})).is_err());
}
+/// Adding a duplicated artboard beside another appends "copy" to its name only while the Layers panel option is on.
#[test]
fn add_beside_duplicate_honors_panel_copy_name_preference() {
for add_copy in [false, true] {
diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rs
index a87c4badd..2081db277 100644
--- a/crates/engine/src/layer_multi_cmds.rs
+++ b/crates/engine/src/layer_multi_cmds.rs
@@ -2787,6 +2787,7 @@ mod tests {
mod layers_panel_parity_tests {
use super::*;
+ /// A 24×16 document with three layers, "Test 0" to "Test 2", above the Background; the last one is active.
fn new_session() -> (Session, Vec<LayerId>) {
let mut s = Session::new();
s.execute("file.new", json!({"width": 24, "height": 16})).unwrap();
@@ -2794,6 +2795,7 @@ mod layers_panel_parity_tests {
(s, ids)
}
+ /// Set one Layers panel option in the preferences, as the Panel Options dialog does.
fn option(s: &mut Session, key: &str, enabled: bool) {
s.prefs.edit(|p| {
let val = p.dialogs.entry("ui.layersPanelOptions".into()).or_insert_with(|| json!({}));
@@ -2803,6 +2805,7 @@ mod layers_panel_parity_tests {
});
}
+ /// The drag preview equals the committed `layer.moveTo` result and never touches the document or its history.
#[test]
fn preview_matches_real_move_and_never_edits_document_or_history() {
let (mut s, ids) = new_session();
@@ -2816,6 +2819,7 @@ mod layers_panel_parity_tests {
assert_eq!(candidate, *s.active().unwrap().doc, "visual preview must match committed layer order");
}
+ /// A previewed ⌥-drag copy names its layers like the committed copy, with and without the "copy" suffix option.
#[test]
fn preview_copy_and_no_copy_suffix_option_match_committed_layers() {
let (mut s, ids) = new_session();
@@ -2836,6 +2840,7 @@ mod layers_panel_parity_tests {
assert!(s.active().unwrap().doc.layer(LayerId(d["layer"].as_u64().unwrap())).unwrap().name.contains("copy"));
}
+ /// Drops the command refuses give no preview and leave the document alone; a multi-layer preview follows the committed stack order.
#[test]
fn preview_rejects_invalid_drop_unchanged() {
let (mut s, ids) = new_session();
@@ -2882,6 +2887,7 @@ mod layers_panel_parity_tests {
doc.layers.iter().map(|l| format!("{}{}", l.name, if l.clipped { "*" } else { "" })).collect::<Vec<_>>().join(" ")
}
+ /// The id of the layer called `name`, searching the whole layer tree.
fn id_of(s: &Session, name: &str) -> u64 {
s.active().unwrap().doc.walk().into_iter().find(|(_, _, l)| l.name == name).unwrap().2.id.0
}
@@ -2898,6 +2904,7 @@ mod layers_panel_parity_tests {
assert_eq!(photocraft_compose::flatten(&preview).px, photocraft_compose::flatten(committed).px, "composite of {params}");
}
+ /// Previewed moves join and release clipping groups exactly as the committed reorder does, copies included.
#[test]
fn preview_follows_clip_groups_like_the_committed_reorder() {
// The fixtures of `clip_group::reorder_joins_and_leaves_groups`.
@@ -2924,6 +2931,7 @@ mod layers_panel_parity_tests {
assert_preview_is_commit(s, json!({"layer": u, "target": c1, "position": "above", "copy": true}), "Background B C1* U copy* C2* U");
}
+ /// Copying the locked Background in a preview gives the same unlocked copy, joining the clip group it lands in, as the command.
#[test]
fn preview_copies_the_background_like_the_committed_copy() {
let mut s = clip_stack("A B*");
@@ -2944,6 +2952,7 @@ mod layers_panel_parity_tests {
assert_eq!(photocraft_compose::flatten(&preview).px, photocraft_compose::flatten(committed).px);
}
+ /// A drop that would move the Background off the bottom has no preview, as the command refuses it.
#[test]
fn preview_refuses_what_would_leave_the_background_off_the_bottom() {
let mut s = clip_stack("A B");
@@ -2960,6 +2969,7 @@ mod layers_panel_parity_tests {
}
}
+ /// The UI runs commands through `start`; Expand New Effects applies there as well as to `execute`.
#[test]
fn expand_new_effects_applies_to_ui_start_dispatch_too() {
let (mut s, ids) = new_session();
@@ -2970,6 +2980,7 @@ mod layers_panel_parity_tests {
assert!(s.active().unwrap().fx_collapsed.contains(&target));
}
+ /// Whether new effects start expanded is view state: it adds no history step, and editing an existing effect keeps its disclosure.
#[test]
fn expand_new_effects_is_view_state_and_keeps_edit_history_clean() {
let (mut s, ids) = new_session();
diff --git a/crates/ui-egui/src/layers_panel_ui.rs b/crates/ui-egui/src/layers_panel_ui.rs
index ef6bbccf7..43955812d 100644
--- a/crates/ui-egui/src/layers_panel_ui.rs
+++ b/crates/ui-egui/src/layers_panel_ui.rs
@@ -67,6 +67,8 @@ pub(crate) struct ReorderPreview {
pub document: std::sync::Arc<Document>,
}
+/// Cache the on-canvas preview of the layer drop in `payload` (a `layer.moveTo` request), unless Preview on Canvas is off.
+/// Marks the destination as hovered every frame; the preview itself is rebuilt only when the target changes.
pub(crate) fn preview_reorder(app: &mut PhotocraftApp, payload: &serde_json::Value) {
if !app.ui.layers_panel_options.preview_on_canvas_when_reordering_layers {
app.reorder_preview = None;
@@ -96,6 +98,7 @@ pub(crate) fn preview_reorder(app: &mut PhotocraftApp, payload: &serde_json::Val
});
}
+/// The cached reorder preview and its compositor cache key for document `index`, if it was built for that document's current revision.
pub(crate) fn display_reorder(app: &PhotocraftApp, index: usize) -> Option<(std::sync::Arc<Document>, u64)> {
if !app.ui.layers_panel_options.preview_on_canvas_when_reordering_layers {
return None;
@@ -244,6 +247,7 @@ fn options_body(ui: &mut Ui, draft: &mut LayersPanelOptions) {
ui.checkbox(&mut draft.show_layer_mask_badges, tl!("Show layer mask badges"));
}
+/// Show the pending Layers panel dialog (Panel Options, New Layer, New Group, Lock Layers) as a modal and apply it on OK.
pub fn show_dialog(app: &mut PhotocraftApp, ctx: &Context) {
let Some(mut dialog) = app.ui.layers_panel_dialog.take() else { return };
let mut confirmed = false;
@@ -335,6 +339,7 @@ pub fn show_dialog(app: &mut PhotocraftApp, ctx: &Context) {
mod tests {
use super::*;
+ /// A fresh install shows Medium thumbnails of the whole document, like Photoshop's Panel Options defaults.
#[test]
fn default_panel_options_match_medium_document_previews() {
let options = LayersPanelOptions::default();
@@ -366,6 +371,7 @@ mod tests {
assert!(!app.session.prefs().interface.clip_thumbnails_to_layer_bounds);
}
+ /// The options survive a UiState round trip; an open dialog is view state and is never serialized.
#[test]
fn options_round_trip_without_a_pending_dialog() {
let mut ui = crate::state::UiState::default();
@@ -379,6 +385,7 @@ mod tests {
assert!(restored.layers_panel_dialog.is_none());
}
+ /// Every option, including the mask badges, goes to the preferences and comes back from them.
#[test]
fn prefs_options_save_and_restore_all_switches() {
let mut session = photocraft_engine::Session::new();
@@ -401,6 +408,7 @@ mod tests {
assert_eq!(app.ui.layers_panel_options, saved);
}
+ /// The flyout's Flatten Image runs the real `layer.flattenImage` command, and Undo brings the layers back.
#[test]
fn flyout_flatten_routes_to_real_engine_and_undo_restores_layers() {
let mut session = photocraft_engine::Session::new();
@@ -420,6 +428,7 @@ mod tests {
assert_eq!(app.session.active().unwrap().doc.layers.len(), count);
}
+ /// Thumbnail and row sizes grow from None to Large in both themes, with the rows at least as tall as the thumbnails.
#[test]
fn geometry_matches_four_real_panel_sizes() {
for pro in [false, true] {
@@ -434,6 +443,7 @@ mod tests {
assert_eq!(thumbnail_geometry(LayerThumbnailSize::Large, true), (48.0, 56.0));
}
+ /// Preferences saved before an option existed still load, with the default for the missing ones.
#[test]
fn panel_options_missing_fields_deserialize_with_defaults() {
let options: LayersPanelOptions = serde_json::from_value(json!({})).unwrap();
diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs
index 0922e800f..7c227e186 100644
--- a/crates/ui-egui/src/state.rs
+++ b/crates/ui-egui/src/state.rs
@@ -39,6 +39,7 @@ pub struct LayersPanelOptions {
}
impl LayersPanelOptions {
+ /// Every option at the Photoshop default: Medium thumbnails, filters shown, new effects expanded, no mask badges.
pub fn defaults() -> Self {
Self {
thumbnail_size: LayerThumbnailSize::Medium,
@@ -53,6 +54,7 @@ impl LayersPanelOptions {
}
impl Default for LayersPanelOptions {
+ /// Same as [`LayersPanelOptions::defaults`], so serde fills missing fields with the Photoshop defaults.
fn default() -> Self {
Self::defaults()
}