mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 04:27:49 +00:00
Framework fwb9-b: port follow-ups (see runs/fwb9-b.md)
This commit is contained in:
@@ -223,12 +223,14 @@ pub struct Shape<O> {
|
||||
/// The named stacking order: stacked parts of one priority rank by it before the nearest centre, the higher order
|
||||
/// winning.
|
||||
pub stacking: u32,
|
||||
/// Whether a passed drag targets the arena root instead of the next shape.
|
||||
pub drag_to_root: bool,
|
||||
}
|
||||
|
||||
impl<O> Shape<O> {
|
||||
/// A shape of `owner` at stack `position`.
|
||||
pub const fn new(shape: HitShape, sense: Sense, owner: O, part: Option<PartRef>, priority: HitPriority, position: u32) -> Self {
|
||||
Shape { shape, sense, owner, part, priority, position, local: false, min_across: 0.0, stacking: 0 }
|
||||
Shape { shape, sense, owner, part, priority, position, local: false, min_across: 0.0, stacking: 0, drag_to_root: false }
|
||||
}
|
||||
|
||||
/// The hit area is at least `across` points wide and high.
|
||||
@@ -280,6 +282,8 @@ pub struct Winner<O> {
|
||||
pub sense: Sense,
|
||||
/// Its shape lies in the body layer's space.
|
||||
pub local: bool,
|
||||
/// A passed drag targets the arena root.
|
||||
pub drag_to_root: bool,
|
||||
}
|
||||
|
||||
/// The winner among `shapes` at `point`: stack position first, then level, sub-level, stacking order, the nearest
|
||||
@@ -303,7 +307,7 @@ pub fn resolve_mapped<O: Copy>(shapes: &[Shape<O>], point: Pos2, what: ResolveFo
|
||||
.then_with(|| distance(b).partial_cmp(&distance(a)).unwrap_or(Ordering::Equal))
|
||||
.then(ia.cmp(ib))
|
||||
})
|
||||
.map(|(index, shape)| Winner { index, owner: shape.owner, part: shape.part, sense: shape.sense, local: shape.local })
|
||||
.map(|(index, shape)| Winner { index, owner: shape.owner, part: shape.part, sense: shape.sense, local: shape.local, drag_to_root: shape.drag_to_root })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -1288,10 +1288,10 @@ impl<'a, 't: 'a> Run<'a, 't> {
|
||||
/// The keys the canvas holds: intents routed to it while it has focus, the arrows, Enter and Escape while nothing
|
||||
/// in the window has focus, and the chords the tool claimed at the end of the last pass. A session control in the
|
||||
/// canvas body (`KeyUse::session`) has taken the arrows, Enter and Escape it acts on before this runs. The Escape
|
||||
/// of the focused canvas reaches the tool once, through the window's intents (`with_window_intents` offers the
|
||||
/// of the focused canvas reaches the tool once, through the window's intents (`with_canvas_window_intents` offers the
|
||||
/// focused document canvas's Escape); one the tool leaves stays the canvas's, for the scopes around it.
|
||||
fn keys(&mut self, ctx: &Context, canvas: Id, rtl: bool) {
|
||||
keys::with_window_intents(ctx, rtl, |window| {
|
||||
keys::with_canvas_window_intents(ctx, rtl, |window| {
|
||||
let mut taken = self.offer(window, |intents| intents.take_nudge().map(|(direction, mods)| ToolKey::Nudge { direction, mods }));
|
||||
taken |= self.offer(window, |intents| intents.take(Intent::Submit).then_some(ToolKey::Submit));
|
||||
if !self.cancelled {
|
||||
|
||||
@@ -96,6 +96,13 @@ pub(crate) struct PartExtras {
|
||||
}
|
||||
|
||||
impl PartBuilder<'_> {
|
||||
/// Send a passed drag to the enclosing arena root, retaining clicks on this part.
|
||||
/// Declare `CLICK | PASSES_DRAG` as the part's sense.
|
||||
pub fn drag_to_root(self) -> Self {
|
||||
self.decl.drag_to_root = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// What the part senses; hover only by default.
|
||||
pub fn sense(self, sense: Sense) -> Self {
|
||||
self.decl.sense = sense;
|
||||
|
||||
@@ -131,6 +131,7 @@ pub(crate) struct PartDecl {
|
||||
pub held: bool,
|
||||
/// The named stacking order among parts of one priority (default 0).
|
||||
pub stacking: u32,
|
||||
pub drag_to_root: bool,
|
||||
}
|
||||
|
||||
impl PartDecl {
|
||||
@@ -148,6 +149,7 @@ impl PartDecl {
|
||||
inactive: false,
|
||||
held: false,
|
||||
stacking: 0,
|
||||
drag_to_root: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -318,7 +320,9 @@ fn rotate(state: &mut ArenaState, pass_nr: u64) {
|
||||
}
|
||||
|
||||
fn own_part_shape(part: &PartDecl, root: Id, position: u32, target: f32) -> Shape<Id> {
|
||||
Shape::new(part.shape.clone(), part.sense, root, Some(part.part_ref()), part.priority, position).stacking(part.stacking).min_across(target)
|
||||
let mut shape = Shape::new(part.shape.clone(), part.sense, root, Some(part.part_ref()), part.priority, position).stacking(part.stacking).min_across(target);
|
||||
shape.drag_to_root = part.drag_to_root;
|
||||
shape
|
||||
}
|
||||
|
||||
/// The root's own parts but the `over` ones, at position 0.
|
||||
@@ -343,10 +347,12 @@ fn over_position(count: usize) -> u32 {
|
||||
}
|
||||
|
||||
fn member_part_shape(member: &MemberDecl, part: &PartDecl, position: u32, target: f32) -> Shape<Id> {
|
||||
Shape::new(part.shape.clone(), part.sense, member.id, Some(part.part_ref()), part.priority, position)
|
||||
let mut shape = Shape::new(part.shape.clone(), part.sense, member.id, Some(part.part_ref()), part.priority, position)
|
||||
.stacking(part.stacking)
|
||||
.local(member.local)
|
||||
.min_across(target)
|
||||
.min_across(target);
|
||||
shape.drag_to_root = part.drag_to_root;
|
||||
shape
|
||||
}
|
||||
|
||||
/// The member's root shape and its parts but the `over` ones, at its stack position.
|
||||
|
||||
@@ -37,7 +37,7 @@ pub(crate) use intents::lent_taken;
|
||||
pub use intents::settle;
|
||||
#[doc(hidden)]
|
||||
pub use intents::take_submit;
|
||||
pub use intents::with_window_intents;
|
||||
pub use intents::{with_canvas_window_intents, with_window_intents};
|
||||
pub(crate) use intents::{
|
||||
Lent, context_part, context_request, give_back, lend, request_context, route_whole_reset, scope_escape, take_cancel, take_every_escape,
|
||||
take_window_cancel,
|
||||
|
||||
@@ -266,38 +266,43 @@ pub fn intents_of(ctx: &Context, id: Option<Id>, rtl: bool) -> Intents {
|
||||
}
|
||||
}
|
||||
|
||||
/// Offers the window's intents to `take` while nothing in the window has framework focus or the focused stop is the
|
||||
/// window's document canvas (`KeyUse::chrome_key`) or one of its canvas controls (`focus::is_canvas_member`), and
|
||||
/// writes back what it left untaken. `take` returns whether it took any. `false` when another stop is focused or
|
||||
/// nothing was taken.
|
||||
/// Offers the window's intents and the focused framework stop's unused Escape to `take`, and writes back what it
|
||||
/// left untaken. `take` returns whether it took any.
|
||||
///
|
||||
/// The window's intents are the keys the runtime routes to no component this pass: an Escape (`Intent::Cancel`)
|
||||
/// or a plain Enter (`Intent::Submit`) while no framework stop is focused, and the arrows' nudge; with the document
|
||||
/// canvas or a canvas control focused, the Escape that stop, the canvas's sessions, its tool and every scope around
|
||||
/// it left. A component or a
|
||||
/// or a plain Enter (`Intent::Submit`) while no framework stop is focused, and the arrows' nudge; with a framework
|
||||
/// stop focused, its unused Escape. A component or a
|
||||
/// scope that runs earlier in the pass takes them first (an open dialog takes the Escape that closes it, a canvas
|
||||
/// session the one that ends it), so an app that hosts the runtime reads them last in its UI to see what no
|
||||
/// component took. `rtl` reads horizontal moves in the window's direction.
|
||||
pub fn with_window_intents(ctx: &Context, rtl: bool, take: impl FnOnce(&mut Intents) -> bool) -> bool {
|
||||
let canvas = match focus::focused_rec(ctx) {
|
||||
Some(rec) if rec.stop.keys.chrome_key || focus::is_canvas_member(ctx, &rec) => Some(rec.stop.id),
|
||||
Some(_) => return false,
|
||||
None => None,
|
||||
};
|
||||
offer_window_intents(ctx, rtl, take)
|
||||
}
|
||||
|
||||
/// Offers window intents to a canvas while the canvas or one of its controls has focus, or nothing has focus.
|
||||
/// Other focused controls get the opportunity to use their Escape before the window reads it.
|
||||
pub fn with_canvas_window_intents(ctx: &Context, rtl: bool, take: impl FnOnce(&mut Intents) -> bool) -> bool {
|
||||
if focus::focused_rec(ctx).is_some_and(|rec| !rec.stop.keys.chrome_key && !focus::is_canvas_member(ctx, &rec)) {
|
||||
return false;
|
||||
}
|
||||
offer_window_intents(ctx, rtl, take)
|
||||
}
|
||||
|
||||
fn offer_window_intents(ctx: &Context, rtl: bool, take: impl FnOnce(&mut Intents) -> bool) -> bool {
|
||||
let focused = focus::focused_rec(ctx).map(|rec| rec.stop.id);
|
||||
let mut window = intents_of(ctx, None, rtl);
|
||||
// The focused canvas's or canvas control's leftover Escape is offered as the window's; it stays in that stop's
|
||||
// slot until taken.
|
||||
let lent_cancel = canvas
|
||||
.filter(|canvas| !window.items.iter().any(|(item, _)| *item == Intent::Cancel) && holds(ctx, Some(*canvas), &Intent::Cancel))
|
||||
.map(|canvas| {
|
||||
// The focused stop's leftover Escape stays in its slot until taken.
|
||||
let lent_cancel = focused
|
||||
.filter(|id| !window.items.iter().any(|(item, _)| *item == Intent::Cancel) && holds(ctx, Some(*id), &Intent::Cancel))
|
||||
.map(|id| {
|
||||
window.items.push((Intent::Cancel, false));
|
||||
(canvas, window.items.len() - 1)
|
||||
(id, window.items.len() - 1)
|
||||
});
|
||||
let taken = take(&mut window);
|
||||
if let Some((canvas, at)) = lent_cancel {
|
||||
if let Some((id, at)) = lent_cancel {
|
||||
let (_, cancel_taken) = window.items.remove(at);
|
||||
if cancel_taken && !crate::runtime::measure::active(ctx) {
|
||||
current(ctx, |stage| take_from(stage, Some(canvas), &Intent::Cancel));
|
||||
current(ctx, |stage| take_from(stage, Some(id), &Intent::Cancel));
|
||||
}
|
||||
}
|
||||
if taken {
|
||||
@@ -403,7 +408,7 @@ pub(crate) fn lent_taken(ctx: &Context) -> u64 {
|
||||
|
||||
/// Takes the window's untaken `Cancel` (Escape) while nothing has focus.
|
||||
pub(crate) fn take_window_cancel(ctx: &Context) -> bool {
|
||||
with_window_intents(ctx, false, |window| window.take(Intent::Cancel))
|
||||
with_canvas_window_intents(ctx, false, |window| window.take(Intent::Cancel))
|
||||
}
|
||||
|
||||
/// Writes back what a component left untaken, so a scope's `Submit` and `Cancel` rules see it.
|
||||
|
||||
@@ -788,7 +788,11 @@ fn find_under(ctx: &Context, viewport: ViewportId, at: Pos2, targets: PressTarge
|
||||
let dragging: Vec<usize> = pressable
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|&index| index != winner.index && entry.shapes[index].sense.contains(craft_behavior::Sense::DRAG))
|
||||
.filter(|&index| {
|
||||
let shape = &entry.shapes[index];
|
||||
index != winner.index && shape.sense.contains(craft_behavior::Sense::DRAG)
|
||||
&& (!winner.drag_to_root || (shape.owner == root && shape.part.is_none()))
|
||||
})
|
||||
.collect();
|
||||
pick(&dragging, ResolveFor::Press)
|
||||
})
|
||||
@@ -839,7 +843,8 @@ fn winners_of(mut ranked: Vec<((u8, u32), Winner<Id>, Id, bool)>) -> PressWinner
|
||||
// shape that passes its drag, the next shape that drags takes it, in the same root or one below.
|
||||
(true, false) => {
|
||||
let passes = winners.click.is_some_and(|(top, _)| top.sense.contains(craft_behavior::Sense::PASSES_DRAG));
|
||||
if drags && (passes || !(clicks || root_clicks)) {
|
||||
let root_target = winners.click.is_some_and(|(top, owner)| top.drag_to_root && (root != owner || winner.part.is_some()));
|
||||
if drags && !root_target && (passes || !(clicks || root_clicks)) {
|
||||
winners.drag = Some((winner, root));
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -46,6 +46,37 @@ fn press_key(frames: &mut Frames, show: &mut impl FnMut(&mut Ui), key: Key) {
|
||||
frames.frame(vec![key_press(key, Modifiers::NONE)], &mut *show);
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn escape_unused_by_a_focused_control_reaches_the_window() {
|
||||
let (host, mut show) = host_window();
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(2, &mut show);
|
||||
request(&frames.ctx, FocusTarget::Id(named_id("button")));
|
||||
frames.idle(2, &mut show);
|
||||
press_key(&mut frames, &mut show, Key::Escape);
|
||||
assert_eq!(seen(&host).1, 1, "the focused button leaves Escape for the window");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_canvas_cannot_take_escape_before_an_unrelated_focused_control() {
|
||||
let (host, mut base) = host_window();
|
||||
let stolen = Rc::new(std::cell::Cell::new(false));
|
||||
let canvas_seen = Rc::clone(&stolen);
|
||||
let mut show = move |ui: &mut Ui| {
|
||||
if crate::runtime::keys::with_canvas_window_intents(ui.ctx(), false, |intents| intents.take(Intent::Cancel)) {
|
||||
canvas_seen.set(true);
|
||||
}
|
||||
base(ui);
|
||||
};
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(2, &mut show);
|
||||
request(&frames.ctx, FocusTarget::Id(named_id("button")));
|
||||
frames.idle(2, &mut show);
|
||||
press_key(&mut frames, &mut show, Key::Escape);
|
||||
assert!(!stolen.get());
|
||||
assert_eq!(seen(&host).1, 1);
|
||||
}
|
||||
|
||||
/// A window with a framework button and the host's own egui text field, with what the host saw.
|
||||
fn host_window() -> (Rc<RefCell<Host>>, impl FnMut(&mut Ui)) {
|
||||
let host = Rc::new(RefCell::new(Host { text: String::new(), rect: Rect::NOTHING, reached: Vec::new(), cancels: 0, start_drag: false }));
|
||||
|
||||
@@ -23,12 +23,13 @@ pub struct ProbePart {
|
||||
priority: HitPriority,
|
||||
over: bool,
|
||||
cursor: Option<CursorIcon>,
|
||||
drag_to_root: bool,
|
||||
}
|
||||
|
||||
impl ProbePart {
|
||||
/// A part `key` over `rect` sensing `sense`.
|
||||
pub fn new(key: PartKey, rect: Rect, sense: Sense) -> Self {
|
||||
ProbePart { key, index: None, rect, sense, priority: HitPriority::BODY, over: false, cursor: None }
|
||||
ProbePart { key, index: None, rect, sense, priority: HitPriority::BODY, over: false, cursor: None, drag_to_root: false }
|
||||
}
|
||||
|
||||
/// The part's tier among the shapes of its stack position.
|
||||
@@ -37,6 +38,12 @@ impl ProbePart {
|
||||
self
|
||||
}
|
||||
|
||||
/// Send a passed drag to the arena root.
|
||||
pub fn drag_to_root(mut self) -> Self {
|
||||
self.drag_to_root = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// The part sits above the body members.
|
||||
pub fn over(mut self) -> Self {
|
||||
self.over = true;
|
||||
@@ -61,6 +68,7 @@ impl ProbePart {
|
||||
decl.priority = self.priority;
|
||||
decl.over = self.over;
|
||||
decl.cursor = self.cursor.map(Cursor::System);
|
||||
decl.drag_to_root = self.drag_to_root;
|
||||
decl
|
||||
}
|
||||
}
|
||||
|
||||
@@ -501,6 +501,25 @@ fn one_pointer_resolution_per_pass_whatever_the_number_of_roots() {
|
||||
const LINK: PartKey = PartKey::new("link");
|
||||
const SURFACE: PartKey = PartKey::new("surface");
|
||||
|
||||
/// A clickable overlay can send its drag to the canvas root past a draggable field.
|
||||
#[trial]
|
||||
fn passed_drag_can_target_the_arena_root_past_a_field() {
|
||||
let page = rect(100.0, 100.0, 400.0, 300.0);
|
||||
let area = rect(150.0, 150.0, 60.0, 40.0);
|
||||
let field = ProbePart::new(SURFACE, area, Sense::CLICK | Sense::DRAG);
|
||||
let comment = ProbePart::new(LINK, area, Sense::CLICK | Sense::PASSES_DRAG).level(HitLevel::Edge, 0).drag_to_root();
|
||||
let root = ProbeRoot::new("canvas", page, Sense::DRAG).arena().member(ProbeMember::new("parts", page, Sense::empty()).part(field).part(comment));
|
||||
let (mut show_frame, log) = scene(vec![root]);
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(2, &mut show_frame);
|
||||
drag(&mut frames, area.center(), Vec2::new(40.0, 30.0), &mut show_frame);
|
||||
assert_eq!(drags(&log.borrow().root_phases(0, None)), 1, "canvas takes the drag");
|
||||
assert_eq!(drags(&log.borrow().member_phases(0, "parts", Some(SURFACE))), 0, "field does not drag");
|
||||
log.borrow_mut().clear();
|
||||
pointer::click(&mut frames, area.center(), &mut show_frame);
|
||||
assert!(log.borrow().member_phases(0, "parts", Some(LINK)).iter().any(|phase| matches!(phase, PressPhase::Release { click: true, .. })));
|
||||
}
|
||||
|
||||
/// L-19: a link on a page clicks, but a drag that starts on it belongs to the page under it (production's
|
||||
/// `Hit::declines_drag`). A press past slop on a click part that passes its drag gives the `DragStart` to the next
|
||||
/// shape below that drags, whether that shape is the same root's own part or a root shown below; the link sees no
|
||||
|
||||
@@ -58,13 +58,13 @@ pub enum ToolbarEdit<T> {
|
||||
/// Where it goes.
|
||||
at: DropAt,
|
||||
},
|
||||
/// Take the tool off the toolbar.
|
||||
/// Take every occurrence of the tool off the toolbar. Use `RemoveFrom` to remove one occurrence.
|
||||
#[non_exhaustive]
|
||||
Remove {
|
||||
/// The tool.
|
||||
tool: T,
|
||||
},
|
||||
/// Move a tool that is on the toolbar to `at`.
|
||||
/// Move a tool by id to `at`, removing its other occurrences. Use `MoveFrom` to move one occurrence.
|
||||
#[non_exhaustive]
|
||||
Move {
|
||||
/// The tool.
|
||||
@@ -72,6 +72,22 @@ pub enum ToolbarEdit<T> {
|
||||
/// Where it goes.
|
||||
at: DropAt,
|
||||
},
|
||||
/// Remove one occurrence, identified by its slot and index.
|
||||
RemoveFrom {
|
||||
/// The source slot.
|
||||
slot: SlotId,
|
||||
/// The index in the source slot.
|
||||
index: usize,
|
||||
},
|
||||
/// Move one occurrence, identified by its slot and index.
|
||||
MoveFrom {
|
||||
/// The source slot.
|
||||
slot: SlotId,
|
||||
/// The index in the source slot.
|
||||
index: usize,
|
||||
/// The destination.
|
||||
at: DropAt,
|
||||
},
|
||||
/// Go back to the default toolbar.
|
||||
Reset,
|
||||
}
|
||||
@@ -91,6 +107,16 @@ impl<T> ToolbarEdit<T> {
|
||||
pub fn move_to(tool: T, at: DropAt) -> Self {
|
||||
ToolbarEdit::Move { tool, at }
|
||||
}
|
||||
|
||||
/// Remove the occurrence at `index` in `slot`.
|
||||
pub fn remove_from(slot: SlotId, index: usize) -> Self {
|
||||
ToolbarEdit::RemoveFrom { slot, index }
|
||||
}
|
||||
|
||||
/// Move the occurrence at `index` in `slot` to `at`.
|
||||
pub fn move_from(slot: SlotId, index: usize, at: DropAt) -> Self {
|
||||
ToolbarEdit::MoveFrom { slot, index, at }
|
||||
}
|
||||
}
|
||||
|
||||
/// What the drawer reports.
|
||||
@@ -240,9 +266,9 @@ fn match_texts<T>(command: &ResolvedCommand, tool: &RailTool<T>) -> Vec<Label> {
|
||||
texts
|
||||
}
|
||||
|
||||
/// Whether the toolbar holds the tool `id`.
|
||||
/// Whether the toolbar already has a separate slot for `id`.
|
||||
fn on_toolbar<T: PartialEq>(toolbar: &[ToolSlot<T>], id: &T) -> bool {
|
||||
toolbar.iter().any(|slot| slot.tools.iter().any(|tool| tool.id == *id))
|
||||
toolbar.iter().any(|slot| slot.tools.len() == 1 && slot.tools.iter().any(|tool| tool.id == *id))
|
||||
}
|
||||
|
||||
impl<T: Value + 'static> ToolbarDrawer<'_, T> {
|
||||
@@ -354,9 +380,9 @@ impl<T: Value + 'static> ToolbarDrawer<'_, T> {
|
||||
});
|
||||
});
|
||||
if let Some(ToolDrag::Rail { slot: from, index }) = slot.output.dropped
|
||||
&& let Some(tool) = toolbar.iter().find(|candidate| candidate.id == from).and_then(|candidate| candidate.tools.get(index))
|
||||
&& toolbar.iter().find(|candidate| candidate.id == from).and_then(|candidate| candidate.tools.get(index)).is_some()
|
||||
{
|
||||
edits.push(ToolbarEdit::Remove { tool: tool.id.clone() });
|
||||
edits.push(ToolbarEdit::RemoveFrom { slot: from, index });
|
||||
}
|
||||
});
|
||||
if restore {
|
||||
@@ -376,11 +402,11 @@ pub fn drop_edit<T: Value>(drag: ToolDrag, at: DropAt, groups: &[DrawerGroup<T>]
|
||||
Some(ToolbarEdit::Add { tool: tool.clone(), at })
|
||||
}
|
||||
ToolDrag::Rail { slot, index } => {
|
||||
let tool = toolbar.iter().find(|candidate| candidate.id == slot)?.tools.get(index)?;
|
||||
toolbar.iter().find(|candidate| candidate.id == slot)?.tools.get(index)?;
|
||||
if matches!(at, DropAt::Slot { slot: onto, .. } if onto == slot) {
|
||||
return None;
|
||||
}
|
||||
Some(ToolbarEdit::Move { tool: tool.id.clone(), at })
|
||||
Some(ToolbarEdit::MoveFrom { slot, index, at })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -389,7 +415,7 @@ pub fn drop_edit<T: Value>(drag: ToolDrag, at: DropAt, groups: &[DrawerGroup<T>]
|
||||
pub fn apply_toolbar_edit<T: Clone + PartialEq>(slots: &[ToolSlot<T>], edit: &ToolbarEdit<T>, defaults: &[ToolSlot<T>]) -> Vec<ToolSlot<T>> {
|
||||
match edit {
|
||||
ToolbarEdit::Reset => defaults.to_vec(),
|
||||
ToolbarEdit::Add { tool, at } => place(slots, tool.clone(), *at),
|
||||
ToolbarEdit::Add { tool, at } => insert(slots.to_vec(), tool.clone(), *at),
|
||||
ToolbarEdit::Move { tool, at } => match slots.iter().flat_map(|slot| &slot.tools).find(|candidate| candidate.id == *tool) {
|
||||
Some(found) => place(slots, found.clone(), *at),
|
||||
None => slots.to_vec(),
|
||||
@@ -402,10 +428,42 @@ pub fn apply_toolbar_edit<T: Clone + PartialEq>(slots: &[ToolSlot<T>], edit: &To
|
||||
out.retain(|slot| !slot.tools.is_empty());
|
||||
out
|
||||
}
|
||||
ToolbarEdit::RemoveFrom { slot, index } => {
|
||||
let mut out = slots.to_vec();
|
||||
let Some(source) = out.iter_mut().find(|candidate| candidate.id == *slot) else { return out };
|
||||
if *index < source.tools.len() {
|
||||
source.tools.remove(*index);
|
||||
}
|
||||
out.retain(|slot| !slot.tools.is_empty());
|
||||
out
|
||||
}
|
||||
ToolbarEdit::MoveFrom { slot, index, at } => {
|
||||
if matches!(at, DropAt::Slot { slot: target, .. } if !slots.iter().any(|candidate| candidate.id == *target))
|
||||
|| matches!(at, DropAt::NewSlot { .. }) && slots.iter().any(|candidate| candidate.id == SlotId::MAX)
|
||||
{
|
||||
return slots.to_vec();
|
||||
}
|
||||
let mut out = slots.to_vec();
|
||||
let Some(source) = out.iter_mut().find(|candidate| candidate.id == *slot) else { return out };
|
||||
if *index >= source.tools.len() {
|
||||
return out;
|
||||
}
|
||||
let tool = source.tools.remove(*index);
|
||||
let destination = match *at {
|
||||
DropAt::Slot { slot: target, index: to } if target == *slot && *index < to => DropAt::Slot { slot: target, index: to - 1 },
|
||||
other => other,
|
||||
};
|
||||
insert(out, tool, destination)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn place<T: Clone + PartialEq>(slots: &[ToolSlot<T>], tool: RailTool<T>, at: DropAt) -> Vec<ToolSlot<T>> {
|
||||
if matches!(at, DropAt::Slot { slot, .. } if !slots.iter().any(|candidate| candidate.id == slot))
|
||||
|| matches!(at, DropAt::NewSlot { .. }) && slots.iter().any(|candidate| candidate.id == SlotId::MAX)
|
||||
{
|
||||
return slots.to_vec();
|
||||
}
|
||||
let mut out = slots.to_vec();
|
||||
let mut was: Option<(SlotId, usize)> = None;
|
||||
for slot in &mut out {
|
||||
@@ -414,17 +472,31 @@ fn place<T: Clone + PartialEq>(slots: &[ToolSlot<T>], tool: RailTool<T>, at: Dro
|
||||
was = Some((slot.id, index));
|
||||
}
|
||||
}
|
||||
let at = match at {
|
||||
DropAt::Slot { slot, index } => {
|
||||
let shift = was.is_some_and(|(from, old)| from == slot && old < index);
|
||||
DropAt::Slot { slot, index: index.saturating_sub(usize::from(shift)) }
|
||||
}
|
||||
other => other,
|
||||
};
|
||||
insert(out, tool, at)
|
||||
}
|
||||
|
||||
fn insert<T: Clone>(mut out: Vec<ToolSlot<T>>, tool: RailTool<T>, at: DropAt) -> Vec<ToolSlot<T>> {
|
||||
let original = out.clone();
|
||||
match at {
|
||||
DropAt::Slot { slot, index } => {
|
||||
let Some(target) = out.iter_mut().find(|candidate| candidate.id == slot) else {
|
||||
return slots.to_vec();
|
||||
return original;
|
||||
};
|
||||
let shift = was.is_some_and(|(from, old)| from == slot && old < index);
|
||||
let index = (index - usize::from(shift)).min(target.tools.len());
|
||||
let index = index.min(target.tools.len());
|
||||
target.tools.insert(index, tool);
|
||||
}
|
||||
DropAt::NewSlot { before } => {
|
||||
let id = slots.iter().map(|slot| slot.id).max().map_or(0, |max| max + 1);
|
||||
let id = match out.iter().map(|slot| slot.id).max() {
|
||||
Some(max) => match max.checked_add(1) { Some(id) => id, None => return original },
|
||||
None => 0,
|
||||
};
|
||||
out.insert(before.min(out.len()), ToolSlot::new(id, vec![tool]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,9 +71,9 @@ fn closing_the_drawer_returns_the_focus_to_the_rails_edit_toolbar_button() {
|
||||
assert!(fixture.by_label("Customize").is_some_and(|button| button.node == focused), "the focus is back on Edit Toolbar");
|
||||
}
|
||||
|
||||
/// A tool already on the toolbar is dimmed and does not drag; a tool that is not on it drags to the rail.
|
||||
/// A grouped tool can also be dragged from the drawer into its own slot.
|
||||
#[trial]
|
||||
fn only_a_tool_off_the_toolbar_drags() {
|
||||
fn a_grouped_tool_also_drags_from_the_drawer() {
|
||||
let entry = stories().into_iter().find(|entry| entry.story.name == "toolbar_drawer").expect("the drawer story");
|
||||
let mut fixture = Fixture::story(&entry.story);
|
||||
fixture.settle();
|
||||
@@ -88,10 +88,24 @@ fn only_a_tool_off_the_toolbar_drags() {
|
||||
fixture.release_at(row.center() + Vec2::new(0.0, 30.0)).settle();
|
||||
started
|
||||
};
|
||||
assert!(!drags(&mut fixture, "Pencil"), "Pencil is on the toolbar");
|
||||
assert!(drags(&mut fixture, "Pencil"), "Pencil can also have its own slot");
|
||||
assert!(drags(&mut fixture, "Eraser"), "Eraser is not");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn adding_a_grouped_tool_keeps_the_group_entry() {
|
||||
let slots = [slot(0, &[UNDO, REDO])];
|
||||
let edit = ToolbarEdit::add(tool(REDO), DropAt::new_slot(1));
|
||||
let result = apply_toolbar_edit(&slots, &edit, &[]);
|
||||
assert_eq!(result.len(), 2);
|
||||
assert_eq!(result[0].tools.len(), 2);
|
||||
assert_eq!(result[1].tools[0].id, REDO);
|
||||
let removed = apply_toolbar_edit(&result, &ToolbarEdit::remove_from(result[1].id, 0), &[]);
|
||||
assert_eq!(layout(&removed), layout(&slots), "removing the separate occurrence preserves the group");
|
||||
let moved = apply_toolbar_edit(&result, &ToolbarEdit::move_from(result[1].id, 0, DropAt::new_slot(0)), &[]);
|
||||
assert_eq!(moved[1].tools.len(), 2, "moving the separate occurrence preserves the group");
|
||||
}
|
||||
|
||||
/// A click on a tool reports it as the one to select and leaves no row selected: the drawer's rows pick, they do not
|
||||
/// hold a selection.
|
||||
#[trial]
|
||||
|
||||
@@ -863,7 +863,7 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Component for TileGrid<'_, K>
|
||||
continue;
|
||||
}
|
||||
let header = match &event {
|
||||
CollectionEvent::Activate(key) | CollectionEvent::LeadMoved { key, .. } => group_of(key),
|
||||
CollectionEvent::Activate(key) => group_of(key),
|
||||
CollectionEvent::ContextMenu(request) => group_of(&request.key),
|
||||
CollectionEvent::Open(change) => group_of(&change.key),
|
||||
_ => None,
|
||||
|
||||
@@ -3,13 +3,31 @@
|
||||
use craft_behavior::{EditEvent, keyed};
|
||||
use craft_ui::contract::{Bind, CollectionEvent, ShowCollection as _};
|
||||
use craft_ui::testing::{Fixture, pointer, trial};
|
||||
use craft_ui::tile_grid::{GridEvent, GridZoom, RatingKeys, Tile, TileGrid, TileZoom};
|
||||
use craft_ui::tile_grid::{GridEvent, GridZoom, RatingKeys, Tile, TileGrid, TileGroup, TileZoom};
|
||||
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"];
|
||||
|
||||
#[trial]
|
||||
fn keyboard_lead_on_a_group_header_is_reported() {
|
||||
let seen = Rc::new(RefCell::new(Vec::new()));
|
||||
let held = seen.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let keys = [1_usize, 2];
|
||||
let source = keyed(&keys, |key: &usize| *key);
|
||||
let groups = [TileGroup::new(0, 0, 2, "Folder")];
|
||||
let out = TileGrid::new("Library").groups(&groups).collapsible().show_items(ui, &source, &mut |_, _| Tile::new("Tile"));
|
||||
held.borrow_mut().extend(out.response.events);
|
||||
});
|
||||
fixture.room(Vec2::new(600.0, 400.0)).settle();
|
||||
fixture.key(Key::Tab, Modifiers::NONE).idle(2);
|
||||
fixture.key(Key::Home, Modifiers::NONE).idle(2);
|
||||
fixture.key(Key::ArrowUp, Modifiers::NONE).idle(2);
|
||||
assert!(seen.borrow().iter().any(|event| matches!(event, CollectionEvent::LeadMoved { key: 0, .. })));
|
||||
}
|
||||
|
||||
/// What the grid reported, kept across frames, and the zoom value a bound grid writes.
|
||||
#[derive(Default)]
|
||||
struct Seen {
|
||||
|
||||
Reference in New Issue
Block a user