From 7002b2ec8f48048ae0a3e55fe6eaeb60a4e3398c Mon Sep 17 00:00:00 2001 From: bflatastic Date: Tue, 24 Nov 2026 08:23:57 +0700 Subject: [PATCH] ui-contract: drag::register takes a target's reach as one TargetReach; collections report an item's output through one helper --- crates/ui-contract/src/contract/cx/pointer.rs | 3 +- .../ui-contract/src/contract/items/hooks.rs | 14 +----- .../ui-contract/src/contract/items/members.rs | 13 +----- .../ui-contract/src/contract/items/press.rs | 18 +++++++- crates/ui-contract/src/runtime/drag.rs | 46 ++++++++----------- 5 files changed, 42 insertions(+), 52 deletions(-) diff --git a/crates/ui-contract/src/contract/cx/pointer.rs b/crates/ui-contract/src/contract/cx/pointer.rs index f7dc0b3cc3..9f31c12991 100644 --- a/crates/ui-contract/src/contract/cx/pointer.rs +++ b/crates/ui-contract/src/contract/cx/pointer.rs @@ -197,7 +197,8 @@ impl Pointer for CxCore<'_> { let op = drag::current_operation(&self.ctx, craft_behavior::CopyKeys::Alt); let drop = target.accept(&payload, op)?; let ordinal = drag::next_ordinal(&self.ctx, self.id); - drag::register(&self.ctx, self.id, self.response.layer_id, target.mark(&drop), op, self.env.shared_tokens(), true, target.under_modal()); + let reach = drag::TargetReach { window: true, under_modal: target.under_modal() }; + drag::register(&self.ctx, self.id, self.response.layer_id, target.mark(&drop), op, self.env.shared_tokens(), reach); drag::take_drop(&self.ctx, self.id, ordinal, op, TypeId::of::

