mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:24:24 +00:00
TileGrid keys: Cmd/Ctrl+digit ratings, Cmd/Ctrl + = / - / 0 zoom steps, and a slow click on the caption renames; the story turns Bridge's keys on
This commit is contained in:
@@ -312,7 +312,7 @@ impl Default for PressState {
|
||||
}
|
||||
|
||||
/// The longest gap between presses that still count as one multi-click, in seconds.
|
||||
const MULTI_CLICK_TIME: f64 = 0.3;
|
||||
pub const MULTI_CLICK_TIME: f64 = 0.3;
|
||||
|
||||
/// The click count of a press at `pos` and `time`, given the previous press `(pos, time, clicks)`: one more when
|
||||
/// it follows within [`MULTI_CLICK_TIME`] and `slop` of the previous one, else 1.
|
||||
|
||||
@@ -129,6 +129,9 @@ pub struct CollectionProps {
|
||||
pub rename: bool,
|
||||
/// A double click on this column of a renamable item opens its rename editor instead of activating it.
|
||||
pub rename_column: Option<ColumnKey>,
|
||||
/// A plain click on the label of the only selected item, with no second click within the double-click interval,
|
||||
/// opens its rename editor (Finder and Bridge on a tile's caption).
|
||||
pub slow_click_rename: bool,
|
||||
/// Empty: no columns and no header row.
|
||||
pub columns: Vec<ColumnSpec>,
|
||||
/// The sorted column and direction; `None` while the rows are unsorted.
|
||||
@@ -243,6 +246,7 @@ impl CollectionProps {
|
||||
reorder: None,
|
||||
rename: false,
|
||||
rename_column: None,
|
||||
slow_click_rename: false,
|
||||
columns: Vec::new(),
|
||||
sort: None,
|
||||
empty: None,
|
||||
@@ -315,6 +319,13 @@ impl CollectionProps {
|
||||
self
|
||||
}
|
||||
|
||||
/// A slow second click on the label of the only selected item opens its rename editor.
|
||||
#[must_use]
|
||||
pub const fn slow_click_rename(mut self, on: bool) -> Self {
|
||||
self.slow_click_rename = on;
|
||||
self
|
||||
}
|
||||
|
||||
/// The columns.
|
||||
#[must_use]
|
||||
pub fn columns(mut self, columns: Vec<ColumnSpec>) -> Self {
|
||||
|
||||
@@ -161,11 +161,26 @@ pub(super) struct Drags<K> {
|
||||
pub(super) activate: Option<K>,
|
||||
/// The marquee a press on empty space started.
|
||||
pub(super) marquee: Option<MarqueeDrag<K>>,
|
||||
/// A plain press on the only selected item of a `slow_click_rename` collection, waiting to become a rename.
|
||||
pub(super) slow: Option<SlowClick<K>>,
|
||||
}
|
||||
|
||||
/// A press that opens a rename when it ends as a click on the item's label and no second press follows within the
|
||||
/// double-click interval.
|
||||
#[derive(Clone)]
|
||||
pub(super) struct SlowClick<K> {
|
||||
pub(super) key: K,
|
||||
/// Where the press landed, in the rows' layer space.
|
||||
pub(super) at: Pos2,
|
||||
/// The pass's item showed its label under the press.
|
||||
pub(super) on_label: bool,
|
||||
/// When the press ended as a click.
|
||||
pub(super) released: Option<f64>,
|
||||
}
|
||||
|
||||
impl<K> Default for Drags<K> {
|
||||
fn default() -> Self {
|
||||
Drags { grabbed: None, sweep: None, moving: None, over: None, collapse: None, rename: None, activate: None, marquee: None }
|
||||
Drags { grabbed: None, sweep: None, moving: None, over: None, collapse: None, rename: None, activate: None, marquee: None, slow: None }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -579,6 +579,7 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
// After the row is shown: its toggle cells have recorded their names by then.
|
||||
self.report_toggle_cells(&ctx, (id, item_id), (at, &key), (shown_cells.as_slice(), cx.core.resolved.enabled), &cx.core.env);
|
||||
let label = out.label_rect().unwrap_or(row);
|
||||
self.slow_click_label(&key, label);
|
||||
crate::runtime::keys::redirect_node(&ctx, item_id, id, ITEM_NODE.at(key_hash(&key)));
|
||||
if let Some(row) = shown_rows.last_mut() {
|
||||
row.openers = self.item_openers(&row_ui, &ctx, id, cx.core.resolved.enabled, openers);
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
//! Presses on rows: select, activate, ask for a row's menu, and the parts an item opens menus from.
|
||||
|
||||
use super::drag::SlowClick;
|
||||
use super::{ItemsHooks, Placement, RowPress, SECTION};
|
||||
use crate::contract::collection::{ActivateOn, CollectionEvent, CollectionProps, ContextMenuRequest, LeadSource, SelectMode};
|
||||
use crate::contract::{Collection, Cx, ItemOpener};
|
||||
use crate::runtime::egui::response;
|
||||
use crate::runtime::env::ArenaId;
|
||||
use crate::runtime::focus;
|
||||
use crate::runtime::frame;
|
||||
use crate::runtime::memory::sub;
|
||||
use crate::runtime::motion::Animate as _;
|
||||
use craft_behavior::{Flow, ItemLayout, PressPhase, SelectionChange, Sense};
|
||||
use egui::{Context, Id, Pos2, Rect, Ui, Vec2};
|
||||
|
||||
@@ -53,6 +56,8 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
if let PressPhase::Down { start, .. } = cx.core.press.phase
|
||||
&& local(start.origin).y >= 0.0
|
||||
{
|
||||
// A second press, slow or not, ends the wait of the first.
|
||||
self.drags.slow = None;
|
||||
let primary = start.button == craft_behavior::PointerButton::Primary;
|
||||
if let Some(at) = layout.hit(local(start.origin), offset).filter(|&at| self.source.enabled(at) && layout.shown(at)) {
|
||||
let on_toggle = self.toggle_cell(props, layout, at, offset, local(start.origin));
|
||||
@@ -74,6 +79,18 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
_ => false,
|
||||
};
|
||||
let hold = primary && !mods.shift && !mods.cmd && holds && self.holds(at);
|
||||
let slow = primary
|
||||
&& start.clicks == 1
|
||||
&& mods == craft_keymap::Mods::NONE
|
||||
&& props.slow_click_rename
|
||||
&& props.rename
|
||||
&& !self.source.section_stop(at)
|
||||
&& comp.can_rename(&key)
|
||||
&& self.selected_keys(&self.selection) == std::slice::from_ref(&key);
|
||||
if slow {
|
||||
let point = xf.map_or(start.origin, |map| map.to_layer(start.origin));
|
||||
self.drags.slow = Some(SlowClick { key: key.clone(), at: point, on_label: false, released: None });
|
||||
}
|
||||
// A secondary press on a row of the selection leaves the selection whole: its menu acts on all of it.
|
||||
let keep = !primary && matches!(Self::select_mode(comp), SelectMode::Multi) && self.holds(at);
|
||||
match column {
|
||||
@@ -149,6 +166,10 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
let request = self.menu_request(layout, Placement { rect, offset, rtl }, row, local(pos));
|
||||
self.events.push(CollectionEvent::ContextMenu(request));
|
||||
}
|
||||
match self.drags.slow.as_mut() {
|
||||
Some(slow) if click && slow.on_label => slow.released = Some(frame::clock(&ctx)),
|
||||
_ => self.drags.slow = None,
|
||||
}
|
||||
if let Some(key) = self.drags.rename.take()
|
||||
&& click
|
||||
&& let Some(at) = self.index_of(&key)
|
||||
@@ -173,7 +194,9 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
}
|
||||
}
|
||||
}
|
||||
PressPhase::DragStart { .. } => self.drags.slow = None,
|
||||
PressPhase::Cancel { .. } => {
|
||||
self.drags.slow = None;
|
||||
self.drags.collapse = None;
|
||||
self.drags.rename = None;
|
||||
self.drags.activate = None;
|
||||
@@ -204,6 +227,32 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
let request = self.menu_request(layout, Placement { rect, offset, rtl }, at, local(pos));
|
||||
self.events.push(CollectionEvent::ContextMenu(request));
|
||||
}
|
||||
self.slow_click_due(cx);
|
||||
}
|
||||
|
||||
/// The rename a slow click waits for opens once the double-click interval has passed with no second press and the
|
||||
/// item still the only one selected; until then the pass asks to run again at that time.
|
||||
fn slow_click_due(&mut self, cx: &mut Cx<'_, C>) {
|
||||
let Some((key, released)) = self.drags.slow.as_ref().and_then(|slow| Some((slow.key.clone(), slow.released?))) else { return };
|
||||
let at = self.index_of(&key).filter(|_| self.selected_keys(&self.selection) == std::slice::from_ref(&key));
|
||||
let Some(at) = at else {
|
||||
self.drags.slow = None;
|
||||
return;
|
||||
};
|
||||
let due = released + craft_behavior::press::MULTI_CLICK_TIME;
|
||||
if cx.now() >= due {
|
||||
self.drags.slow = None;
|
||||
self.begin_rename(cx, at);
|
||||
} else {
|
||||
cx.wake_at(due);
|
||||
}
|
||||
}
|
||||
|
||||
/// Where the item `key` shows its label this pass: a slow click waiting on it counts only when it landed there.
|
||||
pub(super) fn slow_click_label(&mut self, key: &C::Key, label: Rect) {
|
||||
if let Some(slow) = self.drags.slow.as_mut().filter(|slow| slow.key == *key && slow.released.is_none()) {
|
||||
slow.on_label = label.contains(slow.at);
|
||||
}
|
||||
}
|
||||
|
||||
/// The request for the menu of the row `at`, opened by a pointer at `point` (the layout's coordinates): at the
|
||||
|
||||
@@ -151,13 +151,14 @@ type Forwarded = (&'static str, &'static str, &'static str);
|
||||
/// here.
|
||||
const COLLECTION: &[Forwarded] = &[
|
||||
("select", "::craft_ui::contract::SelectMode", "What the user can select."),
|
||||
("reorder", "::craft_ui::contract::ReorderRule", "Rows reorder by dragging and by Alt with the arrow keys."),
|
||||
("reorder", "::craft_ui::contract::ReorderRule", "Rows reorder by dragging and by the stacking keys (Cmd/Ctrl+[ and ])."),
|
||||
("rename", "bool", "Rows rename in place: a double click on the label, F2 or the rename command opens the editor."),
|
||||
(
|
||||
"rename_column",
|
||||
"::craft_ui::contract::ColumnKey",
|
||||
"A double click on this column of a renamable row opens its rename editor; elsewhere on the row it activates.",
|
||||
),
|
||||
("slow_click_rename", "bool", "A plain click on the label of the only selected row, with no second click following, opens its rename editor."),
|
||||
("columns", "::std::vec::Vec<::craft_ui::contract::ColumnSpec>", "The columns."),
|
||||
(
|
||||
"sort",
|
||||
|
||||
@@ -11,7 +11,7 @@ mod stories;
|
||||
use super::headers::Outline;
|
||||
use super::layout::{Around, ArrangeSpacing, GridLayout, Recipe, Slot, TailRow, TileGroup, Tiling, arrange, aspects_of, slot_at};
|
||||
use super::tile::AroundPicture;
|
||||
use super::{CaptionPlace, Tile, TilePartEvent};
|
||||
use super::{CaptionPlace, Tile, TilePartEvent, ZoomStep};
|
||||
use crate::contract::{
|
||||
Access, Bind, BindMut, Collection, CollectionEvent, CollectionProps, CollectionResponse, ColumnKind, ColumnSpec, Common, Component,
|
||||
ContainerProps, Cx, EditResponse, Focus as _, ListDrag, NoValue, Outcome, PaintUi, PartsCx, Pointer as _, Props as _, Role, RowState, SelectMode,
|
||||
@@ -28,7 +28,7 @@ use crate::style::{ComponentStyle, RuleKind, StyleColor, StyleCx};
|
||||
use craft_behavior::{
|
||||
ColumnKey, ColumnWidth, HitLevel, HitPriority, Intent, ItemSource, Metrics, Mixed, PartKey, PointerButton, PressPhase, Selection, Sense,
|
||||
};
|
||||
use craft_keymap::Mods;
|
||||
use craft_keymap::{Chord, Key, Mods};
|
||||
use egui::{CornerRadius, Id, Pos2, Rect, Response, Ui, pos2, vec2};
|
||||
use std::cell::RefCell;
|
||||
use std::hash::Hash;
|
||||
@@ -52,6 +52,8 @@ pub enum RatingKeys {
|
||||
None,
|
||||
/// The digits 1 to 5 rate and 0 clears. Other typed text still moves by type-ahead.
|
||||
Digits,
|
||||
/// Cmd/Ctrl with 1 to 5 rates and with 0 clears (Bridge).
|
||||
CommandDigits,
|
||||
}
|
||||
|
||||
/// What the grid reports besides the collection's events.
|
||||
@@ -83,6 +85,9 @@ pub enum GridEvent<K> {
|
||||
/// The state asked for.
|
||||
open: bool,
|
||||
},
|
||||
/// Cmd/Ctrl with `=` or `+`, `-` or `0` on a grid that `zooms`: a step of the tile size. Cmd/Ctrl+0 is a rating key
|
||||
/// under `RatingKeys::CommandDigits`, so it clears the rating there instead of resetting the size.
|
||||
Zoom(ZoomStep),
|
||||
/// A group header asks for its context menu: a secondary click, the context-menu key or Shift+F10.
|
||||
#[non_exhaustive]
|
||||
GroupMenu {
|
||||
@@ -169,7 +174,7 @@ impl TileGridStyle {
|
||||
}
|
||||
|
||||
/// A grid of tiles: one Tab stop, the selection rules of the collection contract, type-ahead on the tiles' names (with
|
||||
/// `type_ahead`), rename in place, drag to reorder, files dropped from the OS, and the request for the app's context
|
||||
/// `type_ahead`), rename in place (F2, or a slow second click on the caption of the only selected tile), drag to reorder, files dropped from the OS, and the request for the app's context
|
||||
/// menu.
|
||||
///
|
||||
/// The picture side is `zoom` times the thumbnail side (the card thumbnail unless `thumb_size` names one): the factor the app's zoom control holds. A card is the
|
||||
@@ -234,6 +239,10 @@ pub struct TileGrid<'a, K: Clone + Eq + Hash + Send + Sync + 'static> {
|
||||
tail: &'a [TailRow<K>],
|
||||
/// Which keys rate the selected tiles.
|
||||
rating_keys: RatingKeys,
|
||||
/// The grid reports zoom steps: Cmd/Ctrl with `=`, `-` and `0` as `GridEvent::Zoom`, and Ctrl/Cmd with the wheel or a
|
||||
/// pinch as `CollectionEvent::Zoom`.
|
||||
#[prop(flag)]
|
||||
zooms: bool,
|
||||
/// Tiles show a check box and Space toggles the lead tile's check.
|
||||
checks: bool,
|
||||
/// Which tiles may be renamed; a rename request on another tile does nothing.
|
||||
@@ -272,12 +281,14 @@ struct Placed {
|
||||
}
|
||||
|
||||
impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> TileGrid<'a, K> {
|
||||
/// A grid named `label`, with multiple selection and equal square cards of the card thumbnail.
|
||||
/// A grid named `label`, with multiple selection and equal square cards of the card thumbnail. Space picks the lead and
|
||||
/// no key rates or zooms; Bridge's keys are `rating_keys(RatingKeys::CommandDigits)`, `space(SpaceAction::Preview)`
|
||||
/// and `zooms()`.
|
||||
pub fn new(label: impl Into<Label>) -> Self {
|
||||
TileGrid {
|
||||
common: Common::new(),
|
||||
props: ContainerProps::new().sizing(Sizing::Fill),
|
||||
items: CollectionProps::new().select(SelectMode::Multi),
|
||||
items: CollectionProps::new().select(SelectMode::Multi).slow_click_rename(true),
|
||||
selection: None,
|
||||
reveal: None,
|
||||
justified: false,
|
||||
@@ -293,6 +304,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> TileGrid<'a, K> {
|
||||
collapsible: false,
|
||||
tail: &[],
|
||||
rating_keys: RatingKeys::None,
|
||||
zooms: false,
|
||||
checks: false,
|
||||
can_rename: None,
|
||||
can_drag: None,
|
||||
@@ -494,14 +506,38 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Component for TileGrid<'_, K>
|
||||
}
|
||||
|
||||
fn update(&mut self, cx: &mut Cx<'_, Self>) {
|
||||
if self.rating_keys == RatingKeys::Digits {
|
||||
for rating in 0..=MAX_RATING {
|
||||
while cx.intents().take_exact(&Intent::Digit(rating)) {
|
||||
let keys = self.rated(cx);
|
||||
if !keys.is_empty() {
|
||||
self.events.push(GridEvent::Rate { keys, rating });
|
||||
for rating in 0..=MAX_RATING {
|
||||
let mut pressed = 0;
|
||||
match self.rating_keys {
|
||||
RatingKeys::None => {}
|
||||
RatingKeys::Digits => {
|
||||
while cx.intents().take_exact(&Intent::Digit(rating)) {
|
||||
pressed += 1;
|
||||
}
|
||||
}
|
||||
RatingKeys::CommandDigits => {
|
||||
let chord = Chord::new(Mods::CMD, DIGITS[usize::from(rating)]);
|
||||
cx.claim(chord);
|
||||
while cx.take_claimed(chord) {
|
||||
pressed += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
for _ in 0..pressed {
|
||||
let keys = self.rated(cx);
|
||||
if !keys.is_empty() {
|
||||
self.events.push(GridEvent::Rate { keys, rating });
|
||||
}
|
||||
}
|
||||
}
|
||||
if self.zooms {
|
||||
let reset = (self.rating_keys != RatingKeys::CommandDigits).then_some((Key::Num0, ZoomStep::Reset));
|
||||
for (key, step) in [(Key::Equals, ZoomStep::In), (Key::Plus, ZoomStep::In), (Key::Minus, ZoomStep::Out)].into_iter().chain(reset) {
|
||||
let chord = Chord::new(Mods::CMD, key);
|
||||
cx.claim(chord);
|
||||
while cx.take_claimed(chord) {
|
||||
self.events.push(GridEvent::Zoom(step));
|
||||
}
|
||||
}
|
||||
}
|
||||
let placed = self.placed.clone();
|
||||
@@ -668,6 +704,8 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Component for TileGrid<'_, K>
|
||||
|
||||
/// The highest rating a digit key sets.
|
||||
const MAX_RATING: u8 = 5;
|
||||
/// The keys of the digits 0 to `MAX_RATING`.
|
||||
const DIGITS: [Key; 6] = [Key::Num0, Key::Num1, Key::Num2, Key::Num3, Key::Num4, Key::Num5];
|
||||
|
||||
impl<K: Clone + Eq + Hash + Send + Sync + 'static> Collection for TileGrid<'_, K> {
|
||||
type Key = K;
|
||||
@@ -688,6 +726,10 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Collection for TileGrid<'_, K
|
||||
self.reveal.as_ref()
|
||||
}
|
||||
|
||||
fn zooms(&self) -> bool {
|
||||
self.zooms
|
||||
}
|
||||
|
||||
/// The tiles with a header row before each group, then the tail rows: the rows the item step walks and the layout
|
||||
/// places.
|
||||
fn filtered<'s>(&self, source: &'s dyn ItemSource<K>, _: &crate::runtime::env::Env) -> Option<Box<dyn ItemSource<K> + 's>> {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//! Stories of `TileGrid`.
|
||||
|
||||
use super::{GridEvent, TileGrid, TileGridOutput, TileGroup};
|
||||
use crate::contract::ShowCollection as _;
|
||||
use super::{GridEvent, RatingKeys, TileGrid, TileGridOutput, TileGroup};
|
||||
use crate::contract::{ShowCollection as _, SpaceAction};
|
||||
use crate::list::ThumbAspect;
|
||||
use crate::testing::pictures::StoryPictures;
|
||||
use crate::testing::{Drive, StoryData, StoryOutput, story};
|
||||
use crate::tile_grid::{BadgeKind, BadgeSlot, Tile, TileBadge, TileFlag};
|
||||
use crate::tile_grid::{BadgeKind, BadgeSlot, Tile, TileBadge, TileFlag, TileZoom};
|
||||
use craft_behavior::{DisplayOrder, ItemSource, SizeHint};
|
||||
use craft_theme::Srgba;
|
||||
use craft_theme::variant::Tone;
|
||||
@@ -98,32 +98,42 @@ impl ItemSource<usize> for Photos {
|
||||
}
|
||||
}
|
||||
|
||||
fn grid(ui: &mut Ui, data: &mut StoryData, grid: TileGrid<'_, usize>, count: usize) -> TileGridOutput<usize> {
|
||||
/// The grid with Bridge's keys (Cmd/Ctrl+digit ratings, Space previews, Cmd/Ctrl + = / - / 0 zoom) at a zoom kept in the
|
||||
/// story's data, starting at `zoom`.
|
||||
fn grid(ui: &mut Ui, data: &mut StoryData, grid: TileGrid<'_, usize>, count: usize, zoom: f64) -> TileGridOutput<usize> {
|
||||
let pictures = StoryPictures::of(ui.ctx());
|
||||
let zoom = data.value("zoom", zoom);
|
||||
let grid = grid.rating_keys(RatingKeys::CommandDigits).space(SpaceAction::Preview).zooms().zoom(*zoom as f32);
|
||||
let mut ratings = Ratings::of(data, count);
|
||||
let out = grid.show_items(ui, &Photos(count), &mut |key, _| photo(&pictures, *key, ratings.0[*key]));
|
||||
ratings.apply(&out.events);
|
||||
let zoom = data.value("zoom", 1.0);
|
||||
for event in &out.events {
|
||||
if let GridEvent::Zoom(step) = event {
|
||||
*zoom = TileZoom::Ladder.step(*zoom, *step);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[story(TileGrid<'static, usize>, covers = [Default, Many], drive = Drive::Never)]
|
||||
fn tile_grid_many(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
grid(ui, data, TileGrid::new("Library").empty("No photos"), 60).into()
|
||||
grid(ui, data, TileGrid::new("Library").empty("No photos"), 60, 1.0).into()
|
||||
}
|
||||
|
||||
#[story(TileGrid<'static, usize>, covers = [One], drive = Drive::Never)]
|
||||
fn tile_grid_one(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
grid(ui, data, TileGrid::new("Library").empty("No photos"), 1).into()
|
||||
grid(ui, data, TileGrid::new("Library").empty("No photos"), 1, 1.0).into()
|
||||
}
|
||||
|
||||
#[story(TileGrid<'static, usize>, covers = [Empty], drive = Drive::Never)]
|
||||
fn tile_grid_empty(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
grid(ui, data, TileGrid::new("Library").empty("No photos"), 0).into()
|
||||
grid(ui, data, TileGrid::new("Library").empty("No photos"), 0, 1.0).into()
|
||||
}
|
||||
|
||||
#[story(TileGrid<'static, usize>, covers = [], drive = Drive::Never)]
|
||||
fn tile_grid_justified(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
grid(ui, data, TileGrid::new("Library").justified(), 40).into()
|
||||
grid(ui, data, TileGrid::new("Library").justified(), 40, 1.0).into()
|
||||
}
|
||||
|
||||
#[story(TileGrid<'static, usize>, covers = [], drive = Drive::Never)]
|
||||
@@ -134,7 +144,7 @@ fn tile_grid_groups(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
TileGroup::new(GROUP_KEYS[1], 12, 8, "September 2026").detail("8 photos").open(open[1]),
|
||||
TileGroup::new(GROUP_KEYS[2], 20, 10, "August 2026").detail("10 photos").open(open[2]),
|
||||
];
|
||||
let out = grid(ui, data, TileGrid::new("Library").groups(&groups).collapsible().zoom(2.0), 30);
|
||||
let out = grid(ui, data, TileGrid::new("Library").groups(&groups).collapsible(), 30, 2.0);
|
||||
let open = data.value("open", open);
|
||||
for event in &out.events {
|
||||
if let GridEvent::GroupOpen { group, open: wanted } = event {
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
//! The tile grid's own keys and gestures: Bridge's rating and zoom keys, and the slow click that renames.
|
||||
|
||||
use craft_behavior::{EditEvent, keyed};
|
||||
use craft_ui::contract::{CollectionEvent, ShowCollection as _};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use craft_ui::tile_grid::{GridEvent, RatingKeys, Tile, TileGrid, ZoomStep};
|
||||
use egui::{Key, Modifiers, Vec2};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
const NAMES: [&str; 4] = ["Tile 1", "Tile 2", "Tile 3", "Tile 4"];
|
||||
|
||||
/// What the grid reported, kept across frames.
|
||||
#[derive(Default)]
|
||||
struct Seen {
|
||||
grid: Vec<GridEvent<usize>>,
|
||||
collection: Vec<CollectionEvent<usize, craft_ui::tile_grid::TilePartEvent>>,
|
||||
}
|
||||
|
||||
fn rig(make: fn() -> TileGrid<'static, usize>) -> (Fixture, Rc<RefCell<Seen>>) {
|
||||
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 = make().show_items(ui, &source, &mut |key, _| Tile::new(NAMES[*key]));
|
||||
let mut seen = held.borrow_mut();
|
||||
seen.grid.extend(out.events);
|
||||
seen.collection.extend(out.response.events);
|
||||
});
|
||||
fixture.room(Vec2::new(600.0, 400.0));
|
||||
fixture.settle();
|
||||
(fixture, seen)
|
||||
}
|
||||
|
||||
/// With Bridge's keys, Cmd/Ctrl with a digit rates the selected tile and with 0 clears it; Cmd/Ctrl with `=` and `-`
|
||||
/// step the zoom. Without the rating keys, Cmd/Ctrl+0 resets the zoom.
|
||||
#[trial]
|
||||
fn command_digits_rate_and_command_equals_and_minus_zoom() {
|
||||
let (mut fixture, seen) = rig(|| TileGrid::new("Library").rating_keys(RatingKeys::CommandDigits).zooms());
|
||||
fixture.key(Key::Tab, Modifiers::NONE).idle(2);
|
||||
fixture.key(Key::ArrowRight, Modifiers::NONE).idle(2);
|
||||
seen.borrow_mut().grid.clear();
|
||||
for key in [Key::Num3, Key::Num0, Key::Equals, Key::Minus] {
|
||||
fixture.key(key, Modifiers::COMMAND).idle(1);
|
||||
}
|
||||
let reported: Vec<String> = std::mem::take(&mut seen.borrow_mut().grid)
|
||||
.into_iter()
|
||||
.map(|event| match event {
|
||||
GridEvent::Rate { keys, rating, .. } => format!("rate {keys:?} {rating}"),
|
||||
GridEvent::Zoom(step) => format!("zoom {step:?}"),
|
||||
other => format!("{other:?}"),
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(reported, ["rate [0] 3", "rate [0] 0", "zoom In", "zoom Out"], "the selected tile is rated, then cleared; the zoom steps in and out");
|
||||
|
||||
let (mut fixture, seen) = rig(|| TileGrid::new("Library").zooms());
|
||||
fixture.key(Key::Tab, Modifiers::NONE).idle(2);
|
||||
fixture.key(Key::Num0, Modifiers::COMMAND).idle(1);
|
||||
assert!(
|
||||
matches!(seen.borrow().grid.as_slice(), [GridEvent::Zoom(ZoomStep::Reset)]),
|
||||
"without rating keys Cmd/Ctrl+0 resets the zoom: {:?}",
|
||||
seen.borrow().grid
|
||||
);
|
||||
}
|
||||
|
||||
/// A click on the caption of the only selected tile opens its rename once the double-click interval passes; a double
|
||||
/// click there activates the tile and opens no rename.
|
||||
#[trial]
|
||||
fn a_slow_click_on_the_caption_renames_and_a_double_click_activates() {
|
||||
let (mut fixture, seen) = rig(|| TileGrid::new("Library").rename(true));
|
||||
let tile = fixture.by_label("Tile 2").expect("the tile has a node");
|
||||
// The caption band runs under the picture, inside the card's bottom inset.
|
||||
let caption = tile.rect.center_bottom() - Vec2::new(0.0, 10.0);
|
||||
let renames = |seen: &Rc<RefCell<Seen>>| {
|
||||
seen.borrow().collection.iter().filter(|event| matches!(event, CollectionEvent::Rename(EditEvent::Begin { .. }))).count()
|
||||
};
|
||||
fixture.click_at(caption);
|
||||
fixture.advance(1.0).idle(1);
|
||||
fixture.click_at(caption);
|
||||
assert_eq!(renames(&seen), 0, "the rename waits for the double-click interval");
|
||||
fixture.advance(1.0).idle(2);
|
||||
assert_eq!(renames(&seen), 1, "a slow second click on the caption renames the tile");
|
||||
|
||||
let (mut fixture, seen) = rig(|| TileGrid::new("Library").rename(true));
|
||||
fixture.click_at(caption);
|
||||
fixture.advance(1.0).idle(1);
|
||||
fixture.click_at(caption).click_at(caption);
|
||||
fixture.advance(1.0).idle(2);
|
||||
let activated = seen.borrow().collection.iter().any(|event| matches!(event, CollectionEvent::Activate(1)));
|
||||
assert!(activated && renames(&seen) == 0, "a double click activates and opens no rename");
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
//! The tile grid family's own logic.
|
||||
|
||||
mod grid;
|
||||
mod keys;
|
||||
mod tile;
|
||||
|
||||
Reference in New Issue
Block a user