Show the champion's sheet on hover and keyboard focus (#73)
The sheet covers the card with the Season's Matches, Wins, best Placement and Attempts, and the date of the first top 1 for a Completed champion. Its lines are prepared in Card::from_row; only the shown cards build it. The grid keeps the hovered card and one focus handle per card id, and handles Tab and Shift-Tab itself, scrolling the next card's row into view. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bd9f1c7c98
commit
796fae11b7
5 files changed
+520
-8
No files matched your search
+124
-5
@@ -1,6 +1,7 @@
|
||||
//! The card grid: all the cards a list shows, in rows of the same count, only the visible rows
|
||||
//! built. It is its own view because it owns the scrolling and the hover of its cards.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::ops::Range;
|
||||
use std::rc::Rc;
|
||||
|
||||
@@ -8,9 +9,23 @@ use gpui_kit::component::scroll::Scrollbar;
|
||||
use gpui_kit::component::{ActiveTheme as _, StyledExt as _};
|
||||
use gpui_kit::*;
|
||||
|
||||
use crate::card::{ASPECT, Card, card};
|
||||
use crate::card::{ASPECT, Card, CardState, card};
|
||||
use crate::page::PAGE_PADDING;
|
||||
|
||||
actions!(card_grid, [NextCard, PreviousCard]);
|
||||
|
||||
/// The grid's key context, where Tab and Shift-Tab move between its cards.
|
||||
const CONTEXT: &str = "CardGrid";
|
||||
|
||||
/// Binds Tab and Shift-Tab in the grid's context, where they win over gpui-kit's root: the
|
||||
/// rows are virtual, so the next card may not be built yet and the root cannot reach it.
|
||||
pub fn init(cx: &mut App) {
|
||||
cx.bind_keys([
|
||||
KeyBinding::new("tab", NextCard, Some(CONTEXT)),
|
||||
KeyBinding::new("shift-tab", PreviousCard, Some(CONTEXT)),
|
||||
]);
|
||||
}
|
||||
|
||||
/// The widest content column of the home, as in the mockup.
|
||||
pub const MAX_WIDTH: f32 = 1264.;
|
||||
|
||||
@@ -55,12 +70,25 @@ impl Metrics {
|
||||
}
|
||||
}
|
||||
|
||||
/// What makes one card reachable with the keyboard: its handle, and the subscriptions that draw
|
||||
/// the grid again when the focus comes or goes (a cached view is not drawn by the focus alone).
|
||||
struct CardFocus {
|
||||
handle: FocusHandle,
|
||||
_subscriptions: [Subscription; 2],
|
||||
}
|
||||
|
||||
pub struct CardGrid {
|
||||
/// All the cards, A to Z; rebuilt only when the statistics change.
|
||||
cards: Rc<[Card]>,
|
||||
/// Indices into `cards`, after the filter and the search.
|
||||
shown: Vec<usize>,
|
||||
scroll: UniformListScrollHandle,
|
||||
/// The card under the pointer, which shows its sheet.
|
||||
hovered: Option<u32>,
|
||||
/// One per card id, kept across `set_cards` and dropped with the card.
|
||||
focus: HashMap<u32, CardFocus>,
|
||||
/// The cards changed since the focus handles were last matched to them.
|
||||
focus_stale: bool,
|
||||
/// Guard the light rendering in tests.
|
||||
#[cfg(test)]
|
||||
pub(crate) renders: usize,
|
||||
@@ -76,6 +104,9 @@ impl CardGrid {
|
||||
cards: Rc::new([]),
|
||||
shown: Vec::new(),
|
||||
scroll: UniformListScrollHandle::new(),
|
||||
hovered: None,
|
||||
focus: HashMap::new(),
|
||||
focus_stale: false,
|
||||
#[cfg(test)]
|
||||
renders: 0,
|
||||
#[cfg(test)]
|
||||
@@ -94,10 +125,72 @@ impl CardGrid {
|
||||
}
|
||||
if *self.cards != *cards {
|
||||
self.cards = cards;
|
||||
self.focus_stale = true;
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
/// Gives every card a focus handle, reusing the ones of the ids already there. It runs when
|
||||
/// the grid is drawn after `set_cards`, as subscribing needs the window.
|
||||
fn sync_focus(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
self.focus_stale = false;
|
||||
let mut previous = std::mem::take(&mut self.focus);
|
||||
for card in self.cards.iter() {
|
||||
let focus = previous.remove(&card.id).unwrap_or_else(|| {
|
||||
let handle = cx.focus_handle().tab_stop(true);
|
||||
let gained = cx.on_focus_in(&handle, window, |_, _, cx| cx.notify());
|
||||
let lost = cx.on_focus_out(&handle, window, |_, _, _, cx| cx.notify());
|
||||
CardFocus {
|
||||
handle,
|
||||
_subscriptions: [gained, lost],
|
||||
}
|
||||
});
|
||||
self.focus.insert(card.id, focus);
|
||||
}
|
||||
}
|
||||
|
||||
/// Only a change of card draws the grid again.
|
||||
fn set_hovered(&mut self, id: u32, hovered: bool, cx: &mut Context<Self>) {
|
||||
let next = match (hovered, self.hovered) {
|
||||
(true, _) => Some(id),
|
||||
(false, Some(current)) if current == id => None,
|
||||
(false, current) => current,
|
||||
};
|
||||
if next != self.hovered {
|
||||
self.hovered = next;
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
/// Moves the focus `step` cards on in the grid's order, scrolling its row into view since
|
||||
/// only the visible rows are built. At either end the key goes on, to leave the grid.
|
||||
fn move_focus(&mut self, step: isize, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let focused = |card: &Card| {
|
||||
self.focus
|
||||
.get(&card.id)
|
||||
.is_some_and(|focus| focus.handle.is_focused(window))
|
||||
};
|
||||
let current = self
|
||||
.shown
|
||||
.iter()
|
||||
.position(|&ix| self.cards.get(ix).is_some_and(focused));
|
||||
let target = current
|
||||
.and_then(|current| current.checked_add_signed(step))
|
||||
.filter(|&target| target < self.shown.len());
|
||||
let handle = target.and_then(|target| {
|
||||
let card = self.cards.get(*self.shown.get(target)?)?;
|
||||
Some((target, self.focus.get(&card.id)?.handle.clone()))
|
||||
});
|
||||
let Some((target, handle)) = handle else {
|
||||
cx.propagate();
|
||||
return;
|
||||
};
|
||||
let columns = Metrics::of(window.viewport_size().width).columns;
|
||||
self.scroll
|
||||
.scroll_to_item(target / columns, ScrollStrategy::Nearest);
|
||||
window.focus(&handle, cx);
|
||||
}
|
||||
|
||||
pub fn set_shown(&mut self, shown: Vec<usize>, cx: &mut Context<Self>) {
|
||||
if self.shown != shown {
|
||||
self.shown = shown;
|
||||
@@ -120,7 +213,13 @@ impl CardGrid {
|
||||
-self.scroll.0.borrow().base_handle.offset().y
|
||||
}
|
||||
|
||||
fn row(&mut self, ix: usize, metrics: Metrics, cx: &mut Context<Self>) -> AnyElement {
|
||||
fn row(
|
||||
&mut self,
|
||||
ix: usize,
|
||||
metrics: Metrics,
|
||||
window: &Window,
|
||||
cx: &mut Context<Self>,
|
||||
) -> AnyElement {
|
||||
let start = ix * metrics.columns;
|
||||
let row = div()
|
||||
.h_flex()
|
||||
@@ -138,7 +237,19 @@ impl CardGrid {
|
||||
{
|
||||
self.card_builds += 1;
|
||||
}
|
||||
card(data, cx).into_any_element()
|
||||
let focus = self.focus.get(&data.id).map(|focus| &focus.handle);
|
||||
// The sheet shows on the card under the pointer and on the one the
|
||||
// keyboard is on, as `:focus-visible` does.
|
||||
let sheet = self.hovered == Some(data.id)
|
||||
|| focus.is_some_and(|handle| {
|
||||
handle.is_focused(window) && window.last_input_was_keyboard()
|
||||
});
|
||||
let id = data.id;
|
||||
card(data, CardState { sheet, focus }, cx)
|
||||
.on_hover(cx.listener(move |grid, hovered: &bool, _, cx| {
|
||||
grid.set_hovered(id, *hovered, cx)
|
||||
}))
|
||||
.into_any_element()
|
||||
}
|
||||
// The last row keeps every card the same width.
|
||||
None => div().flex_1().min_w_0().into_any_element(),
|
||||
@@ -154,6 +265,9 @@ impl Render for CardGrid {
|
||||
{
|
||||
self.renders += 1;
|
||||
}
|
||||
if self.focus_stale {
|
||||
self.sync_focus(window, cx);
|
||||
}
|
||||
if self.shown.is_empty() {
|
||||
let muted = cx.theme().muted_foreground;
|
||||
return div()
|
||||
@@ -169,13 +283,18 @@ impl Render for CardGrid {
|
||||
div()
|
||||
.relative()
|
||||
.size_full()
|
||||
.key_context(CONTEXT)
|
||||
.on_action(cx.listener(|grid, _: &NextCard, window, cx| grid.move_focus(1, window, cx)))
|
||||
.on_action(
|
||||
cx.listener(|grid, _: &PreviousCard, window, cx| grid.move_focus(-1, window, cx)),
|
||||
)
|
||||
.child(
|
||||
uniform_list(
|
||||
"cards",
|
||||
rows,
|
||||
cx.processor(move |grid, range: Range<usize>, _, cx| {
|
||||
cx.processor(move |grid, range: Range<usize>, window, cx| {
|
||||
range
|
||||
.map(|ix| grid.row(ix, metrics, cx))
|
||||
.map(|ix| grid.row(ix, metrics, window, cx))
|
||||
.collect::<Vec<_>>()
|
||||
}),
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user