From 5309b2e0f7d07d93f2da916ca96e384684ba92fb Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 27 Nov 2026 14:51:31 +0000 Subject: [PATCH] dialogs-w7: net change of 2 commits - dialogs-w7: written from its sheet - dialogs-w7: repair 1 --- crates/ui-egui/src/dialogs/choice_forms.rs | 143 +++++--- crates/ui-egui/src/dialogs/insert_forms.rs | 367 ++++++++++++--------- crates/ui-egui/src/dialogs/zoom.rs | 136 +++----- 3 files changed, 344 insertions(+), 302 deletions(-) diff --git a/crates/ui-egui/src/dialogs/choice_forms.rs b/crates/ui-egui/src/dialogs/choice_forms.rs index 7434d8600..15790bdf5 100644 --- a/crates/ui-egui/src/dialogs/choice_forms.rs +++ b/crates/ui-egui/src/dialogs/choice_forms.rs @@ -1,63 +1,100 @@ -use craft::ui::patterns::form_dialogs::{HostedParamDialog, ParamResult}; - -/// Opens this hosted form with the supplied parameters. -#[allow(dead_code)] // P3(dialogs-w7): dl.A9 -pub(crate) fn open(app: &crate::SlideApp, ctx: &egui::Context, params: &serde_json::Value) -> HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") -} - -/// Applies an accepted form result. -#[allow(dead_code)] // P3(dialogs-w7): dl.A9 -pub(crate) fn finish(app: &mut crate::SlideApp, result: &ParamResult, host: &mut craft::ui::dialog::DialogHost) -> Result<(), String> { - // P3(dialogs-w7): dl.A9 - let _ = host; - let _ = (app, result); - todo!("dl.A9") -} +use crate::SlideApp; +use craft::ui::contract::Bind; +use craft::ui::dialog::DialogHost; +use craft::ui::patterns::form_dialogs::{DialogFields, HostedParamDialog, Param, ParamDialog, ParamKind, ParamLayout, ParamResult}; +use egui::Context; +use serde_json::{Value, json}; +/// The slide show configuration form. pub(crate) mod setup_show { - /// Opens this hosted form with the supplied parameters. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn open( - app: &crate::SlideApp, - ctx: &egui::Context, - params: &serde_json::Value, - ) -> craft::ui::patterns::form_dialogs::HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") + use super::*; + + /// Opens show options seeded from the deck and the cursor preference. + pub(crate) fn open(app: &SlideApp, ctx: &Context, _: &Value) -> HostedParamDialog { + let show = app.session.active().map(|s| s.doc.show.clone()).unwrap_or_default(); + let mut shows = vec![(String::new(), "All slides".into())]; + if let Some(active) = app.session.active() { + shows.extend(active.doc.custom_shows.iter().map(|show| (show.name.clone(), show.name.clone().into()))); + } + let rows = vec![ + Param::new( + "type", + "Show type", + ParamKind::radio( + vec![ + ("speaker".into(), "Presented by a speaker (full screen)".into()), + ("browsed".into(), "Browsed by an individual (window)".into()), + ("kiosk".into(), "Browsed at a kiosk (full screen)".into()), + ], + Some(show.show_type), + ), + ), + Param::new("loop", "Loop continuously until 'Esc'", ParamKind::bool(show.loop_until_esc)), + Param::new("nn", "Show without narration", ParamKind::bool(show.without_narration)), + Param::new("na", "Show without animation", ParamKind::bool(show.without_animation)), + Param::new("ut", "Use timings, if present", ParamKind::bool(show.use_timings)), + Param::new("kc", "Keep mouse cursor visible", ParamKind::bool(app.ui.show_keep_cursor)), + Param::new("custom", "Slides", ParamKind::choice(shows, Some(show.custom_show.unwrap_or_default()))), + ]; + HostedParamDialog::new(ctx, "setupShow", DialogFields::new(), move |fields| { + ParamDialog::new("setupShow", "Set Up Slide Show", rows.clone()) + .values(Bind::bound(fields)) + .layout(ParamLayout::Document) + .caption_column(true) + .ok_label("OK".into()) + }) } - /// Applies an accepted form result through the pump dialog host. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn finish( - app: &mut crate::SlideApp, - result: &craft::ui::patterns::form_dialogs::ParamResult, - host: &mut craft::ui::dialog::DialogHost, - ) -> Result<(), String> { - let _ = (app, result, host); - todo!("dl.A9") + + /// Applies show configuration and the cursor preference through commands. + pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _: &mut DialogHost) -> Result<(), String> { + let fields = &result.fields; + app.run( + "show.setup", + json!({ + "type": fields.text("type"), "loop": fields.bool("loop"), + "noNarration": fields.bool("nn"), "noAnimation": fields.bool("na"), + "useTimings": fields.bool("ut"), "custom": fields.text("custom"), + }), + )?; + app.run("show.keepCursor", json!({"on": fields.bool("kc")})).map(|_| ()) } } +/// The clipboard representation form. pub(crate) mod paste_special { - /// Opens this hosted form with the supplied parameters. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn open( - app: &crate::SlideApp, - ctx: &egui::Context, - params: &serde_json::Value, - ) -> craft::ui::patterns::form_dialogs::HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") + use super::*; + + /// Opens paste choices with reasons for formats requiring copied shapes. + pub(crate) fn open(app: &SlideApp, ctx: &Context, _: &Value) -> HostedParamDialog { + let mut paste = Param::new( + "as", + "As:", + ParamKind::radio( + vec![ + ("destinationTheme".into(), "Use Destination Theme".into()), + ("keepSource".into(), "Keep Source Formatting".into()), + ("picture".into(), "Picture (PNG)".into()), + ("text".into(), "Keep Text Only".into()), + ], + Some("destinationTheme".into()), + ), + ); + if app.session.clipboard.shapes.is_empty() { + paste = paste + .disabled_option("keepSource", "Copy shapes to keep their source formatting.") + .disabled_option("picture", "Copy shapes to paste them as a picture."); + } + HostedParamDialog::new(ctx, "pasteSpecial", DialogFields::new(), move |fields| { + ParamDialog::new("pasteSpecial", "Paste Special", vec![paste.clone()]) + .values(Bind::bound(fields)) + .layout(ParamLayout::Document) + .caption_column(true) + .ok_label("OK".into()) + }) } - /// Applies an accepted form result through the pump dialog host. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn finish( - app: &mut crate::SlideApp, - result: &craft::ui::patterns::form_dialogs::ParamResult, - host: &mut craft::ui::dialog::DialogHost, - ) -> Result<(), String> { - let _ = (app, result, host); - todo!("dl.A9") + + /// Pastes the clipboard using the accepted representation. + pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _: &mut DialogHost) -> Result<(), String> { + app.run("edit.pasteSpecial", json!({"as": result.fields.text("as")})).map(|_| ()) } } diff --git a/crates/ui-egui/src/dialogs/insert_forms.rs b/crates/ui-egui/src/dialogs/insert_forms.rs index 08fd986bd..35a27fb8d 100644 --- a/crates/ui-egui/src/dialogs/insert_forms.rs +++ b/crates/ui-egui/src/dialogs/insert_forms.rs @@ -1,201 +1,250 @@ -use craft::ui::patterns::form_dialogs::{HostedParamDialog, ParamResult}; +use crate::SlideApp; +use craft::ui::choice::Segmented; +use craft::ui::contract::{Bind, Entry, Show as _}; +use craft::ui::dialog::DialogHost; +use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, HostedParamDialog, Param, ParamDialog, ParamKind, ParamLayout, ParamResult}; +use egui::Context; +use serde_json::{Value, json}; -/// Opens this hosted form with the supplied parameters. -#[allow(dead_code)] // P3(dialogs-w7): dl.A9 -pub(crate) fn open(app: &crate::SlideApp, ctx: &egui::Context, params: &serde_json::Value) -> HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") -} - -/// Applies an accepted form result. -#[allow(dead_code)] // P3(dialogs-w7): dl.A9 -pub(crate) fn finish(app: &mut crate::SlideApp, result: &ParamResult, host: &mut craft::ui::dialog::DialogHost) -> Result<(), String> { - // P3(dialogs-w7): dl.A9 - let _ = host; - let _ = (app, result); - todo!("dl.A9") +fn form(ctx: &Context, id: &'static str, title: &'static str, fields: DialogFields, rows: Vec, ok: &'static str) -> HostedParamDialog { + HostedParamDialog::new(ctx, id, fields, move |fields| { + ParamDialog::new(id, title, rows.clone()).values(Bind::bound(fields)).layout(ParamLayout::Document).caption_column(true).ok_label(ok.into()) + }) } +/// The table dimensions form. pub(crate) mod table { - /// Opens this hosted form with the supplied parameters. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn open( - app: &crate::SlideApp, - ctx: &egui::Context, - params: &serde_json::Value, - ) -> craft::ui::patterns::form_dialogs::HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") + use super::*; + + /// Opens the table dimensions with positive row and column counts. + pub(crate) fn open(_: &SlideApp, ctx: &Context, _: &Value) -> HostedParamDialog { + form( + ctx, + "table", + "Insert Table", + DialogFields::new(), + vec![ + Param::new("cols", "Number of columns:", ParamKind::int(Some(1), None, 5)), + Param::new("rows", "Number of rows:", ParamKind::int(Some(1), None, 2)), + ], + "Insert", + ) } - /// Applies an accepted form result through the pump dialog host. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn finish( - app: &mut crate::SlideApp, - result: &craft::ui::patterns::form_dialogs::ParamResult, - host: &mut craft::ui::dialog::DialogHost, - ) -> Result<(), String> { - let _ = (app, result, host); - todo!("dl.A9") + + /// Inserts a table with the accepted dimensions. + pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _: &mut DialogHost) -> Result<(), String> { + app.run("insert.table", json!({"rows": result.fields.int("rows"), "cols": result.fields.int("cols")})).map(|_| ()) } } +/// The comment composition form. pub(crate) mod comment { - /// Opens this hosted form with the supplied parameters. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn open( - app: &crate::SlideApp, - ctx: &egui::Context, - params: &serde_json::Value, - ) -> craft::ui::patterns::form_dialogs::HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") + use super::*; + + /// Opens a multiline comment editor. + pub(crate) fn open(_: &SlideApp, ctx: &Context, _: &Value) -> HostedParamDialog { + form( + ctx, + "comment", + "New Comment", + DialogFields::new(), + vec![Param::new("text", "Comment", ParamKind::text("")).multiline(4).hint("Comment").full_width()], + "Post", + ) } - /// Applies an accepted form result through the pump dialog host. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn finish( - app: &mut crate::SlideApp, - result: &craft::ui::patterns::form_dialogs::ParamResult, - host: &mut craft::ui::dialog::DialogHost, - ) -> Result<(), String> { - let _ = (app, result, host); - todo!("dl.A9") + + /// Posts nonblank text and shows its comment pane. + pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _: &mut DialogHost) -> Result<(), String> { + let text = result.fields.text("text").unwrap_or_default(); + if !text.trim().is_empty() { + app.run("comment.add", json!({"text": text}))?; + app.ui.pane = Some("comments".into()); + } + Ok(()) } } +/// The outline insertion form. pub(crate) mod outline { - /// Opens this hosted form with the supplied parameters. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn open( - app: &crate::SlideApp, - ctx: &egui::Context, - params: &serde_json::Value, - ) -> craft::ui::patterns::form_dialogs::HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") + use super::*; + + /// Opens an outline editor that preserves indentation. + pub(crate) fn open(_: &SlideApp, ctx: &Context, _: &Value) -> HostedParamDialog { + form( + ctx, + "outline", + "Slides from Outline", + DialogFields::new(), + vec![ + Param::read_only_text( + "note", + "", + "Paste or type an outline. Unindented lines become slide titles; tab-indented lines become bullets.", + ) + .full_width(), + Param::new("text", "Outline", ParamKind::text("")).multiline(10).mono().hint("Outline").full_width(), + ], + "Insert", + ) } - /// Applies an accepted form result through the pump dialog host. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn finish( - app: &mut crate::SlideApp, - result: &craft::ui::patterns::form_dialogs::ParamResult, - host: &mut craft::ui::dialog::DialogHost, - ) -> Result<(), String> { - let _ = (app, result, host); - todo!("dl.A9") + + /// Inserts slides from the accepted outline. + pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _: &mut DialogHost) -> Result<(), String> { + app.run("slide.fromOutline", json!({"text": result.fields.text("text").unwrap_or_default()})).map(|_| ()) } } +fn rename(ctx: &Context, params: &Value, id: &'static str, title: &'static str, target: Option<&'static str>) -> HostedParamDialog { + let mut fields = DialogFields::from_pairs([("name", params.get("name").and_then(Value::as_str).unwrap_or_default())]); + let mut rows = vec![Param::new("name", "Name:", ParamKind::text(""))]; + if let Some(key) = target { + // The target travels with its answer, including section index zero. + fields.set(key, FieldValue::Json(params.get(key).cloned().unwrap_or_default())); + rows.push(Param::new(key, key, ParamKind::int(Some(0), None, 0)).shown(false)); + } + form(ctx, id, title, fields, rows, "Rename") +} + +fn finish_rename(app: &mut SlideApp, result: &ParamResult) -> Result<(), String> { + let fields = &result.fields; + let name = fields.text("name").unwrap_or_default(); + let (command, params) = if fields.contains("index") { + ("section.rename", json!({"index": fields.get("index"), "name": name})) + } else if fields.contains("id") { + ("shape.rename", json!({"id": fields.get("id"), "name": name})) + } else { + ("master.renameLayout", json!({"name": name})) + }; + app.run(command, params).map(|_| ()) +} + +/// The section rename form. pub(crate) mod rename_section { - /// Opens this hosted form with the supplied parameters. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn open( - app: &crate::SlideApp, - ctx: &egui::Context, - params: &serde_json::Value, - ) -> craft::ui::patterns::form_dialogs::HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") + use super::*; + + /// Opens a name editor carrying the section index. + pub(crate) fn open(_: &SlideApp, ctx: &Context, params: &Value) -> HostedParamDialog { + rename(ctx, params, "renameSection", "Rename Section", Some("index")) } - /// Applies an accepted form result through the pump dialog host. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn finish( - app: &mut crate::SlideApp, - result: &craft::ui::patterns::form_dialogs::ParamResult, - host: &mut craft::ui::dialog::DialogHost, - ) -> Result<(), String> { - let _ = (app, result, host); - todo!("dl.A9") + + /// Renames the section carried by the answer. + pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _: &mut DialogHost) -> Result<(), String> { + finish_rename(app, result) } } +/// The shape rename form. pub(crate) mod rename_shape { - /// Opens this hosted form with the supplied parameters. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn open( - app: &crate::SlideApp, - ctx: &egui::Context, - params: &serde_json::Value, - ) -> craft::ui::patterns::form_dialogs::HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") + use super::*; + + /// Opens a name editor carrying the shape id. + pub(crate) fn open(_: &SlideApp, ctx: &Context, params: &Value) -> HostedParamDialog { + rename(ctx, params, "renameShape", "Rename", Some("id")) } - /// Applies an accepted form result through the pump dialog host. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn finish( - app: &mut crate::SlideApp, - result: &craft::ui::patterns::form_dialogs::ParamResult, - host: &mut craft::ui::dialog::DialogHost, - ) -> Result<(), String> { - let _ = (app, result, host); - todo!("dl.A9") + + /// Renames the shape carried by the answer. + pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _: &mut DialogHost) -> Result<(), String> { + finish_rename(app, result) } } +/// The layout rename form. pub(crate) mod rename_layout { - /// Opens this hosted form with the supplied parameters. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn open( - app: &crate::SlideApp, - ctx: &egui::Context, - params: &serde_json::Value, - ) -> craft::ui::patterns::form_dialogs::HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") + use super::*; + + /// Opens the layout name editor. + pub(crate) fn open(_: &SlideApp, ctx: &Context, params: &Value) -> HostedParamDialog { + rename(ctx, params, "renameLayout", "Rename Layout", None) } - /// Applies an accepted form result through the pump dialog host. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn finish( - app: &mut crate::SlideApp, - result: &craft::ui::patterns::form_dialogs::ParamResult, - host: &mut craft::ui::dialog::DialogHost, - ) -> Result<(), String> { - let _ = (app, result, host); - todo!("dl.A9") + + /// Renames the active layout. + pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _: &mut DialogHost) -> Result<(), String> { + finish_rename(app, result) } } +/// The linear equation insertion form. pub(crate) mod equation { - /// Opens this hosted form with the supplied parameters. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn open( - app: &crate::SlideApp, - ctx: &egui::Context, - params: &serde_json::Value, - ) -> craft::ui::patterns::form_dialogs::HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") + use super::*; + + /// Opens the equation editor with its linear notation guide. + pub(crate) fn open(_: &SlideApp, ctx: &Context, _: &Value) -> HostedParamDialog { + form( + ctx, + "equation", + "Equation", + DialogFields::new(), + vec![ + Param::read_only_text("note", "", "Type an equation in linear form (e.g. a^2+b^2=c^2, x=(-b±√(b^2-4ac))/2a).").full_width(), + Param::new("eq", "Equation", ParamKind::text("")).hint("Equation").full_width(), + ], + "Insert", + ) } - /// Applies an accepted form result through the pump dialog host. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn finish( - app: &mut crate::SlideApp, - result: &craft::ui::patterns::form_dialogs::ParamResult, - host: &mut craft::ui::dialog::DialogHost, - ) -> Result<(), String> { - let _ = (app, result, host); - todo!("dl.A9") + + /// Inserts and formats a nonempty equation through registered commands. + pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _: &mut DialogHost) -> Result<(), String> { + let text = result.fields.text("eq").unwrap_or_default(); + if text.is_empty() { + return Ok(()); + } + let pretty = crate::dialogs::equation::pretty_equation(text); + let size = app.session.active().map(|s| s.doc.slide_size).unwrap_or(deckcraft_model::defaults::WIDE); + app.run("insert.textBox", json!({"rect": [size.width / 2.0 - 150.0, size.height / 2.0 - 25.0, 300, 50], "text": pretty}))?; + app.run("text.exit", json!({}))?; + app.run("format.font", json!({"family": "Liberation Serif"}))?; + app.run("format.italic", json!({"on": true}))?; + app.run("format.size", json!({"size": 28})).map(|_| ()) } } +/// The diagram insertion form. pub(crate) mod smartart { - /// Opens this hosted form with the supplied parameters. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn open( - app: &crate::SlideApp, - ctx: &egui::Context, - params: &serde_json::Value, - ) -> craft::ui::patterns::form_dialogs::HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") + use super::*; + + /// Opens diagram kinds as segments alongside the item editor. + pub(crate) fn open(_: &SlideApp, ctx: &Context, _: &Value) -> HostedParamDialog { + let kind = Param::custom("kind", "Graphic", |ui, fields, custom| { + let entries: Vec<_> = [ + ("list", "Basic List"), + ("process", "Basic Process"), + ("cycle", "Basic Cycle"), + ("hierarchy", "Hierarchy"), + ("pyramid", "Pyramid"), + ("matrix", "Matrix"), + ] + .into_iter() + .map(|(id, label)| Entry::new(id.to_owned(), label)) + .collect(); + let mut value = fields.text("kind").unwrap_or("process").to_owned(); + let shown = Segmented::new("Graphic", Bind::bound(&mut value), &entries).show(ui); + custom.gesture(&shown.events); + if !shown.events.is_empty() { + fields.set("kind", value); + } + shown.response + }) + .opens_with("process"); + form( + ctx, + "smartart", + "Choose a Diagram Graphic", + DialogFields::new(), + vec![ + Param::read_only_text("note", "", "Pick a graphic; type one item per line.").full_width(), + kind, + Param::new("text", "Diagram items", ParamKind::text("Plan\nBuild\nLaunch")).multiline(5).hint("Diagram items").full_width(), + ], + "Insert", + ) } - /// Applies an accepted form result through the pump dialog host. - #[allow(dead_code)] // P3(dialogs-w7): dl.A9 - pub(crate) fn finish( - app: &mut crate::SlideApp, - result: &craft::ui::patterns::form_dialogs::ParamResult, - host: &mut craft::ui::dialog::DialogHost, - ) -> Result<(), String> { - let _ = (app, result, host); - todo!("dl.A9") + + /// Inserts the nonblank lines without trimming their contents. + pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _: &mut DialogHost) -> Result<(), String> { + let fields = &result.fields; + let items: Vec<_> = fields.text("text").unwrap_or_default().lines().filter(|line| !line.trim().is_empty()).collect(); + app.run("insert.smartArt", json!({"kind": fields.text("kind"), "items": items}))?; + app.ui.pane = Some("smartart".into()); + Ok(()) } } diff --git a/crates/ui-egui/src/dialogs/zoom.rs b/crates/ui-egui/src/dialogs/zoom.rs index 49e81424f..3cd975a4a 100644 --- a/crates/ui-egui/src/dialogs/zoom.rs +++ b/crates/ui-egui/src/dialogs/zoom.rs @@ -1,102 +1,58 @@ -use super::{Dialog, SlideApp, Ui, buttons}; +use crate::SlideApp; +use craft::ui::contract::Bind; +use craft::ui::dialog::DialogHost; +use craft::ui::patterns::form_dialogs::{DialogFields, HostedParamDialog, Param, ParamDialog, ParamKind, ParamLayout, ParamResult}; +use craft::ui::runtime::env::UnitKind; +use egui::Context; +use serde_json::{Value, json}; /// Preset percents in the Zoom dialog, largest first. const ZOOM_PRESETS: &[u32] = &[400, 200, 150, 100, 75, 66, 50, 33]; -/// A typed zoom percent as a scale in `0.1..=4` (10%–400%). `None` when it is not a finite number. -pub fn parse_zoom_percent(text: &str) -> Option { - let t = text.trim().trim_end_matches('%').trim(); - if t.is_empty() { - return None; - } - let v = t.parse::().ok().filter(|n| n.is_finite())?; - Some(((v / 100.0) as f32).clamp(0.1, 4.0)) +fn preset(percent: f64) -> Option { + ZOOM_PRESETS.iter().find(|&&value| f64::from(value) == percent).map(u32::to_string) } -/// Zoom dialog: a percent or Fit. Nothing changes until OK. -pub(super) fn zoom_dialog(app: &mut SlideApp, ui: &mut Ui, d: &mut Dialog) -> bool { - if !d.fields.contains_key("mode") { - let shown = app.ui.zoom.unwrap_or(app.canvas_scale); - let pct = if shown.is_finite() { (shown * 100.0).round().clamp(10.0, 400.0) } else { 100.0 }; - d.fields.insert("mode".into(), if app.ui.zoom.is_none() { "fit".into() } else { "percent".into() }); - d.fields.insert("percent".into(), format!("{pct:.0}")); - } - let mut mode = d.get("mode", "percent"); - let mut percent = d.get("percent", "100"); - ui.label("Zoom to"); - if ui.radio(mode == "fit", "Fit").clicked() { - mode = "fit".into(); - } - for pct in ZOOM_PRESETS { - let on = mode == "percent" && percent == pct.to_string(); - if ui.radio(on, format!("{pct}%")).clicked() { - mode = "percent".into(); - percent = pct.to_string(); - } - } - ui.horizontal(|ui| { - let l = ui.label("Percent:"); - let edit = ui.add(egui::TextEdit::singleline(&mut percent).desired_width(72.0)).labelled_by(l.id); - if edit.changed() { - mode = "percent".into(); - } - ui.label("%"); - }); - d.fields.insert("mode".into(), mode.clone()); - d.fields.insert("percent".into(), percent.clone()); - let (ok, cancel) = buttons(ui, "OK"); - if ok { - if mode == "fit" { - app.ui.zoom = None; - return true; - } - match parse_zoom_percent(&percent) { - Some(scale) => app.ui.zoom = Some(scale), - None => { - app.set_status("Enter a zoom percent"); - return false; - } - } - } - ok || cancel -} - -#[cfg(test)] -mod zoom_tests { - use super::parse_zoom_percent; - - fn near(v: f32, want: f32) -> bool { - (v - want).abs() < 1.0e-4 - } - - #[test] - fn zoom_percent_accepts_a_number_and_clamps() { - assert!(parse_zoom_percent("150").is_some_and(|v| near(v, 1.5))); - assert!(parse_zoom_percent("150%").is_some_and(|v| near(v, 1.5))); - assert!(parse_zoom_percent(" 100 ").is_some_and(|v| near(v, 1.0))); - assert!(parse_zoom_percent("1").is_some_and(|v| near(v, 0.1))); - assert!(parse_zoom_percent("900").is_some_and(|v| near(v, 4.0))); - assert!(parse_zoom_percent("").is_none()); - assert!(parse_zoom_percent("fit").is_none()); - assert!(parse_zoom_percent("nan").is_none()); - assert!(parse_zoom_percent("inf").is_none()); +fn zoom_changed(fields: &mut DialogFields) { + if let Some(percent) = fields.text("zoom").and_then(|value| value.parse::().ok()) { + fields.set("percent", percent); } } -use craft::ui::patterns::form_dialogs::{HostedParamDialog, ParamResult}; - -/// Opens this hosted form with the supplied parameters. -#[allow(dead_code)] // P3(dialogs-w7): dl.A9 -pub(crate) fn open(app: &crate::SlideApp, ctx: &egui::Context, params: &serde_json::Value) -> HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") +fn percent_changed(fields: &mut DialogFields) { + if let Some(value) = fields.number("percent").and_then(preset) { + fields.set("zoom", value); + } else { + fields.set_mixed("zoom"); + } } -/// Applies an accepted form result. -#[allow(dead_code)] // P3(dialogs-w7): dl.A9 -pub(crate) fn finish(app: &mut crate::SlideApp, result: &ParamResult, host: &mut craft::ui::dialog::DialogHost) -> Result<(), String> { - // P3(dialogs-w7): dl.A9 - let _ = host; - let _ = (app, result); - todo!("dl.A9") +/// Opens Fit, preset percents and a bounded percent field over the current canvas scale. +pub(crate) fn open(app: &SlideApp, ctx: &Context, _: &Value) -> HostedParamDialog { + let shown = app.ui.zoom.unwrap_or(app.canvas_scale()); + let percent = if shown.is_finite() { f64::from((shown * 100.0).round().clamp(10.0, 400.0)) } else { 100.0 }; + let mut fields = DialogFields::from_pairs([("percent", percent)]); + percent_changed(&mut fields); + if app.ui.zoom.is_none() { + fields.set("zoom", "fit"); + } + let mut options = vec![("fit".into(), "Fit".into())]; + options.extend(ZOOM_PRESETS.iter().map(|percent| (percent.to_string(), format!("{percent}%").into()))); + let rows = vec![ + Param::new("zoom", "Zoom to", ParamKind::radio(options, None)).on_change(zoom_changed), + Param::number("percent", "Percent:", 100.0, 1.0, 0).bounds(Some(10.0), Some(400.0)).unit(UnitKind::Percent).on_change(percent_changed), + ]; + HostedParamDialog::new(ctx, "zoom", fields, move |fields| { + ParamDialog::new("zoom", "Zoom", rows.clone()).values(Bind::bound(fields)).layout(ParamLayout::Document).caption_column(true).ok_label("OK".into()) + }) +} + +/// Applies the accepted zoom through the registered view commands. +pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _: &mut DialogHost) -> Result<(), String> { + if result.fields.text("zoom") == Some("fit") { + app.run("view.fit", json!({})) + } else { + app.run("view.zoom", json!({"percent": result.fields.number("percent")})) + } + .map(|_| ()) }