PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Keep typed font sizes accessible and within the current panel layout
FredApps4b319bb38357
diff --git a/crates/ui-egui/src/panels/format_text_ui.rs b/crates/ui-egui/src/panels/format_text_ui.rsindex 5807bb173..8b36e7984 100644--- a/crates/ui-egui/src/panels/format_text_ui.rs+++ b/crates/ui-egui/src/panels/format_text_ui.rs@@ -320,32 +320,40 @@ fn colour_button(ui: &mut egui::Ui, t: &Tokens, color: [f64; 3]) -> egui::Respon/// 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) {- 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 response = ui.add(egui::TextEdit::singleline(&mut text).id(id).desired_width(44.0)).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();- 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) {- *size = value;+ ui.scope(|ui| {+ ui.spacing_mut().item_spacing.x = 2.0;+ 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();}- text = size.to_string();- }- let before = *size;- craft_ui::ComboBox::from_id_salt("font-size").selected_text("pt").width(28.0).show_ui(ui, |ui| {- for value in SIZES {- ui.selectable_value(size, value, format!("{value} pt"));+ let field = egui::TextEdit::singleline(&mut text).id(id).desired_width(40.0);+ let response = ui.add_sized([40.0, ui.spacing().interact_size.y], 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();+ 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) {+ *size = value;+ }+ text = size.to_string();+ }+ let before = *size;+ let list = icons::button(ui, "chevron-down", PRESET_LIST, false, tl!("Font size"));+ egui::Popup::menu(&list).show(|ui| {+ for value in SIZES {+ if ui.selectable_label(*size == value, format!("{value} pt")).clicked() {+ *size = value;+ ui.close();+ }+ }+ });+ if *size != before {+ text = size.to_string();}+ ui.data_mut(|d| d.insert_temp(id, (size.to_bits(), text)));});- if *size != before {- text = size.to_string();- }- ui.data_mut(|d| d.insert_temp(id, (size.to_bits(), text)));}fn typed_font_size(text: &str) -> Option<f64> {diff --git a/crates/ui-egui/tests/it/content.rs b/crates/ui-egui/tests/it/content.rsindex 4a7927f69..aa832fe2d 100644--- a/crates/ui-egui/tests/it/content.rs+++ b/crates/ui-egui/tests/it/content.rs@@ -699,7 +699,7 @@ fn format_text_rows_line_up_and_fit_the_panel() {let combo = |h: &Harness<'static, PdfCraftApp>, value: &str| {h.query_all_by_role(Role::ComboBox).find(|n| n.value().as_deref() == Some(value)).map(|n| n.rect()).expect(value)};- let (font, size) = (combo(&h, "Helvetica"), combo(&h, "12"));+ let (font, size) = (combo(&h, "Helvetica"), h.get_by_label("Font size:").rect());let middle = |r: egui::Rect| r.center().y;for label in ["Colour", "Alignment"] {let other = h.get_by_label(label).rect();@@ -716,7 +716,7 @@ fn format_text_rows_line_up_and_fit_the_panel() {for r in &scale_row {assert!((middle(*r) - middle(scale_row[0])).abs() < 0.5, "{scale_row:?}");}- for r in h.query_all_by_role(Role::ComboBox).map(|n| n.rect()).chain(scale_row) {+ for r in h.query_all_by_role(Role::ComboBox).map(|n| n.rect()).chain(scale_row).chain([size, h.get_by_label("Font size").rect()]) {assert!(r.right() <= font.right() + 0.5, "{r:?} reaches past the panel ({font:?})");}}diff --git a/crates/ui-egui/tests/it/editing.rs b/crates/ui-egui/tests/it/editing.rsindex 0dc4914b1..b0a8be83d 100644--- a/crates/ui-egui/tests/it/editing.rs+++ b/crates/ui-egui/tests/it/editing.rs@@ -4166,6 +4166,8 @@ fn typed_small_coloured_text_size_survives_save_and_reopen() {h.event(egui::Event::Text("6.5".into()));h.key_press(Key::Enter);h.run_steps(4);+ h.get_by_label("Colour").click();+ h.run_steps(2);h.get_by_label("Blue").click();h.run_steps(4);let id = h.state().views[0].id;