diff --git a/crates/ui-egui/src/mini_toolbar.rs b/crates/ui-egui/src/mini_toolbar.rs index 439e26d1cd..2646ff338f 100644 --- a/crates/ui-egui/src/mini_toolbar.rs +++ b/crates/ui-egui/src/mini_toolbar.rs @@ -23,15 +23,15 @@ const ANCHOR_INSET: f32 = 4.0; /// Least height of a selection rectangle (screen points) that counts as visible for anchoring. const MIN_VISIBLE_H: f32 = 1.0; -/// The part of the selection the bar anchors to: the topmost selection rectangle that is actually -/// visible inside `clip` (the scroll viewport), cut down to its visible portion. `None` when no -/// usable part of the selection is on screen. Rectangles that only touch the clip's edge, or are +/// The part of the selection the bar anchors to: the topmost visible line of the selection inside +/// `clip` (the scroll viewport; the union of its runs), cut down to its visible portion. `None` when no usable part of the selection is on screen. Rectangles that only touch the clip's edge, or are /// not finite, are ignored. Pure geometry, unit tested without a window. pub fn visible_anchor(selection_rects: impl IntoIterator, clip: Rect) -> Option { if !clip.is_finite() || !clip.is_positive() { return None; } let mut first: Option = None; + let mut visible_rects = Vec::new(); for sr in selection_rects { if !sr.is_finite() || !sr.intersects(clip) { continue; @@ -44,8 +44,12 @@ pub fn visible_anchor(selection_rects: impl IntoIterator, clip: Rec if first.is_none_or(|f| visible.min.y < f.min.y) { first = Some(visible); } + visible_rects.push(visible); } - first + // A bidirectional line is emitted as several visual runs, left to right: span the whole line, so + // its right edge (the start of a right-to-left paragraph) is not that of the first run. + let top = first?; + Some(visible_rects.into_iter().filter(|r| (top.min.y..=top.max.y).contains(&r.center().y)).fold(top, |u, r| u.union(r))) } /// The x the bar centres at over `anchor`: its start edge, less the inset, mirrored for right-to-left. diff --git a/crates/ui-egui/src/mini_toolbar/tests.rs b/crates/ui-egui/src/mini_toolbar/tests.rs index a147f12d07..c741369e57 100644 --- a/crates/ui-egui/src/mini_toolbar/tests.rs +++ b/crates/ui-egui/src/mini_toolbar/tests.rs @@ -732,3 +732,15 @@ fn the_bar_anchors_at_the_start_of_a_multi_line_left_to_right_selection() { assert!(anchor.width() > 100.0, "the first selected line is a wide rect: {anchor:?}"); assert!((x - anchor.min.x).abs() < (x - anchor.max.x).abs(), "the bar x={x} is nearer the start (left) edge of {anchor:?}"); } + +/// #7464: a right-to-left line mixing Arabic with Latin words or digits is several visual runs +/// (emitted left to right): the bar hangs from the whole line's right edge, not the first run's. +#[test] +fn the_anchor_spans_every_run_on_the_topmost_line() { + let clip = vp(); + let run = |x: f32, y: f32| Rect::from_min_size(pos2(x, y), Vec2::new(50.0, 20.0)); + let a = visible_anchor([run(200.0, 100.0), run(300.0, 100.0), run(250.0, 100.0), run(900.0, 130.0)], clip).unwrap(); + assert!(a.min.x == 200.0 && a.max.x == 350.0 && a.min.y == 100.0 && a.max.y == 120.0, "later line excluded, runs unioned: {a:?}"); + let x = start_x(a, true); + assert!((x - a.max.x).abs() < (x - a.min.x).abs(), "RTL bar x={x} is nearer the line's right edge {}, not the left {}", a.max.x, a.min.x); +}