LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
LR-LIB-RATING: focus follows the Tab stop when the rating changes elsewhere
Nathaniel Ledford5947667c6ca7
diff --git a/crates/ui-egui/src/rating.rs b/crates/ui-egui/src/rating.rsindex 2325ed09a..16cd611d2 100644--- a/crates/ui-egui/src/rating.rs+++ b/crates/ui-egui/src/rating.rs@@ -131,7 +131,7 @@ impl Rating {let cell = RatingSize::Large.cell();let mut out = None;let mut hover_n = None;- let mut focused = false;+ let mut focused_at = None;let mut cells = [Rect::NOTHING; STARS as usize];// One Tab stop for the row, like a radio group: the star of the current rating (the first when// unrated), plus whichever star already has the focus so it keeps it@@ -150,7 +150,7 @@ impl Rating {out = Some(clicked_rating(self.rating, i + 1));}if resp.has_focus() {- focused = true;+ focused_at = Some(i);// ← and → step the rating instead of moving the focus to the neighbouring widgetui.memory_mut(|m| {m.set_focus_lock_filter(resp.id, egui::EventFilter { horizontal_arrows: true, ..Default::default() });@@ -159,6 +159,7 @@ impl Rating {}*slot = r;}+ let focused = focused_at.is_some();if focused && out.is_none() {let (left, right) = ui.input_mut(|i| (i.consume_key(Modifiers::NONE, Key::ArrowLeft), i.consume_key(Modifiers::NONE, Key::ArrowRight)));if left || right {@@ -171,6 +172,10 @@ impl Rating {// keep the focus on the star that is now the row's Tab stoplet to = self.star_id(ui, n.max(1) - 1);ui.memory_mut(|m| m.request_focus(to));+ } else if focused_at != Some(stop) {+ // the rating changed from outside (undo, a shortcut, another photo): the focus follows the Tab stop+ let to = self.star_id(ui, stop);+ ui.memory_mut(|m| m.request_focus(to));}}// The hovered star also gets the icon-button hover square, so hovering the current rating (wherediff --git a/crates/ui-egui/src/tests_rating.rs b/crates/ui-egui/src/tests_rating.rsindex db6453e77..fd1fff09b 100644--- a/crates/ui-egui/src/tests_rating.rs+++ b/crates/ui-egui/src/tests_rating.rs@@ -163,3 +163,30 @@ fn a_rating_row_is_one_tab_stop() {// frames 1 to 3 press Tab: left neighbour, the row, right neighbourassert_eq!(focused, vec![false, false, false, true, false], "{focused:?}");}++#[test]+fn a_rating_changed_from_outside_moves_the_focus_to_the_tab_stop() {+ use egui::Key::Tab;+ let ctx = egui::Context::default();+ crate::theme::install_fonts(&ctx);+ let rating = std::cell::Cell::new(2u8);+ let mut stars_focused = Vec::new();+ for frame in 0..5 {+ // frame 1: Tab focuses the row's stop (star 2); frame 2: the rating changes elsewhere to 5+ if frame == 2 {+ rating.set(5);+ }+ let events = if frame == 1 {+ vec![egui::Event::Key { key: Tab, physical_key: Some(Tab), pressed: true, repeat: false, modifiers: Default::default() }]+ } else {+ Vec::new()+ };+ let raw = egui::RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(600.0, 200.0))), events, ..Default::default() };+ let mut out = ctx.run_ui(raw, |ui| {+ let _ = row(rating.get()).show(ui);+ stars_focused = (0..5).map(|i| ctx.memory(|m| m.has_focus(ui.id().with(("rating-star", "t", i as u8))))).collect();+ });+ out.textures_delta.clear();+ }+ assert_eq!(stars_focused, vec![false, false, false, false, true], "{stars_focused:?}");+}