mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-22 22:06:26 -06:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aeb65b3c28 | ||
|
|
e0b201102f |
@@ -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, crate::mini_toolbar::starts_rtl(app));
|
||||
app.canvas.mini_anchor = crate::mini_toolbar::visible_anchor(anchor_rects, body.view.rect);
|
||||
// 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);
|
||||
}
|
||||
|
||||
@@ -14,12 +14,14 @@ use craft::behavior::IconId;
|
||||
use craft::theme::Variant;
|
||||
use craft::ui::buttons::Button;
|
||||
use craft::ui::choice::Checkbox;
|
||||
use craft::ui::contract::{Bind, Props as _, Show as _, ShowContainer as _, clicked};
|
||||
use craft::ui::contract::{Bind, Pad, Props as _, Show as _, ShowContainer as _, clicked};
|
||||
use craft::ui::dialog::{Dialog, DialogId, DialogResult, DialogStack, StackDialog, dialog_id};
|
||||
use craft::ui::display::{Badge, Icon, Shade, Text};
|
||||
use craft::ui::layout_components::Row as Line;
|
||||
use craft::ui::layout::RowAlign;
|
||||
use craft::ui::layout_components::{Row as Line, Stack};
|
||||
use craft::ui::primitives::IconSpec;
|
||||
use craft::ui::sections::{DialogRole, FooterButton, GroupBox};
|
||||
use craft::ui::sections::{DialogRole, FooterButton};
|
||||
use craft::ui::style::{SurfaceKind, TypeRole};
|
||||
use egui::Ui;
|
||||
use serde::Serialize;
|
||||
|
||||
@@ -272,15 +274,21 @@ impl StackDialog for FontBox {
|
||||
.shade(Shade::Faint)
|
||||
.show(ui);
|
||||
for (at, g) in groups(fonts).into_iter().enumerate() {
|
||||
// The names are the document's own words, not interface text.
|
||||
GroupBox::new(g.fonts.join(", "), None).id_salt(("download", at)).show(ui, |ui, _| {
|
||||
let shown_as =
|
||||
crate::i18n::fmt(tl!("Shown as {fonts} · {publisher}"), &[("fonts", &g.shown_as.join(", ")), ("publisher", g.publisher)]);
|
||||
Text::new(shown_as).shade(Shade::Faint).show(ui);
|
||||
if g.restricted {
|
||||
Text::new(tl!("Its licence limits who may use it: read the terms first.")).shade(Shade::Faint).show(ui);
|
||||
}
|
||||
Line::new().show(ui, |ui, _| {
|
||||
// One row per download, as before the craft port: the names, what they are shown as and the licence
|
||||
// note stacked at the start edge, the button at the end. The names are the document's own words.
|
||||
Stack::new().surface(SurfaceKind::Frame).pad(Pad::Regular).fill_width().id_salt(("download_group", at)).show(ui, |ui, _| {
|
||||
Line::new().fill_width().align(RowAlign::SpaceBetween).id_salt(("download", at)).show(ui, |ui, _| {
|
||||
Stack::new().show(ui, |ui, _| {
|
||||
Text::new(g.fonts.join(", ")).role(TypeRole::BodyStrong).show(ui);
|
||||
let shown_as = crate::i18n::fmt(
|
||||
tl!("Shown as {fonts} · {publisher}"),
|
||||
&[("fonts", &g.shown_as.join(", ")), ("publisher", g.publisher)],
|
||||
);
|
||||
Text::new(shown_as).shade(Shade::Faint).show(ui);
|
||||
if g.restricted {
|
||||
Text::new(tl!("Its licence limits who may use it: read the terms first.")).shade(Shade::Faint).show(ui);
|
||||
}
|
||||
});
|
||||
if clicked(&Button::new(tl!("Download")).variant(Variant::PRIMARY).id_salt(("download_button", at)).show(ui)) {
|
||||
asks.push(Ask::Download(g.url.to_string()));
|
||||
}
|
||||
|
||||
@@ -233,3 +233,33 @@ fn only_so_many_documents_are_remembered() {
|
||||
assert_eq!(a.font_notice_seen.len(), REMEMBERED);
|
||||
assert_eq!(a.font_notice_seen.last(), Some(&(REMEMBERED as u64 + 49)));
|
||||
}
|
||||
|
||||
/// Each missing font's names, substitute and Download button sit in one compact row, not one under another, and a
|
||||
/// narrow window keeps them inside the box.
|
||||
#[test]
|
||||
fn each_download_is_one_row() {
|
||||
let row = |font: &str, shown: &str, url: &str| Row {
|
||||
font: font.into(),
|
||||
shown_as: vec![shown.into()],
|
||||
download: font.into(),
|
||||
publisher: "Microsoft".into(),
|
||||
url: url.into(),
|
||||
restricted: false,
|
||||
};
|
||||
for width in [1440.0, 520.0] {
|
||||
let mut a = WordApp::new(Session::new(wordcraft_doc::Document::new()), crate::Services::default());
|
||||
let fonts = vec![row("Aptos", "Carlito", "https://a"), row("Cascadia Code", "Liberation Mono", "https://c")];
|
||||
open_with(&mut a, None, FontBox::new(fonts, false), DialogHooks { prepare: None, settle });
|
||||
let h = harness_sized(a, egui::vec2(width, 900.0));
|
||||
let buttons: Vec<_> = h.query_all_by_role_and_label(egui::accesskit::Role::Button, "Download").collect();
|
||||
assert_eq!(buttons.len(), 2);
|
||||
for (button, shown) in buttons.iter().zip(["Shown as Carlito · Microsoft", "Shown as Liberation Mono · Microsoft"]) {
|
||||
let shown = h.get_by_label(shown).rect();
|
||||
let button = button.rect();
|
||||
// The button is beside the text block, level with it, not under it.
|
||||
assert!(button.min.x >= shown.max.x, "{width}: {button:?} {shown:?}");
|
||||
assert!(button.center().y < shown.max.y, "{width}: button under the text: {button:?} {shown:?}");
|
||||
assert!(button.max.x <= width, "{width}: clipped {button:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,22 +18,20 @@ use serde_json::{Value, json};
|
||||
use crate::WordApp;
|
||||
use crate::colors::{Remember, Target};
|
||||
|
||||
/// Distance from the selection's start (its left edge, or right in a right-to-left paragraph) at which the bar is centred.
|
||||
/// Distance from the selection's start 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 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> {
|
||||
/// 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> {
|
||||
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;
|
||||
@@ -46,26 +44,8 @@ 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);
|
||||
}
|
||||
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)
|
||||
first
|
||||
}
|
||||
|
||||
/// Whether the bar may be shown for the current app state.
|
||||
@@ -121,9 +101,8 @@ 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);
|
||||
// a right-to-left paragraph at the start puts it at the right edge (#7464).
|
||||
let start_x = start_x(anchor, starts_rtl(app));
|
||||
// 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;
|
||||
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()
|
||||
|
||||
@@ -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, false), Some(inside));
|
||||
assert_eq!(visible_anchor([above, inside], clip), 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, false).unwrap();
|
||||
let a = visible_anchor([partial], clip).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, false), None);
|
||||
assert_eq!(visible_anchor([], clip, false), None);
|
||||
assert_eq!(visible_anchor([above, below, touching], clip), None);
|
||||
assert_eq!(visible_anchor([], clip), 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, false), Some(inside));
|
||||
assert_eq!(visible_anchor([lower, inside], clip), 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, false), None);
|
||||
assert_eq!(visible_anchor([nan], clip), None);
|
||||
}
|
||||
|
||||
/// The whole window over a document with `pick` selected, run until the bar is up.
|
||||
@@ -55,11 +55,6 @@ 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();
|
||||
@@ -687,67 +682,3 @@ 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:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user