Compare commits

...
Author SHA1 Message Date
Martin BallandClaude Opus 5.5 b9bcf8c2ef #7464: Union line runs for the mini toolbar anchor in RTL only
Vertical text stacks whole lines at one y-range, so the union moved the left-to-right anchor off the first line. Left to right keeps the first rectangle again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FypgCZhHKRuRB5wZUiw6BE
2026-11-22 22:06:11 +00:00
Martin BallandClaude Opus 5.5 da876aa7dc #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
2026-11-22 21:57:46 +00:00
Martin BallandClaude Opus 5.5 a242d95ca4 #7464: Mini toolbar anchors at the right edge in right-to-left paragraphs
The bar sat at anchor.min.x + inset, the end of a selection that starts in an
RTL paragraph. Mirror it to anchor.max.x - inset when the paragraph where the
selection starts is RTL. Adds an RTL test and a multi-line LTR test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FypgCZhHKRuRB5wZUiw6BE
2026-11-22 19:47:17 +00:00
3 changed files with 106 additions and 16 deletions
+1 -1
View File
@@ -864,7 +864,7 @@ fn show_pages(app: &mut WordApp, ui: &mut Ui, layout: &Arc<DocLayout>, full: Rec
} else {
screen_rects(&selected, body.pages)
};
app.canvas.mini_anchor = crate::mini_toolbar::visible_anchor(anchor_rects, body.view.rect);
app.canvas.mini_anchor = crate::mini_toolbar::visible_anchor(anchor_rects, body.view.rect, crate::mini_toolbar::starts_rtl(app));
// The bar is its own floating layer: shown after the host, it keeps its buttons' presses (S-L410).
crate::mini_toolbar::show(app, ui, body);
}
+30 -9
View File
@@ -18,20 +18,22 @@ use serde_json::{Value, json};
use crate::WordApp;
use crate::colors::{Remember, Target};
/// Distance from the selection's start at which the bar is centred.
/// Distance from the selection's start (its left edge, or right in a right-to-left paragraph) at which the bar is centred.
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
/// 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> {
/// The part of the selection the bar anchors to: the topmost visible line of the selection 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 not finite, are ignored.
/// For `rtl` the result spans every run on that line (see below); otherwise it is the first such
/// rectangle. Pure geometry, unit tested without a window.
pub fn visible_anchor(selection_rects: impl IntoIterator<Item = Rect>, clip: Rect, rtl: bool) -> 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 +46,26 @@ 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
let top = first?;
if !rtl {
return Some(top);
}
// 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.
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.
fn start_x(anchor: Rect, rtl: bool) -> f32 {
if rtl { anchor.max.x - ANCHOR_INSET } else { anchor.min.x + ANCHOR_INSET }
}
/// Whether the paragraph where the selection starts is right-to-left.
pub(crate) fn starts_rtl(app: &WordApp) -> bool {
let start = app.session.sel.ordered().0;
app.session.doc.para_at(&start).is_some_and(|p| app.session.doc.styles.resolve_para(&p.props).bidi)
}
/// Whether the bar may be shown for the current app state.
@@ -101,8 +121,9 @@ pub(crate) fn show(app: &mut WordApp, ui: &mut Ui, body: &PageBody<'_>) {
return;
}
let Some(anchor) = app.canvas.mini_anchor else { return };
// The bar centres over the selection's start rather than drifting to the centre of long selections (#7447).
let start_x = anchor.min.x + ANCHOR_INSET;
// The bar centres over the selection's start rather than drifting to the centre of long selections (#7447);
// a right-to-left paragraph at the start puts it at the right edge (#7464).
let start_x = start_x(anchor, starts_rtl(app));
let anchor_box = Rect::from_min_max(pos2(start_x, anchor.min.y), pos2(start_x, anchor.max.y));
let points: Vec<_> = [anchor_box.left_top(), anchor_box.right_top(), anchor_box.right_bottom(), anchor_box.left_bottom()]
.into_iter()
+75 -6
View File
@@ -32,22 +32,22 @@ fn anchor_skips_rects_scrolled_offscreen_and_clips_partial_ones() {
// Page 1's line is above the viewport, page 2's line is inside it.
let above = Rect::from_min_size(pos2(100.0, clip.top() - 300.0), Vec2::new(200.0, 20.0));
let inside = Rect::from_min_size(pos2(120.0, clip.top() + 50.0), Vec2::new(200.0, 20.0));
assert_eq!(visible_anchor([above, inside], clip), Some(inside));
assert_eq!(visible_anchor([above, inside], clip, false), Some(inside));
// Partly scrolled off the top: anchored to the visible part, never above the viewport.
let partial = Rect::from_min_size(pos2(100.0, clip.top() - 10.0), Vec2::new(200.0, 20.0));
let a = visible_anchor([partial], clip).unwrap();
let a = visible_anchor([partial], clip, false).unwrap();
assert!((a.min.y - clip.top()).abs() < 1e-3 && (a.height() - 10.0).abs() < 1e-3);
// Entirely off either end, or only touching the edge: nothing to anchor to.
let below = Rect::from_min_size(pos2(100.0, clip.bottom() + 5.0), Vec2::new(200.0, 20.0));
let touching = Rect::from_min_size(pos2(100.0, clip.top() - 20.0), Vec2::new(200.0, 20.0));
assert_eq!(visible_anchor([above, below, touching], clip), None);
assert_eq!(visible_anchor([], clip), None);
assert_eq!(visible_anchor([above, below, touching], clip, false), None);
assert_eq!(visible_anchor([], clip, false), None);
// Input order does not matter: the topmost visible rectangle wins.
let lower = Rect::from_min_size(pos2(100.0, clip.top() + 200.0), Vec2::new(200.0, 20.0));
assert_eq!(visible_anchor([lower, inside], clip), Some(inside));
assert_eq!(visible_anchor([lower, inside], clip, false), Some(inside));
// Degenerate input is rejected, not propagated.
let nan = Rect::from_min_size(pos2(f32::NAN, 0.0), Vec2::new(10.0, 10.0));
assert_eq!(visible_anchor([nan], clip), None);
assert_eq!(visible_anchor([nan], clip, false), None);
}
/// The whole window over a document with `pick` selected, run until the bar is up.
@@ -55,6 +55,11 @@ fn bar_over(pick: &str) -> H {
let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), Default::default());
a.run("text.insert", json!({"text": "The quick brown fox jumps over the lazy dog"})).unwrap();
a.run("select.text", json!({"text": pick})).unwrap();
bar_over_app(a)
}
/// `bar_over` for an app whose document and selection are already set up.
fn bar_over_app(a: WordApp) -> H {
let mut h = crate::test_harness::builder().with_size(Vec2::new(1440.0, 900.0)).build_ui_state(
|ui, app: &mut WordApp| {
let ctx = ui.ctx().clone();
@@ -682,3 +687,67 @@ fn a_clicked_member_holds_its_preview_until_the_pointer_leaves() {
}
}
}
/// The bar's centre, and the selection's first visible line (the anchor) it hangs from.
fn bar_centre_and_anchor(h: &H) -> (f32, Rect) {
let c = canvas(h);
let bar = h
.query_all_by_role(Role::Group)
.filter(|n| n.accesskit_node().label().as_deref() == Some("Formatting"))
.map(|n| n.rect())
.find(|r| c.contains_rect(*r))
.expect("the selection bar is shown inside the canvas");
(bar.center().x, h.state().canvas.mini_anchor.expect("the selection has an anchor"))
}
/// A paragraph long enough to wrap onto several lines, with a selection from `from_char` to its end.
fn wrapped_selection(text: &str, rtl: bool, from_char: usize) -> H {
let text = text.repeat(40);
let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::from_text(&text)), Default::default());
if rtl {
a.run("select.all", json!({})).unwrap();
a.run("para.rtl", json!({})).unwrap();
}
let off = text.char_indices().nth(from_char).map_or(0, |(i, _)| i);
let end = text.len();
a.session.sel = wordcraft_engine::Selection { anchor: wordcraft_doc::Pos::body(0, off), focus: wordcraft_doc::Pos::body(0, end) };
bar_over_app(a)
}
/// #7464: in a right-to-left paragraph the selection starts at the right, so the bar sits there,
/// not at the left margin where the selection's first line ends.
#[test]
fn the_bar_anchors_at_the_right_in_a_right_to_left_paragraph() {
let h = wrapped_selection("سلام دنیا ", true, 12);
let (x, anchor) = bar_centre_and_anchor(&h);
assert!(anchor.width() > 100.0, "the first selected line is a wide rect: {anchor:?}");
assert!((x - anchor.max.x).abs() < (x - anchor.min.x).abs(), "the bar x={x} is nearer the start (right) edge of {anchor:?}");
}
/// #7464: a left-to-right selection from mid-line 1 to a later line keeps the bar at its start.
#[test]
fn the_bar_anchors_at_the_start_of_a_multi_line_left_to_right_selection() {
let h = wrapped_selection("The quick brown fox ", false, 12);
let (x, anchor) = bar_centre_and_anchor(&h);
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 line1 = [run(200.0, 100.0), run(300.0, 100.0), run(250.0, 100.0)];
let later = run(900.0, 130.0);
let rects = [line1[0], line1[1], line1[2], later];
let a = visible_anchor(rects, clip, true).unwrap();
assert!(line1.iter().all(|r| a.max.x >= r.max.x && a.min.x <= r.min.x), "spans every run of line 1: {a:?}");
assert!(a.max.x < later.min.x && a.max.y <= later.min.y, "the later line is excluded: {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);
// Left to right keeps the first rectangle (vertical text stacks whole lines at one y-range).
let l = visible_anchor(rects, clip, false).unwrap();
assert!(l.min.x == line1[0].min.x && l.max.x < a.max.x, "LTR anchor is the top run only: {l:?}");
}