mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
ui-contract: a drag carries any number of extra payloads, one per type; PresetBrowser and SwatchesPanel take a host drag payload beside their own
This commit is contained in:
@@ -2,13 +2,16 @@
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft_cms::convert::CmykConverter;
|
||||
use craft_i18n::Format;
|
||||
use craft_theme::Srgba;
|
||||
use craft_theme::variant::Tone;
|
||||
use craft_ui::bars::StatusMessage;
|
||||
use craft_ui::contract::{Bind, DropAt, DropPlace, DropZone, Entry, Held, MemoProps, NoValue, Outcome, Pattern, SelectMode, ShowContainer as _};
|
||||
use craft_ui::contract::{
|
||||
Bind, DropAt, DropPlace, DropZone, Entry, Held, ListDrag, MemoProps, NoValue, Outcome, Pattern, SelectMode, ShowContainer as _,
|
||||
};
|
||||
use craft_ui::layout_components::Stack;
|
||||
use craft_ui::patterns::presets::{
|
||||
PresetBrowser, PresetDrop, PresetEvent, PresetFileEvent, PresetFooterOptions, PresetReorder, PresetRowMenu, PresetWords, ThumbFn,
|
||||
@@ -51,6 +54,7 @@ type ToolsFn<'a> = Box<dyn FnMut(&mut Ui) + 'a>;
|
||||
type RowIconsFn<'a> = Box<dyn Fn(&SwatchKey) -> Vec<(IconSpec<'static>, String)> + 'a>;
|
||||
type RowItemsFn<'a> = Box<dyn Fn(&SwatchKey) -> Vec<Entry<String>> + 'a>;
|
||||
type DropMaker<'a> = Box<dyn Fn() -> PresetDrop<'a, SwatchKey> + 'a>;
|
||||
type PayloadAdder<'a> = Box<dyn for<'b> Fn(PresetBrowser<'b, SwatchKey>) -> PresetBrowser<'b, SwatchKey> + 'a>;
|
||||
|
||||
/// The Swatches panel on the library's groups, built on `PresetBrowser`: groups as folders, swatches as flat touching
|
||||
/// squares with no gap or corner radius (Photoshop), the size the browser's zoom chooses.
|
||||
@@ -61,6 +65,8 @@ type DropMaker<'a> = Box<dyn Fn() -> PresetDrop<'a, SwatchKey> + 'a>;
|
||||
/// the canvas.
|
||||
/// - The footer's + is New Swatch: the foreground colour into the selected swatch's group, named "Color Swatch N", then
|
||||
/// renamed in place. Its Delete removes every selected swatch and group.
|
||||
/// - A dragged swatch carries the host's payloads ([`drag_payload`](SwatchesPanel::drag_payload)) to the host's drop
|
||||
/// targets (a gradient's stops, the canvas), beside the move within the panel.
|
||||
/// - Every change of the library comes back as a [`SwatchEdit`] with resolved names and positions; the host runs it.
|
||||
/// - Tooltips name the swatch and its value; AccessKit as the preset browser's grid, each swatch named.
|
||||
/// - The files of Import, Replace and Export go out as `PresetFileRequest`s from the panel menu; the host answers
|
||||
@@ -87,6 +93,7 @@ pub struct SwatchesPanel<'a> {
|
||||
row_icons: Option<RowIconsFn<'a>>,
|
||||
select: SelectMode,
|
||||
takes_drop: Option<DropMaker<'a>>,
|
||||
payloads: Vec<PayloadAdder<'a>>,
|
||||
row_items: Option<RowItemsFn<'a>>,
|
||||
}
|
||||
|
||||
@@ -116,6 +123,7 @@ impl<'a> SwatchesPanel<'a> {
|
||||
row_icons: None,
|
||||
select: SelectMode::Single,
|
||||
takes_drop: None,
|
||||
payloads: Vec::new(),
|
||||
row_items: None,
|
||||
}
|
||||
}
|
||||
@@ -190,6 +198,17 @@ impl<'a> SwatchesPanel<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// A swatch drag carries `map` of the dragged swatches too, for the host's drop targets of type `P` (the app's
|
||||
/// paint payload), as `PresetBrowser::drag_payload` does.
|
||||
pub fn drag_payload<P: Send + Sync + 'static>(mut self, map: impl Fn(&ListDrag<SwatchKey>) -> Option<P> + Send + Sync + 'static) -> Self {
|
||||
let map = Arc::new(map);
|
||||
self.payloads.push(Box::new(move |browser| {
|
||||
let map = Arc::clone(&map);
|
||||
browser.drag_payload(move |drag: &ListDrag<SwatchKey>| map(drag))
|
||||
}));
|
||||
self
|
||||
}
|
||||
|
||||
/// The host's rows of the row menu for a swatch; the standard rows give way to them.
|
||||
pub fn row_items(mut self, items: impl Fn(&SwatchKey) -> Vec<Entry<String>> + 'a) -> Self {
|
||||
self.row_items = Some(Box::new(items));
|
||||
@@ -212,6 +231,7 @@ impl<'a> SwatchesPanel<'a> {
|
||||
row_icons,
|
||||
select,
|
||||
takes_drop,
|
||||
payloads,
|
||||
row_items,
|
||||
} = self;
|
||||
let cmyk = cmyk.unwrap_or_else(|| CmykConverter::coated());
|
||||
@@ -264,6 +284,9 @@ impl<'a> SwatchesPanel<'a> {
|
||||
if let Some(make) = &takes_drop {
|
||||
browser = browser.takes_drop(make());
|
||||
}
|
||||
for add in &payloads {
|
||||
browser = add(browser);
|
||||
}
|
||||
browser.show(ui)
|
||||
};
|
||||
let mut used = None;
|
||||
|
||||
@@ -399,6 +399,61 @@ fn swatches_panel_reports_each_edit_and_leaves_the_library() {
|
||||
}
|
||||
|
||||
/// `count` entries of a colour table, each named by its index.
|
||||
/// A swatch dragged out of the panel carries the host's payload to the host's drop target (the app's paint, which a
|
||||
/// gradient's stops take), and the panel's own drag still moves a swatch onto another group.
|
||||
#[trial]
|
||||
fn a_dragged_swatch_carries_the_hosts_payload_beside_the_panels_own_move() {
|
||||
use craft_ui::contract::{ListDrag, ShowContainer as _};
|
||||
use craft_ui::display::Text;
|
||||
use craft_ui::sections::DropAction;
|
||||
use egui::{Pos2, Rect, UiBuilder};
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
struct Paint(String);
|
||||
const PANEL: Rect = Rect { min: Pos2::new(0.0, 0.0), max: Pos2::new(320.0, 300.0) };
|
||||
const TARGET: Rect = Rect { min: Pos2::new(0.0, 320.0), max: Pos2::new(320.0, 400.0) };
|
||||
let entry = |name: &str| craft_color_ui::palette::SwatchEntry::new(name.to_owned(), SwatchColor::Rgb([1.0, 0.0, 0.0]));
|
||||
let groups = vec![
|
||||
SwatchGroup { name: "Basics".to_owned(), entries: vec![entry("Red"), entry("Rose")] },
|
||||
SwatchGroup { name: "Other".to_owned(), entries: vec![entry("Blue")] },
|
||||
];
|
||||
let edits = Rc::new(RefCell::new(Vec::new()));
|
||||
let dropped = Rc::new(RefCell::new(Vec::new()));
|
||||
let (seen, taken) = (Rc::clone(&edits), Rc::clone(&dropped));
|
||||
let mut state = SwatchesState::default();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
ui.scope_builder(UiBuilder::new().max_rect(PANEL), |ui| {
|
||||
let out = SwatchesPanel::new(&groups, Bind::bound(&mut state), RED)
|
||||
.drag_payload(|drag: &ListDrag<SwatchKey>| match &drag.grabbed {
|
||||
SwatchKey::Swatch(name) => Some(Paint(name.clone())),
|
||||
_ => None,
|
||||
})
|
||||
.show(ui);
|
||||
seen.borrow_mut().extend(out.edits);
|
||||
});
|
||||
ui.scope_builder(UiBuilder::new().max_rect(TARGET), |ui| {
|
||||
let out = DropAction::<Paint>::new(|_| true).show(ui, |ui, _| {
|
||||
Text::new("Gradient").show(ui);
|
||||
});
|
||||
taken.borrow_mut().extend(out.output.dropped);
|
||||
});
|
||||
});
|
||||
fixture.room(Vec2::new(320.0, 400.0)).settle();
|
||||
let red = fixture.by_label("Red, #ff0000").expect("the Red swatch").center();
|
||||
let other = fixture.by_label("Other").expect("the Other group's header").center();
|
||||
fixture.drag_at(red, other - red);
|
||||
fixture.settle();
|
||||
let reported = edits.borrow().clone();
|
||||
assert!(matches!(&reported[..], [SwatchEdit::MoveTo { swatch, group, .. }] if swatch == "Red" && group == "Other"), "{reported:?}");
|
||||
assert!(dropped.borrow().is_empty(), "the move within the panel reaches no host target");
|
||||
|
||||
let rose = fixture.by_label("Rose, #ff0000").expect("the Rose swatch").center();
|
||||
let target = fixture.by_label("Gradient").expect("the host's drop target").center();
|
||||
fixture.drag_at(rose, target - rose);
|
||||
fixture.settle();
|
||||
assert_eq!(*dropped.borrow(), [Paint("Rose".to_owned())], "the host's target takes the swatch's paint");
|
||||
assert_eq!(edits.borrow().len(), 1, "a drop outside the panel edits nothing: {:?}", edits.borrow());
|
||||
}
|
||||
|
||||
fn table_entries(count: usize) -> Vec<(Label, Srgba)> {
|
||||
(0..count).map(|i| (Label::from(format!("Index {i}")), Srgba::rgb(i as f32 / count as f32, 0.5, 0.25))).collect()
|
||||
}
|
||||
|
||||
@@ -1044,7 +1044,7 @@ impl<'a, 't: 'a> Run<'a, 't> {
|
||||
|
||||
/// Starts the drag session of `request` under the canvas's id. A running session stays (`drag::start`).
|
||||
fn open_session(&self, request: DragRequest) {
|
||||
drag::start(self.ctx, self.canvas, &self.env.view(), request.payload, request.also, request.preview);
|
||||
drag::start(self.ctx, self.canvas, &self.env.view(), request.payload, request.also.into_iter().collect(), request.preview);
|
||||
}
|
||||
|
||||
/// A tool asked for a drag session: one per gesture. Before the gesture is stored (a request made in `press`) the
|
||||
|
||||
@@ -219,9 +219,9 @@ pub struct CollectionProps {
|
||||
pub marquee: bool,
|
||||
/// Delete and Backspace at the lead report `CollectionEvent::Delete`.
|
||||
pub deletable: bool,
|
||||
/// 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>,
|
||||
/// Map a row drag to the payloads that other drop targets take (8.4), one per type. A map starts row drags by
|
||||
/// itself, with or without a reorder rule.
|
||||
pub drag_payloads: Vec<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>,
|
||||
@@ -329,7 +329,7 @@ impl CollectionProps {
|
||||
item_keys: ItemKeys::Own,
|
||||
marquee: false,
|
||||
deletable: false,
|
||||
drag_payload: None,
|
||||
drag_payloads: Vec::new(),
|
||||
drag_count: None,
|
||||
natural_height: false,
|
||||
min_rows: 0,
|
||||
@@ -370,7 +370,14 @@ impl CollectionProps {
|
||||
/// unless a reorder rule is set. The drag stays one session with one ghost.
|
||||
#[must_use]
|
||||
pub fn drag_payload<K: 'static, P: Send + Sync + 'static>(mut self, map: impl Fn(&ListDrag<K>) -> Option<P> + Send + Sync + 'static) -> Self {
|
||||
self.drag_payload = Some(DragPayload::new(map));
|
||||
self.carry(DragPayload::new(map))
|
||||
}
|
||||
|
||||
/// A row drag carries `payload` of it too, beside the payloads given before: a host adds its own payload type to a
|
||||
/// drag the component already maps.
|
||||
#[must_use]
|
||||
pub fn carry(mut self, payload: DragPayload) -> Self {
|
||||
self.drag_payloads.push(payload);
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
@@ -46,8 +46,14 @@ pub trait Pointer {
|
||||
fn cancel_press(&mut self, reason: PressCancel);
|
||||
/// Starts the drag session with `payload`; the context holds one session at a time, and a running one stays.
|
||||
fn start_drag<P: Clone + Send + Sync + 'static>(&mut self, payload: P, preview: DragPreview);
|
||||
/// Starts the drag session with `payload`, which also carries `also`: targets of either type see the one drag.
|
||||
fn start_drag_also<P: Clone + Send + Sync + 'static>(&mut self, payload: P, also: Arc<dyn std::any::Any + Send + Sync>, preview: DragPreview);
|
||||
/// Starts the drag session with `payload`, which also carries the payloads `also`, one per type: targets of any of
|
||||
/// these types see the one drag.
|
||||
fn start_drag_also<P: Clone + Send + Sync + 'static>(
|
||||
&mut self,
|
||||
payload: P,
|
||||
also: Vec<Arc<dyn std::any::Any + Send + Sync>>,
|
||||
preview: DragPreview,
|
||||
);
|
||||
/// The active drag payload of type `P` and the pointer position in this component's egui layer space (8.3), from any
|
||||
/// viewport.
|
||||
fn dragging<P: Send + Sync + 'static>(&self) -> Option<(Arc<P>, Pos2)>;
|
||||
@@ -142,11 +148,16 @@ impl Pointer for CxCore<'_> {
|
||||
}
|
||||
|
||||
fn start_drag<P: Clone + Send + Sync + 'static>(&mut self, payload: P, preview: DragPreview) {
|
||||
drag::start(&self.ctx, self.id, &self.env, Arc::new(payload), None, preview);
|
||||
drag::start(&self.ctx, self.id, &self.env, Arc::new(payload), Vec::new(), preview);
|
||||
}
|
||||
|
||||
fn start_drag_also<P: Clone + Send + Sync + 'static>(&mut self, payload: P, also: Arc<dyn std::any::Any + Send + Sync>, preview: DragPreview) {
|
||||
drag::start(&self.ctx, self.id, &self.env, Arc::new(payload), Some(also), preview);
|
||||
fn start_drag_also<P: Clone + Send + Sync + 'static>(
|
||||
&mut self,
|
||||
payload: P,
|
||||
also: Vec<Arc<dyn std::any::Any + Send + Sync>>,
|
||||
preview: DragPreview,
|
||||
) {
|
||||
drag::start(&self.ctx, self.id, &self.env, Arc::new(payload), also, preview);
|
||||
}
|
||||
|
||||
fn dragging<P: Send + Sync + 'static>(&self) -> Option<(Arc<P>, Pos2)> {
|
||||
@@ -240,7 +251,12 @@ impl<C: Component> Pointer for Cx<'_, C> {
|
||||
self.core.start_drag(payload, preview);
|
||||
}
|
||||
|
||||
fn start_drag_also<P: Clone + Send + Sync + 'static>(&mut self, payload: P, also: Arc<dyn std::any::Any + Send + Sync>, preview: DragPreview) {
|
||||
fn start_drag_also<P: Clone + Send + Sync + 'static>(
|
||||
&mut self,
|
||||
payload: P,
|
||||
also: Vec<Arc<dyn std::any::Any + Send + Sync>>,
|
||||
preview: DragPreview,
|
||||
) {
|
||||
self.core.start_drag_also(payload, also, preview);
|
||||
}
|
||||
|
||||
|
||||
@@ -580,10 +580,13 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
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)) {
|
||||
Some(also) => cx.start_drag_also(drag, also, preview),
|
||||
None if props.reorder.is_some() => cx.start_drag(drag, preview),
|
||||
None => return,
|
||||
let also: Vec<_> = props.drag_payloads.iter().filter_map(|map| map.of(&drag)).collect();
|
||||
if !also.is_empty() {
|
||||
cx.start_drag_also(drag, also, preview);
|
||||
} else if props.reorder.is_some() {
|
||||
cx.start_drag(drag, preview);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
self.drags.grabbed = Some(key);
|
||||
}
|
||||
@@ -744,7 +747,7 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
{
|
||||
if free {
|
||||
self.free_move(cx, origin, layer(pos));
|
||||
} else if (props.reorder.is_some() || props.drag_payload.is_some()) && self.drags.grabbed.is_none() {
|
||||
} else if (props.reorder.is_some() || !props.drag_payloads.is_empty()) && self.drags.grabbed.is_none() {
|
||||
self.begin_row_drag(cx, props, at);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,8 +217,9 @@ struct Session {
|
||||
viewport: ViewportId,
|
||||
button: PointerButton,
|
||||
payload: Arc<dyn Any + Send + Sync>,
|
||||
/// A second payload of the same drag, which targets of its type take: a collection's row drag maps to it.
|
||||
also: Option<Arc<dyn Any + Send + Sync>>,
|
||||
/// More payloads of the same drag, one per type, which targets of their types take: a collection's row drag maps
|
||||
/// to them.
|
||||
also: Vec<Arc<dyn Any + Send + Sync>>,
|
||||
preview: DragPreview,
|
||||
/// The env of the region the drag started in, which its preview paints with; `None` for files from the OS.
|
||||
env: Option<Env>,
|
||||
@@ -297,7 +298,7 @@ pub fn start(
|
||||
source: Id,
|
||||
env: &EnvView<'_>,
|
||||
payload: Arc<dyn Any + Send + Sync>,
|
||||
also: Option<Arc<dyn Any + Send + Sync>>,
|
||||
also: Vec<Arc<dyn Any + Send + Sync>>,
|
||||
preview: DragPreview,
|
||||
) {
|
||||
let viewport = WindowHost::of(ctx).viewport_of(source).unwrap_or_else(|| pass::current_viewport(ctx));
|
||||
@@ -336,7 +337,7 @@ fn operation(ctx: &Context, mods: Mods) -> DropOperation {
|
||||
drop_operation(windows::os_of(ctx), mods)
|
||||
}
|
||||
|
||||
/// Changes the running session's payload of type `P` (or its second payload of that type) with `change`, in the frame
|
||||
/// Changes the running session's payload of type `P` (or its further payload of that type) with `change`, in the frame
|
||||
/// the session started only: a host completes what a drag its component started carries with what only the host knows
|
||||
/// (a dock measures the panel a tab drag floats). Returns whether a payload changed; from the next frame on, the
|
||||
/// payload is fixed and nothing changes.
|
||||
@@ -344,7 +345,7 @@ pub fn amend<P: Clone + Send + Sync + 'static>(ctx: &Context, change: impl FnOnc
|
||||
let frame = pass::root_frame_nr(ctx);
|
||||
board(ctx, |board| {
|
||||
let Some(session) = board.session.as_mut().filter(|session| session.began == frame) else { return false };
|
||||
let slot = if session.payload.is::<P>() { Some(&mut session.payload) } else { session.also.as_mut().filter(|also| also.is::<P>()) };
|
||||
let slot = if session.payload.is::<P>() { Some(&mut session.payload) } else { session.also.iter_mut().find(|also| also.is::<P>()) };
|
||||
let Some(slot) = slot else { return false };
|
||||
let Some(mut payload) = slot.downcast_ref::<P>().cloned() else { return false };
|
||||
change(&mut payload);
|
||||
@@ -359,15 +360,15 @@ pub fn active<P: Send + Sync + 'static>(ctx: &Context) -> Option<Arc<P>> {
|
||||
active_of(ctx, TypeId::of::<P>())?.downcast::<P>().ok()
|
||||
}
|
||||
|
||||
/// The active drag payload whose type is `payload`: the session's own payload, else its `also`. For a target that
|
||||
/// picks the payload types it takes at run time.
|
||||
/// The active drag payload whose type is `payload`: the session's own payload, else the one of its `also`. For a
|
||||
/// target that picks the payload types it takes at run time.
|
||||
pub(crate) fn active_of(ctx: &Context, payload: TypeId) -> Option<Arc<dyn Any + Send + Sync>> {
|
||||
crate::runtime::replay::note_input(ctx, crate::runtime::inputs::Input::Drag);
|
||||
let (own, also) = board(ctx, |board| board.session.as_ref().map(|session| (session.payload.clone(), session.also.clone())))?;
|
||||
if Any::type_id(&*own) == payload {
|
||||
return Some(own);
|
||||
}
|
||||
also.filter(|also| Any::type_id(&**also) == payload)
|
||||
also.into_iter().find(|also| Any::type_id(&**also) == payload)
|
||||
}
|
||||
|
||||
/// How many sessions have started on this context.
|
||||
@@ -379,10 +380,10 @@ pub fn started(ctx: &Context) -> u64 {
|
||||
|
||||
/// What a drag reader sees of the session, as the tracked input `Drag` compares it: the sessions started, and the
|
||||
/// payloads of the running one.
|
||||
pub(crate) fn state(ctx: &Context) -> (u64, Option<(usize, Option<usize>)>) {
|
||||
pub(crate) fn state(ctx: &Context) -> (u64, Option<(usize, Vec<usize>)>) {
|
||||
board(ctx, |board| {
|
||||
let payloads = board.session.as_ref().map(|session| {
|
||||
(Arc::as_ptr(&session.payload) as *const () as usize, session.also.as_ref().map(|also| Arc::as_ptr(also) as *const () as usize))
|
||||
(Arc::as_ptr(&session.payload) as *const () as usize, session.also.iter().map(|also| Arc::as_ptr(also) as *const () as usize).collect())
|
||||
});
|
||||
(board.started, payloads)
|
||||
})
|
||||
@@ -435,7 +436,7 @@ fn os_files(ctx: &Context) {
|
||||
viewport,
|
||||
button: PointerButton::Primary,
|
||||
payload: Arc::new(OsFiles { files: seen.hovered }),
|
||||
also: None,
|
||||
also: Vec::new(),
|
||||
preview: DragPreview::None,
|
||||
env: None,
|
||||
mark: None,
|
||||
@@ -533,7 +534,7 @@ pub fn moves(ctx: &Context, viewport: ViewportId) -> bool {
|
||||
/// of its own. For trials.
|
||||
#[cfg(feature = "testing")]
|
||||
pub(crate) fn begin_window_drag(ctx: &Context, source: Id) {
|
||||
start(ctx, source, &Env::of_root(ctx).view(), Arc::new(()), None, DragPreview::None);
|
||||
start(ctx, source, &Env::of_root(ctx).view(), Arc::new(()), Vec::new(), DragPreview::None);
|
||||
}
|
||||
|
||||
/// Sets the mark of the target that would take a drop, as one in another window would. For trials.
|
||||
|
||||
@@ -60,7 +60,7 @@ fn host_window() -> (Rc<RefCell<Host>>, impl FnMut(&mut Ui)) {
|
||||
register_stop(ui.ctx(), Stop::new(named_id("button"), KeyUse::of(Input::Pointer)));
|
||||
host.rect = fixture::host_field(ui, field(), &mut host.text);
|
||||
if std::mem::take(&mut host.start_drag) {
|
||||
drag::start(ui.ctx(), named_id("button"), &Env::of(ui).view(), Arc::new(()), None, DragPreview::None);
|
||||
drag::start(ui.ctx(), named_id("button"), &Env::of(ui).view(), Arc::new(()), Vec::new(), DragPreview::None);
|
||||
}
|
||||
if with_window_intents(ui.ctx(), false, |intents| intents.take(Intent::Cancel)) {
|
||||
host.cancels += 1;
|
||||
|
||||
@@ -21,9 +21,9 @@ use super::words::PresetWords;
|
||||
use crate::bars::{Bar, BarKind};
|
||||
use crate::choice::Segmented;
|
||||
use crate::contract::{
|
||||
Bind, BodyCx, CollectionEvent, DropAt, DropKind, DropMark, DropPayload, DropPlace, DropTarget, DropZone, Entry, ListDrag, MemoProps, MemoView,
|
||||
OpenChange, Outbox, Pattern, Props, Rename, ReorderMove, ReorderRule, RowHeight, SelectMode, Show as _, ShowCollection as _, ShowContainer as _,
|
||||
TooltipSpec, request_rename,
|
||||
Bind, BodyCx, CollectionEvent, DragPayload, DropAt, DropKind, DropMark, DropPayload, DropPlace, DropTarget, DropZone, Entry, ListDrag, MemoProps,
|
||||
MemoView, OpenChange, Outbox, Pattern, Props, Rename, ReorderMove, ReorderRule, RowHeight, SelectMode, Show as _, ShowCollection as _,
|
||||
ShowContainer as _, TooltipSpec, request_rename,
|
||||
};
|
||||
use crate::display::Divider;
|
||||
use crate::layout_components::Stack;
|
||||
@@ -353,6 +353,8 @@ pub struct PresetBrowser<'a, K: Clone + Eq + Hash + Send + Sync + MemoProps + 's
|
||||
#[prop(boxed = "Fn(&mut Ui, &K) + 'a")]
|
||||
tip: Option<TipFn<'a, K>>,
|
||||
drops: Vec<PresetDrop<'a, K>>,
|
||||
/// The host's payloads a preset drag carries beside its `ListDrag<K>`.
|
||||
payloads: Vec<DragPayload>,
|
||||
/// The search field is its scope's default stop (`Initial::Default`): a picker's popover opens with the focus in
|
||||
/// it, so typing filters the presets.
|
||||
search_first: bool,
|
||||
@@ -406,6 +408,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> PresetBrowser
|
||||
actions: Vec::new(),
|
||||
tip: None,
|
||||
drops: Vec::new(),
|
||||
payloads: Vec::new(),
|
||||
search_first: false,
|
||||
footer: None,
|
||||
max_rows: None,
|
||||
@@ -421,6 +424,13 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> PresetBrowser
|
||||
self
|
||||
}
|
||||
|
||||
/// A preset drag carries `map` of it too, for the host's drop targets of type `P` (an app's paint payload), beside
|
||||
/// the `ListDrag<K>` the browser's own targets take. Presets the map gives a payload for drag without `drags`.
|
||||
pub fn drag_payload<P: Send + Sync + 'static>(mut self, map: impl Fn(&ListDrag<K>) -> Option<P> + Send + Sync + 'static) -> Self {
|
||||
self.payloads.push(DragPayload::new(move |drag: &ListDrag<BrowserKey<K>>| map(&app_drag(drag)?)));
|
||||
self
|
||||
}
|
||||
|
||||
/// The id of the browser's list, the `ListDrag::list` of its drags: a footer action accepts only drags from its own
|
||||
/// browser.
|
||||
pub fn list_id(browser: Id) -> Id {
|
||||
@@ -723,6 +733,7 @@ impl<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> PresetBrowser<'_,
|
||||
actions,
|
||||
tip: tip_painter,
|
||||
drops,
|
||||
payloads,
|
||||
search_first,
|
||||
footer,
|
||||
max_rows,
|
||||
@@ -817,10 +828,11 @@ impl<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> PresetBrowser<'_,
|
||||
row_menu,
|
||||
drags,
|
||||
group_heads,
|
||||
host: (tip_painter.is_some() || row_items.is_some() || !drops.is_empty()).then(|| HostFns {
|
||||
host: (tip_painter.is_some() || row_items.is_some() || !drops.is_empty() || !payloads.is_empty()).then(|| HostFns {
|
||||
tip: tip_painter.as_ref(),
|
||||
row_items: row_items.as_ref(),
|
||||
drops: &drops,
|
||||
payloads: &payloads,
|
||||
}),
|
||||
empty: empty_text.clone(),
|
||||
sent: Outbox::new(&mut sent),
|
||||
@@ -888,6 +900,7 @@ struct HostFns<'p, 'a, K> {
|
||||
tip: Option<&'p TipFn<'a, K>>,
|
||||
row_items: Option<&'p RowItems<'a, K>>,
|
||||
drops: &'p [PresetDrop<'a, K>],
|
||||
payloads: &'p [DragPayload],
|
||||
}
|
||||
|
||||
/// Every input of the browser's presets view: the source, the state the toolbar left (the open groups, the search,
|
||||
@@ -963,7 +976,8 @@ fn list_view<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static>(props: &m
|
||||
(*list_id, *view, *zoom, *cell_shape, *tile_size, *columns, *max_rows, *reorder, *drop_zone, *row_menu, *select, *drags, *group_heads);
|
||||
let label = label.clone();
|
||||
let actions = props.actions;
|
||||
let (tip_painter, row_items, drops) = props.host.as_ref().map_or((None, None, &[][..]), |host| (host.tip, host.row_items, host.drops));
|
||||
let (tip_painter, row_items, drops, payloads) =
|
||||
props.host.as_ref().map_or((None, None, &[][..], &[][..]), |host| (host.tip, host.row_items, host.drops, host.payloads));
|
||||
let reveal = props.reveal.clone();
|
||||
let mut held = props.selection.clone();
|
||||
let mut menu_target = props.menu_target.clone();
|
||||
@@ -981,7 +995,7 @@ fn list_view<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static>(props: &m
|
||||
held.lead.clone().map(BrowserKey::Preset),
|
||||
held.anchor.clone().map(BrowserKey::Preset),
|
||||
);
|
||||
let payload_drag = reorder.is_some() || drop_zone.is_some() || !drops.is_empty() || drags;
|
||||
let payload_drag = reorder.is_some() || drop_zone.is_some() || !drops.is_empty() || !payloads.is_empty() || drags;
|
||||
let stamp = query_stamp(&query);
|
||||
|
||||
let can_drag = |key: &BrowserKey<K>| {
|
||||
@@ -1055,6 +1069,9 @@ fn list_view<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static>(props: &m
|
||||
if payload_drag {
|
||||
grid = grid.drag_payload(app_drag::<K>);
|
||||
}
|
||||
for payload in payloads {
|
||||
grid = grid.carry(payload.clone());
|
||||
}
|
||||
for drop in drops {
|
||||
grid = grid.takes_drop(drop.kind(Rc::clone(&map)));
|
||||
}
|
||||
@@ -1112,6 +1129,9 @@ fn list_view<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static>(props: &m
|
||||
if payload_drag {
|
||||
list = list.drag_payload(app_drag::<K>);
|
||||
}
|
||||
for payload in payloads {
|
||||
list = list.carry(payload.clone());
|
||||
}
|
||||
for drop in drops {
|
||||
list = list.takes_drop(drop.kind(Rc::clone(&map)));
|
||||
}
|
||||
|
||||
@@ -347,7 +347,7 @@ impl Component for ListRow<'_> {
|
||||
&& start.button == PointerButton::Primary
|
||||
{
|
||||
let preview = DragPreview::frame(self.thumb_size(&cx.style.sizes), None);
|
||||
crate::runtime::drag::start(&cx.ctx, cx.id, &cx.env, payload.clone(), None, preview);
|
||||
crate::runtime::drag::start(&cx.ctx, cx.id, &cx.env, payload.clone(), Vec::new(), preview);
|
||||
self.event = Some(RowEvent::PartDrag(RowDragSpot::Thumb(target)));
|
||||
} else if let Some(source) = cx.part_activated(target.part()) {
|
||||
let clicks = part_clicks(source, phase);
|
||||
@@ -369,7 +369,7 @@ impl Component for ListRow<'_> {
|
||||
&& matches!(result.press.phase, PressPhase::DragStart { start, .. } if start.button == PointerButton::Primary)
|
||||
{
|
||||
let preview = DragPreview::frame(result.rect.size(), None);
|
||||
crate::runtime::drag::start(&cx.ctx, cx.id, &cx.env, payload.clone(), None, preview);
|
||||
crate::runtime::drag::start(&cx.ctx, cx.id, &cx.env, payload.clone(), Vec::new(), preview);
|
||||
self.event = Some(RowEvent::PartDrag(RowDragSpot::Trailing(*part)));
|
||||
}
|
||||
continue;
|
||||
|
||||
@@ -7,7 +7,7 @@ use super::predicate::{DropTest, RowTest, RowText};
|
||||
use super::{ListRow, RowEvent, TreeLeaves};
|
||||
use crate::contract::{
|
||||
Access, ActivateOn, Bind, BindMut, Collection, CollectionProps, CollectionResponse, ColumnLayout, Common, Component, ContainerProps, Cx,
|
||||
DropKind, EdgePress, EditResponse, HoverLead, LeadOnOpen, ListDrag, Props as _, Role, RowState, SelectMode, Show as _, Sizing,
|
||||
DragPayload, DropKind, EdgePress, EditResponse, HoverLead, LeadOnOpen, ListDrag, Props as _, Role, RowState, SelectMode, Show as _, Sizing,
|
||||
};
|
||||
use crate::runtime::env::{Env, Label};
|
||||
use crate::style::{ComponentStyle, StyleCx};
|
||||
@@ -164,6 +164,13 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> List<'a, K> {
|
||||
self
|
||||
}
|
||||
|
||||
/// A row drag carries `payload` of it too, beside the payloads given before (`CollectionProps::carry`).
|
||||
#[must_use]
|
||||
pub fn carry(mut self, payload: DragPayload) -> Self {
|
||||
self.items = self.items.carry(payload);
|
||||
self
|
||||
}
|
||||
|
||||
/// Takes the drops of `kind` from drags that did not start in the list; kinds given earlier come first.
|
||||
#[must_use]
|
||||
pub fn takes_drop(mut self, kind: DropKind<'a, K>) -> Self {
|
||||
|
||||
@@ -14,8 +14,9 @@ use super::tile::AroundPicture;
|
||||
use super::{CaptionPlace, CardText, Tile, TilePartEvent, ZoomStep};
|
||||
use crate::contract::{
|
||||
Access, Bind, BindMut, Collection, CollectionEvent, CollectionProps, CollectionResponse, ColumnKind, ColumnSpec, Common, Component,
|
||||
ContainerProps, Cx, DropKind, EditResponse, Focus as _, ListDrag, Meta as _, NoValue, Notch, Outcome, PaintUi, PartsCx, Pointer as _, Props as _,
|
||||
Role, RowHeight, RowState, SelectMode, Show as _, Sizing, SpaceAction, SweepApply, SweepStep, ToggleColumn, section_part,
|
||||
ContainerProps, Cx, DragPayload, DropKind, EditResponse, Focus as _, ListDrag, Meta as _, NoValue, Notch, Outcome, PaintUi, PartsCx,
|
||||
Pointer as _, Props as _, Role, RowHeight, RowState, SelectMode, Show as _, Sizing, SpaceAction, SweepApply, SweepStep, ToggleColumn,
|
||||
section_part,
|
||||
};
|
||||
use crate::controls::disclosure::Disclosure;
|
||||
use crate::list::{DropTest, ListRow, ListRowStyle, RowTest, ThumbSize};
|
||||
@@ -392,6 +393,13 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> TileGrid<'a, K> {
|
||||
self
|
||||
}
|
||||
|
||||
/// A tile drag carries `payload` of it too, beside the payloads given before (`CollectionProps::carry`).
|
||||
#[must_use]
|
||||
pub fn carry(mut self, payload: DragPayload) -> Self {
|
||||
self.items = self.items.carry(payload);
|
||||
self
|
||||
}
|
||||
|
||||
/// Takes the drops of `kind` from drags that did not start in the grid; kinds given earlier come first.
|
||||
#[must_use]
|
||||
pub fn takes_drop(mut self, kind: DropKind<'a, K>) -> Self {
|
||||
|
||||
Reference in New Issue
Block a user