Skip to content

Commit 790f128

Browse files
nledfordclaude
andcommitted
LR-BEHAV-LOCALIZE: longer words keep to their place in Edit, the choices and the filter bar
Seen in German at 1600 x 1000: Edit's Reset lay over HDR; rows of choices (crop overlays, Upright, vignette styles) gave each choice an equal share whatever its name; the filter bar chose one row or two by a width that suits English and ran off the grid with both side panels open. - Edit: the head row wraps, and Reset takes a row of its own when it has no room beside the others (widgets::text_button_width). In English nothing moves. - widgets::segmented puts fewer choices to a row when the longest name would not fit its share, and cuts a name only where a row to itself is too narrow. - The filter bar remembers what its three parts measure and puts on each row what fits: one, two or three rows. Each part is measured from where it begins, so the bar comes to rest at every width. Tests: tests_type_fit.rs (in English, German, Russian and French: the four buttons of Edit's head do not touch, each choice's name is inside the choice, every control of the filter bar is inside the bar and a sweep of widths shows it comes to rest; Reset is on the row of the others in English and on a row of its own, at the right, in German; an Info value begins after its label, in Portuguese and Spanish too). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6b6bf8f commit 790f128

6 files changed

Lines changed: 318 additions & 18 deletions

File tree

‎crates/ui-egui/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ mod tests_tool_overlay;
212212
#[cfg(test)]
213213
mod tests_type_contrast;
214214
#[cfg(test)]
215+
mod tests_type_fit;
216+
#[cfg(test)]
215217
mod tests_type_on_photo;
216218
#[cfg(test)]
217219
mod tests_type_readout;

