From c2eb2a0baacec767ab9ff4de8dfb1040517d3ce0 Mon Sep 17 00:00:00 2001 From: "Bruno D." Date: Wed, 2 Dec 2026 19:06:46 -0500 Subject: [PATCH] LRC-MAP-VIEW: pins and groups work by keyboard From review on the Map view: pins could only be reached with the mouse. Each pin or group on the canvas is now a focusable button (Tab), named for screen readers ("Photo pin", "Group of 3 photos, 1 selected"). With one focused, the arrow keys move to the nearest pin that way, Return or Space selects its photos (Cmd/Ctrl+ Return adds them), a screen reader's Click selects them too, and its preview shows as on hover (not under its open loupe). The other Map keys keep working: a focused pin keeps only its arrows, Return and Space (canvas_keys::mark_focused). Pins a cell past the canvas, and the repeated world copies of a pin, take no focus. Also: the test for tiles evicted before they were drawn keeps one view and redraws until loaded, as the map does, since tiles dropped from the arrival queue are now looked for again on a later frame. --- crates/ui-egui/src/canvas_keys.rs | 8 +- crates/ui-egui/src/headless.rs | 2 +- crates/ui-egui/src/panels/map.rs | 129 +++++++++++++++++++++++-- crates/ui-egui/src/tests/map_geotag.rs | 64 ++++++++++++ docs/map.md | 5 + 5 files changed, 196 insertions(+), 12 deletions(-) diff --git a/crates/ui-egui/src/canvas_keys.rs b/crates/ui-egui/src/canvas_keys.rs index d0c9ee3adf..79629d04c9 100644 --- a/crates/ui-egui/src/canvas_keys.rs +++ b/crates/ui-egui/src/canvas_keys.rs @@ -20,6 +20,12 @@ pub fn id(key: &str) -> Id { Id::unique("lc-canvas-handle").with(key) } +/// Mark `id`, which has the focus this frame, as a stand-in on a canvas: [`has_focus`] then holds, +/// so the app's shortcuts leave it the arrows, Return and Space. +pub fn mark_focused(ctx: &egui::Context, id: Id) { + ctx.data_mut(|d| d.insert_temp(focused_key(), id)); +} + /// Whether a canvas handle holds the keyboard focus: it keeps the arrow keys and Return from the /// app's shortcuts (next photo, and so on). pub fn has_focus(ctx: &egui::Context) -> bool { @@ -58,7 +64,7 @@ pub fn handle(ui: &mut Ui, key: &str, name: &str, value: String, at: Pos2) -> Ha if !resp.has_focus() { return out; } - ui.data_mut(|d| d.insert_temp(focused_key(), id)); + mark_focused(ui.ctx(), id); // a ring shows where the focus is ui.painter().circle_stroke(at, 9.0, egui::Stroke::new(2.0, ui.visuals().selection.stroke.color)); ui.painter().circle_stroke(at, 11.0, egui::Stroke::new(1.0, egui::Color32::BLACK)); diff --git a/crates/ui-egui/src/headless.rs b/crates/ui-egui/src/headless.rs index 7a05a8daa7..712e59cc0b 100644 --- a/crates/ui-egui/src/headless.rs +++ b/crates/ui-egui/src/headless.rs @@ -88,7 +88,7 @@ pub struct HeadlessView { /// The pretend host's clipboard: what the UI last copied, and what a paste request pastes. pub clipboard: String, /// Events the host owes the next frame (a paste request's paste, as a desktop host sends it). - owed: Vec, + pub(crate) owed: Vec, /// The last frame's AccessKit tree, once a test has turned AccessKit on (`ctx.enable_accesskit()`). #[cfg(test)] pub(crate) accesskit: Option, diff --git a/crates/ui-egui/src/panels/map.rs b/crates/ui-egui/src/panels/map.rs index 4584452a7f..b1b9ef73e1 100644 --- a/crates/ui-egui/src/panels/map.rs +++ b/crates/ui-egui/src/panels/map.rs @@ -549,7 +549,7 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) { app.map_drawn.without_gps = app.map_points.without; // 3. Render Clusters and Markers - let PinInput { clicked, double_clicked_photo, zoom_target, hovered } = + let PinInput { clicked, double_clicked_photo, zoom_target, hovered, focused, .. } = draw_pins(app, ui, &painter, &clusters, (canvas_rect, &canvas_response), &map_ui); if let Some((target, next_z)) = zoom_target { center = target; @@ -569,12 +569,14 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) { } // 4. Render Active Pin Loupe Popover Card + let mut loupe_shown = None; if let Some(active_id) = map_ui.active_pin_photo { let pin_coord = app.session.catalog.photo(active_id).and_then(|ph| lightcraft_engine::map::from_gps(ph.meta.gps)); if let Some(coord) = pin_coord { let pin_pos = screen(vp.to_screen(coord)); if canvas_rect.contains(pin_pos) && hovered.is_none() && map_ui.pin_drag.is_none() { render_loupe_popover(app, ui, pin_pos, active_id, &mut map_ui); + loupe_shown = Some(active_id.0); } } } @@ -584,11 +586,16 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) { locations::draw(app, &painter, canvas_rect, &vp, &map_ui); tracks::draw(app, &painter, &t, canvas_rect, &vp); - // the hovered pin's thumbnail (not while something is carried) - if let Some(ids) = hovered.as_ref().filter(|_| !carrying && map_ui.pin_drag.is_none()) - && let Some(at) = app.map_drawn.pins.iter().find(|p| &p.ids == ids).map(|p| p.at) - { - hover_thumbnail(app, ui, &t, at, ids); + // the hovered pin's thumbnail, else the focused one's (not while something is carried) + // (the focused one's not when its loupe is drawn: the card already shows it) + let focused = focused.filter(|(ids, _)| loupe_shown.is_none_or(|a| !ids.contains(&a))); + let preview = match (&hovered, focused) { + (Some(ids), _) => app.map_drawn.pins.iter().find(|p| &p.ids == ids).map(|p| (ids.clone(), p.at)), + // where the focused copy is (the world repeats at low zoom) + (None, focused) => focused, + }; + if let Some((ids, at)) = preview.filter(|_| !carrying && map_ui.pin_drag.is_none()) { + hover_thumbnail(app, ui, &t, at, &ids); } app.map_drawn.hovered = hovered; @@ -892,6 +899,25 @@ struct PinInput { zoom_target: Option<(LatLon, f64)>, /// The photos of the pin under the pointer. hovered: Option>, + /// The photos of the pin with the keyboard focus, and where that copy of it is drawn. + focused: Option<(Vec, Pos2)>, + /// An arrow key pressed on the focused pin: where it is and the direction. + move_from: Option<(Pos2, egui::Vec2)>, +} + +/// The egui id of the pin (or cluster) whose first photo is `first`: pins take the keyboard focus +/// (Tab reaches them, the arrow keys move between them), never the pointer, which the canvas keeps. +pub(crate) fn pin_focus_id(first: u64) -> egui::Id { + egui::Id::unique(("map-pin", first)) +} + +/// What a screen reader says for a pin of `n` photos, `selected` of them selected. +fn pin_name(n: usize, selected: usize) -> String { + match n { + 1 if selected > 0 => crate::i18n::tr("Photo pin, selected").to_string(), + 1 => crate::i18n::tr("Photo pin").to_string(), + _ => crate::i18n::tr_format!("Group of {n} photos, {selected} selected", n = n, selected = selected), + } } /// Draw the pins and clusters (selected ones highlighted, a cluster with its selected count), @@ -907,7 +933,8 @@ fn draw_pins( map_ui: &crate::state::MapUi, ) -> PinInput { let pointer = canvas_response.hover_pos(); - let mut input = PinInput { clicked: None, double_clicked_photo: None, zoom_target: None, hovered: None }; + let mut input = PinInput { clicked: None, double_clicked_photo: None, zoom_target: None, hovered: None, focused: None, move_from: None }; + let accent = Tokens::get(ui.ctx()).accent; // while photos are carried over the map, or a pin is moved, pins are not hovered or clicked let busy = app.ui.dragging_photos.is_some() || map_ui.pin_drag.is_some(); let selected: HashSet = app.session.selection.ids.iter().map(|p| p.0).collect(); @@ -915,6 +942,9 @@ fn draw_pins( let to_screen = |c: &Cluster| pos2(canvas.min.x + c.center.x as f32, canvas.min.y + c.center.y as f32); let local = pointer.filter(|p| canvas.contains(*p)).map(|p| Point::new(f64::from(p.x - canvas.min.x), f64::from(p.y - canvas.min.y))); let hovered = if busy { None } else { local.and_then(|p| lightcraft_engine::map::cluster::hit(clusters, p, |_| PIN_HIT)) }; + // the pins the keyboard reaches: on the canvas (clusters reach a cell past it), and one copy of + // a pin the world repeats at low zoom (one focus id per pin) + let mut focusable: Vec<(u64, Pos2)> = Vec::new(); for (i, c) in clusters.iter().enumerate() { let at = to_screen(c); let n_selected = c.ids.iter().filter(|id| selected.contains(id)).count(); @@ -923,6 +953,61 @@ fn draw_pins( crate::widgets::register(ui, format!("map:pin:{first}"), Rect::from_center_size(at, vec2(32.0, 32.0))); } let is_hovered = hovered == Some(i); + // keyboard: focus, Return selects (Cmd/Ctrl+Return adds), as a click does + if let Some(&first) = c.ids.first() + && canvas.contains(at) + && !focusable.iter().any(|(f, _)| *f == first) + { + focusable.push((first, at)); + let focus = ui.interact( + Rect::from_center_size(pos2(at.x, at.y - 10.0), vec2(28.0, 28.0)), + pin_focus_id(first), + Sense::focusable_noninteractive(), + ); + let name = pin_name(c.len(), n_selected); + focus.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, true, n_selected > 0, &name)); + // a screen reader's Click selects the pin, as Return does + ui.ctx().accesskit_node_builder(focus.id, |node| node.add_action(egui::accesskit::Action::Click)); + if !busy && ui.input(|inp| inp.has_accesskit_action_request(focus.id, egui::accesskit::Action::Click)) { + input.clicked = Some((c.ids.iter().map(|id| PhotoId(*id)).collect(), egui::Modifiers::NONE)); + } + if focus.has_focus() && !busy { + // the app's shortcuts leave this pin its arrows, Return and Space + crate::canvas_keys::mark_focused(ui.ctx(), focus.id); + painter.circle_stroke(pos2(at.x, at.y - 12.0), 13.0, Stroke::new(2.0, accent)); + input.focused = Some((c.ids.clone(), at)); + // the arrow keys move between pins (not to whatever widget is nearest) + ui.memory_mut(|m| { + m.set_focus_lock_filter( + focus.id, + egui::EventFilter { tab: false, horizontal_arrows: true, vertical_arrows: true, escape: false }, + ); + }); + let arrow = ui.input_mut(|inp| { + [ + (egui::Key::ArrowLeft, vec2(-1.0, 0.0)), + (egui::Key::ArrowRight, vec2(1.0, 0.0)), + (egui::Key::ArrowUp, vec2(0.0, -1.0)), + (egui::Key::ArrowDown, vec2(0.0, 1.0)), + ] + .into_iter() + .find(|(k, _)| inp.consume_key(egui::Modifiers::NONE, *k)) + .map(|(_, d)| d) + }); + if let Some(d) = arrow { + // egui may have picked a neighbour to focus from the same key: cancel that + ui.memory_mut(|m| m.move_focus(egui::FocusDirection::None)); + input.move_from = Some((at, d)); + } + let enter = ui.input_mut(|inp| { + let m = inp.modifiers; + (inp.consume_key(m, egui::Key::Enter) || inp.consume_key(egui::Modifiers::NONE, egui::Key::Space)).then_some(m) + }); + if let Some(m) = enter { + input.clicked = Some((c.ids.iter().map(|id| PhotoId(*id)).collect(), m)); + } + } + } if let [id] = c.ids[..] { if moving == Some(id) { // the pin being moved: a faint mark where it was @@ -949,6 +1034,22 @@ fn draw_pins( input.clicked = Some((c.ids.iter().map(|id| PhotoId(*id)).collect(), ui.input(|i| i.modifiers))); } } + // the nearest pin that way (straight ahead counts more than off to the side) + if let Some((from, d)) = input.move_from { + let next = focusable + .iter() + .filter_map(|&(first, at)| { + let v = at - from; + let ahead = v.x * d.x + v.y * d.y; + let side = (v.x * d.y - v.y * d.x).abs(); + (ahead > 1.0).then_some((ahead + 2.0 * side, first)) + }) + .min_by(|a, b| a.0.total_cmp(&b.0)) + .map(|(_, first)| first); + if let Some(first) = next { + ui.memory_mut(|m| m.request_focus(pin_focus_id(first))); + } + } input } @@ -1577,8 +1678,9 @@ mod tests { let fake = fake_tiles(); let mut session = session_with_fake_tiles("ui-evicted", fake.clone()); let center = LatLon::new(48.8566, 2.3522).unwrap(); - let draw = |session: &mut lightcraft_engine::Session| { - let mut view = HeadlessView::new(); + // one view across frames, as the app has: its textures keep what was drawn + let mut view = HeadlessView::new(); + let mut draw = |session: &mut lightcraft_engine::Session| { let raw = HeadlessView::raw_input(vec2(600.0, 600.0), 1.0, 0.0, vec![]); let mut drawn = tiles::Drawn::default(); view.run(raw, |ui| { @@ -1591,7 +1693,14 @@ mod tests { let first = draw(&mut session); assert!(first.missing > lightcraft_engine::map::service::MEMORY_TILES, "{first:?}"); wait_idle(&session); - let loaded = draw(&mut session); + // a tile dropped before a frame took it is looked for again while in view (the map repaints + // every 100 ms while one is missing) + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20); + let mut loaded = draw(&mut session); + while loaded.missing + loaded.from_parent > 0 && std::time::Instant::now() < deadline { + std::thread::sleep(std::time::Duration::from_millis(100)); + loaded = draw(&mut session); + } assert_eq!(loaded.missing + loaded.from_parent, 0, "{loaded:?}"); } diff --git a/crates/ui-egui/src/tests/map_geotag.rs b/crates/ui-egui/src/tests/map_geotag.rs index ae94e101cf..ff8f929968 100644 --- a/crates/ui-egui/src/tests/map_geotag.rs +++ b/crates/ui-egui/src/tests/map_geotag.rs @@ -400,3 +400,67 @@ fn a_click_on_a_button_over_a_pin_does_not_select_the_pin() { assert_ne!(state(&mut h)["lockMarkers"].as_bool(), locked, "the button took the click"); assert_eq!(h.app.session.selection.ids, vec![PhotoId(ids[0])], "the pin beneath wasn't selected"); } + +/// Pins work by keyboard: a focused pin shows its preview, Return selects its photos (as a click +/// does), and an arrow key moves the focus to another pin. +#[test] +fn pins_work_by_keyboard() { + let (mut h, ids) = demo(); + view(&mut h, 45.0, -36.0, 3.0); + let s = state(&mut h); + // (a pin's focus id is its first photo in its own order; only pins on the canvas take focus) + let c = &s["canvas"]; + let (x0, y0) = (c["x"].as_f64().unwrap(), c["y"].as_f64().unwrap()); + let (x1, y1) = (x0 + c["width"].as_f64().unwrap(), y0 + c["height"].as_f64().unwrap()); + let first: Vec = pins(&s) + .iter() + .filter(|p| (x0..x1).contains(&p["x"].as_f64().unwrap()) && (y0..y1).contains(&p["y"].as_f64().unwrap())) + .filter_map(|p| p["ids"][0].as_u64()) + .collect(); + assert!(first.len() >= 2, "{s}"); + h.app.run("library.select", json!({"ids": []})).unwrap(); + h.view.ctx.memory_mut(|m| m.request_focus(crate::panels::map::pin_focus_id(first[0]))); + frames(&mut h, 3); + assert!(widget(&h, "map:hoverThumb").is_some(), "the focused pin shows its preview"); + let key = |h: &mut Headless, k: &str| { + let r = h.request("ui.key", json!({"key": k}), std::time::Duration::from_secs(20)); + assert_eq!(r["ok"], true, "{k}: {r}"); + frames(h, 3); + }; + key(&mut h, "Enter"); + let pin = pin_of(&state(&mut h), first[0]); + let mut selected: Vec = h.app.session.selection.ids.iter().map(|p| p.0).collect(); + selected.sort_unstable(); + assert_eq!(selected, ids_of(&pin), "Return selects the pin's photos"); + // a map shortcut still works with a pin focused + let zoom = state(&mut h)["zoom"].as_f64().unwrap(); + key(&mut h, "Minus"); + assert!(state(&mut h)["zoom"].as_f64().unwrap() < zoom, "- zooms out with a pin focused"); + view(&mut h, 45.0, -36.0, 3.0); + // a screen reader's Click on a pin selects it + h.app.run("library.select", json!({"ids": []})).unwrap(); + h.view.owed.push(egui::Event::AccessKitActionRequest(egui::accesskit::ActionRequest { + action: egui::accesskit::Action::Click, + target_tree: egui::accesskit::TreeId::ROOT, + target_node: crate::panels::map::pin_focus_id(first[1]).accesskit_id(), + data: None, + })); + frames(&mut h, 3); + let pin = pin_of(&state(&mut h), first[1]); + let mut selected: Vec = h.app.session.selection.ids.iter().map(|p| p.0).collect(); + selected.sort_unstable(); + assert_eq!(selected, ids_of(&pin), "AccessKit Click selects the pin's photos"); + h.view.ctx.memory_mut(|m| m.request_focus(crate::panels::map::pin_focus_id(first[0]))); + frames(&mut h, 3); + // an arrow key: the focus goes to another pin + for k in ["ArrowRight", "ArrowLeft", "ArrowDown", "ArrowUp"] { + key(&mut h, k); + let now = h.view.ctx.memory(|m| m.focused()); + if now != Some(crate::panels::map::pin_focus_id(first[0])) { + assert!(first.iter().any(|f| Some(crate::panels::map::pin_focus_id(*f)) == now), "{k}: focus on another pin, not elsewhere"); + let _ = ids; + return; + } + } + panic!("no arrow key moved the focus off the first pin"); +} diff --git a/docs/map.md b/docs/map.md index 49824cade1..d05f485aa4 100644 --- a/docs/map.md +++ b/docs/map.md @@ -226,6 +226,11 @@ In the Map view (and only there; elsewhere the keys keep their meaning, see `doc | `=` / `-`, ⌘= / ⌘- | Zoom the map in / out | | ⌘0 | Fit to Photos | +**By keyboard.** Tab reaches the pins; with a pin (or group) focused, the arrow keys move to the nearest pin in +that direction, Return or Space selects its photos (⌘/Ctrl+Return adds them to the selection) and its preview +shows, as on hover; the other Map keys keep working. Screen readers hear each pin as a button ("Photo pin" or +"Group of 3 photos, 1 selected") that their Click action selects. + **Hide Pins** (`H`, the bottom toolbar, `map.showPins {on?}`) hides the photo pins: hidden, they can't be clicked, hovered or dragged. Saved locations and the track log stay. The choice is saved with the window state (`ui.json`).