Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Smart album rules: + and − never cover a rule, and hovering a field doesn't wrap it

Reva Doni Aprilio116230eb21f0
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index bb290bd86..0bb8b787c 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -148,6 +148,8 @@ mod tests_recheck;
#[cfg(test)]
mod tests_rename_person;
#[cfg(test)]
+mod tests_rules_editor;
+#[cfg(test)]
mod tests_sam_setup;
#[cfg(test)]
mod tests_scroll;
diff --git a/crates/ui-egui/src/panels/rules_editor.rs b/crates/ui-egui/src/panels/rules_editor.rs
index 1ec211d51..d69a8fa43 100644
--- a/crates/ui-egui/src/panels/rules_editor.rs
+++ b/crates/ui-egui/src/panels/rules_editor.rs
@@ -247,10 +247,9 @@ fn field_menu(ui: &mut egui::Ui, salt: &str, field: &mut String, op: &mut String
}
ui.separator();
// submenu rows look like the items above them; the one holding the current field stands out
- // (a transparent frame, not none: a frameless button drops its padding and hover fill)
- let inactive = &mut ui.visuals_mut().widgets.inactive;
- inactive.weak_bg_fill = egui::Color32::TRANSPARENT;
- inactive.bg_stroke = egui::Stroke::NONE;
+ // (a transparent frame, not none: a frameless button drops its padding and hover fill; the
+ // list's style already drops the resting border)
+ ui.visuals_mut().widgets.inactive.weak_bg_fill = egui::Color32::TRANSPARENT;
for (group, ids) in FIELD_GROUPS {
let text = RichText::new(crate::i18n::tr(group));
let text = if ids.contains(&current.as_str()) { text.strong() } else { text };
@@ -276,6 +275,12 @@ fn field_menu(ui: &mut egui::Ui, salt: &str, field: &mut String, op: &mut String
*value = default_value(field, op);
}
+/// How wide `ui.small_button(text)` is: its text and the button's padding.
+fn small_button_width(ui: &egui::Ui, text: &str) -> f32 {
+ let galley = egui::WidgetText::from(text).into_galley(ui, Some(egui::TextWrapMode::Extend), f32::INFINITY, egui::TextStyle::Button);
+ galley.size().x + 2.0 * ui.spacing().button_padding.x
+}
+
/// A rule's problem, shown under it in the caution colour (`ruleProblem:<salt>` for tests and
/// agents).
fn problem_note(ui: &mut egui::Ui, problem: Option<&Problem>, salt: &str) {
@@ -329,22 +334,25 @@ pub fn edit(ui: &mut egui::Ui, rs: &mut RuleSet, salt: &str, depth: usize, path:
}
});
value_editor(ui, field, op, value, &rsalt, env);
- ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
- let add = ui.small_button("+").on_hover_text(crate::i18n::tr("Add a rule (⌥-click: a group)"));
- register(ui.ctx(), format!("button:ruleAdd-{rsalt}"), add.rect);
- if add.clicked() {
- let group = ui.input(|i| i.modifiers.alt) && depth < 3;
- insert = Some((
- i + 1,
- if group { Rule::Group { group: RuleSet { mode: Match::Any, rules: vec![new_rule()] } } } else { new_rule() },
- ));
- }
- let del = ui.small_button("−").on_hover_text(crate::i18n::tr("Remove this rule"));
- register(ui.ctx(), format!("button:ruleRemove-{rsalt}"), del.rect);
- if del.clicked() {
- remove = Some(i);
- }
- });
+ // − and + at the end of the row, after the value however long it is: a
+ // right-to-left layout put them over the value when the row had no room left
+ // (#2353), and the dialog widens to the row instead
+ let buttons = small_button_width(ui, "−") + ui.spacing().item_spacing.x + small_button_width(ui, "+");
+ ui.add_space((ui.available_width() - buttons).max(0.0));
+ let del = ui.small_button("−").on_hover_text(crate::i18n::tr("Remove this rule"));
+ register(ui.ctx(), format!("button:ruleRemove-{rsalt}"), del.rect);
+ if del.clicked() {
+ remove = Some(i);
+ }
+ let add = ui.small_button("+").on_hover_text(crate::i18n::tr("Add a rule (⌥-click: a group)"));
+ register(ui.ctx(), format!("button:ruleAdd-{rsalt}"), add.rect);
+ if add.clicked() {
+ let group = ui.input(|i| i.modifiers.alt) && depth < 3;
+ insert = Some((
+ i + 1,
+ if group { Rule::Group { group: RuleSet { mode: Match::Any, rules: vec![new_rule()] } } } else { new_rule() },
+ ));
+ }
});
problem_note(ui, problem, &rsalt);
}
diff --git a/crates/ui-egui/src/tests_rules_editor.rs b/crates/ui-egui/src/tests_rules_editor.rs
new file mode 100644
index 000000000..6ce8b7428
--- /dev/null
+++ b/crates/ui-egui/src/tests_rules_editor.rs
@@ -0,0 +1,110 @@
+//! The smart-album rule editor's layout: a rule's + and − never cover its value (#2353), so both
+//! calendars of a "between" can be clicked (#2352), and hovering an item of the field menu doesn't
+//! make it wrap (#2354).
+
+use std::time::Duration;
+
+use lightcraft_catalog::rules::{FIELD_GROUPS, TOP_LEVEL_FIELDS};
+use serde_json::json;
+
+use crate::headless::Headless;
+use crate::{LightcraftApp, Services};
+
+const T: Duration = Duration::from_secs(20);
+const SETTLE: Duration = Duration::from_secs(120);
+
+/// The rule editor holding `rules`, opened after a narrower dialog: the dialogs share one window,
+/// which keeps the width the last one left it at.
+fn editor(rules: serde_json::Value) -> Headless {
+ let app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), Services { png: None, ..Default::default() });
+ let mut h = Headless::new(app, [1200.0, 800.0], 1.0);
+ let r = h.request("engine.execute", json!({"command": "dialog.newSmartAlbum", "params": {}}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ h.settle(SETTLE);
+ h.app.ui.dialog = None;
+ h.settle(SETTLE);
+ let r = h.request("engine.execute", json!({"command": "dialog.smartAlbum", "params": {"name": "Dates"}}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ let Some(crate::state::Dialog::SmartRules { rules: r, .. }) = &mut h.app.ui.dialog else { panic!("no rule editor") };
+ *r = serde_json::from_value(json!({"rules": rules})).unwrap();
+ h.settle(SETTLE);
+ h
+}
+
+fn rect(h: &Headless, id: &str) -> egui::Rect {
+ h.app.widgets.iter().find(|(w, _)| w == id).map(|(_, r)| *r).unwrap_or_else(|| panic!("no widget {id}"))
+}
+
+fn has(h: &Headless, id: &str) -> bool {
+ h.app.widgets.iter().any(|(w, _)| w == id)
+}
+
+fn request(h: &mut Headless, method: &str, id: &str) {
+ let r = h.request(method, json!({"id": id}), T);
+ assert_eq!(r["ok"], true, "{method} {id}: {r}");
+ h.settle(SETTLE);
+}
+
+fn rule_count(h: &Headless) -> usize {
+ let Some(crate::state::Dialog::SmartRules { rules, .. }) = &h.app.ui.dialog else { panic!("no rule editor") };
+ rules.rules.len()
+}
+
+/// A "between" on a date is the longest rule: its + and − follow its second date and calendar
+/// instead of being drawn over them, the dialog widens to hold it, and every row's buttons stay
+/// lined up at the right.
+#[test]
+fn add_and_remove_never_cover_a_between_dates_rule() {
+ let h = editor(json!([
+ {"field": "captureDate", "op": "between", "value": ["2026-02-01", "2026-02-28"]},
+ {"field": "rating", "op": "gte", "value": 3},
+ ]));
+ let dialog = rect(&h, "dialog:window");
+ for b in ["button:ruleAdd-rules-0", "button:ruleRemove-rules-0"] {
+ let b_rect = rect(&h, b);
+ assert!(dialog.contains_rect(b_rect), "{b} {b_rect:?} is outside the dialog {dialog:?}");
+ for v in ["field:rules-0-a", "datePicker:rules-0-a", "field:rules-0-b", "datePicker:rules-0-b"] {
+ let v_rect = rect(&h, v);
+ assert!(dialog.contains_rect(v_rect), "{v} {v_rect:?} is outside the dialog {dialog:?}");
+ assert!(!b_rect.intersects(v_rect), "{b} {b_rect:?} covers {v} {v_rect:?}");
+ }
+ }
+ let (long, short) = (rect(&h, "button:ruleAdd-rules-0"), rect(&h, "button:ruleAdd-rules-1"));
+ assert!((long.right() - short.right()).abs() < 0.5, "the rows' + don't line up: {long:?} vs {short:?}");
+}
+
+/// The end date's calendar is there and opens when clicked; the click doesn't land on the + that
+/// used to cover it (and add a rule).
+#[test]
+fn the_end_dates_calendar_opens() {
+ let mut h = editor(json!([{"field": "captureDate", "op": "between", "value": ["2026-03", "2026-06"]}]));
+ request(&mut h, "ui.clickWidget", "datePicker:rules-0-b");
+ assert!(has(&h, "datePickerCell:2026-06:rules-0-b"), "the end date's calendar opened on its month");
+ assert_eq!(rule_count(&h), 1, "no rule was added");
+}
+
+/// Hovering any field in the field menu, top-level or in a group's submenu, keeps it the size it
+/// was: a hovered item used to need 2 points more, so the longest name of a submenu sized to it
+/// (Date ▸ Capture Date, Keywords & People ▸ Keyword Count) wrapped onto a second line and the
+/// menu jumped.
+#[test]
+fn hovering_a_field_keeps_its_size() {
+ let mut h = editor(json!([{"field": "rating", "op": "gte", "value": 3}]));
+ request(&mut h, "ui.clickWidget", "ruleField:rules-0");
+ let hover = |h: &mut Headless, id: &str| {
+ let item = format!("ruleFieldItem:{id}:rules-0");
+ let before = rect(h, &item);
+ request(h, "ui.hoverWidget", &item);
+ let after = rect(h, &item);
+ assert_eq!(after.size(), before.size(), "{id} changed size when hovered: {before:?} → {after:?}");
+ };
+ for id in TOP_LEVEL_FIELDS {
+ hover(&mut h, id);
+ }
+ for (group, ids) in FIELD_GROUPS {
+ request(&mut h, "ui.hoverWidget", &format!("ruleFieldGroup:{group}:rules-0"));
+ for id in *ids {
+ hover(&mut h, id);
+ }
+ }
+}
diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs
index a00bca4b2..c9110309f 100644
--- a/crates/ui-egui/src/widgets.rs
+++ b/crates/ui-egui/src/widgets.rs
@@ -10,9 +10,18 @@ use crate::theme::Tokens;
/// Standard dropdown chevrons, including settings, import/export and rule editors.
pub fn combo_box(id: impl egui::AsIdSalt) -> egui::ComboBox {
- egui::ComboBox::from_id_salt(id).icon(|ui, rect, visuals, _| {
- paint(ui.painter(), Rect::from_center_size(rect.center(), vec2(16.0, 16.0)), Icon::ChevronDown, visuals.fg_stroke.color);
- })
+ egui::ComboBox::from_id_salt(id)
+ .icon(|ui, rect, visuals, _| {
+ paint(ui.painter(), Rect::from_center_size(rect.center(), vec2(16.0, 16.0)), Icon::ChevronDown, visuals.fg_stroke.color);
+ })
+ .popup_style(list_style.into())
+}
+
+/// A dropdown's list and its submenus: items without the resting border of a button, as egui's
+/// own menus draw them. An unhovered item gives up its border's width of padding, so with one it
+/// grew 2 points when hovered, and the longest name of a list sized to it wrapped (#2354).
+fn list_style(style: &mut egui::Style) {
+ style.visuals.widgets.inactive.bg_stroke = Stroke::NONE;
}
/// A native button with a vector arrow beside its text; egui reserves the icon's space.