‎crates/ui-egui/src/panels/edit.rs‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId) {
108108
let (hr, _) = ui.allocate_exact_size(vec2(ui.available_width(), 40.0), Sense::hover());
109109
ui.painter().text(pos2(hr.left() + 24.0, hr.bottom() - 10.0), Align2::LEFT_CENTER, crate::i18n::tr("Edit"), t.semibold(15.0), t.text);
110110
egui::Frame::NONE.inner_margin(egui::Margin { left: 24, right: 22, top: 8, bottom: 14 }).show(ui, |ui| {
111-
ui.horizontal(|ui| {
112-
ui.spacing_mut().item_spacing.x = 8.0;
111+
ui.horizontal_wrapped(|ui| {
112+
ui.spacing_mut().item_spacing = vec2(8.0, 6.0);
113113
if text_button(ui, "auto", crate::i18n::tr("Auto"), false).clicked() {
114114
let _ = app.run("develop.auto", json!({}));
115115
app.toast(ui.ctx(), crate::i18n::tr("Auto settings applied"));
@@ -125,8 +125,15 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId) {
125125
{
126126
let _ = app.run("develop.hdr", json!({"enabled": !d.hdr.enabled}));
127127
}
128-
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
129-
if text_button(ui, "reset", crate::i18n::tr("Reset"), false)
128+
// (Reset at the right of the row when it has room there, else on a row of its own: in
129+
// German it is "Zurücksetzen" and lay over HDR)
130+
let reset = crate::i18n::tr("Reset");
131+
if ui.available_size_before_wrap().x < crate::widgets::text_button_width(ui, reset) {
132+
ui.end_row();
133+
}
134+
// (a layout of the width left on this row, 24 pt tall: after `end_row`, the new row)
135+
ui.allocate_ui_with_layout(vec2(ui.available_size_before_wrap().x, 24.0), egui::Layout::right_to_left(egui::Align::Center), |ui| {
136+
if text_button(ui, "reset", reset, false)
130137
.on_hover_text(reset_tip(&app.ui.settings.keymap, ui.ctx().os() == egui::os::OperatingSystem::Mac))
131138
.clicked()
132139
{

‎crates/ui-egui/src/panels/filterbar.rs‎

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use crate::widgets::register;
1414
pub const HEIGHT: f32 = 40.0;
1515
/// Bar width (points) below which the controls take two rows.
1616
const WIDE: f32 = 1240.0;
17+
/// Between two controls of a row.
18+
const GAP: f32 = 4.0;
1719

1820
/// Colour-label swatches (UI colours, our own choice).
1921
pub use crate::theme::label_color;
@@ -95,22 +97,43 @@ fn picker(app: &mut LightcraftApp, ui: &mut egui::Ui, id: &str, current: &str, a
9597

9698
pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
9799
let t = Tokens::get(ui.ctx());
98-
// one row when there is room, else the metadata pickers and actions go to a second row
99-
let two_rows = ui.available_width() < WIDE;
100-
let (bar, _) = ui.allocate_exact_size(vec2(ui.available_width(), if two_rows { 2.0 * HEIGHT - 8.0 } else { HEIGHT }), Sense::hover());
100+
// How wide the three parts of the bar were when last drawn (rating, flag and label; type and
101+
// edited; the metadata pickers and the buttons): their words are longer in some languages,
102+
// and what a row holds follows what fits, not a width that suits English.
103+
let widths_id = egui::Id::unique("filterbar:widths");
104+
let seen: [f32; 3] = ui.data(|d| d.get_temp(widths_id)).unwrap_or([0.0; 3]);
105+
let room = ui.available_width() - 32.0;
106+
// One row when there is room, else the metadata pickers and actions go to a second row. Type
107+
// and edited go down with them when they would run off the first, and stand on a row of
108+
// their own between the two when they fit on neither.
109+
// (Each part is measured from where it begins, so it measures the same on whichever row it
110+
// stands, and parts that share a row have a gap between them: what fits is then a matter of
111+
// the widths alone, and the bar comes to rest. Measured with the gap before it, a part was
112+
// four points wider when it followed another, and in that band the bar changed its mind
113+
// every frame.)
114+
let [first, second, third] = seen;
115+
let two_rows = ui.available_width() < WIDE || first + GAP + second + GAP + third > room;
116+
let (second_row, third_row) = match (two_rows, first + GAP + second <= room, second + GAP + third <= room) {
117+
(false, _, _) => (0, 0),
118+
(true, true, _) => (0, 1),
119+
(true, false, true) => (1, 1),
120+
(true, false, false) => (1, 2),
121+
};
122+
let rows = third_row as f32 + 1.0;
123+
let (bar, _) = ui.allocate_exact_size(vec2(ui.available_width(), rows * (HEIGHT - 8.0) + 8.0), Sense::hover());
101124
ui.painter().rect_filled(bar, 0.0, t.chrome);
102125
ui.painter().line_segment([bar.left_bottom(), bar.right_bottom()], Stroke::new(1.0, t.divider));
103126
register(ui, "filterbar", bar);
104127
let f = app.session.filter.clone();
105128
let row = |ui: &mut egui::Ui, i: f32| {
106129
let r = Rect::from_min_size(bar.min + vec2(16.0, 4.0 + i * (HEIGHT - 8.0)), vec2(bar.width() - 32.0, HEIGHT - 8.0));
107130
let mut c = ui.new_child(egui::UiBuilder::new().max_rect(r).layout(egui::Layout::left_to_right(egui::Align::Center)));
108-
c.spacing_mut().item_spacing.x = 4.0;
131+
c.spacing_mut().item_spacing.x = GAP;
109132
c
110133
};
111-
let mut row1 = row(ui, 0.0);
112-
let mut row2 = row(ui, 1.0);
113-
let ui = &mut row1;
134+
let mut rows = [row(ui, 0.0), row(ui, 1.0), row(ui, 2.0)];
135+
let [ui, ..] = &mut rows;
136+
let began = ui.cursor().left();
114137

115138
// rating: operator + stars
116139
caption(ui, "Rating");
@@ -180,8 +203,11 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
180203
}
181204
}
182205
ui.add_space(14.0);
206+
let first_w = ui.min_rect().right() - began;
183207

184208
// kind + edited
209+
let Some(ui) = rows.get_mut(second_row) else { return };
210+
let began = ui.cursor().left();
185211
let kind_label = match f.kind {
186212
None => "All types",
187213
Some(MediaKind::Image) => "Photos",
@@ -233,8 +259,11 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
233259
.collect(),
234260
);
235261

262+
let second_w = ui.min_rect().right() - began;
263+
236264
// metadata pickers
237-
let ui: &mut egui::Ui = if two_rows { &mut row2 } else { &mut row1 };
265+
let Some(ui) = rows.get_mut(third_row) else { return };
266+
let began = ui.cursor().left();
238267
let values = app.caches.filter_values(&app.session.catalog);
239268
let (cameras, lenses, keywords) = (values.cameras.clone(), values.lenses.clone(), values.keywords.clone());
240269
for (id, key, all_label, current, values) in [
@@ -266,6 +295,12 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
266295
// filter presets: apply one, save the current filter, delete
267296
let presets = crate::widgets::dropdown(ui, "filterPresets", crate::i18n::tr("Presets"), t.label(), t.text);
268297
register(ui, "button:filterPresets", presets.rect);
298+
let now = [first_w, second_w, ui.min_rect().right() - began];
299+
// (a number that is none would never equal itself: it is not kept, nor asked about again)
300+
if now.iter().all(|w| w.is_finite()) && now.iter().zip(seen).any(|(now, seen)| (now - seen).abs() > 0.5) {
301+
ui.data_mut(|d| d.insert_temp(widths_id, now));
302+
ui.ctx().request_repaint();
303+
}
269304
egui::Popup::menu(&presets).show(|ui| {
270305
let names: Vec<String> = app.session.filter_presets.iter().map(|f| f.name.clone()).collect();
271306
for n in &names {
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
//! Words that are longer in another language keep to their place: a button does not lie over
2+
//! its neighbour, a choice's name stays inside the choice, a row of the filter bar ends inside
3+
//! the bar, and a value begins after its label.
4+
//!
5+
//! Before, in German: Reset ("Zurücksetzen") lay over HDR in the head of the Edit panel, "Mit
6+
//! Hilfslinien" ran out of its third of the Upright choices, and the filter bar's first row ran
7+
//! off the grid at "Bearbeitet oder nicht". (The card of Info already gives a long label its
8+
//! room, "Velocidade do obturador" among them: its scenario keeps it so.)
9+
10+
use std::time::Duration;
11+
12+
use egui::Rect;
13+
use serde_json::{Value, json};
14+
15+
use crate::headless::Headless;
16+
use crate::{LightcraftApp, Services};
17+
18+
const T: Duration = Duration::from_secs(20);
19+
20+
fn demo(language: &str) -> Headless {
21+
let services = Services { png: None, write_shared: Some(std::sync::Arc::new(|_: &str, _: &[u8]| Ok(()))), ..Default::default() };
22+
let mut h = Headless::new(LightcraftApp::new(lightcraft_engine::Session::with_demo(), services), [1600.0, 1000.0], 1.0);
23+
// let the first renders finish (a render still running at exit can crash in GPU teardown)
24+
h.settle(T);
25+
run(&mut h, language, json!({}));
26+
run(&mut h, "library.select", json!({"ids": [1]}));
27+
h
28+
}
29+
30+
fn run(h: &mut Headless, command: &str, params: Value) {
31+
let r = h.request("engine.execute", json!({"command": command, "params": params}), T);
32+
assert_eq!(r["ok"], true, "{command}: {r}");
33+
rest(h);
34+
}
35+
36+
fn set(h: &mut Headless, what: Value) {
37+
let r = h.request("ui.set", what, T);
38+
assert_eq!(r["ok"], true, "ui.set: {r}");
39+
rest(h);
40+
}
41+
42+
fn rest(h: &mut Headless) {
43+
h.settle(T);
44+
h.request("ui.move", json!({"x": 2.0, "y": 2.0}), T);
45+
// (a layout that follows what it measured the frame before has come to rest)
46+
for _ in 0..5 {
47+
h.step();
48+
}
49+
}
50+
51+
fn rect(h: &Headless, id: &str) -> Rect {
52+
h.app.widgets.iter().find(|(w, _)| w == id).map(|(_, r)| *r).unwrap_or_else(|| panic!("no widget {id}"))
53+
}
54+
55+
fn widgets(h: &Headless, prefix: &str) -> Vec<(String, Rect)> {
56+
h.app.widgets.iter().filter(|(w, _)| w.starts_with(prefix)).cloned().collect()
57+
}
58+
59+
/// Every piece of text drawn, with where it is.
60+
fn texts(h: &Headless) -> Vec<(String, Rect)> {
61+
h.view
62+
.painted_shapes()
63+
.iter()
64+
.filter_map(|shape| match shape {
65+
egui::Shape::Text(text) => Some((text.galley.job.text.clone(), text.visual_bounding_rect())),
66+
_ => None,
67+
})
68+
.collect()
69+
}
70+
71+
#[test]
72+
fn the_buttons_at_the_head_of_edit_do_not_lie_over_one_another() {
73+
for language in ["app.language.english", "app.language.german", "app.language.russian", "app.language.french"] {
74+
// Given a photo being edited in a language
75+
let mut h = demo(language);
76+
run(&mut h, "view.detail", json!({}));
77+
set(&mut h, json!({"leftPanel": false, "right": "edit", "openSections": ["light"]}));
78+
// When the head of the Edit panel is drawn
79+
let buttons: Vec<(&str, Rect)> = ["auto", "bw", "hdr", "reset"].into_iter().map(|id| (id, rect(&h, &format!("button:{id}")))).collect();
80+
// Then no button touches another
81+
for (i, (a, ra)) in buttons.iter().enumerate() {
82+
for (b, rb) in buttons.iter().skip(i + 1) {
83+
assert!(!ra.expand(1.0).intersects(*rb), "{language}: {a} {ra:?} lies over {b} {rb:?}");
84+
}
85+
}
86+
}
87+
}
88+
89+
/// Reset used to be drawn in a right-to-left layout of the head's single row, and lay over HDR in
90+
/// German. Now the head wraps: Reset goes on the row of the others where it has room (English),
91+
/// on a row of its own where it has not (German), and at the right of the head either way.
92+
#[test]
93+
fn reset_is_on_the_row_of_the_others_when_it_has_room_and_below_them_when_it_has_not() {
94+
for (language, own_row) in [("app.language.english", false), ("app.language.german", true)] {
95+
// Given a photo being edited in a language
96+
let mut h = demo(language);
97+
run(&mut h, "view.detail", json!({}));
98+
set(&mut h, json!({"leftPanel": false, "right": "edit", "openSections": ["light"]}));
99+
// When the head of the Edit panel is drawn
100+
let hdr = rect(&h, "button:hdr");
101+
let reset = rect(&h, "button:reset");
102+
// Then Reset is below the others only where it has no room beside them
103+
if own_row {
104+
assert!(reset.top() >= hdr.bottom(), "{language}: reset {reset:?} is not below hdr {hdr:?}");
105+
} else {
106+
assert!((reset.center().y - hdr.center().y).abs() < 4.0, "{language}: reset {reset:?} is not on the row of hdr {hdr:?}");
107+
assert!(reset.left() >= hdr.right(), "{language}: reset {reset:?} lies over hdr {hdr:?}");
108+
}
109+
// And at the right of the head either way
110+
assert!(reset.right() > hdr.right(), "{language}: reset {reset:?} is not at the right of hdr {hdr:?}");
111+
}
112+
}
113+
114+
#[test]
115+
fn the_name_of_a_choice_stays_inside_the_choice() {
116+
for language in ["app.language.english", "app.language.german", "app.language.russian", "app.language.french", "app.language.spanish"] {
117+
// Given the Crop panel in a language, with its rows of choices
118+
let mut h = demo(language);
119+
run(&mut h, "view.detail", json!({}));
120+
set(&mut h, json!({"leftPanel": false, "right": "crop"}));
121+
let drawn = texts(&h);
122+
let mut choices = widgets(&h, "button:cropOverlay-");
123+
choices.extend(widgets(&h, "button:upright-"));
124+
assert!(choices.len() >= 10, "{language}: the overlay and upright choices are on screen ({})", choices.len());
125+
// Then each choice's name lies inside it, with a little room at each side
126+
for (id, choice) in choices {
127+
let name = drawn.iter().find(|(_, at)| choice.contains(at.center())).unwrap_or_else(|| panic!("{language}: {id} has no name"));
128+
assert!(
129+
choice.shrink2(egui::vec2(2.0, 0.0)).contains_rect(name.1),
130+
"{language}: {:?} at {:?} runs out of {id} {choice:?}",
131+
name.0,
132+
name.1
133+
);
134+
}
135+
}
136+
}
137+
138+
#[test]
139+
fn a_row_of_the_filter_bar_ends_inside_the_bar() {
140+
for language in ["app.language.english", "app.language.german", "app.language.russian", "app.language.french"] {
141+
// Given the grid with both side panels and the filter bar, in a language
142+
let mut h = demo(language);
143+
run(&mut h, "view.photoGrid", json!({}));
144+
set(&mut h, json!({"leftPanel": true, "right": "info", "thumbSize": 200, "filterBar": true}));
145+
let bar = rect(&h, "filterbar");
146+
// When its controls are drawn
147+
let mut controls = widgets(&h, "dropdown:filter-");
148+
controls.extend(["button:filterSave", "button:filterClear", "button:filterPresets"].into_iter().map(|id| (id.to_string(), rect(&h, id))));
149+
assert!(controls.len() >= 8, "{language}: {}", controls.len());
150+
// Then every one of them lies in the bar
151+
for (id, control) in controls {
152+
assert!(bar.contains_rect(control), "{language}: {id} {control:?} runs out of the bar {bar:?}");
153+
}
154+
}
155+
}
156+
157+
#[test]
158+
fn a_value_on_the_card_of_info_begins_after_its_label() {
159+
for language in ["app.language.english", "app.language.german", "app.language.portuguese", "app.language.spanish", "app.language.french"] {
160+
// Given the Info panel of a photo in a language
161+
let mut h = demo(language);
162+
run(&mut h, "view.photoGrid", json!({}));
163+
set(&mut h, json!({"leftPanel": true, "right": "info", "thumbSize": 200}));
164+
let drawn = texts(&h);
165+
let meta = h.app.session.catalog.photo(lightcraft_catalog::PhotoId(1)).map(|p| p.meta.clone()).expect("photo 1");
166+
let mut begins = Vec::new();
167+
for (label, value) in [
168+
("Focal length", meta.focal_mm.and_then(crate::readout::focal)),
169+
("Shutter speed", crate::readout::shutter(&meta.shutter)),
170+
("Aperture", meta.aperture.and_then(crate::readout::aperture)),
171+
] {
172+
let (label, value) = (crate::i18n::tr(label).to_string(), value.expect("the demo photo has it"));
173+
let at = |said: &str| {
174+
drawn.iter().find(|(text, _)| text == said).map(|(_, at)| *at).unwrap_or_else(|| panic!("{language}: {said:?} is not drawn"))
175+
};
176+
let (label_at, value_at) = (at(&label), at(&value));
177+
// Then the value begins at least a gap after its label ends
178+
assert!(value_at.left() >= label_at.right() + 8.0, "{language}: {label:?} {label_at:?} and {value:?} {value_at:?}");
179+
begins.push(value_at.left());
180+
}
181+
// And the values stand under one another
182+
assert!(begins.iter().all(|x| (x - begins[0]).abs() < 1.0), "{language}: {begins:?}");
183+
}
184+
}
185+
186+
#[test]
187+
fn the_filter_bar_comes_to_rest_at_every_width() {
188+
for language in ["app.language.english", "app.language.german", "app.language.french"] {
189+
// Given the grid and the filter bar in a language, and what the bar's three parts measure
190+
let mut h = demo(language);
191+
run(&mut h, "view.photoGrid", json!({}));
192+
set(&mut h, json!({"leftPanel": true, "right": "info", "thumbSize": 200, "filterBar": true}));
193+
let parts: [f32; 3] = h.view.ctx.data(|d| d.get_temp(egui::Id::unique("filterbar:widths"))).expect("the bar has measured its parts");
194+
assert!(parts.iter().all(|w| *w > 40.0), "{language}: {parts:?}");
195+
let gap = 4.0;
196+
// When the window is as wide as leaves the bar a point or two more or less than each of
197+
// the widths at which what a row holds changes
198+
for edge in [parts[0] + parts[1] + parts[2] + 2.0 * gap, parts[0] + parts[1] + gap, parts[1] + parts[2] + gap] {
199+
for tenth in -40..=40 {
200+
let room = edge + tenth as f32 / 10.0;
201+
h.size.x += room - (rect(&h, "filterbar").width() - 32.0);
202+
let mut heights = Vec::new();
203+
for _ in 0..12 {
204+
h.step();
205+
heights.push(rect(&h, "filterbar").height());
206+
}
207+
assert!(((rect(&h, "filterbar").width() - 32.0) - room).abs() < 0.05, "{language}: the bar has the room asked for");
208+
// Then within a few frames the bar is one height and stays it: it used to change
209+
// every frame in a band four points wide, and the grid under it jumped
210+
let rest: Vec<f32> = heights.iter().skip(6).copied().collect();
211+
assert!(rest.iter().all(|height| *height == rest[0]), "{language}: at {room} pt of room the bar is {heights:?} high frame by frame");
212+
// And nothing in it runs out of it
213+
let bar = rect(&h, "filterbar");
214+
for (id, control) in widgets(&h, "dropdown:filter-") {
215+
assert!(control.right() <= bar.right() + 0.5, "{language}: at {room} pt {id} {control:?} runs out of {bar:?}");
216+
}
217+
}
218+
}
219+
}
220+
}

0 commit comments

Comments
 (0)