PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Type options: the "T" icon shows its "Font size" tooltip (#6437)
Marco Arnulfo08a7ff622a64
diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rsindex ed530fb2d..5ce01c559 100644--- a/crates/ui-egui/src/type_tool.rs+++ b/crates/ui-egui/src/type_tool.rs@@ -1744,10 +1744,9 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {}let (r, icon) = ui.allocate_exact_size(egui::vec2(18.0, 22.0), egui::Sense::hover());crate::icons::paint(ui, r, "type", 13.0, t.icon);- let icon = icon.on_hover_text(tl!("Font size"));icon.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Label, true, tl!("Font size")));// Photoshop's "T" scrubs the size field beside it, like the Character panel's icons (#6437).- crate::widgets::scrub_option_label(ui, &icon);+ crate::widgets::scrub_option_icon(ui, &icon, tl!("Font size"));let (unit, label) = type_unit(app);let mut display = size * unit;if crate::widgets::value_field(ui, &mut display, unit..=1296.0 * unit, label, 66.0).changed() {diff --git a/crates/ui-egui/src/type_tool_tests.rs b/crates/ui-egui/src/type_tool_tests.rsindex b14cd16cf..d89704db5 100644--- a/crates/ui-egui/src/type_tool_tests.rs+++ b/crates/ui-egui/src/type_tool_tests.rs@@ -491,6 +491,28 @@ fn options_bar_t_icon_scrub_stops_at_the_size_limits() {assert_eq!(size_at(h.state(), id, 0), 1296.0, "Shift (10 pt per point) past the top stops at the maximum");}+/// Rests the pointer on the widget named `label` past the tooltip delay; true when its tooltip+/// (a second node with the same name) shows.+fn tooltip_shows(h: &mut Harness<'static, PhotocraftApp>, label: &str) -> bool {+ let at = h.get_by_label(label).rect().center();+ h.hover_at(at);+ h.run_steps(8);+ h.query_all_by_label(label).count() == 2+}++/// The scrubby "T" icon keeps its "Font size" tooltip: the size field's drag area, laid over the+/// icon after it, must not take its hover.+#[test]+fn options_bar_t_icon_shows_its_tooltip() {+ let mut app = new_app();+ app.run("type.create", json!({"text": "Hello", "size": 20, "x": 300, "y": 420})).unwrap();+ let mut h = options_harness(app);+ assert!(tooltip_shows(&mut h, "Set the text color"), "the probe sees an ordinary tooltip");+ h.hover_at(egui::pos2(890.0, 55.0));+ h.run_steps(4);+ assert!(tooltip_shows(&mut h, "Font size"), "hovering the \"T\" icon shows \"Font size\"");+}+/// A point inside glyph `i` of vertical type: `f` of the way down its advance, right of the/// column's centre line.fn vglyph(h: &mut Harness<'static, PhotocraftApp>, id: LayerId, i: usize, f: f32) -> Pos2 {diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rsindex 6e841292e..455c13187 100644--- a/crates/ui-egui/src/widgets.rs+++ b/crates/ui-egui/src/widgets.rs@@ -386,6 +386,8 @@ struct LabelScrub {max_gap: f32,/// Options captions defer interaction until the adjacent field bounds are known.option_area: bool,+ /// An options icon's tooltip, shown by the drag area laid over it (which takes its hover).+ tip: Option<&'static str>,/// Whole steps to move the value (left negative; Shift ×10, Alt a tenth as fast).steps: f32,started: bool,@@ -407,12 +409,23 @@ pub fn scrub_label(ui: &Ui, label: &Response) -> Response {/// A tool-options caption whose scrub target includes the adjacent field's outer box./// Keep the original label bounds for linking; register the hit area before the numeric editor.pub fn scrub_option_label(ui: &Ui, label: &Response) {+ scrub_option_area(ui, label, None);+}++/// [`scrub_option_label`] for an icon with tooltip `tip`. The field's drag area is laid over the+/// icon after it, so egui hovers the area and not the icon: `on_hover_text` on the icon never shows.+pub fn scrub_option_icon(ui: &Ui, icon: &Response, tip: &'static str) {+ scrub_option_area(ui, icon, Some(tip));+}++fn scrub_option_area(ui: &Ui, label: &Response, tip: Option<&'static str>) {let scrub = LabelScrub {pass: ui.ctx().cumulative_pass_nr(),label: label.id.with("pc-option-scrub"),rect: label.rect,max_gap: 24.0,option_area: true,+ tip,steps: 0.0,started: false,dragging: false,@@ -468,6 +481,7 @@ fn label_scrub_response(ui: &Ui, resp: &Response, original_rect: Rect, max_gap:rect: original_rect,max_gap,option_area: false,+ tip: None,steps,started: resp.drag_started(),dragging: resp.dragged(),@@ -537,7 +551,12 @@ fn value_field_box(// numeric button. It is not focusable; the editor drawn next wins inside its bounds.// Preserve a popup field's trailing arrow, and never extend outside the UI clip.let hit = Rect::from_min_max(pos2(s.rect.left(), rect.top()), pos2(rect.right() - trailing, rect.bottom())).intersect(ui.clip_rect());- let resp = ui.interact(hit, s.label, Sense::CLICK | Sense::DRAG);+ let mut resp = ui.interact(hit, s.label, Sense::CLICK | Sense::DRAG);+ if let Some(tip) = s.tip+ && resp.hover_pos().is_some_and(|p| s.rect.contains(p))+ {+ resp = resp.on_hover_text(tip);+ }label_scrub_response(ui, &resp, s.rect, s.max_gap)});let (dpi, ppi) = ui.data(|d| d.get_temp::<(f64, f64)>(field_resolution_id())).unwrap_or((72.0, 72.0));