6ef13b2cc8a6
paragraph.rs
use super::{BTreeSet, Dialog, MeasureExt, Ui, Value, WordApp, json};
/// The Paragraph dialog as one paragraph's formatting fills it, on page `tab`.
pub(super) fn paragraph_dialog(rp: &wordcraft_doc::resolve::ResolvedPara, tab: u8) -> Dialog {
let line = match rp.line_spacing {
wordcraft_doc::props::LineSpacing::Multiple(m) => m,
_ => 1.0,
};
let (vl, vr) = rp.visual_indents();
let unit = crate::options::unit().pt_per_unit();
Dialog::Paragraph {
rtl: rp.bidi,
align: format!("{:?}", rp.align.visual(rp.bidi)).to_lowercase(),
left: vl / unit,
right: vr / unit,
first: rp.indent_first / unit,
before: rp.space_before,
after: rp.space_after,
line,
keep_next: rp.keep_next,
keep_lines: rp.keep_lines,
page_break: rp.page_break_before,
widow: rp.widow_control,
tab,
asian: [rp.kinsoku, rp.word_wrap, rp.overflow_punct, rp.top_line_punct, rp.auto_space_de, rp.auto_space_dn],
opts: [rp.mirror_indents, rp.contextual_spacing, rp.suppress_line_numbers, rp.suppress_hyphens],
start: serde_json::Map::new(),
mixed: BTreeSet::new(),
}
}
/// The Paragraph dialog's fields as `para.set` takes them: `align`, `left` and `right` as seen
/// on the page (its `onPage`), the rest as paragraph properties, line spacing as a multiple.
pub(super) fn para_fields(d: &Dialog) -> serde_json::Map<String, Value> {
let Dialog::Paragraph { rtl, align, left, right, first, before, after, line, keep_next, keep_lines, page_break, widow, asian, opts, .. } = d
else {
return serde_json::Map::new();
};
let unit = crate::options::unit().pt_per_unit();
let [kinsoku, word_wrap, overflow, top_line, de, dn] = asian;
let [mirror, contextual, no_numbers, no_hyphens] = opts;
let fields = json!({
"bidi": rtl, "align": align, "left": left * unit, "right": right * unit, "indentFirst": first * unit,
"spaceBefore": before, "spaceAfter": after, "lineSpacing": {"rule": "multiple", "value": line},
"keepNext": keep_next, "keepLines": keep_lines, "pageBreakBefore": page_break, "widowControl": widow,
"mirrorIndents": mirror, "contextualSpacing": contextual, "suppressLineNumbers": no_numbers, "suppressHyphens": no_hyphens,
"kinsoku": kinsoku, "wordWrap": word_wrap, "overflowPunct": overflow, "topLinePunct": top_line, "autoSpaceDE": de, "autoSpaceDN": dn,
});
match fields {
Value::Object(m) => m,
_ => serde_json::Map::new(),
}
}
/// The Paragraph dialog's number boxes: a length in the measurement unit (indents), points
/// (spacing) or lines (line spacing).
#[derive(Clone, Copy)]
pub(super) enum ParaNumber {
Length,
Points,
Lines,
}
/// The right-to-left mark.
const RLM: char = '\u{200F}';
/// A Paragraph dialog number box for the field `key`. While the selected paragraphs differ in
/// it, it shows nothing, not even the unit, until a value is typed or dragged in.
pub(super) fn para_number(ui: &mut Ui, value: &mut f32, kind: ParaNumber, key: &str, mixed: &mut BTreeSet<String>, label: &egui::Response) {
let unit = crate::options::unit();
let blank = mixed.contains(key);
// A value from the document outside the range (line spacing 6) stays until edited.
let field = match kind {
ParaNumber::Length => egui::DragValue::new(value).speed(3.6 / unit.pt_per_unit()).max_decimals(2),
ParaNumber::Points => egui::DragValue::new(value).speed(1.0).range(0.0..=1584.0).clamp_existing_to_range(false),
ParaNumber::Lines => egui::DragValue::new(value).speed(0.05).range(0.5..=5.0).clamp_existing_to_range(false),
};
let field = match kind {
ParaNumber::Length if blank => field.measure_blank(unit),
ParaNumber::Length => field.measure(unit),
_ if blank => field.custom_formatter(|_, _| String::new()),
// Right to left, the unit's space goes after the word and a right-to-left mark closes it: a
// plain leading space between the digits and an Arabic unit is drawn with no width.
ParaNumber::Points if crate::rtl::on() => field.suffix(format!("{} {RLM}", tl!("pt"))),
ParaNumber::Points => field.suffix(format!(" {}", tl!("pt"))),
ParaNumber::Lines => field,
};
if ui.add(field).labelled_by(label.id).changed() {
mixed.remove(key);
}
}
/// The Paragraph dialog's Special box and its By value, as in Word: the first-line indent
/// `first` (in the measurement unit) is a First line indent when above 0 and a Hanging one when
/// below. Picking First line or Hanging from (none), or while the selected paragraphs differ,
/// starts it at half an inch as Word does; switching between the two keeps By.
pub(super) fn para_special(r: &mut crate::rtl::Cells, first: &mut f32, mixed: &mut BTreeSet<String>) {
const SPECIAL: [&str; 3] = ["(none)", "First line", "Hanging"];
let unit = crate::options::unit();
let kind_of = |first: f32| {
if first > 0.0 {
1
} else if first < 0.0 {
2
} else {
0
}
};
let blank = mixed.contains("indentFirst");
let kind = kind_of(*first);
let special_label = r.cell(|ui| ui.label(tl!("Special:")));
let shown = if blank { "" } else { tl!(SPECIAL[kind]) };
r.cell(|ui| {
let combo = craft_ui::ComboBox::from_id_salt("para_special").selected_text(shown).show_ui(ui, |ui| {
for (k, l) in SPECIAL.into_iter().enumerate() {
if ui.selectable_label(!blank && k == kind, tl!(l)).clicked() && (blank || k != kind) {
let by = if blank || kind == 0 { 36.0 / unit.pt_per_unit() } else { first.abs() };
*first = [0.0, by, -by][k];
mixed.remove("indentFirst");
}
}
});
combo.response.labelled_by(special_label.id);
});
// A kind just picked counts from this frame on.
let (blank, kind) = (mixed.contains("indentFirst"), kind_of(*first));
let by_label = r.cell(|ui| ui.label(tl!("By:")));
r.cell(|ui| {
if blank || kind == 0 {
// Nothing to set until Special says which way.
let mut none = 0.0;
ui.add_enabled(false, egui::DragValue::new(&mut none).measure_blank(unit)).labelled_by(by_label.id);
} else {
// Typed values count once committed: the 0 of "0.25" would otherwise turn Special
// to (none) and disable the box mid-typing.
let mut by = first.abs();
// By stops at 0 when dragged down instead of turning negative for a frame (which the
// `abs` below would flip back), and at Word's 22".
let field = egui::DragValue::new(&mut by)
.speed(3.6 / unit.pt_per_unit())
.range(0.0..=1584.0 / unit.pt_per_unit())
.clamp_existing_to_range(false)
.max_decimals(2)
.update_while_editing(false)
.measure(unit);
if ui.add(field).labelled_by(by_label.id).changed() {
mixed.remove("indentFirst");
}
*first = if kind == 2 { -by.abs() } else { by.abs() };
}
});
}
/// A Paragraph dialog check box for the field `key`. While the selected paragraphs differ in
/// it, it shows as neither on nor off, and a click turns it on for all of them.
pub(super) fn para_check(ui: &mut Ui, on: &mut bool, text: &str, key: &str, mixed: &mut BTreeSet<String>) {
if !mixed.contains(key) {
ui.checkbox(on, text);
} else if ui.add(egui::Checkbox::new(&mut false, text).indeterminate(true)).clicked() {
*on = true;
mixed.remove(key);
}
}
/// The Paragraph dialog's OK, as one `para.set` and so one undo step (#1346): the fields that
/// differ from what every selected paragraph had when it opened, not the blank ones, which
/// stay as each paragraph has them. The alignment and indents are as seen on the page, so a
/// new direction keeps them where the dialog shows them.
pub(super) fn apply_paragraph(app: &mut WordApp, d: &Dialog) {
let Dialog::Paragraph { start, mixed, .. } = d else { return };
let fields = para_fields(d);
let turned = !mixed.contains("bidi") && fields.get("bidi") != start.get("bidi");
let (mut props, mut page) = (serde_json::Map::new(), serde_json::Map::new());
for (k, v) in fields {
let on_page = matches!(k.as_str(), "align" | "left" | "right");
if mixed.contains(&k) || (start.get(&k) == Some(&v) && !(turned && on_page)) {
continue;
}
if on_page {
page.insert(k, v);
} else {
props.insert(k, v);
}
}
if !props.is_empty() || !page.is_empty() {
let _ = app.run("para.set", json!({"props": props, "onPage": page}));
}
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.