Skip to content

Commit 6bfe223

Browse files
committed
LRC-SS-TEMPLATES: slideshow templates: built-ins of our own, user templates, browser with hover preview
A slideshow template is the Playback (not "prepare previews"), Layout / Options / Backdrop, Overlays and Titles settings as one versioned JSON document (format lightcraft.slideshowTemplate, version 1). - Engine: slide_template (SlideLook, Template, built-ins, user template files in <settings>/slideshow-templates read through each section's own merged() validation; size and count caps; newer versions refused; bad files reported and skipped; file names sanitised so ids never leave the folder). Applying replaces the four settings in one step and keeps the replaced ones as "previous" for the session (prefs have no undo). - Five built-ins of our own: Plain Black, Gallery Wall, Edge to Edge, Cinema Wide, Shooting Data (no Lightroom names or designs, no images). - Commands slideshow.templates.list/apply/save/update/rename/delete/ import/export; slideshow.preview {template} previews without applying. - UI: Template Browser at the top of Settings > Slideshow (built-in, user, previous settings; click applies, hover previews on the selected photo, right-click update / rename / export / delete; save and import), file dialogs on the desktop; strings in the nine locales. - Docs: parity row (partial), whats-new, control protocol and MCP.
1 parent f980784 commit 6bfe223

42 files changed

Lines changed: 2135 additions & 57 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/lightcraft-cli/src/main.rs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -732,12 +732,15 @@ fn snapshot(args: &[String]) -> Result<(), String> {
732732
let t0 = Instant::now();
733733
let mut session = match &library {
734734
Some(dir) => {
735-
let mut s = Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models();
735+
let mut s =
736+
Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_slide_templates();
736737
s.open_library(dir, false).map_err(|e| library_error(dir, e))?;
737738
s
738739
}
739-
None if files.is_empty() => Session::with_demo().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models(),
740-
None => Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models(),
740+
None if files.is_empty() => {
741+
Session::with_demo().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_slide_templates()
742+
}
743+
None => Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_slide_templates(),
741744
};
742745
// Apply compute policy before imports or the first scripted query can discover an adapter.
743746
session.execute("app.gpu", &json!({"enabled": false})).map_err(|e| e.to_string())?;

‎apps/lightcraft-web/src/web.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ fn services(originals: Originals, backend: Option<Backend>, files: Files, frozen
218218
save_keyword_list: None,
219219
pick_curve_preset_files: None,
220220
save_curve_preset_file: None,
221+
pick_slideshow_templates: None,
222+
save_slideshow_template: None,
221223
write: Some(Box::new(download)),
222224
// downloads happen on the main thread: exports run in the foreground on the web
223225
write_shared: None,

‎apps/lightcraft/src/main.rs‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,24 @@ fn services(ctx: egui::Context, log_file: Option<&std::path::Path>) -> Services
567567
.save_file()
568568
.map(|p| p.to_string_lossy().to_string())
569569
})),
570+
pick_slideshow_templates: Some(Box::new(|| {
571+
rfd::FileDialog::new()
572+
.set_title(lightcraft_ui_egui::i18n::tr("Import Slideshow Templates"))
573+
.add_filter_nocase(lightcraft_ui_egui::i18n::tr("Slideshow Templates"), &["json"])
574+
.pick_files()
575+
.unwrap_or_default()
576+
.into_iter()
577+
.map(|p| p.to_string_lossy().to_string())
578+
.collect()
579+
})),
580+
save_slideshow_template: Some(Box::new(|name: &str| {
581+
rfd::FileDialog::new()
582+
.set_title(lightcraft_ui_egui::i18n::tr("Export Slideshow Template"))
583+
.add_filter_nocase(lightcraft_ui_egui::i18n::tr("Slideshow Templates"), &["json"])
584+
.set_file_name(name)
585+
.save_file()
586+
.map(|p| p.to_string_lossy().to_string())
587+
})),
570588
// atomic (temp file + sync + rename): a failed write never leaves a truncated file
571589
write_shared: Some(std::sync::Arc::new(lightcraft_engine::export::write_file)),
572590
write: Some(Box::new(lightcraft_engine::export::write_file)),
@@ -579,10 +597,12 @@ fn services(ctx: egui::Context, log_file: Option<&std::path::Path>) -> Services
579597
}
580598
}
581599

