- Draw the section's grid again once the draw that moved it is over: a notify inside a draw dirties nothing for the next one, so its rows stayed those of the former place. - Keep the first row of a grid wholly below the window built, so that Tab from the search enters it, and scroll the page to any card the keyboard focuses. - Leave the scrollbar its own lane in the Anvil and « Autres champions » sections, so it no longer covers the sort selector, and make it a view of its own so that its fade does not draw the whole page. - Store the page's Paging once; correct the performance notes on scrolling. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
113 lines
4.7 KiB
Rust
113 lines
4.7 KiB
Rust
//! The grid's keyboard: each card has a focus handle, and Tab and Shift-Tab move between the
|
|
//! cards, bringing the row of the one reached into view.
|
|
|
|
use gpui_kit::*;
|
|
|
|
use super::geometry::Metrics;
|
|
use super::{CardGrid, RevealRow};
|
|
use crate::card::Card;
|
|
|
|
actions!(card_grid, [NextCard, PreviousCard]);
|
|
|
|
/// The grid's key context, where Tab and Shift-Tab move between its cards.
|
|
pub(super) 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)),
|
|
]);
|
|
}
|
|
|
|
/// What makes one card reachable with the keyboard: its handle, and the subscriptions that lift
|
|
/// it while the keyboard is on it.
|
|
pub(super) struct CardFocus {
|
|
pub(super) handle: FocusHandle,
|
|
_subscriptions: [Subscription; 2],
|
|
}
|
|
|
|
impl CardGrid {
|
|
/// 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.
|
|
pub(super) 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 id = card.id;
|
|
let focus = previous.remove(&id).unwrap_or_else(|| {
|
|
let handle = cx.focus_handle().tab_stop(true);
|
|
// As `:focus-visible`: a card focused by a click is already under the pointer.
|
|
let gained = cx.on_focus_in(&handle, window, move |grid, window, cx| {
|
|
if window.last_input_was_keyboard() {
|
|
grid.hover_by(Some(id), true, cx);
|
|
// Tab came from outside the grid too: the page is to show the card.
|
|
let position = grid.position_of(id);
|
|
grid.reveal_in_page(position, window, cx);
|
|
}
|
|
});
|
|
let lost = cx.on_focus_out(&handle, window, move |grid, _, _, cx| {
|
|
grid.set_hovered(id, false, cx);
|
|
});
|
|
CardFocus {
|
|
handle,
|
|
_subscriptions: [gained, lost],
|
|
}
|
|
});
|
|
self.focus.insert(id, focus);
|
|
}
|
|
}
|
|
|
|
/// Moves the focus `step` cards on in the grid's order, scrolling its row into view since
|
|
/// only the visible rows are built (a paged grid asks the page to). At either end the key
|
|
/// goes on, to leave the grid.
|
|
pub(super) 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;
|
|
};
|
|
if self.paged.is_some() {
|
|
self.reveal_in_page(Some(target), window, cx);
|
|
} else if !self.fit_content {
|
|
let columns = Metrics::of(window.viewport_size().width).columns;
|
|
self.scroll
|
|
.scroll_to_item(target / columns, ScrollStrategy::Nearest);
|
|
}
|
|
window.focus(&handle, cx);
|
|
}
|
|
|
|
/// Where the card with this id is among the cards shown.
|
|
fn position_of(&self, id: u32) -> Option<usize> {
|
|
let is_it = |&ix: &usize| self.cards.get(ix).is_some_and(|card| card.id == id);
|
|
self.shown.iter().position(is_it)
|
|
}
|
|
|
|
/// A paged grid asks the page to scroll to the row of the card at this position.
|
|
fn reveal_in_page(&mut self, position: Option<usize>, window: &Window, cx: &mut Context<Self>) {
|
|
let (Some(position), true) = (position, self.paged.is_some()) else {
|
|
return;
|
|
};
|
|
let metrics = Metrics::of(window.viewport_size().width);
|
|
let top = px(metrics.row_height() * (position / metrics.columns) as f32);
|
|
let bottom = top + px(metrics.card_height);
|
|
cx.emit(RevealRow { top, bottom });
|
|
}
|
|
}
|