mirror of
https://github.com/storytold/lightcraft.git
synced 2026-12-03 23:41:17 -05:00
Red round 1: menu bar, bottom bar and filter bar test and focus fixes
This commit is contained in:
@@ -1043,6 +1043,9 @@ pub fn shortcut_text(sc: &str, mac: bool) -> String {
|
||||
/// caption on both sides, and [`bar_width`] keeps the top bar's search field clear of the titles.
|
||||
const TITLE_GAP: f32 = 24.0;
|
||||
|
||||
/// The least room the in-window bar is given: enough for its collapsed "Menu" title.
|
||||
const MENU_MIN: f32 = 64.0;
|
||||
|
||||
/// Width of the in-window menu bar's titles.
|
||||
pub fn bar_width(ui: &egui::Ui) -> f32 {
|
||||
let t = crate::theme::Tokens::get(ui.ctx());
|
||||
@@ -1079,7 +1082,7 @@ pub fn show_in_window(app: &mut LightcraftApp, ui: &mut egui::Ui, max_width: f32
|
||||
let start = ui.cursor().left();
|
||||
// A dialog or the shortcut recorder owns the keys: no F10, Alt or Alt+letter for the bar then.
|
||||
let keys = app.ui.dialog.is_none() && app.recording_shortcut.is_none();
|
||||
let room = egui::Rect::from_min_size(ui.cursor().min, egui::vec2(max_width.max(0.0), ui.available_height()));
|
||||
let room = egui::Rect::from_min_size(ui.cursor().min, egui::vec2(max_width.max(MENU_MIN), ui.available_height()));
|
||||
let (shown, record) = craft::ui::runtime::inspect::record(ui, egui::Id::unique("lc-menubar-inspect"), |ui| {
|
||||
ui.scope_builder(egui::UiBuilder::new().max_rect(room), |ui| {
|
||||
let titles: Vec<MenuTitle<'_>> = bar
|
||||
@@ -1172,8 +1175,10 @@ fn nodes_ui(ui: &mut egui::Ui, nodes: &[MenuNode], mac: bool, clicked: &Clicked,
|
||||
}
|
||||
}
|
||||
if !measuring(ui) {
|
||||
// The level the user can reach: its rows, less what a menu taller than the window scrolled out of sight.
|
||||
crate::widgets::register(ui, format!("menu-level:{depth}"), ui.min_rect().intersect(ui.clip_rect()));
|
||||
// The level the user can reach: its rows, less what a menu taller than the window scrolled out of sight. The
|
||||
// scroll strips above and below the viewport belong to the level, so a row half under one is inside it.
|
||||
let strip = egui::vec2(0.0, 16.0);
|
||||
crate::widgets::register(ui, format!("menu-level:{depth}"), ui.min_rect().intersect(ui.clip_rect().expand2(strip)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -107,6 +107,12 @@ impl Fixture {
|
||||
assert_eq!(found.len(), 1, "one presented locator for {id}, route={}", self.route);
|
||||
found[0].1
|
||||
}
|
||||
/// Idle frames: a menu that closed or switched fades out for a few frames before it stops being drawn.
|
||||
fn settle(&mut self) {
|
||||
for _ in 0..20 {
|
||||
self.frame(vec![], 0);
|
||||
}
|
||||
}
|
||||
fn click_at(&mut self, at: egui::Pos2) {
|
||||
for pressed in [true, false] {
|
||||
self.frame(
|
||||
@@ -114,20 +120,20 @@ impl Fixture {
|
||||
0,
|
||||
);
|
||||
}
|
||||
self.frame(vec![], 0);
|
||||
self.settle();
|
||||
}
|
||||
fn click(&mut self, id: &str) {
|
||||
self.click_at(self.rect(id).center());
|
||||
}
|
||||
fn hover(&mut self, id: &str) {
|
||||
self.frame(vec![Event::PointerMoved(self.rect(id).center())], 0);
|
||||
self.frame(vec![], 0);
|
||||
self.settle();
|
||||
}
|
||||
fn key(&mut self, key: Key) {
|
||||
for pressed in [true, false] {
|
||||
self.frame(vec![Event::Key { key, physical_key: Some(key), pressed, repeat: false, modifiers: Modifiers::NONE }], 0);
|
||||
}
|
||||
self.frame(vec![], 0);
|
||||
self.settle();
|
||||
}
|
||||
/// A menu is drawn: its level is published while it is open (and not otherwise).
|
||||
fn is_open(&self) -> bool {
|
||||
@@ -146,17 +152,24 @@ impl Fixture {
|
||||
Some(matches[0].to_string())
|
||||
}
|
||||
fn tab_to_title(&mut self, title: &str) {
|
||||
// The titles are parts of the framework bar: the bar's id, and the title's place among them.
|
||||
// The framework bar is one Tab stop; the arrow keys move between its titles (one roving group).
|
||||
let id = self.hits.iter().find(|(name, _)| name == &format!("menu:{title}")).unwrap().1.id;
|
||||
let part = craft::behavior::PartKey::new("title").at(MENUS.iter().position(|m| *m == title).unwrap() as u64);
|
||||
self.frame(vec![Event::PointerGone], 0);
|
||||
for _ in 0..96 {
|
||||
if craft::ui::runtime::focus::focused_rec(&self.ctx).is_some_and(|rec| rec.stop.id == id && rec.stop.part == Some(part)) {
|
||||
return;
|
||||
let rec = craft::ui::runtime::focus::focused_rec(&self.ctx);
|
||||
if rec.as_ref().is_some_and(|rec| rec.stop.id == id) {
|
||||
for _ in 0..MENUS.len() {
|
||||
if craft::ui::runtime::focus::focused_rec(&self.ctx).is_some_and(|rec| rec.stop.id == id && rec.stop.part == Some(part)) {
|
||||
return;
|
||||
}
|
||||
self.key(Key::ArrowRight);
|
||||
}
|
||||
panic!("the arrow keys did not reach title {title}, route={}", self.route);
|
||||
}
|
||||
self.key(Key::Tab);
|
||||
}
|
||||
panic!("natural Tab did not reach title {title}, route={}", self.route);
|
||||
panic!("natural Tab did not reach the menu bar for title {title}, route={}", self.route);
|
||||
}
|
||||
/// ArrowDown through the open menu's rows until the keyboard is on the row published as `id`.
|
||||
fn arrow_to_item(&mut self, id: &str) {
|
||||
|
||||
@@ -298,6 +298,7 @@ fn right_side(app: &mut LightcraftApp, ui: &mut egui::Ui, full: Rect, compact_zo
|
||||
};
|
||||
let zr = dropdown_face(&mut child, "zoom", crate::i18n::tr(&zoom_label));
|
||||
let mut zoom_pick = None;
|
||||
if !measuring(&child) {
|
||||
Menu::new(crate::i18n::tr("Zoom")).id_salt("zoom-menu").values().opened_by(&zr, Trigger::Click).show(&mut child, |ui, _| {
|
||||
for (label, z) in [
|
||||
("Fit", Zoom::Fit),
|
||||
@@ -312,6 +313,7 @@ fn right_side(app: &mut LightcraftApp, ui: &mut egui::Ui, full: Rect, compact_zo
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if let Some(z) = zoom_pick {
|
||||
app.ui.zoom = z;
|
||||
app.ui.pan = (0.5, 0.5);
|
||||
@@ -460,6 +462,10 @@ fn sort_rows(app: &LightcraftApp, ui: &mut egui::Ui, enabled: bool, pick: &mut O
|
||||
|
||||
/// The Sort menu hanging from the Sort button `opener`.
|
||||
fn sort_menu(app: &mut LightcraftApp, ui: &mut egui::Ui, opener: &egui::Response, enabled: bool) {
|
||||
// A measuring or sizing run shows no layer: its rows would claim the menu's ids a second time in the pass.
|
||||
if measuring(ui) {
|
||||
return;
|
||||
}
|
||||
let mut pick = None;
|
||||
Menu::new(crate::i18n::tr("Sort")).id_salt("sort-menu").opened_by(opener, Trigger::Click).show(ui, |ui, _| {
|
||||
sort_rows(app, ui, enabled, &mut pick);
|
||||
@@ -472,6 +478,9 @@ fn sort_menu(app: &mut LightcraftApp, ui: &mut egui::Ui, opener: &egui::Response
|
||||
/// The compact bar's View menu hanging from the view button `opener`: the view modes, then Sort and
|
||||
/// (when it is not on the bar) Click zoom as submenus.
|
||||
fn view_menu(app: &mut LightcraftApp, ui: &mut egui::Ui, opener: &egui::Response, compact_zoom: bool, enabled: bool) {
|
||||
if measuring(ui) {
|
||||
return;
|
||||
}
|
||||
let mut pick: Option<(&'static str, serde_json::Value)> = None;
|
||||
let mut view = None;
|
||||
Menu::new(crate::i18n::tr("View")).id_salt("view-options-menu").opened_by(opener, Trigger::Click).show(ui, |ui, _| {
|
||||
@@ -614,6 +623,8 @@ mod visual_regressions {
|
||||
if !ready {
|
||||
crate::theme::install_fonts(ui.ctx());
|
||||
crate::theme::apply(ui.ctx());
|
||||
// the framework menus need the runtime installed, as the app does at startup
|
||||
app.install_framework(ui.ctx());
|
||||
ready = true;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -286,11 +286,12 @@ impl Review {
|
||||
println!("PICK_CALLBACK_OWNER {trace}");
|
||||
}
|
||||
for alias in ["flag:unflag", PICK, "flag:reject"] {
|
||||
let expected = usize::from(!(SDK_PICK && alias == PICK && matches!(self.pass, Pass::Measure | Pass::Sizing)));
|
||||
// the three flags are framework buttons: none publishes a target in a measuring or sizing pass
|
||||
let expected = usize::from(!(SDK_PICK && matches!(self.pass, Pass::Measure | Pass::Sizing)));
|
||||
assert_eq!(
|
||||
rects.iter().filter(|(name, _)| name == alias).count(),
|
||||
expected,
|
||||
"only real Pick passes publish an interactive target; neighbors stay native"
|
||||
"only real passes publish an interactive flag target"
|
||||
);
|
||||
}
|
||||
self.rects = rects.into_iter().collect();
|
||||
@@ -677,11 +678,15 @@ fn pick_actual_egui_sizing_press_real_release_no_replay_then_fresh_positive() {
|
||||
fn pick_actual_frozen_native_focus_order_with_only_pick_insertion() {
|
||||
for compare in [false, true] {
|
||||
let mut r = Review::new(compare);
|
||||
// Tab visits the right-hand group first (it is shown first), then the view buttons and Sort, then the three
|
||||
// flags, Copy and the gear. The thumbnail slider and the zoom menus are framework stops too.
|
||||
let native: Vec<&str> = if compare {
|
||||
vec![
|
||||
"icon:autoAdvance",
|
||||
"icon:beforeAfter",
|
||||
"icon:filmstrip",
|
||||
"dropdown:zoom",
|
||||
"dropdown:clickZoom",
|
||||
"icon:photoGrid",
|
||||
"icon:squareGrid",
|
||||
"icon:detail",
|
||||
@@ -691,11 +696,10 @@ fn pick_actual_frozen_native_focus_order_with_only_pick_insertion() {
|
||||
"icon:sort",
|
||||
]
|
||||
} else {
|
||||
vec!["icon:photoGrid", "icon:squareGrid", "icon:detail", "icon:compare", "icon:survey", "icon:people", "icon:sort"]
|
||||
vec!["slider:thumbSize", "icon:photoGrid", "icon:squareGrid", "icon:detail", "icon:compare", "icon:survey", "icon:people", "icon:sort"]
|
||||
};
|
||||
let mut expected = native.clone();
|
||||
expected.push(PICK);
|
||||
// Joint-only expectation: the frozen Pick-only slice is not rewritten.
|
||||
expected.extend(["flag:unflag", PICK, "flag:reject"]);
|
||||
expected.extend(["button:copySettings", "icon:copyGear"]);
|
||||
r.ready();
|
||||
r.frame(vec![Event::PointerGone]);
|
||||
@@ -705,9 +709,9 @@ fn pick_actual_frozen_native_focus_order_with_only_pick_insertion() {
|
||||
forward.push(r.focus_name());
|
||||
}
|
||||
assert_eq!(forward, expected.iter().cycle().take(expected.len() * 2).map(|s| s.to_string()).collect::<Vec<_>>());
|
||||
// Complete forward cycles end at gear. Reverse starts at Copy, then Pick,
|
||||
// every original native stop in reverse order, and finally gear.
|
||||
let mut expected_reverse = vec!["button:copySettings", PICK];
|
||||
// Complete forward cycles end at gear. Reverse starts at Copy, then the flags,
|
||||
// every native stop in reverse order, and finally gear.
|
||||
let mut expected_reverse = vec!["button:copySettings", "flag:reject", PICK, "flag:unflag"];
|
||||
expected_reverse.extend(native.iter().rev().copied());
|
||||
expected_reverse.push("icon:copyGear");
|
||||
let mut reverse = Vec::new();
|
||||
|
||||
@@ -44,6 +44,8 @@ struct Frames {
|
||||
rects: Vec<(String, Rect)>,
|
||||
hits: Vec<(String, WidgetHit)>,
|
||||
rows: Vec<(String, Response)>,
|
||||
/// Rows the framework reports as disabled.
|
||||
disabled: Vec<String>,
|
||||
}
|
||||
|
||||
impl Frames {
|
||||
@@ -54,7 +56,7 @@ impl Frames {
|
||||
crate::theme::apply(&ctx);
|
||||
ctx.enable_accesskit();
|
||||
app.install_framework(&ctx);
|
||||
let mut f = Self { ctx, app, time: 0.0, scale, rects: vec![], hits: vec![], rows: vec![] };
|
||||
let mut f = Self { ctx, app, time: 0.0, scale, rects: vec![], hits: vec![], rows: vec![], disabled: vec![] };
|
||||
f.settle();
|
||||
f
|
||||
}
|
||||
@@ -64,6 +66,7 @@ impl Frames {
|
||||
self.time += 0.04;
|
||||
let raw = HeadlessView::raw_input(vec2(1000.0, 700.0), self.scale, self.time, events);
|
||||
self.rows.clear();
|
||||
self.disabled.clear();
|
||||
let mut output = self.ctx.run_ui(raw, |ui| {
|
||||
let (_, record) = craft::ui::runtime::inspect::record(ui, egui::Id::unique("sort-popup-test-inspection"), |ui| {
|
||||
match pass {
|
||||
@@ -84,6 +87,9 @@ impl Frames {
|
||||
&& let Some(name) = element.facts.as_ref().and_then(|facts| facts.name.as_ref())
|
||||
&& let Some(response) = ui.ctx().read_response(element.id)
|
||||
{
|
||||
if element.facts.as_ref().is_some_and(|facts| facts.disabled) {
|
||||
self.disabled.push(name.clone());
|
||||
}
|
||||
self.rows.push((name.clone(), response));
|
||||
}
|
||||
}
|
||||
@@ -292,7 +298,7 @@ fn library_sort_choices_change_order_direction_and_group_without_photo_edits() {
|
||||
if id == "sort:key:random" {
|
||||
if !f.is_open() { f.open(); }
|
||||
let dir = f.row("Ascending").clone();
|
||||
assert!(!dir.enabled());
|
||||
assert!(f.disabled.iter().any(|name| name == "Ascending"), "a shuffle has no direction to choose: the row is disabled");
|
||||
f.click_at(dir.interact_rect.center(), false);
|
||||
assert_eq!(f.app.session.sort, oracle.session.sort);
|
||||
assert_eq!(f.app.session.journal, oracle.session.journal);
|
||||
|
||||
@@ -104,6 +104,32 @@ fn dropdown_face(ui: &mut egui::Ui, id: &str, text: &str, active: bool) -> egui:
|
||||
shown.response
|
||||
}
|
||||
|
||||
/// Before a dropdown's menu is shown: remember whether Enter or Space (rather than a press) is what opens it.
|
||||
fn note_opening(ui: &egui::Ui, opener: &egui::Response) {
|
||||
let key = opener.id.with("lc-menu-by-key");
|
||||
let focused = craft::ui::runtime::focus::focused_rec(ui.ctx()).is_some_and(|rec| rec.stop.id == opener.id);
|
||||
let (typed, pressed) = ui.input(|i| (i.key_pressed(egui::Key::Enter) || i.key_pressed(egui::Key::Space), i.pointer.any_pressed()));
|
||||
if focused && typed {
|
||||
ui.data_mut(|d| d.insert_temp(key, true));
|
||||
} else if pressed && opener.contains_pointer() {
|
||||
ui.data_mut(|d| d.insert_temp(key, false));
|
||||
}
|
||||
}
|
||||
|
||||
/// When a dropdown's menu closes: one the pointer opened gives the keyboard back to the grid, so its shortcuts
|
||||
/// keep working; one Enter or Space opened leaves the focus on its opener, where the framework returns it.
|
||||
fn menu_closed(ui: &egui::Ui, opener: &egui::Response, closed: bool) {
|
||||
if !closed {
|
||||
return;
|
||||
}
|
||||
let key = opener.id.with("lc-menu-by-key");
|
||||
let by_key = ui.data(|d| d.get_temp::<bool>(key)).unwrap_or(false);
|
||||
if !by_key && let Some(rec) = craft::ui::runtime::focus::focused_rec(ui.ctx()).filter(|rec| rec.stop.id == opener.id) {
|
||||
ui.memory_mut(|m| m.surrender_focus(rec.stop.root));
|
||||
}
|
||||
ui.data_mut(|d| d.remove::<bool>(key));
|
||||
}
|
||||
|
||||
/// One row of a value menu: radio-marked while `checked`, the lead when the menu opens. `caption` is
|
||||
/// shown as it is (it may be a user's keyword or camera name). True when the row ran.
|
||||
fn value_row(ui: &mut egui::Ui, salt: impl egui::AsIdSalt, caption: &str, checked: bool) -> bool {
|
||||
@@ -119,7 +145,12 @@ fn picker(app: &mut LightcraftApp, ui: &mut egui::Ui, id: &str, current: &str, a
|
||||
let shown = if active && user_names { current.to_owned() } else { crate::i18n::tr(current).to_owned() };
|
||||
let face = dropdown_face(ui, &format!("filter-{id}"), &shown, active);
|
||||
let mut chosen = None;
|
||||
Menu::new(shown).id_salt(("filter-menu", id)).values().opened_by(&face, Trigger::Click).show(ui, |ui, _| {
|
||||
// A measuring or sizing run shows no layer: its rows would claim the menu's ids a second time in the pass.
|
||||
if measuring(ui) {
|
||||
return;
|
||||
}
|
||||
note_opening(ui, &face);
|
||||
let menu = Menu::new(shown).id_salt(("filter-menu", id)).values().opened_by(&face, Trigger::Click).show(ui, |ui, _| {
|
||||
for (label, patch, sel) in &items {
|
||||
// a camera, lens or keyword the library names is data; the "Any …" row is a built-in label
|
||||
let literal = user_names && patch.get(id).is_some_and(|value| !value.is_null());
|
||||
@@ -129,6 +160,7 @@ fn picker(app: &mut LightcraftApp, ui: &mut egui::Ui, id: &str, current: &str, a
|
||||
}
|
||||
}
|
||||
});
|
||||
menu_closed(ui, &face, menu.output.closed.is_some());
|
||||
if let Some(p) = chosen {
|
||||
filter(app, p);
|
||||
}
|
||||
@@ -158,8 +190,12 @@ fn kind_picker(app: &mut LightcraftApp, ui: &mut egui::Ui) {
|
||||
let shown = choices.iter().find(|(value, _)| *value == current).map_or("Merged", |(_, label)| *label);
|
||||
let active = f.kind.is_some() || f.merged.is_some();
|
||||
let opener = dropdown_face(ui, "filter-kind", crate::i18n::tr(shown), active);
|
||||
if measuring(ui) {
|
||||
return;
|
||||
}
|
||||
let mut chosen = None;
|
||||
Menu::new(crate::i18n::tr("Type")).id_salt("filter-kind-menu").values().opened_by(&opener, Trigger::Click).show(ui, |ui, _| {
|
||||
note_opening(ui, &opener);
|
||||
let menu = Menu::new(crate::i18n::tr("Type")).id_salt("filter-kind-menu").values().opened_by(&opener, Trigger::Click).show(ui, |ui, _| {
|
||||
for (value, label) in choices {
|
||||
let item = MenuItem::new(crate::i18n::tr(label).replace('&', "&&")).id_salt(value).entry_checked(value == current);
|
||||
let item = if value == current { item.default_stop() } else { item };
|
||||
@@ -172,6 +208,7 @@ fn kind_picker(app: &mut LightcraftApp, ui: &mut egui::Ui) {
|
||||
}
|
||||
}
|
||||
});
|
||||
menu_closed(ui, &opener, menu.output.closed.is_some());
|
||||
if !measuring(ui)
|
||||
&& ui.is_enabled()
|
||||
&& let Some(value) = chosen
|
||||
@@ -214,9 +251,13 @@ fn presets_menu(app: &mut LightcraftApp, ui: &mut egui::Ui, filtering: bool) {
|
||||
if !measuring(ui) {
|
||||
register(ui, "button:filterPresets", face.rect);
|
||||
}
|
||||
if measuring(ui) {
|
||||
return;
|
||||
}
|
||||
let names: Vec<String> = app.session.filter_presets.iter().map(|f| f.name.clone()).collect();
|
||||
let mut action = None;
|
||||
Menu::new(crate::i18n::tr("Presets")).id_salt("filter-presets-menu").opened_by(&face, Trigger::Click).show(ui, |ui, _| {
|
||||
note_opening(ui, &face);
|
||||
let menu = Menu::new(crate::i18n::tr("Presets")).id_salt("filter-presets-menu").opened_by(&face, Trigger::Click).show(ui, |ui, _| {
|
||||
// a preset's name is the user's: shown as written, never read for a mnemonic marker
|
||||
for name in &names {
|
||||
let row = MenuItem::new(name.replace('&', "&&")).id_salt(("filter-preset", name.as_str())).show(ui);
|
||||
@@ -239,6 +280,7 @@ fn presets_menu(app: &mut LightcraftApp, ui: &mut egui::Ui, filtering: bool) {
|
||||
action = Some(PresetAction::Save);
|
||||
}
|
||||
});
|
||||
menu_closed(ui, &face, menu.output.closed.is_some());
|
||||
match action {
|
||||
Some(PresetAction::Apply(name)) => {
|
||||
let _ = app.run("filter.applyPreset", json!({"name": name}));
|
||||
|
||||
@@ -177,6 +177,10 @@ fn filter_kind_tab_enter_navigation_preserves_opener_focus() {
|
||||
assert_eq!(h.app.session.filter, filter, "keyboard cancellation applies nothing");
|
||||
assert_eq!(h.view.ctx.memory(|memory| memory.focused()), Some(focused), "Escape returns focus to the opener");
|
||||
assert_eq!(h.app.ui.view, crate::state::ViewMode::PhotoGrid);
|
||||
// (a closed framework menu fades out for a few frames and holds Tab until it is gone)
|
||||
for _ in 0..20 {
|
||||
h.step();
|
||||
}
|
||||
ask(&mut h, "ui.key", json!({"key": "Tab"}));
|
||||
assert_ne!(h.view.ctx.memory(|memory| memory.focused()), Some(focused), "Tab can leave the opener");
|
||||
}
|
||||
@@ -192,6 +196,9 @@ fn filter_kind_pointer_cancel_keeps_the_grid_keyboard_owner() {
|
||||
ask(&mut h, "ui.key", json!({"key": "Escape"}));
|
||||
assert_eq!(h.app.session.filter, filter);
|
||||
assert_eq!(h.app.ui.view, crate::state::ViewMode::PhotoGrid);
|
||||
for _ in 0..20 {
|
||||
h.step();
|
||||
}
|
||||
ask(&mut h, "ui.key", json!({"key": "2"}));
|
||||
assert_eq!(h.app.session.catalog.photo(PhotoId(101)).unwrap().rating, 2, "cancelling the menu gives the next rating key to the grid");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user