Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ command leaves the lists that name it, such as `ON_A_COPY`, the policy tables an
at nothing. A literal that isn't a command goes in its `EXEMPT` list with a reason (file and host names
ending in a `NOT_IDS` word need none), and an `EXEMPT` entry the source no longer
holds fails it too.
The tests `every_control_can_be_reached_*` (`crates/ui-egui/src/layout_reach.rs`, Linux) show each ribbon tab, Workflows section
and listed pane, dialog and main view with English labels 40% longer (labels that hold a placeholder, and text
that isn't passed through `tl!` or `tl_raw!`, keep their length), at 1280×720 and the smallest window,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence names only tl! and tl_raw! as the padded routes, but t_raw, fmt and fmt_raw also reach tr_raw, which pads since 95c4229 (t_raw calls tr_raw at i18n/mod.rs:436-437). A contributor reading it would take a t_raw or fmt label as untested at 40% longer. Suggested wording for this line, which completes the sentence that starts on line 196:

Suggested change
that isn't passed through `tl!` or `tl_raw!`, keep their length), at 1280×720 and the smallest window,
that isn't passed through `tl!`, `tl_raw!`, `t_raw` or `fmt`, keep their length), at 1280×720 and the smallest window,

Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left for the release quality pass.

and fail on a control that lies on another or that scrolling doesn't bring into view: let the row wrap, scroll or
shrink rather than listing it. `KNOWN` holds the problems there are; take lines out, don't add them. A new pane,
dialog or main view goes in the test's lists, with how to tell it is shown.
While you work, `cargo xtask changed` runs the same checks on only the packages your branch changes
and those that depend on them (and `test-hygiene` on the whole workspace, and formatting, `layers`,
`attribution` and `rows` when packages are checked; `attribution` also when only non-code files change), or everything when a
Expand Down
2 changes: 2 additions & 0 deletions crates/ui-egui/src/catalogsui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ impl Default for State {
}

fn fetch(url: &str) -> Answer {
#[cfg(all(test, target_os = "linux"))]
crate::layout_reach::downloaded();
let slot: Answer = Slot::default();
let land = slot.clone();
cache::download(url, Box::new(move |r| *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(r)));
Expand Down
19 changes: 19 additions & 0 deletions crates/ui-egui/src/i18n/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@ pub fn tr(lang: Lang, s: &str) -> &str {
return t;
}
if lang == Lang::EN {
#[cfg(all(test, target_os = "linux"))]
if pseudo::on() {
return pseudo::pad(s);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage gap, not a crash: the pseudo-English padding is applied only in tr(). tr_raw() (used by t_raw, tl_raw!, fmt/fmt_raw and fmt_context) never reaches it. The repo has about 233 tl_raw!/t_raw call sites against about 4,040 tl! sites, so those labels are drawn at their English length in the layout test. AGENTS.md's new line says "text that isn't drawn through tl!" keeps its length, which describes this, but the PR text says "each label 40% longer" and "a stand-in like this found 16 of the 19 surfaces". Either pad in tr_raw as well (the fmt templates with { are already skipped by pseudo::pad), or say in the PR text how much of the English UI the stand-in misses.

Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 95c4229: tr_raw pads too, so tl_raw!, t_raw and the fmt fragments are drawn longer. Templates holding a placeholder still keep their length. KNOWN didn't change.

}
return s;
}
plugin_tr(lang, s).unwrap_or(s)
Expand Down Expand Up @@ -417,6 +421,10 @@ pub fn tr_raw(lang: Lang, s: &str) -> &str {
return t;
}
if lang == Lang::EN {
#[cfg(all(test, target_os = "linux"))]
if pseudo::on() {
return pseudo::pad(s);
}
return s;
}
plugin_tr_raw(lang, s).unwrap_or(s)
Expand Down Expand Up @@ -745,5 +753,16 @@ macro_rules! tl_raw {

#[cfg(test)]
mod boundary_tests;
#[cfg(all(test, target_os = "linux"))]
pub(crate) mod pseudo;

/// Whether the layout tests' long English is on, on this thread; never outside them.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn pseudo_on() -> bool {
#[cfg(all(test, target_os = "linux"))]
return pseudo::on();
#[cfg(not(all(test, target_os = "linux")))]
false
}
#[cfg(test)]
mod tests;
88 changes: 88 additions & 0 deletions crates/ui-egui/src/i18n/pseudo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//! A stand-in for a long language: English with each line about 40% longer. Layout tests draw the
//! interface in it, so a label that only fits in English fails them without a translation or a
//! font of its own.

use std::cell::{Cell, RefCell};
use std::collections::HashMap;

thread_local! {
static ON: Cell<bool> = const { Cell::new(false) };
/// The padded text of each string drawn. Leaked, so a label can keep borrowing it.
static PADDED: RefCell<HashMap<String, &'static str>> = RefCell::default();
}

/// Whether this thread draws English padded.
pub(crate) fn on() -> bool {
ON.get()
}

/// Turns padding on or off for this thread.
pub(crate) fn set(on: bool) {
ON.set(on);
}

/// `s` padded: each line grows by 40% of its length. A line with a placeholder is left alone,
/// since what fills it isn't translated.
pub(crate) fn pad(s: &str) -> &'static str {
PADDED.with_borrow_mut(|seen| {
if let Some(done) = seen.get(s) {
return *done;
}
let line = |l: &str| match l.trim().is_empty() || l.contains('{') {
true => l.to_string(),
false => format!("{l}{}", "o".repeat((l.chars().count() * 2).div_ceil(5))),
};
let padded: &'static str = Box::leak(s.split('\n').map(line).collect::<Vec<_>>().join("\n").into_boxed_str());
seen.insert(s.to_string(), padded);
padded
})
}

/// `padded` with each padded string in it put back as it was: a whole label, or one built around
/// padded words, such as `▶ Run`.
pub(crate) fn unpadded(padded: &str) -> String {
// An accessible name joins a label's lines with a space.
let mut text = padded.replace('\n', " ");
PADDED.with_borrow(|seen| {
let mut pairs: Vec<(String, String)> =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unpadded rebuilds and sorts the whole table of padded strings on every call, cloning each entry. Control::name() calls it for every control in every report, and lie_on calls it for each overlapping pair, so the cost grows with the number of distinct strings the thread has drawn. About 6 s per test on the fork's CI is fine. A reverse map built once per surface, or a cache, would keep it flat as more strings get drawn.

Confidence: low; I didn't measure it.

Category: performance

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left for the release quality pass: it costs little at today's size.

seen.iter().filter(|(s, p)| s.as_str() != **p).map(|(s, p)| (p.replace('\n', " "), s.replace('\n', " "))).collect();
// The longest first, so a padded phrase isn't undone a word at a time.
pairs.sort_by_key(|(p, _)| std::cmp::Reverse(p.len()));
// A whole label first; else the padded words in it, where they aren't part of a longer word
// (so `Goo` isn't undone inside `Goodbye`).
if let Some((_, s)) = pairs.iter().find(|(p, _)| *p == text) {
text = s.clone();
return;
}
for (p, s) in pairs {
text = replace_words(&text, &p, &s);
}
});
text
}

/// `text` with each `from` that isn't next to a letter or digit replaced by `to`.
fn replace_words(text: &str, from: &str, to: &str) -> String {
let word = |c: Option<char>| c.is_some_and(char::is_alphanumeric);
let (mut out, mut last) = (String::new(), 0);
// The letters either side come from the whole text, so a match beside a kept one still sees them.
for (at, _) in text.match_indices(from) {
let (before, after) = (text[..at].chars().next_back(), text[at + from.len()..].chars().next());
out.push_str(&text[last..at]);
out.push_str(if word(before) || word(after) { from } else { to });
last = at + from.len();
}
out + &text[last..]
}
Comment on lines +65 to +76

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace_words loses the word context between matches. rest is re-sliced after each match, so for the next match before is read from an empty slice and comes back None, even when the previous match ends right in front of it. Example: replace_words("GooGoo", "Goo", "Go"). The first Goo is kept (followed by G). The second is then treated as a whole word and becomes Go, which gives GooGo. The second Goo is inside the run GooGoo, so it should stay. The case is narrow (padded words that touch with no separator), and in practice it only misnames a label in a problem line, but the function's own rule ("not next to a letter or digit") is broken here.

Matching over the whole text keeps the context. Suggested replacement, plus a regression assert for assert_eq!(super::replace_words("GooGoo", "Goo", "Go"), "GooGoo") in the test module:

Suggested change
fn replace_words(text: &str, from: &str, to: &str) -> String {
let word = |c: Option<char>| c.is_some_and(char::is_alphanumeric);
let (mut out, mut rest) = (String::new(), text);
while let Some(at) = rest.find(from) {
let (before, after) = (rest[..at].chars().next_back(), rest[at + from.len()..].chars().next());
out.push_str(&rest[..at]);
out.push_str(if word(before) || word(after) { from } else { to });
rest = &rest[at + from.len()..];
}
out + rest
}
fn replace_words(text: &str, from: &str, to: &str) -> String {
let word = |c: Option<char>| c.is_some_and(char::is_alphanumeric);
let (mut out, mut last) = (String::new(), 0);
// The letters either side come from the whole text, so a match beside a kept one still sees them.
for (at, _) in text.match_indices(from) {
let (before, after) = (text[..at].chars().next_back(), text[at + from.len()..].chars().next());
out.push_str(&text[last..at]);
out.push_str(if word(before) || word(after) { from } else { to });
last = at + from.len();
}
out + &text[last..]
}

Category: bug

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6377581, as suggested, with the GooGoo assert.


#[cfg(test)]
mod tests {
/// A padded word isn't undone inside a longer one.
#[test]
fn unpadding_keeps_to_whole_words() {
let (long, short) = (super::pad("Goodbye"), super::pad("Go"));
assert_eq!(super::unpadded(long), "Goodbye");
assert_eq!(super::unpadded(&format!("{short} {long}")), "Go Goodbye");
assert_eq!(super::replace_words("GooGoo", "Goo", "Go"), "GooGoo");
}
}
Loading
Loading