Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Merge main into fix/format-text-fields-fit-pane

RiceDeef711b163c6eb
diff --cc crates/ui-egui/src/panels/format_text_ui.rs
index 7b2763bf3,dbb50f73e..38ccb96b1
--- a/crates/ui-egui/src/panels/format_text_ui.rs
+++ b/crates/ui-egui/src/panels/format_text_ui.rs
@@@ -102,18 -107,6 +107,22 @@@ const LABEL_ICON: f32 = 22.0
/// The button beside a number field that opens its list of common values: just an arrow, so the
/// field keeps the room its value needs.
const PRESET_LIST: f32 = 22.0;
+/// The room a number field keeps for its value ("-5.0 pt"), beside the arrow in its box.
+const NUMBER_MIN: f32 = 52.0;
++/// The size field, which reserves room for "Mixed" so it doesn't resize while typing.
++/// shortcut: measured for English "Mixed"; a longer translation widens the size row past the
++/// narrowest pane. Upgrade by having the panel report its measured row width to the pane.
++const SIZE_FIELD: f32 = 62.0;
+/// The least width the panel's widest rows need, which sets how narrow the Tools pane may be
+/// dragged. Size and alignment: the size field and its arrow (in their box), the swatch, the
+/// divider, three 30 pt buttons, and the gaps between those six. The spacing rows: two icons and
+/// two number fields with their arrows (in a box each), the space between the columns, and four
+/// gaps.
+pub(crate) const MIN_ROW: f32 = {
- let size_row = (40.0 + PRESET_LIST + 2.0) + 28.0 + 9.0 + 3.0 * 30.0 + 5.0 * GAP;
++ let size_row = (SIZE_FIELD + PRESET_LIST + 2.0) + 28.0 + 9.0 + 3.0 * 30.0 + 5.0 * GAP;
+ let spacing_row = 2.0 * LABEL_ICON + 2.0 * (NUMBER_MIN + PRESET_LIST + 2.0) + 8.0 + 4.0 * GAP;
+ if size_row > spacing_row { size_row } else { spacing_row }
+};
/// What can be typed: horizontal scale in percent, character spacing in points.
const SCALE_RANGE: std::ops::RangeInclusive<f64> = 10.0..=400.0;
const CHAR_SPACING_RANGE: std::ops::RangeInclusive<f64> = -5.0..=50.0;
@@@ -137,55 -130,109 +146,116 @@@ pub(crate) fn panel
widgets::section_title(ui, tl!("Format text"));
// The font, across the panel.
+ ui.spacing_mut().combo_height = 360.0;
let width = ui.available_width();
- outlined(ui, |ui| {
- let font_resp = craft_ui::ComboBox::from_id_salt("format-font")
- .selected_text(fonts.shown.as_str())
- .width(width)
- .show_ui(ui, |ui| {
- if let Some(sources) = fonts.sources {
- if ui.selectable_label(false, tl!("Keep source fonts")).clicked() {
- out.font = Some(FontChoice::KeepSource);
- }
- for (resource, face) in sources.iter().filter(|(r, _)| !r.starts_with("PCApproved")) {
- if ui.selectable_label(false, face).clicked() {
- out.font = Some(FontChoice::Resource(resource.clone()));
++ let font_resp = outlined(ui, |ui| {
+ craft_ui::ComboBox::from_id_salt("format-font")
+ .selected_text(fonts.shown.as_str())
+ .width(width)
+ .show_ui(ui, |ui| {
+ if let Some(sources) = fonts.sources {
+ if ui.selectable_label(false, tl!("Keep source fonts")).clicked() {
+ out.font = Some(FontChoice::KeepSource);
}
+ for (resource, face) in sources.iter().filter(|(r, _)| !r.starts_with("PCApproved")) {
+ if ui.selectable_label(false, face).clicked() {
+ out.font = Some(FontChoice::Resource(resource.clone()));
+ }
+ }
+ ui.separator();
}
- ui.separator();
- }
- for f in [FontFamily::Helvetica, FontFamily::Times, FontFamily::Courier] {
- if ui.selectable_label(fonts.sources.is_none() && look.family == f, f.label()).clicked() {
- out.font = Some(FontChoice::Family(f));
- }
- }
- if fonts.sources.is_some() {
- ui.separator();
- if ui.selectable_label(false, tl!("Load font…")).clicked() {
- out.font = Some(FontChoice::Load);
+ for f in [FontFamily::Helvetica, FontFamily::Times, FontFamily::Courier] {
+ if ui.selectable_label(fonts.sources.is_none() && look.family == f, f.label()).clicked() {
+ out.font = Some(FontChoice::Family(f));
+ }
}
- #[cfg(not(target_arch = "wasm32"))]
- ui.menu_button(tl!("Installed fonts…"), |ui| {
- ui.set_min_width(260.0);
- let reload = ui.button(tl!("Reload")).clicked();
- match crate::files::installed_font_files(ui.ctx(), reload) {
- Some(list) if list.is_empty() => {
- ui.label(tl!("No openly licensed document fonts found. Load a font file."));
- }
- Some(list) => {
- let search_id = ui.scope_id().with("installed-font-search");
- let mut query = ui.data(|d| d.get_temp::<String>(search_id)).unwrap_or_default();
- ui.horizontal(|ui| {
- let (icon_rect, _) = ui.allocate_exact_size(egui::vec2(16.0, 16.0), egui::Sense::hover());
- crate::icons::paint(ui, icon_rect, "search", 13.0, ui.visuals().text_color().gamma_multiply(0.6));
- let resp = ui
- .add(egui::TextEdit::singleline(&mut query).hint_text(tl!("Search…")).desired_width(ui.available_width() - 8.0));
- if resp.changed() {
- ui.data_mut(|d| d.insert_temp(search_id, query.clone()));
- }
- });
- ui.separator();
+ if fonts.sources.is_some() {
+ ui.separator();
+ if ui.selectable_label(false, tl!("Load font…")).clicked() {
+ out.font = Some(FontChoice::Load);
+ }
+ #[cfg(not(target_arch = "wasm32"))]
- ui.menu_button(tl!("Installed fonts…"), |ui| match crate::files::installed_font_files(ui.ctx()) {
- Some(list) if list.is_empty() => {
- ui.label(tl!("No openly licensed document fonts found. Load a font file."));
- }
- Some(list) => {
- for font in list {
- if ui.button(&font.face).clicked() {
- out.font = Some(FontChoice::Installed(font.path));
- ui.close();
++ ui.menu_button(tl!("Installed fonts…"), |ui| {
++ ui.set_min_width(260.0);
++ let reload = ui.button(tl!("Reload")).clicked();
++ match crate::files::installed_font_files(ui.ctx(), reload) {
++ Some(list) if list.is_empty() => {
++ ui.label(tl!("No openly licensed document fonts found. Load a font file."));
++ }
++ Some(list) => {
++ let search_id = ui.scope_id().with("installed-font-search");
++ let mut query = ui.data(|d| d.get_temp::<String>(search_id)).unwrap_or_default();
++ ui.horizontal(|ui| {
++ let (icon_rect, _) = ui.allocate_exact_size(egui::vec2(16.0, 16.0), egui::Sense::hover());
++ crate::icons::paint(ui, icon_rect, "search", 13.0, ui.visuals().text_color().gamma_multiply(0.6));
++ let resp = ui.add(
++ egui::TextEdit::singleline(&mut query).hint_text(tl!("Search…")).desired_width(ui.available_width() - 8.0),
++ );
++ if resp.changed() {
++ ui.data_mut(|d| d.insert_temp(search_id, query.clone()));
++ }
++ });
++ ui.separator();
+
- let query_lower = query.trim().to_lowercase();
- let mut families: std::collections::BTreeMap<&str, Vec<&pdfcraft_engine::text_fonts::FontFile>> =
- std::collections::BTreeMap::new();
- for font in &list {
- if query_lower.is_empty()
- || font.family.to_lowercase().contains(&query_lower)
- || font.face.to_lowercase().contains(&query_lower)
- {
- families.entry(&font.family).or_default().push(font);
++ let query_lower = query.trim().to_lowercase();
++ let mut families: std::collections::BTreeMap<&str, Vec<&pdfcraft_engine::text_fonts::FontFile>> =
++ std::collections::BTreeMap::new();
++ for font in &list {
++ if query_lower.is_empty()
++ || font.family.to_lowercase().contains(&query_lower)
++ || font.face.to_lowercase().contains(&query_lower)
++ {
++ families.entry(&font.family).or_default().push(font);
++ }
+ }
- }
+
- if families.is_empty() {
- ui.label(tl!("No openly licensed document fonts found. Load a font file."));
- } else {
- egui::ScrollArea::vertical().max_height(350.0).show(ui, |ui| {
- for (family, faces) in families {
- if faces.len() == 1 {
- if let Some(&font) = faces.first() {
- let label =
- if font.face != font.family && !font.face.is_empty() { font.face.as_str() } else { family };
- if ui.button(label).clicked() {
- out.font = Some(FontChoice::Installed(font.path.clone()));
- ui.close();
- }
- }
- } else {
- ui.menu_button(family, |ui| {
- ui.set_min_width(180.0);
- for font in faces {
++ if families.is_empty() {
++ ui.label(tl!("No openly licensed document fonts found. Load a font file."));
++ } else {
++ egui::ScrollArea::vertical().max_height(350.0).show(ui, |ui| {
++ for (family, faces) in families {
++ if faces.len() == 1 {
++ if let Some(&font) = faces.first() {
+ let label =
- font.face.strip_prefix(family).map(str::trim).filter(|s| !s.is_empty()).unwrap_or(&font.face);
++ if font.face != font.family && !font.face.is_empty() { font.face.as_str() } else { family };
+ if ui.button(label).clicked() {
+ out.font = Some(FontChoice::Installed(font.path.clone()));
+ ui.close();
+ }
+ }
- });
++ } else {
++ ui.menu_button(family, |ui| {
++ ui.set_min_width(180.0);
++ for font in faces {
++ let label = font
++ .face
++ .strip_prefix(family)
++ .map(str::trim)
++ .filter(|s| !s.is_empty())
++ .unwrap_or(&font.face);
++ if ui.button(label).clicked() {
++ out.font = Some(FontChoice::Installed(font.path.clone()));
++ ui.close();
++ }
++ }
++ });
++ }
+ }
- }
- });
++ });
+ }
+ }
- }
- None => {
- ui.label(tl!("Finding document fonts…"));
++ None => {
++ ui.label(tl!("Finding document fonts…"));
+ }
}
- None => {
- ui.label(tl!("Finding document fonts…"));
- }
- }
- });
- ui.add_space(4.0);
- }
- })
- .response;
+ });
++ ui.add_space(4.0);
+ }
+ })
+ .response
- .on_hover_text(format!("{}\n{}", tl!("Font"), tl!("Thai and Arabic use regular fallback fonts when available.")))
+ });
+ font_resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ComboBox, ui.is_enabled(), tl!("Font")));
+ font_resp.on_hover_text(format!("{}\n{}", tl!("Font"), tl!("Thai and Arabic use regular fallback fonts when available.")));
ui.add_space(4.0);
// Size and colour | bullets, numbering and alignment.
@@@ -261,44 -318,36 +341,49 @@@
// Line and paragraph spacing; horizontal scale and character spacing: two columns, each an
// icon and its control, `half` wide.
-- let half = ((ui.available_width() - 2.0 * LABEL_ICON - 8.0 - 3.0 * GAP) / 2.0).max(40.0);
++ // From the font list's width, not what is left now: a row above that overflows (a long
++ // translation of "Mixed" in the size field) widens the panel's layout.
++ let half = ((width - 2.0 * LABEL_ICON - 8.0 - 3.0 * GAP) / 2.0).max(40.0);
let editing = extras.is_some();
let mut fallback = Extras::default();
let e = extras.as_deref_mut().unwrap_or(&mut fallback);
ui.add_enabled_ui(editing, |ui| {
row(ui, |ui| {
- labelled_icon(ui, t, "arrow-up-down", tl!("Line spacing"));
+ let label = labelled_icon(ui, t, "arrow-up-down", tl!("Line spacing"));
let own = tl!("Auto").to_string();
let shown = if e.line_spacing == 0.0 { own.clone() } else { number(e.line_spacing) };
- let spacing_resp = craft_ui::ComboBox::from_id_salt("format-line-spacing")
- .selected_text(shown)
- .width(half)
- .show_ui(ui, |ui| {
- ui.selectable_value(&mut e.line_spacing, 0.0, own.as_str());
- for v in LINE_SPACINGS {
- ui.selectable_value(&mut e.line_spacing, v, number(v));
- }
- })
- .response;
- spacing_resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ComboBox, ui.is_enabled(), tl!("Line spacing")));
+ outlined(ui, |ui| {
+ // The column is `half` wide in every language: a longer "Auto" is cut short (the
+ // menu and the tooltip have it whole).
+ ui.set_max_width(half);
- let list = craft_ui::ComboBox::from_id_salt("format-line-spacing")
++ let spacing_resp = craft_ui::ComboBox::from_id_salt("format-line-spacing")
+ .selected_text(centred(ui, &shown))
+ .width(half)
+ .wrap_mode(egui::TextWrapMode::Truncate)
+ .show_ui(ui, |ui| {
+ ui.selectable_value(&mut e.line_spacing, 0.0, own.as_str());
+ for v in LINE_SPACINGS {
+ ui.selectable_value(&mut e.line_spacing, v, number(v));
+ }
- });
- list.response.labelled_by(label.id).on_hover_text(&shown);
++ })
++ .response
++ .labelled_by(label.id);
++ spacing_resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ComboBox, ui.is_enabled(), tl!("Line spacing")));
++ spacing_resp.on_hover_text(&shown);
+ });
ui.add_space(8.0);
ui.add_enabled_ui(false, |ui| {
- labelled_icon(ui, t, "pilcrow", tl!("Paragraph spacing"));
+ let label = labelled_icon(ui, t, "pilcrow", tl!("Paragraph spacing"));
let tip = format!("{} — {}", tl!("Paragraph spacing"), tl!("Planned"));
- let _ = craft_ui::ComboBox::from_id_salt("format-paragraph-spacing")
- .selected_text("0")
- .width(half)
- .show_ui(ui, |_| {})
- .response
- .on_disabled_hover_text(tip);
+ let _ = outlined(ui, |ui| {
+ craft_ui::ComboBox::from_id_salt("format-paragraph-spacing")
+ .selected_text(centred(ui, "0"))
+ .width(half)
+ .show_ui(ui, |_| {})
+ .response
+ .labelled_by(label.id)
+ .on_disabled_hover_text(tip)
+ });
});
});
});
@@@ -424,10 -424,13 +510,11 @@@ fn boxed(ui: &mut egui::Ui, add: impl F
/// A number field with an arrow beside it that lists common values, in `width` all told: type any
/// value in its range, or pick one. Both are named by `label`: the icon before them and its text.
fn number_with_presets(ui: &mut egui::Ui, n: Number<'_>, presets: &[f64], width: f32, label: (egui::Id, &str)) {
- ui.scope(|ui| {
- ui.spacing_mut().item_spacing.x = 2.0;
+ boxed(ui, |ui| {
let field = egui::DragValue::new(&mut *n.value).range(n.range).speed(n.speed).max_decimals(2).suffix(n.suffix);
- ui.add_sized([(width - PRESET_LIST - 2.0).max(30.0), 26.0], field).labelled_by(label.0);
- let height = ui.spacing().interact_size.y;
- let drag_resp = ui.add_sized([(width - PRESET_LIST - 2.0).max(30.0), height], field).labelled_by(label.0);
++ let drag_resp = ui.add_sized([(width - PRESET_LIST - 2.0).max(30.0), 26.0], field).labelled_by(label.0);
+ drag_resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::SpinButton, ui.is_enabled(), label.1));
- let list = icons::button(ui, "chevron-down", PRESET_LIST, false, label.1);
+ let list = preset_arrow(ui, label.1);
egui::Popup::menu(&list).show(|ui| {
for &v in presets {
if ui.selectable_label(*n.value == v, number(v)).clicked() {
@@@ -453,27 -456,44 +540,50 @@@ fn colour_button(ui: &mut egui::Ui, t:
/// Commit a typed size once (Enter or leaving the field), rather than one undo step per key.
/// The bounds match AddedText validation, so the panel also accepts coloured text below 8 pt.
- fn font_size_input(ui: &mut egui::Ui, size: &mut f64) {
+ fn font_size_input(ui: &mut egui::Ui, size: &mut f64, mixed: bool) -> bool {
+ let mut committed = false;
- ui.scope(|ui| {
- ui.spacing_mut().item_spacing.x = 2.0;
++ // The panel's own padding, read before the box tightens it, so "Mixed" keeps its margins.
++ let margin = egui::Margin::from(ui.spacing().button_padding);
+ boxed(ui, |ui| {
let id = ui.make_persistent_id("font-size-input");
- let (previous, mut text) = ui.data(|d| d.get_temp::<(u64, String)>(id)).unwrap_or_else(|| (size.to_bits(), size.to_string()));
- if previous != size.to_bits() {
- text = size.to_string();
+ let initial_text = || {
+ if mixed || *size <= 0.0 { tl!("Mixed").to_string() } else { size.to_string() }
+ };
+ // A selection can become mixed without changing its representative numeric size.
+ let (previous, previous_mixed, mut text) =
+ ui.data(|d| d.get_temp::<(u64, bool, String)>(id)).unwrap_or_else(|| (size.to_bits(), mixed, initial_text()));
+ if previous != size.to_bits() || previous_mixed != mixed {
+ text = initial_text();
}
- let field = egui::TextEdit::singleline(&mut text).id(id).desired_width(40.0).frame(egui::Frame::NONE).align(egui::Align2::CENTER_CENTER);
- let response = ui.add_sized([40.0, 26.0], field).accessible_name(tl!("Font size:"));
+ // Reserve the localized mixed label, field margins and caret without resizing while typing.
+ let font = egui::TextStyle::Body.resolve(ui.style());
- let margin = egui::Margin::from(ui.spacing().button_padding);
+ let mixed_width = ui.painter().layout_no_wrap(tl!("Mixed").into(), font.clone(), ui.visuals().text_color()).size().x;
+ let width = (mixed_width + margin.sum().x + 2.0).ceil().max(40.0);
- let field = egui::TextEdit::singleline(&mut text).id(id).font(font).margin(margin).desired_width(width);
- let response = ui.add_sized([width, ui.spacing().interact_size.y], field).accessible_name(tl!("Font size:"));
++ let field = egui::TextEdit::singleline(&mut text)
++ .id(id)
++ .font(font)
++ .margin(margin)
++ .desired_width(width)
++ .frame(egui::Frame::NONE)
++ .align(egui::Align2::CENTER_CENTER);
++ let response = ui.add_sized([width, 26.0], field).accessible_name(tl!("Font size:"));
let cancel = (response.has_focus() || response.lost_focus()) && ui.input(|i| i.key_pressed(egui::Key::Escape));
if cancel {
- text = size.to_string();
+ text = initial_text();
response.surrender_focus();
} else if response.lost_focus() || (response.has_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter))) {
if let Some(value) = typed_font_size(&text) {
+ if mixed || *size != value {
+ committed = true;
+ }
*size = value;
+ text = size.to_string();
+ } else {
+ text = initial_text();
}
- text = size.to_string();
}
let before = *size;
- let list = icons::button(ui, "chevron-down", PRESET_LIST, false, tl!("Font size"));
+ let list = preset_arrow(ui, tl!("Font size"));
egui::Popup::menu(&list).show(|ui| {
for value in SIZES {
if ui.selectable_label(*size == value, format!("{value} pt")).clicked() {
diff --cc crates/ui-egui/src/panels/side_panels.rs
index 1fdebdfaa,447aeac9f..78cf967df
--- a/crates/ui-egui/src/panels/side_panels.rs
+++ b/crates/ui-egui/src/panels/side_panels.rs
@@@ -488,7 -509,7 +512,7 @@@ mod tests
]),
);
let restored = restore(&old);
- assert_eq!(restored.panels, SidePanels { tools_side: ToolsSide::Right, tools_width: 300.0, side_width: 380.0 });
- assert_eq!(restored.panels, SidePanels { tools_side: ToolsSide::Right, rail_side: ToolsSide::Right, tools_width: 250.0, side_width: 380.0 });
++ assert_eq!(restored.panels, SidePanels { tools_side: ToolsSide::Right, rail_side: ToolsSide::Right, tools_width: 300.0, side_width: 380.0 });
assert_eq!((restored.tools_open, restored.side_closed), (Some(false), true));
}