()).then_some(drop) } diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index 7039260b80..f25092d50c 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -8,8 +8,7 @@ use super::{Held, ITEM_NODE, ItemsHooks, LastLayout, Lead, Placement, Renaming, use super::{request, rows}; use crate::contract::ValueOf; use crate::contract::collection::{ - CollectionEvent, CollectionProps, ColumnLayout, ContextMenuRequest, DropAt, Gestures, HoverLead, ItemsOut, OpenChange, PendingDrop, Rename, - RowState, ShownRow, + CollectionEvent, CollectionProps, ColumnLayout, DropAt, Gestures, HoverLead, ItemsOut, PendingDrop, Rename, RowState, ShownRow, }; use crate::contract::cx::{BodyCx, CxCore, Pointer as _}; use crate::contract::run::{Measuring, ShapeHooks, ShowAt}; @@ -644,16 +643,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { if let Some(row) = shown_rows.last_mut() { row.openers = self.item_openers(&row_ui, &ctx, id, cx.core.resolved.enabled, openers); } - if let Some(ask) = out.open_request() { - self.events.push(CollectionEvent::Open(OpenChange { key: key.clone(), open: ask.open, all: ask.all })); - } - if let Some(rect) = out.menu_request() { - crate::runtime::keys::request_context(&ctx, id, rect, None); - self.ask_menu(ContextMenuRequest { key: key.clone(), at: self.on_screen(rect.left_bottom()), column: None, part: None }); - } - if let Some(event) = out.into_event() { - self.events.push(CollectionEvent::Item(key.clone(), event)); - } + self.report_item_output(&ctx, id, &key, out); if state.renaming { self.rename_row(comp, &mut row_ui, (&ctx, id, rtl), (at, &key), label); } diff --git a/crates/ui-contract/src/contract/items/members.rs b/crates/ui-contract/src/contract/items/members.rs index 72b61d9100..405d60dcc9 100644 --- a/crates/ui-contract/src/contract/items/members.rs +++ b/crates/ui-contract/src/contract/items/members.rs @@ -2,7 +2,7 @@ //! arena, updated, painted and finished at their rows, reported as sub-nodes of the collection. use super::{ITEM_NODE, ItemsHooks, key_hash}; -use crate::contract::collection::{CollectionEvent, ContextMenuRequest, OpenChange, ShownRow}; +use crate::contract::collection::ShownRow; use crate::contract::run; use crate::contract::{Collection, Component as _, Cx, Internal, ItemOpener, ItemOutput as _, Meta as _}; use crate::primitives::sensing::{self, MemberSpec}; @@ -87,16 +87,7 @@ impl<'r, C: Collection> ItemsHooks<'_, 'r, C> { shown.openers = self.item_openers(ui, ctx, id, enabled, openers); } let label = output.label_rect().unwrap_or(row); - if let Some(ask) = output.open_request() { - self.events.push(CollectionEvent::Open(OpenChange { key: key.clone(), open: ask.open, all: ask.all })); - } - if let Some(rect) = output.menu_request() { - crate::runtime::keys::request_context(ctx, id, rect, None); - self.ask_menu(ContextMenuRequest { key: key.clone(), at: self.on_screen(rect.left_bottom()), column: None, part: None }); - } - if let Some(event) = output.into_event() { - self.events.push(CollectionEvent::Item(key.clone(), event)); - } + self.report_item_output(ctx, id, &key, output); if renaming { self.rename_row(comp, ui, (ctx, id, rtl), (at, &key), label); } diff --git a/crates/ui-contract/src/contract/items/press.rs b/crates/ui-contract/src/contract/items/press.rs index df7c599778..cf6819543d 100644 --- a/crates/ui-contract/src/contract/items/press.rs +++ b/crates/ui-contract/src/contract/items/press.rs @@ -3,8 +3,8 @@ use super::drag::SlowClick; use super::lead::PickBy; use super::{ItemsHooks, Placement, RowPress, SECTION}; -use crate::contract::collection::{ActivateOn, CollectionEvent, CollectionProps, ContextMenuRequest, LeadSource, RowPressed, SelectMode}; -use crate::contract::{Collection, Cx, ItemOpener}; +use crate::contract::collection::{ActivateOn, CollectionEvent, CollectionProps, ContextMenuRequest, LeadSource, OpenChange, RowPressed, SelectMode}; +use crate::contract::{Collection, Cx, ItemOpener, ItemOutput}; use crate::runtime::egui::response; use crate::runtime::env::ArenaId; use crate::runtime::focus; @@ -380,6 +380,20 @@ impl ItemsHooks<'_, '_, C> { self.events.push(CollectionEvent::ContextMenu(request)); } + /// Reports what the shown item `key` asked for: opening or closing its children, its menu, and its event. + pub(super) fn report_item_output(&mut self, ctx: &Context, id: Id, key: &C::Key, output: impl ItemOutput) { + if let Some(ask) = output.open_request() { + self.events.push(CollectionEvent::Open(OpenChange { key: key.clone(), open: ask.open, all: ask.all })); + } + if let Some(rect) = output.menu_request() { + crate::runtime::keys::request_context(ctx, id, rect, None); + self.ask_menu(ContextMenuRequest { key: key.clone(), at: self.on_screen(rect.left_bottom()), column: None, part: None }); + } + if let Some(event) = output.into_event() { + self.events.push(CollectionEvent::Item(key.clone(), event)); + } + } + /// The response of each part an item asked to open a menu from (`ItemCx::part_opener`). pub(super) fn item_openers(&self, ui: &Ui, ctx: &Context, id: Id, enabled: bool, openers: Vec) -> Vec<(ItemOpener, egui::Response)> { item_openers(ui, ctx, id, enabled, openers) diff --git a/crates/ui-contract/src/runtime/drag.rs b/crates/ui-contract/src/runtime/drag.rs index 423bc707e1..bfe0156945 100644 --- a/crates/ui-contract/src/runtime/drag.rs +++ b/crates/ui-contract/src/runtime/drag.rs @@ -74,7 +74,7 @@ pub enum DropMark { }, } -crate::stays!(Arc, DropOperation); +crate::stays!(Arc, DropOperation, TargetReach); impl crate::runtime::Shift for DropMark { fn shifted(&self, by: &egui::RecordShift) -> Self { @@ -301,10 +301,16 @@ struct Registration { op: DropOperation, /// The tokens of the target's region. tokens: Arc, + reach: TargetReach, +} + +/// Where a drop target takes drops beyond its own mark. +#[derive(Clone, Copy, Debug, PartialEq)] +pub(crate) struct TargetReach { /// The target covers the whole window: any target over a component beats it. - window: bool, + pub(crate) window: bool, /// The target takes OS files under a modal layer (`DropTarget::under_modal`). - under_modal: bool, + pub(crate) under_modal: bool, } /// A viewport's registrations: this pass's, and the target that won last pass. @@ -635,24 +641,12 @@ pub(crate) fn show_mark(ctx: &Context, mark: Option) { } /// Records that `owner`'s `ordinal`th target on `layer` accepts at the pointer a drop doing `op`, with its mark in -/// layer space and the tokens of the target's region. A `window` target covers the whole window; an `under_modal` one -/// takes OS files under a modal layer. -pub(crate) fn register( - ctx: &Context, - owner: Id, - layer: Layer, - mark: DropMark, - op: DropOperation, - tokens: Arc, - window: bool, - under_modal: bool, -) { +/// layer space, the tokens of the target's region and its `reach`. +pub(crate) fn register(ctx: &Context, owner: Id, layer: Layer, mark: DropMark, op: DropOperation, tokens: Arc, reach: TargetReach) { crate::runtime::replay::journal( ctx, - || (owner, layer, mark, op, tokens.clone(), (window, under_modal)), - |ctx, (owner, layer, mark, op, tokens, (window, under_modal))| { - register(ctx, *owner, *layer, *mark, *op, tokens.clone(), *window, *under_modal) - }, + || (owner, layer, mark, op, tokens.clone(), reach), + |ctx, (owner, layer, mark, op, tokens, reach)| register(ctx, *owner, *layer, *mark, *op, tokens.clone(), *reach), ); let mark = match areas::layer_map(ctx, layer) { Some(map) => mark.mapped(|point| map.to_screen(point)), @@ -661,7 +655,7 @@ pub(crate) fn register( let scope = overlay::drawing_parent(ctx); PerViewport::::with(ctx, pass::current_viewport(ctx), |targets| { let ordinal = targets.this.iter().filter(|registration| registration.owner == owner).count() as u32; - targets.this.push(Registration { owner, ordinal, layer, scope, mark, op, tokens, window, under_modal }); + targets.this.push(Registration { owner, ordinal, layer, scope, mark, op, tokens, reach }); }); } @@ -747,7 +741,7 @@ pub(crate) fn target_of>( let op = current_operation(ctx, target.copy_keys()); let drop = target.accept(&*payload, at, op)?; let ordinal = next_ordinal(ctx, owner); - register(ctx, owner, zone.layer, target.mark(&drop), op, tokens, false, target.under_modal()); + register(ctx, owner, zone.layer, target.mark(&drop), op, tokens, TargetReach { window: false, under_modal: target.under_modal() }); take_drop(ctx, owner, ordinal, op, payload_type).then_some(drop) } @@ -811,7 +805,7 @@ pub(crate) fn end_pass(ctx: &Context) { // that takes them there. let os = carries_os_files(ctx); let scopes: Vec<(Option, bool)> = PerViewport::::with(ctx, viewport, |targets| { - targets.this.iter().map(|registration| (registration.scope, os && registration.under_modal)).collect() + targets.this.iter().map(|registration| (registration.scope, os && registration.reach.under_modal)).collect() }); let open: Vec = scopes.iter().map(|(scope, through)| *through || overlay::key_layer(ctx, scope.as_slice(), None) != LayerKeys::Blocked).collect(); @@ -823,10 +817,10 @@ pub(crate) fn end_pass(ctx: &Context) { .iter() .enumerate() .filter(|(at, _)| open[*at]) - .max_by_key(|(at, registration)| (!registration.window, areas::layer_position(ctx, registration.layer), *at)) + .max_by_key(|(at, registration)| (!registration.reach.window, areas::layer_position(ctx, registration.layer), *at)) .map(|(_, registration)| registration); - targets.component = best.is_some_and(|registration| !registration.window); - targets.winner_window = best.is_some_and(|registration| registration.window); + targets.component = best.is_some_and(|registration| !registration.reach.window); + targets.winner_window = best.is_some_and(|registration| registration.reach.window); targets.pointer = position; let best = best.map(|registration| ((registration.owner, registration.ordinal), (registration.mark, registration.op, registration.tokens.clone()))); @@ -909,7 +903,7 @@ pub(crate) fn held_elsewhere(ctx: &Context) -> bool { let viewport = pass::current_viewport(ctx); pass::live_viewports(ctx).into_iter().filter(|other| *other != viewport).any(|other| { PerViewport::::with(ctx, other, |targets| { - (targets.winner.is_some() && targets.component) || targets.this.iter().any(|registration| !registration.window) + (targets.winner.is_some() && targets.component) || targets.this.iter().any(|registration| !registration.reach.window) }) }) }