Files
deckcraft/crates/ui-egui/src/dialogs/tabs.rs
T
Dylan M. TaylorandClaude Opus 5.5 b9185dec4c tabs: the Tabs dialog shows lengths in the document's unit
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>
2026-11-28 11:03:56 +00:00

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));
}
}