#7464: Anchor the mini toolbar at the whole line's edge in mixed-direction RTL lines

visible_anchor kept only the first emitted run of the topmost line; runs come out left to right, so an RTL line with several runs anchored at the leftmost run's right edge. Union the runs on that line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FypgCZhHKRuRB5wZUiw6BE
This commit is contained in:
Martin Ball
2026-11-22 21:57:46 +00:00
co-authored by Claude Opus 5.5
parent a242d95ca4
commit da876aa7dc
2 changed files with 20 additions and 4 deletions
+8 -4
View File
@@ -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<Item = Rect>, clip: Rect) -> Option<Rect> {
if !clip.is_finite() || !clip.is_positive() {
return None;
}
let mut first: Option<Rect> = 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<Item = Rect>, 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.
+12
View File
@@ -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);
}