Files
deckcraft/crates/ui-egui/src/dialogs/insert_forms.rs
T
Dylan M. Taylor 5309b2e0f7 dialogs-w7: net change of 2 commits
- dialogs-w7: written from its sheet
- dialogs-w7: repair 1
2026-11-27 14:51:31 +00:00

251 lines
9.6 KiB
Rust

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};
fn form(ctx: &Context, id: &'static str, title: &'static str, fields: DialogFields, rows: Vec<Param>, 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 {
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",
)
}
/// 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 {
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",
)
}
/// 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 {
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",
)
}
/// 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 {
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"))
}
/// 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 {
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"))
}
/// 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 {
use super::*;
/// Opens the layout name editor.
pub(crate) fn open(_: &SlideApp, ctx: &Context, params: &Value) -> HostedParamDialog {
rename(ctx, params, "renameLayout", "Rename Layout", None)
}
/// 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 {
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",
)
}
/// 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 {
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",
)
}
/// 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(())
}
}