582-
/// The library session with the folder face models are kept in (`<config>/models`).
600+
/// The library session with the folders face models (`<config>/models`) and slideshow templates
601+
/// (`<config>/slideshow-templates`) are kept in.
583602
fn open_session(in_memory: bool, dir: Option<std::path::PathBuf>, seed_demo: bool) -> (Session, Option<LibraryProblem>) {
584603
let (mut s, problem) = open_library_session(in_memory, dir, seed_demo);
585604
s.face_models_dir = lightcraft_engine::config::default_face_models_dir();
605+
s.slide_templates_dir = lightcraft_engine::config::config_dir().map(|d| d.join(lightcraft_engine::slide_template::DIR_NAME));
586606
(s, problem)
587607
}
588608

‎crates/engine/src/cmd/slideshow.rs‎

Lines changed: 200 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
//! the identity plate, rating stars, text overlays with tokens and the watermark drawn on every
77
//! slide (also a library preference). `slideshow.titles`: the intro and ending screens (colour,
88
//! identity plate or text, scale, duration; a library preference). `slideshow.preview` composes
9-
//! one slide, or a title screen, as a PNG.
9+
//! one slide, or a title screen, as a PNG (with the library's settings, or a template's).
10+
//! `slideshow.templates.*`: the Template Browser (built-in and user templates; see
11+
//! [`crate::slide_template`]).
1012
1113
use lightcraft_catalog::PhotoId;
1214
use serde_json::{Value, json};
1315

1416
use super::{CommandSpec, always, bad, cmd, str_param};
17+
use crate::slide_template::{self as tpl, SlideLook};
1518
use crate::{Result, Session};
1619

