diff --git a/crates/ui-widgets/src/tile_grid/grid.rs b/crates/ui-widgets/src/tile_grid/grid.rs index 552ad7c15..b7369205d 100644 --- a/crates/ui-widgets/src/tile_grid/grid.rs +++ b/crates/ui-widgets/src/tile_grid/grid.rs @@ -60,8 +60,8 @@ pub enum RatingKeys { #[derive(Clone, Debug, PartialEq)] #[non_exhaustive] pub enum GridEvent { - /// A tile's stars were clicked (that tile), or a rating key was pressed (the selected tiles, or the lead when none - /// is selected): the rating, 0 clears. The one route to a rating; the tile's own `TilePartEvent::Rate` does not + /// A tile's stars were clicked (the selected tiles when the tile is one of them, else that tile), or a rating key was + /// pressed (the selected tiles, or the lead when none is selected): the rating, 0 clears. The one route to a rating; the tile's own `TilePartEvent::Rate` does not /// reach the collection's events. #[non_exhaustive] Rate { @@ -673,7 +673,8 @@ impl Component for TileGrid<'_, K> /// collection's events on a header row are the group events instead: Enter picks the group, the context-menu /// key asks for its menu, Left and Right open and close it, and the lead on it is not reported. A tail row keeps its /// Enter and the lead on it, and adds its click as `CollectionEvent::Activate`; it has no menu. - fn finish(self, mut cx: Cx<'_, Self>) -> TileGridOutput { + fn finish(mut self, mut cx: Cx<'_, Self>) -> TileGridOutput { + let selected = self.rated(&cx); let mut response = cx.collection_response(); let mut events = self.events; let mods = crate::runtime::pointer::frame(&cx.core.ctx).mods; @@ -701,7 +702,11 @@ impl Component for TileGrid<'_, K> continue; } match event { - CollectionEvent::Item(key, TilePartEvent::Rate(rating)) => events.push(GridEvent::Rate { keys: vec![key], rating }), + CollectionEvent::Item(key, TilePartEvent::Rate(rating)) => { + // Stars on a selected tile rate the whole selection. + let keys = if selected.contains(&key) { selected.clone() } else { vec![key] }; + events.push(GridEvent::Rate { keys, rating }); + } CollectionEvent::Toggle(edit) if self.checks => { // The check column does not sweep, so each flip arrives whole as a `Set`. if let Some(SweepStep::Flip(toggle)) = SweepApply::default().feed(&edit) { diff --git a/crates/ui-widgets/src/tile_grid/mod.rs b/crates/ui-widgets/src/tile_grid/mod.rs index 6ae2df271..19322017a 100644 --- a/crates/ui-widgets/src/tile_grid/mod.rs +++ b/crates/ui-widgets/src/tile_grid/mod.rs @@ -128,7 +128,7 @@ pub enum TileStatus { #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] #[non_exhaustive] pub enum BadgeVisibility { - /// While the pointer is over the tile, or when a rating, flag or label is set. + /// While the pointer is over the tile, while it is selected, or when a rating, flag or label is set. #[default] Auto, /// Always. diff --git a/crates/ui-widgets/src/tile_grid/tile.rs b/crates/ui-widgets/src/tile_grid/tile.rs index fd09fd8f1..734424737 100644 --- a/crates/ui-widgets/src/tile_grid/tile.rs +++ b/crates/ui-widgets/src/tile_grid/tile.rs @@ -704,7 +704,7 @@ impl Component for Tile<'_> { && match self.bar { BadgeVisibility::Always => true, BadgeVisibility::Never => false, - _ => cx.hovered() || self.has_bar_values(), + _ => cx.hovered() || cx.resolved.selected == Some(true) || self.has_bar_values(), }; let mut placed = self.place(&sizes, cx.content_rect, rtl, bar_shown, &faces, &pill_widths); let _ = cx.part(PICTURE, placed.picture).sense(Sense::HOVER); diff --git a/crates/ui-widgets/tests/it/tile_grid/keys.rs b/crates/ui-widgets/tests/it/tile_grid/keys.rs index 39bfe68d9..8a604dea1 100644 --- a/crates/ui-widgets/tests/it/tile_grid/keys.rs +++ b/crates/ui-widgets/tests/it/tile_grid/keys.rs @@ -134,3 +134,32 @@ fn a_wheel_notch_scrolls_a_third_of_the_view() { let share = moved / view.get().height(); assert!((0.3..0.37).contains(&share), "one notch scrolls a third of the view: {moved} of {}", view.get().height()); } + +/// Stars clicked on a selected tile rate every selected tile; on a tile outside the selection they rate that tile +/// alone. A selected tile shows its badge bar, so its stars can be clicked without hovering it first. +#[trial] +fn stars_on_a_selected_tile_rate_the_selection() { + let seen = Rc::new(RefCell::new(Seen::default())); + let held = seen.clone(); + let mut fixture = Fixture::new(move |ui| { + let keys = [0_usize, 1, 2, 3]; + let source = keyed(&keys, |key: &usize| *key); + let out = TileGrid::new("Library").show_items(ui, &source, &mut |key, _| Tile::new(NAMES[*key]).rating(0)); + held.borrow_mut().grid.extend(out.events); + }); + fixture.room(Vec2::new(600.0, 400.0)); + fixture.settle(); + for (name, mods) in [("Tile 1", Modifiers::NONE), ("Tile 2", Modifiers::SHIFT)] { + let tile = fixture.by_label(name).expect("the tile has a node"); + fixture.hold(mods).click(&tile).hold(Modifiers::NONE).settle(); + } + let stars = fixture.by_label("Rating of Tile 2").expect("a selected tile shows its stars"); + fixture.click(&stars).settle(); + let tile = fixture.by_label("Tile 4").expect("the tile has a node"); + fixture.frame(vec![pointer::moved(tile.rect.center())]).settle(); + let stars = fixture.by_label("Rating of Tile 4").expect("the hovered tile shows its stars"); + fixture.click(&stars).settle(); + let rated: Vec> = + seen.borrow().grid.iter().filter_map(|event| if let GridEvent::Rate { keys, .. } = event { Some(keys.clone()) } else { None }).collect(); + assert_eq!(rated, [vec![0, 1], vec![3]], "the selection is rated together, an unselected tile alone"); +}