Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Albums: address review of folder creation

Nathaniel Ledford5443ae28f9ad
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 9829e0576..17ed2ef68 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -229,12 +229,12 @@ fn adjust_brush(app: &mut LightcraftApp, k: f32, df: f32) -> Value {
}
}
-/// An sRGB colour from `"#rrggbb"` or `[r, g, b]` (0..255).
/// The `parent` folder id of a `dialog.new*` command (none: the top level).
fn parent_param(p: &Value) -> Option<u64> {
p.get("parent").and_then(Value::as_u64)
}
+/// An sRGB colour from `"#rrggbb"` or `[r, g, b]` (0..255).
pub fn parse_rgb(v: &Value) -> Option<[u8; 3]> {
if let Some(s) = v.as_str() {
let h = s.strip_prefix('#').unwrap_or(s);
diff --git a/crates/ui-egui/src/panels/dialogs.rs b/crates/ui-egui/src/panels/dialogs.rs
index 4eff2f391..b1c168e67 100644
--- a/crates/ui-egui/src/panels/dialogs.rs
+++ b/crates/ui-egui/src/panels/dialogs.rs
@@ -847,7 +847,18 @@ pub fn show(app: &mut LightcraftApp, ctx: &egui::Context) {
match confirm_dialog(app, &dlg) {
// the import review stays open on an error (e.g. an unusable folder template), and so
// does Export (e.g. no folder chosen) so the choices aren't lost
- Err(e) if matches!(dlg, Dialog::Import { .. } | Dialog::Export { .. }) => app.toast(ctx, e),
+ Err(e)
+ if matches!(
+ dlg,
+ Dialog::Import { .. }
+ | Dialog::Export { .. }
+ | Dialog::NewAlbum { .. }
+ | Dialog::NewSmartAlbum { .. }
+ | Dialog::SmartRules { .. }
+ ) =>
+ {
+ app.toast(ctx, e)
+ }
// the SAM 3 dialog stays open to show the download (or why it can't start)
Err(e) if matches!(dlg, Dialog::SamModel { .. }) => {
if let Dialog::SamModel { error, .. } = &mut dlg {
diff --git a/crates/ui-egui/src/panels/left.rs b/crates/ui-egui/src/panels/left.rs
index 100493e2b..12db28b2f 100644
--- a/crates/ui-egui/src/panels/left.rs
+++ b/crates/ui-egui/src/panels/left.rs
@@ -218,10 +218,10 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
app.ui.dialog = Some(crate::state::Dialog::NewAlbum { name: String::new(), folder: true, parent: None });
}
});
+ // the album just made: the folders down to it open, once (also when the section is shut)
+ let reveal = app.ui.reveal_album.take();
if albums_open {
let albums: Vec<Album> = app.session.catalog.albums().cloned().collect();
- // the album just made: the folders down to it open, once
- let reveal = app.ui.reveal_album.take();
let mut open_to = Vec::new();
let mut cur = reveal.map(AlbumId).and_then(|id| albums.iter().find(|a| a.id == id)).and_then(|a| a.parent);
while let Some(p) = cur.filter(|p| !open_to.contains(p) && open_to.len() < 64) {
@@ -666,8 +666,15 @@ fn albums_tree(app: &mut LightcraftApp, ui: &mut egui::Ui, all: &[Album], parent
}
let resp = row(app, ui, &format!("folder:{}", a.id.0), Icon::Folder, &a.name, None, false, indent);
// a folder is no source, so its row folds it too; the triangle is the same click, aimed
- let tri = disclosure_triangle(ui, &resp, indent, open, egui::Id::new(("album-tri", a.id.0)), format!("albumToggle:{}", a.id.0));
- if resp.clicked() || tri.clicked() {
+ let has_children = all.iter().any(|c| c.parent == Some(a.id));
+ let mut toggled = resp.clicked();
+ if has_children {
+ let tri = disclosure_triangle(ui, &resp, indent, open, egui::Id::new(("album-tri", a.id.0)), format!("albumToggle:{}", a.id.0));
+ toggled |= tri.clicked();
+ // the triangle sits on the row and takes its clicks: the menu opens from it too
+ folder_menu(app, &tri, a);
+ }
+ if toggled {
ui.data_mut(|d| d.insert_temp(open_id, !open));
}
folder_menu(app, &resp, a);
@@ -725,10 +732,10 @@ fn folder_menu(app: &mut LightcraftApp, resp: &egui::Response, a: &Album) {
// the same commands as File ▸ New…, aimed at this folder
ui.menu_button(crate::i18n::tr("New"), |ui| {
for (label, command) in [
- ("Album…", "dialog.newAlbum"),
- ("Smart Album…", "dialog.smartAlbum"),
- ("Smart Album from Filter…", "dialog.newSmartAlbum"),
- ("Folder…", "dialog.newFolder"),
+ ("Create Album…", "dialog.newAlbum"),
+ ("Create Smart Album…", "dialog.smartAlbum"),
+ ("Create Smart Album from Filter…", "dialog.newSmartAlbum"),
+ ("Create Folder…", "dialog.newFolder"),
] {
if ui.button(crate::i18n::tr(label)).clicked() {
let _ = app.run(command, json!({"parent": a.id.0}));
diff --git a/crates/ui-egui/src/tests_panels.rs b/crates/ui-egui/src/tests_panels.rs
index edb71fde9..5f81ee36f 100644
--- a/crates/ui-egui/src/tests_panels.rs
+++ b/crates/ui-egui/src/tests_panels.rs
@@ -252,8 +252,37 @@ fn album_folders_have_a_disclosure_triangle() {
assert!(has(&h, &format!("source:album:{best}")));
toggle(&mut h, europe);
assert!(!has(&h, &format!("source:album:{best}")) && has(&h, &format!("source:folder:{europe}")));
- // the triangle is not the row: clicking it does not make the folder the source
- assert_ne!(h.app.session.source, lightcraft_engine::LibrarySource::Album(lightcraft_catalog::AlbumId(europe)));
+ // an empty folder has nothing to open: no triangle until it holds something
+ let empty = make(&mut h, json!({"name": "Empty", "folder": true}));
+ h.step();
+ h.step();
+ assert!(has(&h, &format!("source:folder:{empty}")) && !has(&h, &format!("albumToggle:{empty}")));
+ make(&mut h, json!({"name": "First", "parent": empty}));
+ h.step();
+ h.step();
+ assert!(has(&h, &format!("albumToggle:{empty}")), "it gets one with its first child");
+}
+
+/// A dialog that cannot do its job (here: the chosen parent is a plain album, as when the folder
+/// was deleted or turned out not to be one) stays open with the error shown, the typed name kept.
+#[test]
+fn creating_in_a_non_folder_keeps_the_dialog_open() {
+ let mut h = demo([1400.0, 900.0], json!({"view": "photoGrid", "leftPanel": true}));
+ let plain = h.app.session.execute("album.create", &json!({"name": "Plain"})).unwrap()["id"].as_u64().unwrap();
+ let before = h.app.session.catalog.albums().count();
+ let r = h.request("engine.execute", json!({"command": "dialog.newAlbum", "params": {"name": "Kept", "parent": plain}}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ h.step();
+ let r = h.request("ui.clickWidget", json!({"id": "button:dialogOk"}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ h.step();
+ h.step();
+ assert_eq!(h.app.session.catalog.albums().count(), before, "nothing was made");
+ assert!(
+ matches!(&h.app.ui.dialog, Some(crate::state::Dialog::NewAlbum { name, .. }) if name == "Kept"),
+ "the dialog stays, with its text: {:?}",
+ h.app.ui.dialog
+ );
}
/// Creating from a folder's menu: every `dialog.new*` command takes the folder to create in, the
@@ -265,6 +294,7 @@ fn albums_are_created_inside_the_chosen_folder() {
let make = |h: &mut Headless, params: serde_json::Value| h.app.session.execute("album.create", &params).unwrap()["id"].as_u64().unwrap();
let trips = make(&mut h, json!({"name": "Trips", "folder": true}));
let europe = make(&mut h, json!({"name": "Europe", "folder": true, "parent": trips}));
+ make(&mut h, json!({"name": "Seed", "parent": europe}));
h.step();
h.step();
let has = |h: &Headless, id: &str| h.app.widgets.iter().any(|(w, _)| w == id);
@@ -317,6 +347,11 @@ fn album_dialogs_without_a_parent_still_deserialize() {
assert!(matches!(d, Dialog::NewAlbum { parent: None, .. }));
let d: Dialog = serde_json::from_value(json!({"kind": "newSmartAlbum", "name": "x"})).unwrap();
assert!(matches!(d, Dialog::NewSmartAlbum { parent: None, .. }));
+ let mut v = serde_json::to_value(Dialog::SmartRules { id: None, name: "x".into(), rules: Default::default(), parent: Some(3) }).unwrap();
+ assert_eq!(v["parent"], 3);
+ v.as_object_mut().unwrap().remove("parent");
+ let d: Dialog = serde_json::from_value(v).unwrap();
+ assert!(matches!(d, Dialog::SmartRules { parent: None, .. }));
}
/// A headless app over a library of file-backed photos that exist only in the catalog.