Drag badge text: CollectionProps::drag_count names the dragged items with a count word (PageThumbnails: "1 page", "3 pages"); a dragged section shows its title by the pointer

This commit is contained in:
aiongg
2026-11-12 14:32:41 +00:00
parent 23934cdd6f
commit fa1466efa4
7 changed files with 34 additions and 14 deletions
+1 -1
View File
@@ -304,7 +304,7 @@ impl<'a> PageThumbnails<'a> {
grid = grid.collapsible();
}
if editable {
grid = grid.reorder(ReorderRule::new()).takes_files(true).deletable(true);
grid = grid.reorder(ReorderRule::new()).drag_count(words.count.counted()).takes_files(true).deletable(true);
}
let ctx = ui.ctx().clone();
let thumbnails = Thumbnails::<usize>::in_context(&ctx, "page-thumbnails", ThumbnailOptions::new());
@@ -2,7 +2,7 @@
use super::columns::{ColumnSort, ColumnSpec};
use super::events::ListDrag;
use crate::runtime::env::Label;
use crate::runtime::env::{Counted, Label};
use craft_behavior::{Axis, ColumnKey};
use std::any::Any;
use std::sync::Arc;
@@ -182,6 +182,9 @@ pub struct CollectionProps {
/// Maps a row drag to the payload that other drop targets take (8.4). A map starts row drags by itself, with or
/// without a reorder rule.
pub drag_payload: Option<DragPayload>,
/// The count word the drag badge shows for the dragged items, one item included ("1 page", "3 pages"); `None`
/// shows the number of items for several and the item's text for one.
pub drag_count: Option<Counted>,
/// The collection takes the full height of its rows and owns no scroll, so it can sit inside a scrolling page:
/// every row is built, the wheel and the keys that move past an end act on the page's scroll owner, and moving
/// the lead scrolls the page to keep the row in view. For short collections only; a long one keeps its own
@@ -268,6 +271,7 @@ impl CollectionProps {
marquee: false,
deletable: false,
drag_payload: None,
drag_count: None,
natural_height: false,
overflow: Axis::Y,
pages: false,
@@ -297,6 +301,13 @@ impl CollectionProps {
self
}
/// The drag badge reads `count` for the number of dragged items.
#[must_use]
pub const fn drag_count(mut self, count: Counted) -> Self {
self.drag_count = Some(count);
self
}
/// The items are pages (`CollectionProps::pages`).
#[must_use]
pub const fn pages(mut self, on: bool) -> Self {
@@ -368,10 +368,11 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
let id = cx.core.id;
let key = self.source.key(at);
let keys: Vec<C::Key> = if self.holds(at) { self.selected_keys(&self.selection) } else { vec![key.clone()] };
let preview = if keys.len() > 1 {
DragPreview::Badge(Label::from(keys.len().to_string()))
} else {
self.source.text(at).map_or(DragPreview::None, |text| DragPreview::Badge(Label::from(text.into_owned())))
let count = keys.len() as u64;
let preview = match props.drag_count {
Some(word) => DragPreview::Badge(Label::from(cx.env.counted(word, count))),
None if count > 1 => DragPreview::Badge(Label::from(count.to_string())),
None => self.source.text(at).map_or(DragPreview::None, |text| DragPreview::Badge(Label::from(text.into_owned()))),
};
let drag = ListDrag::new(id, keys, key.clone());
match props.drag_payload.as_ref().and_then(|map| map.of(&drag)) {
+6 -1
View File
@@ -8,7 +8,7 @@ use super::{ItemFacts, Scoped};
use crate::runtime::commands::CommandChain;
use craft_host::Host;
use craft_host::target::ShellInfo;
use craft_i18n::{AgeStyle, Direction, WordSet};
use craft_i18n::{AgeStyle, Counted, Direction, WordSet};
use craft_i18n::{CalendarDate, DateFormat, Format};
use craft_theme::Mode;
use egui::Id;
@@ -135,6 +135,11 @@ impl<'a> EnvView<'a> {
craft_i18n::relative_age(&self.scoped.words, &self.scoped.format, seconds, style)
}
/// `n` things by the count word `count`, in the scope's language and digits: "1 page", "3 pages".
pub fn counted(&self, count: Counted, n: u64) -> String {
self.scoped.words.counted(count, n, &self.scoped.format.integer(n))
}
/// `label` with a `Word` replaced by its text in the scope's language: what domain code that draws a `Label`
/// itself shows.
pub fn resolve(&self, label: &Label) -> Label {
+1
View File
@@ -187,6 +187,7 @@ const COLLECTION: &[Forwarded] = &[
("item_keys", "::craft_ui::contract::ItemKeys", "Who takes the arrow keys: the collection (moves the lead) or the component around it."),
("marquee", "bool", "A drag on the background selects the rows it covers."),
("deletable", "bool", "Delete and Backspace at the lead report the items to delete."),
("drag_count", "::craft_ui::runtime::env::Counted", "The drag badge reads this count word for the dragged items, one included."),
("natural_height", "bool", "The collection takes its full content height and owns no scroll: for a short collection inside a scrolling page."),
(
"current",
+4 -3
View File
@@ -35,8 +35,8 @@ pub struct SectionOutput {
pub response: Response,
/// The header action part taken this pass.
pub action: Option<HeaderAction>,
/// The header band was press-dragged past the drag threshold this pass.
pub grabbed: bool,
/// The header band was press-dragged past the drag threshold this pass: the section's title, for the drag badge.
pub grabbed: Option<Label>,
/// A stacking key was pressed with focus in the section.
pub nudged: Option<reorder::ItemMove>,
}
@@ -196,7 +196,8 @@ impl Component for Section<'_> {
let grabbed = cx
.core
.part_if_declared(REORDER_BAND)
.is_some_and(|band| matches!(band.press.phase, PressPhase::DragStart { start, .. } if start.button == PointerButton::Primary));
.is_some_and(|band| matches!(band.press.phase, PressPhase::DragStart { start, .. } if start.button == PointerButton::Primary))
.then(|| cx.label());
SectionOutput { response: cx.response.clone(), action: self.taken, grabbed, nudged: self.nudged }
}
}
+5 -4
View File
@@ -9,6 +9,7 @@ use crate::contract::{
ReorderMove, Role, Sizing,
};
use crate::runtime::access::SetPosition;
use crate::runtime::env::Label;
use crate::style::{ComponentStyle, StyleCx};
use craft_behavior::{Axis, EditEvent, Metrics, Mixed, Sense};
use egui::{Pos2, Rect, Response, Ui};
@@ -67,7 +68,8 @@ impl<K> Outcome for SectionStackOutput<K> {
struct Seen<K> {
key: K,
rect: Rect,
grabbed: bool,
/// The title of a section whose header was grabbed this pass.
grabbed: Option<Label>,
nudged: Option<reorder::ItemMove>,
}
@@ -131,9 +133,8 @@ impl<'a, K: Clone + PartialEq + std::hash::Hash + Send + Sync + 'static> Section
/// Starts a drag from a grabbed header, takes a drop over the sections and turns a nudge into a move.
fn drive(&mut self, seen: &[Seen<K>], cx: &mut Cx<'_, Self>) {
let id = cx.core.id;
if let Some(grabbed) = seen.iter().find(|section| section.grabbed) {
let key = grabbed.key.clone();
cx.start_drag(ListDrag::new(id, vec![key.clone()], key), DragPreview::None);
if let Some((key, title)) = seen.iter().find_map(|section| section.grabbed.clone().map(|title| (section.key.clone(), title))) {
cx.start_drag(ListDrag::new(id, vec![key.clone()], key), DragPreview::Badge(title));
}
if let Some((payload, at)) = cx.dragging::<ListDrag<K>>()
&& payload.list == id