diff --git a/crates/behavior/src/press.rs b/crates/behavior/src/press.rs index 0618978be..9dbdd00ae 100644 --- a/crates/behavior/src/press.rs +++ b/crates/behavior/src/press.rs @@ -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. diff --git a/crates/ui-contract/src/contract/collection/props.rs b/crates/ui-contract/src/contract/collection/props.rs index 98dd2380a..c754c0b9c 100644 --- a/crates/ui-contract/src/contract/collection/props.rs +++ b/crates/ui-contract/src/contract/collection/props.rs @@ -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, + /// 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, /// 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) -> Self { diff --git a/crates/ui-contract/src/contract/items/drag.rs b/crates/ui-contract/src/contract/items/drag.rs index 3aa5cd2d4..e97ffd581 100644 --- a/crates/ui-contract/src/contract/items/drag.rs +++ b/crates/ui-contract/src/contract/items/drag.rs @@ -161,11 +161,26 @@ pub(super) struct Drags { pub(super) activate: Option, /// The marquee a press on empty space started. pub(super) marquee: Option>, + /// A plain press on the only selected item of a `slow_click_rename` collection, waiting to become a rename. + pub(super) slow: Option>, +} + +/// 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 { + 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, } impl Default for Drags { 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 } } } diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index 64a680f94..7337f43ba 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -579,6 +579,7 @@ impl ShapeHooks 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); diff --git a/crates/ui-contract/src/contract/items/press.rs b/crates/ui-contract/src/contract/items/press.rs index c65efea63..4ffd7f9fd 100644 --- a/crates/ui-contract/src/contract/items/press.rs +++ b/crates/ui-contract/src/contract/items/press.rs @@ -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 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 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 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 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 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 diff --git a/crates/ui-macros/src/props.rs b/crates/ui-macros/src/props.rs index a67adb9b0..ceb3bae8e 100644 --- a/crates/ui-macros/src/props.rs +++ b/crates/ui-macros/src/props.rs @@ -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", diff --git a/crates/ui-widgets/src/tile_grid/grid.rs b/crates/ui-widgets/src/tile_grid/grid.rs index 5b0409555..a823838d1 100644 --- a/crates/ui-widgets/src/tile_grid/grid.rs +++ b/crates/ui-widgets/src/tile_grid/grid.rs @@ -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 { /// 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], /// 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