Skip to content

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

Layers panel: show the lock icon for every lock (#4158) (#4159)

David Manna8a30aed33fa
diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs
index 354de06d3..e2a46e407 100644
--- a/crates/doc/src/lib.rs
+++ b/crates/doc/src/lib.rs
@@ -99,6 +99,11 @@ pub struct Locks {
}
impl Locks {
+ /// Whether any lock is set: Photoshop then shows the lock icon on the layer's row.
+ pub fn any(self) -> bool {
+ self.transparency || self.pixels || self.position || self.artboard || self.all
+ }
+
pub fn union(self, o: Locks) -> Locks {
Locks {
transparency: self.transparency || o.transparency,
@@ -1195,6 +1200,19 @@ mod tests {
Document::with_background("t", Size::new(100, 50), ColorMode::Rgb, SampleType::U8, Color::WHITE)
}
+ #[test]
+ fn any_lock_counts_every_lock() {
+ assert!(!Locks::default().any());
+ let each = [
+ Locks { transparency: true, ..Locks::default() },
+ Locks { pixels: true, ..Locks::default() },
+ Locks { position: true, ..Locks::default() },
+ Locks { artboard: true, ..Locks::default() },
+ Locks { all: true, ..Locks::default() },
+ ];
+ assert!(each.iter().all(|k| k.any()));
+ }
+
#[test]
fn unused_link_group_never_wraps() {
let mut d = doc();
diff --git a/crates/ui-egui/src/layer_row_ui.rs b/crates/ui-egui/src/layer_row_ui.rs
index fb229fcbc..7ac725a7c 100644
--- a/crates/ui-egui/src/layer_row_ui.rs
+++ b/crates/ui-egui/src/layer_row_ui.rs
@@ -71,7 +71,7 @@ pub fn layout(row: Rect, name_left: f32, items: &[(Indicator, f32)]) -> (Vec<(In
/// Width the always-shown indicators of `l` take at the row's right (blend label excluded).
pub fn reserved_width(l: &Layer) -> f32 {
let mut w = RIGHT_PAD;
- if l.locks.transparency || l.locks.position || l.locks.all {
+ if l.locks.any() {
w += ICON_W + GAP;
}
if !l.effects.items.is_empty() {
@@ -113,7 +113,7 @@ pub fn indicators(
actions: &mut Vec<(String, Value)>,
) -> (f32, Vec<(Indicator, Rect)>, bool) {
let t = Tokens::get(ui.ctx());
- let locked = l.locks.transparency || l.locks.position || l.locks.all;
+ let locked = l.locks.any();
let fx_font = theme::semibold(11.0);
let blend_font = FontId::proportional(10.5);
let has_fx = !l.effects.items.is_empty();
diff --git a/crates/ui-egui/src/layer_row_ui/tests.rs b/crates/ui-egui/src/layer_row_ui/tests.rs
index f2ac633ba..e134897cf 100644
--- a/crates/ui-egui/src/layer_row_ui/tests.rs
+++ b/crates/ui-egui/src/layer_row_ui/tests.rs
@@ -3,7 +3,7 @@
use egui::{Modifiers, PointerButton, Pos2, Rect, pos2, vec2};
use egui_kittest::Harness;
-use egui_kittest::kittest::Queryable;
+use egui_kittest::kittest::{NodeT, Queryable};
use photocraft_doc::{Effect, LayerContent};
use serde_json::json;
@@ -536,3 +536,48 @@ fn press_drag_release_on_the_fx_button_opens_that_style() {
let effects = open[0].fields["effects"].as_array().unwrap();
assert!(effects.iter().any(|e| e["kind"] == json!("dropShadow")), "on Drop Shadow: {effects:?}");
}
+
+/// One layer per lock, each with only that lock set (#4158).
+fn one_lock_each() -> (photocraft_engine::Session, Vec<(&'static str, u64)>) {
+ let mut s = photocraft_engine::Session::new();
+ s.execute("file.new", json!({"width": 64, "height": 48})).unwrap();
+ let mut ids = Vec::new();
+ for key in ["transparency", "pixels", "position", "artboard", "all"] {
+ let id = s.execute("layer.new.layer", json!({"name": key})).unwrap()["layer"].as_u64().unwrap();
+ s.execute("layer.setProps", json!({"layer": id, "locks": {key: true}})).unwrap();
+ ids.push((key, id));
+ }
+ (s, ids)
+}
+
+#[test]
+fn every_lock_shows_the_lock_icon_on_the_row() {
+ // #4158: "Lock image pixels" and the artboard lock left the row without its lock icon.
+ // Photoshop shows it whenever any lock is set.
+ let (s, ids) = one_lock_each();
+ let doc = &s.active().unwrap().doc;
+ for &(key, id) in &ids {
+ let l = doc.layers.iter().find(|l| l.id.0 == id).unwrap();
+ assert_eq!(super::reserved_width(l), super::RIGHT_PAD + super::ICON_W + super::GAP, "{key}: room for the lock icon");
+ }
+ let h = harness(s, 1.0, "promedium", 290.0);
+ let rows = recorded(&h.ctx);
+ for &(key, id) in &ids {
+ let r = rows.iter().find(|r| r.layer == id).unwrap_or_else(|| panic!("{key}: row drawn"));
+ assert!(r.indicators.iter().any(|(k, _)| *k == Indicator::Lock), "{key}: lock icon drawn ({:?})", r.indicators);
+ }
+}
+
+#[test]
+fn the_studio_lock_button_shows_every_lock() {
+ // #4158: the simple themes' lock button drew "unlocked" for a pixels or artboard lock, though
+ // clicking it then unlocked the layer.
+ for key in ["transparency", "pixels", "position", "artboard", "all"] {
+ let mut s = photocraft_engine::Session::new();
+ s.execute("file.new", json!({"width": 64, "height": 48})).unwrap();
+ s.execute("layer.new.layer", json!({"name": key})).unwrap();
+ s.execute("layer.setProps", json!({"locks": {key: true}})).unwrap();
+ let h = harness(s, 1.0, "studio", 290.0);
+ assert_eq!(h.get_by_label("Lock layer").accesskit_node().toggled(), Some(egui::accesskit::Toggled::True), "{key}");
+ }
+}
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index bb4d2f1e9..f0e004671 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -2013,9 +2013,8 @@ fn simple_lock_toggle(background: bool, l: &Layer) -> (String, Value) {
if background {
return ("layer.new.layerFromBackground".into(), json!({}));
}
- let k = &l.locks;
- let locked = k.transparency || k.pixels || k.position || k.artboard || k.all;
- let locks = if locked { json!({"transparency": false, "pixels": false, "position": false, "artboard": false, "all": false}) } else { json!({"all": true}) };
+ let locks =
+ if l.locks.any() { json!({"transparency": false, "pixels": false, "position": false, "artboard": false, "all": false}) } else { json!({"all": true}) };
("layer.setProps".into(), json!({"layer": l.id.0, "locks": locks}))
}
@@ -2174,7 +2173,7 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
});
}
if !t.pro {
- let locked = l.locks.transparency || l.locks.position || l.locks.all;
+ let locked = l.locks.any();
if icons::button(ui, if locked { "lock" } else { "lock-open" }, 22.0, locked, tl!("Lock layer")).clicked() {
actions.push(simple_lock_toggle(bg, l));
}
@@ -4604,7 +4603,7 @@ mod lock_tests {
click_lock(&mut app);
let l = &app.session.active().unwrap().doc.layers[0];
assert_eq!(l.name, "Layer 0");
- assert!(!(l.locks.transparency || l.locks.pixels || l.locks.position || l.locks.all));
+ assert!(!l.locks.any());
erase_line(&mut app);
assert!(!app.ui.status_error, "{}", app.ui.status);
assert_eq!(app.session.active().unwrap().doc.layers[0].surface().unwrap().rgba(50, 40)[3], 0.0, "erased to transparency");
@@ -4612,8 +4611,7 @@ mod lock_tests {
click_lock(&mut app);
assert!(app.session.active().unwrap().doc.layers[0].locks.all);
click_lock(&mut app);
- let k = app.session.active().unwrap().doc.layers[0].locks;
- assert!(!(k.transparency || k.pixels || k.position || k.artboard || k.all));
+ assert!(!app.session.active().unwrap().doc.layers[0].locks.any());
// A layer with only transparency locked unlocks in one click.
app.run("layer.setProps", json!({"locks": {"transparency": true}})).unwrap();
click_lock(&mut app);