1720
/// Largest side `slideshow.preview` renders.
@@ -71,7 +74,7 @@ fn titles(s: &mut Session, p: &Value) -> Result<Value> {
7174

7275
/// A slide size from the request: both sides given, one (the other from the aspect preview), or
7376
/// neither (1280 wide). Each side 16–[`PREVIEW_MAX`].
74-
fn preview_size(s: &Session, p: &Value) -> Result<(usize, usize)> {
77+
fn preview_size(layout: &lightcraft_slideshow::SlideSettings, p: &Value) -> Result<(usize, usize)> {
7578
const C: &str = "slideshow.preview";
7679
let side = |k: &str| -> Result<Option<usize>> {
7780
match p.get(k) {
@@ -82,7 +85,7 @@ fn preview_size(s: &Session, p: &Value) -> Result<(usize, usize)> {
8285
},
8386
}
8487
};
85-
let ratio = s.slide_layout.aspect.ratio(f64::NAN);
88+
let ratio = layout.aspect.ratio(f64::NAN);
8689
let clamp = |v: f64| (v.round() as usize).clamp(16, PREVIEW_MAX);
8790
Ok(match (side("width")?, side("height")?) {
8891
(Some(w), Some(h)) => (w, h),
@@ -110,7 +113,7 @@ fn encode_png(img: &lightcraft_raster::Rgba8) -> Result<Vec<u8>> {
110113
}
111114

112115
/// `slideshow.preview {title}`: a title screen (whether or not it is switched on).
113-
fn preview_title(s: &mut Session, p: &Value, title: &Value) -> Result<Value> {
116+
fn preview_title(s: &mut Session, p: &Value, title: &Value, look: &SlideLook) -> Result<Value> {
114117
const C: &str = "slideshow.preview";
115118
let t = title
116119
.as_str()
@@ -119,18 +122,18 @@ fn preview_title(s: &mut Session, p: &Value, title: &Value) -> Result<Value> {
119122
if p.get("id").is_some_and(|v| !v.is_null()) {
120123
return Err(bad(C, "pass `id` or `title`, not both"));
121124
}
122-
let (w, h) = preview_size(s, p)?;
125+
let (w, h) = preview_size(&look.layout, p)?;
123126
let path = str_param(p, "path").map(str::trim).filter(|x| !x.is_empty());
124127
if let Some(path) = path {
125128
s.check_write_target(path).map_err(|e| bad(C, e))?;
126129
}
127-
let c = s.title_compose(t, w, h);
130+
let c = crate::slide_title::TitleCompose::new(&look.titles, &look.overlays, t, w, h);
128131
let img = c.compose();
129132
let png = encode_png(&img)?;
130133
let rect = |r: lightcraft_slideshow::Rect| json!([r.x0, r.y0, r.x1, r.y1]);
131134
let mut out = json!({
132135
"title": t.id(),
133-
"enabled": s.slide_titles.screen(t).enabled,
136+
"enabled": look.titles.screen(t).enabled,
134137
"width": img.width,
135138
"height": img.height,
136139
"plate": c.plate_box().map(rect),
@@ -141,8 +144,19 @@ fn preview_title(s: &mut Session, p: &Value, title: &Value) -> Result<Value> {
141144

142145
fn preview(s: &mut Session, p: &Value) -> Result<Value> {
143146
const C: &str = "slideshow.preview";
147+
// a template's look (the Template Browser's hover preview), else the library's settings
148+
let template = match p.get("template") {
149+
None | Some(Value::Null) => None,
150+
Some(Value::String(id)) => Some(s.slide_template(id.trim()).map_err(|e| bad(C, e))?),
151+
Some(v) => return Err(bad(C, format!("`template` must be a template id, not {v}"))),
152+
};
153+
let look = template.as_ref().map_or_else(|| SlideLook::of(s), |t| t.look.clone());
144154
if let Some(t) = p.get("title").filter(|v| !v.is_null()) {
145-
return preview_title(s, p, t);
155+
let mut out = preview_title(s, p, t, &look)?;
156+
if let Some(t) = &template {
157+
out["template"] = json!(t.id);
158+
}
159+
return Ok(out);
146160
}
147161
let id = match p.get("id") {
148162
None | Some(Value::Null) => s.active().ok_or_else(|| bad(C, "no photo selected (select one or pass `id`)"))?,
@@ -151,13 +165,13 @@ fn preview(s: &mut Session, p: &Value) -> Result<Value> {
151165
if s.catalog.photo(id).is_none() {
152166
return Err(bad(C, format!("no photo {}", id.0)));
153167
}
154-
let (w, h) = preview_size(s, p)?;
168+
let (w, h) = preview_size(&look.layout, p)?;
155169
let path = str_param(p, "path").map(str::trim).filter(|x| !x.is_empty());
156170
if let Some(path) = path {
157171
s.check_write_target(path).map_err(|e| bad(C, e))?;
158172
}
159-
let settings = s.slide_layout.clone();
160-
let img = s.slide_now(&settings, id, w, h).map_err(crate::EngineError::Other)?;
173+
let settings = look.layout.clone();
174+
let img = s.slide_now_look(&settings, &look.overlays, id, w, h).map_err(crate::EngineError::Other)?;
161175
let png = encode_png(&img)?;
162176
let aspect = s.slide_photo_aspect(id).unwrap_or(1.5);
163177
let l = lightcraft_slideshow::layout(&settings, aspect, img.width, img.height);
@@ -170,8 +184,11 @@ fn preview(s: &mut Session, p: &Value) -> Result<Value> {
170184
"photo": rect(l.visible),
171185
});
172186
let photo = s.catalog.photo(id).cloned();
173-
let o = crate::slide_overlay::SlideOverlays::resolve(&s.slide_overlays, photo.as_deref());
187+
let o = crate::slide_overlay::SlideOverlays::resolve(&look.overlays, photo.as_deref());
174188
out["overlays"] = o.boxes(&l).into_iter().map(|(k, r)| json!({"kind": k.id(), "box": rect(r)})).collect();
189+
if let Some(t) = &template {
190+
out["template"] = json!(t.id);
191+
}
175192
deliver(&png, path, &mut out)?;
176193
Ok(out)
177194
}
@@ -192,6 +209,104 @@ fn base64(bytes: &[u8]) -> String {
192209
out
193210
}
194211

212+
// ------------------------------------------------------------------------------- templates
213+
214+
/// The settings as the `slideshow.*` commands return them (for template replies).
215+
fn settings_json(s: &Session) -> Value {
216+
json!({
217+
"playback": s.slideshow.to_json(),
218+
"layout": s.slide_layout.to_json(),
219+
"overlays": s.slide_overlays.to_json(),
220+
"titles": s.slide_titles.to_json(),
221+
})
222+
}
223+
224+
fn tpl_list(s: &mut Session, _p: &Value) -> Result<Value> {
225+
let l = s.slide_templates();
226+
Ok(json!({
227+
"templates": l.templates.iter().map(|t| t.info()).collect::<Vec<_>>(),
228+
"previous": s.slide_templates_previous.is_some(),
229+
"folder": s.slide_templates_dir.as_ref().map(|d| d.to_string_lossy().to_string()),
230+
"problems": l.problems,
231+
}))
232+
}
233+
234+
/// The template `id` or `name` names.
235+
fn named(s: &mut Session, p: &Value, c: &str) -> Result<std::sync::Arc<tpl::Template>> {
236+
s.slide_template_named(str_param(p, "id"), str_param(p, "name")).map_err(|e| bad(c, e))
237+
}
238+
239+
fn tpl_apply(s: &mut Session, p: &Value) -> Result<Value> {
240+
const C: &str = "slideshow.templates.apply";
241+
let t = named(s, p, C)?;
242+
let warnings = s.apply_slide_look(&t.look)?;
243+
let mut out = json!({"applied": t.id, "name": t.name, "warnings": warnings, "previous": s.slide_templates_previous.is_some()});
244+
out["settings"] = settings_json(s);
245+
Ok(out)
246+
}
247+
248+
fn tpl_reply(t: &tpl::Template) -> Value {
249+
t.info()
250+
}
251+
252+
fn tpl_save(s: &mut Session, p: &Value) -> Result<Value> {
253+
const C: &str = "slideshow.templates.save";
254+
let name = str_param(p, "name").ok_or_else(|| bad(C, "missing `name`"))?;
255+
let replace = match p.get("replace") {
256+
None | Some(Value::Null) => false,
257+
Some(v) => v.as_bool().ok_or_else(|| bad(C, format!("`replace` must be true or false, not {v}")))?,
258+
};
259+
let t = s.save_slide_template(name, replace).map_err(|e| bad(C, e))?;
260+
Ok(tpl_reply(&t))
261+
}
262+
263+
fn id_param<'a>(p: &'a Value, c: &str) -> Result<&'a str> {
264+
str_param(p, "id").map(str::trim).filter(|x| !x.is_empty()).ok_or_else(|| bad(c, "missing `id` (a template id from slideshow.templates.list)"))
265+
}
266+
267+
fn tpl_update(s: &mut Session, p: &Value) -> Result<Value> {
268+
const C: &str = "slideshow.templates.update";
269+
let id = id_param(p, C)?;
270+
let t = s.update_slide_template(id).map_err(|e| bad(C, e))?;
271+
Ok(tpl_reply(&t))
272+
}
273+
274+
fn tpl_rename(s: &mut Session, p: &Value) -> Result<Value> {
275+
const C: &str = "slideshow.templates.rename";
276+
let id = id_param(p, C)?;
277+
let name = str_param(p, "name").ok_or_else(|| bad(C, "missing `name`"))?;
278+
let t = s.rename_slide_template(id, name).map_err(|e| bad(C, e))?;
279+
Ok(tpl_reply(&t))
280+
}
281+
282+
fn tpl_delete(s: &mut Session, p: &Value) -> Result<Value> {
283+
const C: &str = "slideshow.templates.delete";
284+
let id = id_param(p, C)?;
285+
let t = s.delete_slide_template(id).map_err(|e| bad(C, e))?;
286+
Ok(json!({"deleted": t.id, "name": t.name}))
287+
}
288+
289+
fn tpl_import(s: &mut Session, p: &Value) -> Result<Value> {
290+
const C: &str = "slideshow.templates.import";
291+
let path = str_param(p, "path").map(str::trim).filter(|x| !x.is_empty()).ok_or_else(|| bad(C, "missing `path`"))?;
292+
let (t, renamed) = s.import_slide_template(std::path::Path::new(path)).map_err(|e| bad(C, e))?;
293+
let mut out = tpl_reply(&t);
294+
out["renamed"] = json!(renamed);
295+
out["warnings"] = json!(t.look.warnings(s));
296+
Ok(out)
297+
}
298+
299+
fn tpl_export(s: &mut Session, p: &Value) -> Result<Value> {
300+
const C: &str = "slideshow.templates.export";
301+
let id = id_param(p, C)?;
302+
let path = str_param(p, "path").map(str::trim).filter(|x| !x.is_empty()).ok_or_else(|| bad(C, "missing `path`"))?;
303+
let t = s.slide_template(id).map_err(|e| bad(C, e))?;
304+
s.check_write_target(path).map_err(|e| bad(C, e))?;
305+
let bytes = serde_json::to_vec_pretty(&t.to_doc()).map_err(|e| crate::EngineError::Other(e.to_string()))?;
306+
crate::export::write_file(path, &bytes).map_err(crate::EngineError::Other)?;
307+
Ok(json!({"id": t.id, "name": t.name, "path": path}))
308+
}
309+
195310
pub fn specs() -> Vec<CommandSpec> {
196311
vec![
197312
cmd!(
@@ -230,12 +345,84 @@ pub fn specs() -> Vec<CommandSpec> {
230345
always,
231346
titles
232347
),
348+
cmd!(
349+
query "slideshow.templates.list",
350+
"Slideshow Templates",
351+
[],
352+
None,
353+
"{} → {templates: [{id: \"builtin:…\"|\"user:…\", name, builtin, group: \"builtin\"|\"user\", description?, path? (a user template's file)}], previous: bool (the settings before the last apply can be restored with apply {id: \"previous\"}), folder: the user templates folder | null, problems: [\"file: why it was skipped\"]} — built-ins first, then user templates by name (files in <settings>/slideshow-templates, at most 1000)",
354+
always,
355+
tpl_list
356+
),
357+
cmd!(
358+
"slideshow.templates.apply",
359+
"Apply Slideshow Template",
360+
[],
361+
None,
362+
"{id: template id | \"previous\" (the settings before the last apply) | name: template name (case-insensitive)} — replaces the playback (prepare previews kept), layout, overlays and titles settings with the template's and saves them with the library; there is no undo: the replaced settings are kept as \"previous\" for this session → {applied, name, warnings: [a background photo not in this library, an unreadable identity plate / watermark image…], previous, settings: {playback, layout, overlays, titles}}",
363+
always,
364+
tpl_apply
365+
),
366+
cmd!(
367+
"slideshow.templates.save",
368+
"Save Slideshow Template",
369+
[],
370+
None,
371+
"{name: 1–80 characters, replace?: bool (over a user template of that name)} — the current playback, layout, overlays and titles settings as a user template, <settings>/slideshow-templates/<name>.json (path separators and other reserved characters become _); a name taken (case-insensitive) or a built-in's is refused unless replace → {id, name, builtin, group, path}",
372+
always,
373+
tpl_save
374+
),
375+
cmd!(
376+
"slideshow.templates.update",
377+
"Update Slideshow Template",
378+
[],
379+
None,
380+
"{id: user template} — replace its settings with the current ones (name kept); built-ins can't change → {id, name, builtin, group, path}",
381+
always,
382+
tpl_update
383+
),
384+
cmd!(
385+
"slideshow.templates.rename",
386+
"Rename Slideshow Template",
387+
[],
388+
None,
389+
"{id: user template, name} — its file follows the name; a name taken is refused → {id (the new one), name, builtin, group, path}",
390+
always,
391+
tpl_rename
392+
),
393+
cmd!(
394+
"slideshow.templates.delete",
395+
"Delete Slideshow Template",
396+
[],
397+
None,
398+
"{id: user template} — deletes its file → {deleted, name}",
399+
always,
400+
tpl_delete
401+
),
402+
cmd!(
403+
"slideshow.templates.import",
404+
"Import Slideshow Template",
405+
[],
406+
None,
407+
"{path: a template .json file (at most 256 KB)} — checked like a saved one (format, version, every setting) and saved as a user template under its name (else the file's); a name taken gets \" (2)\"… → {id, name, builtin, group, path, renamed, warnings}",
408+
always,
409+
tpl_import
410+
),
411+
cmd!(
412+
"slideshow.templates.export",
413+
"Export Slideshow Template",
414+
[],
415+
None,
416+
"{id: template (built-in or user), path: .json file to write} → {id, name, path}",
417+
always,
418+
tpl_export
419+
),
233420
cmd!(
234421
query "slideshow.preview",
235422
"Slideshow Slide Preview",
236423
[],
237424
None,
238-
"{id?: photo (default: the active one) | title?: \"intro\"|\"ending\", width?, height?: pixels (16–4096; one given: the other from the aspect preview; neither: 1280 wide), path?: write the PNG here} — the photo's slide composed with the slideshow layout and overlays → {id, width, height, cell: [x0,y0,x1,y1], photo: [x0,y0,x1,y1] (the visible photo, slide pixels), overlays: [{kind: identityPlate|ratingStars|textN|watermark, box: [x0,y0,x1,y1]}], path | png (base64 PNG)}; with `title`, that title screen (`slideshow.titles`; drawn even while it is off) → {title, enabled, width, height, plate: [x0,y0,x1,y1] | null (the plate or text), path | png}",
425+
"{id?: photo (default: the active one) | title?: \"intro\"|\"ending\", template?: a template id (slideshow.templates.list): compose with its settings instead of the library's, which don't change (the Template Browser's hover preview), width?, height?: pixels (16–4096; one given: the other from the aspect preview; neither: 1280 wide), path?: write the PNG here} — the photo's slide composed with the slideshow layout and overlays → {id, width, height, cell: [x0,y0,x1,y1], photo: [x0,y0,x1,y1] (the visible photo, slide pixels), overlays: [{kind: identityPlate|ratingStars|textN|watermark, box: [x0,y0,x1,y1]}], path | png (base64 PNG)}; with `title`, that title screen (`slideshow.titles`; drawn even while it is off) → {title, enabled, width, height, plate: [x0,y0,x1,y1] | null (the plate or text), path | png}",
239426
always,
240427
preview
241428
),

0 commit comments

Comments
 (0)