mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-02 10:38:15 -06:00
The dialog showed points; production shows inches, the default document unit. It now takes the unit the ruler that opens it shows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
86 lines
3.0 KiB
Rust
86 lines
3.0 KiB
Rust
use craft::text::ui::character::ParagraphFormat;
|
|
use craft::text::ui::ruler::{TabAlign, TabLeader, TabStop};
|
|
use craft::text::ui::tabs_dialog::{TabStopsDialog, TabStopsParts};
|
|
use craft::ui::dialog::{DialogHost, DialogId};
|
|
use egui::Context;
|
|
use serde_json::{Value, json};
|
|
|
|
use crate::SlideApp;
|
|
|
|
/// Opens seeded tab stops, or asks the engine to enqueue its selection's seed.
|
|
pub(crate) fn open(app: &mut SlideApp, _ctx: &Context, host: &mut DialogHost, params: &Value) -> Result<Option<DialogId>, String> {
|
|
if params.get("tabs").is_none() {
|
|
// format.tabs emits UiRequest::Dialog with tabs_params, including after text.exit.
|
|
app.run("format.tabs", json!({}))?;
|
|
return Ok(None);
|
|
}
|
|
let stops = params["tabs"]
|
|
.as_array()
|
|
.into_iter()
|
|
.flatten()
|
|
.filter_map(|stop| {
|
|
Some(TabStop::new(
|
|
stop["pos"].as_f64()? as f32,
|
|
match stop["align"].as_str() {
|
|
Some("ctr") => TabAlign::Center,
|
|
Some("r") => TabAlign::Right,
|
|
Some("dec") => TabAlign::Decimal,
|
|
_ => TabAlign::Left,
|
|
},
|
|
TabLeader::None,
|
|
))
|
|
})
|
|
.collect();
|
|
let default_tab = params["defaultTab"].as_f64().unwrap_or(72.0) as f32;
|
|
Ok(Some(
|
|
host.open(
|
|
TabStopsDialog::new(stops, default_tab)
|
|
.unit(crate::canvas::doc_unit(app.session.prefs.units))
|
|
.key("tabs".to_owned())
|
|
.parts(TabStopsParts::Basic)
|
|
.held_ok(),
|
|
),
|
|
))
|
|
}
|
|
|
|
/// Keeps Paragraph's unapplied format while the engine supplies the Tabs seed.
|
|
pub(crate) fn open_from_paragraph(
|
|
app: &mut SlideApp,
|
|
ctx: &Context,
|
|
host: &mut DialogHost,
|
|
draft: ParagraphFormat,
|
|
) -> Result<Option<DialogId>, String> {
|
|
app.dialogs.paragraph_draft = Some(draft);
|
|
let result = open(app, ctx, host, &json!({}));
|
|
if result.is_err() {
|
|
app.dialogs.paragraph_draft = None;
|
|
}
|
|
result
|
|
}
|
|
|
|
/// Applies each held answer once and restores Paragraph on either successful OK or Cancel.
|
|
pub(crate) fn pump(app: &mut SlideApp, ctx: &Context, host: &mut DialogHost) {
|
|
if host.take::<TabStopsDialog>().is_some()
|
|
&& let Some(draft) = app.dialogs.paragraph_draft.take()
|
|
{
|
|
super::paragraph::reopen(app, ctx, host, draft);
|
|
}
|
|
let Some(answer) = host.get_mut::<TabStopsDialog>().and_then(TabStopsDialog::take_ok) else { return };
|
|
let set: Vec<Value> = answer
|
|
.stops
|
|
.iter()
|
|
.map(|stop| {
|
|
json!({"pos": stop.position, "align": match stop.align {
|
|
TabAlign::Center => "ctr",
|
|
TabAlign::Right => "r",
|
|
TabAlign::Decimal => "dec",
|
|
_ => "l",
|
|
}})
|
|
})
|
|
.collect();
|
|
let result = app.run("format.tabs", json!({"set": set, "defaultTab": answer.default_interval})).map(|_| ());
|
|
if let Some(dialog) = host.get_mut::<TabStopsDialog>() {
|
|
dialog.answer_ok(result.map_err(Into::into));
|
|
}
|
|
}
|