ui-contract: DropTarget::under_modal and WindowDropTarget::under_modal let a target take OS file drops while a modal layer holds input, through the modal's scrim but not its body: a modal refused every drop below it and on the window, where VectorCraft placed files dropped under its alert; off by default

This commit is contained in:
bflatastic
2026-11-23 21:26:08 +07:00
parent 46b9fe002d
commit fe2d1d09d7
4 changed files with 133 additions and 25 deletions
@@ -174,7 +174,7 @@ impl Pointer for CxCore<'_> {
let op = drag::current_operation(&self.ctx);
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), self.env.shared_tokens(), true);
drag::register(&self.ctx, self.id, self.response.layer_id, target.mark(&drop), self.env.shared_tokens(), true, target.under_modal());
drag::take_drop(&self.ctx, self.id, ordinal, op, TypeId::of::<P>()).then_some(drop)
}
+46 -13
View File
@@ -162,6 +162,12 @@ pub trait DropTarget<P: ?Sized> {
/// Where `drop` would land, a value the ghost layer paints.
fn mark(&self, drop: &Self::Drop) -> DropMark;
/// Whether the target takes OS file drops (`OsFiles`) while a modal layer above it holds input, as an app whose
/// alerts let files through (VectorCraft placed files dropped under its alert). By default a modal refuses them.
fn under_modal(&self) -> bool {
false
}
}
/// What a component registers with `Pointer::window_drop_target`: a target over the whole window, which needs no
@@ -175,6 +181,12 @@ pub trait WindowDropTarget<P> {
/// Where `drop` would land, a value the ghost layer paints.
fn mark(&self, drop: &Self::Drop) -> DropMark;
/// Whether the target takes OS file drops (`OsFiles`) while a modal layer holds input, as
/// [`DropTarget::under_modal`] says.
fn under_modal(&self) -> bool {
false
}
}
/// Where a drop target lies: a rect on an egui layer, in that layer's space. A component's own target lies over its
@@ -276,6 +288,8 @@ struct Registration {
tokens: Arc<UiTokens>,
/// The target covers the whole window: any target over a component beats it.
window: bool,
/// The target takes OS files under a modal layer (`DropTarget::under_modal`).
under_modal: bool,
}
/// A viewport's registrations: this pass's, and the target that won last pass.
@@ -394,6 +408,11 @@ pub fn mark(ctx: &Context) -> Option<DropMark> {
board(ctx, |board| board.session.as_ref().and_then(|session| session.mark.as_ref().map(|shown| shown.mark)))
}
/// Whether the running session carries files from the OS.
fn carries_os_files(ctx: &Context) -> bool {
board(ctx, |board| board.session.as_ref().is_some_and(|session| session.os))
}
/// Whether a session runs.
pub(crate) fn running(ctx: &Context) -> bool {
board(ctx, |board| board.session.is_some())
@@ -556,12 +575,13 @@ pub(crate) fn show_mark(ctx: &Context, mark: Option<DropMark>) {
}
/// Records that `owner`'s `ordinal`th target on `layer` accepts at the pointer, with its mark in layer space and the
/// tokens of the target's region. A `window` target covers the whole window.
pub(crate) fn register(ctx: &Context, owner: Id, layer: Layer, mark: DropMark, tokens: Arc<UiTokens>, window: bool) {
/// 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, tokens: Arc<UiTokens>, window: bool, under_modal: bool) {
crate::runtime::replay::journal(
ctx,
|| (owner, layer, mark, tokens.clone(), window),
|ctx, (owner, layer, mark, tokens, window)| register(ctx, *owner, *layer, *mark, tokens.clone(), *window),
|| (owner, layer, mark, tokens.clone(), window, under_modal),
|ctx, (owner, layer, mark, tokens, window, under_modal)| register(ctx, *owner, *layer, *mark, tokens.clone(), *window, *under_modal),
);
let mark = match areas::layer_map(ctx, layer) {
Some(map) => mark.mapped(|point| map.to_screen(point)),
@@ -570,7 +590,7 @@ pub(crate) fn register(ctx: &Context, owner: Id, layer: Layer, mark: DropMark, t
let scope = overlay::drawing_parent(ctx);
PerViewport::<Targets>::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, tokens, window });
targets.this.push(Registration { owner, ordinal, layer, scope, mark, tokens, window, under_modal });
});
}
@@ -641,9 +661,14 @@ pub(crate) fn target_of<T: DropTarget<dyn Any + Send + Sync>>(
) -> Option<T::Drop> {
let (payload, screen) = dragging_of(ctx, payload_type)?;
let at = areas::layer_map(ctx, zone.layer).map_or(screen, |map| map.to_layer(screen));
let top = match moving_layer(ctx) {
Some(moving) => areas::layer_at_without(ctx, screen, moving),
None => areas::layer_at(ctx, screen),
let top = if carries_os_files(ctx) && target.under_modal() {
// A target that takes OS files under a modal sees through the modal's scrim, not through its body.
areas::layer_at_under_modal(ctx, screen, overlay::top_modal_in(ctx, pass::current_viewport(ctx)).and_then(|modal| modal.cover))
} else {
match moving_layer(ctx) {
Some(moving) => areas::layer_at_without(ctx, screen, moving),
None => areas::layer_at(ctx, screen),
}
};
if moves_viewport(ctx) || !zone.rect.contains(at) || top != Some(zone.layer) {
return None;
@@ -651,7 +676,7 @@ pub(crate) fn target_of<T: DropTarget<dyn Any + Send + Sync>>(
let op = current_operation(ctx);
let drop = target.accept(&*payload, at, op)?;
let ordinal = next_ordinal(ctx, owner);
register(ctx, owner, zone.layer, target.mark(&drop), tokens, false);
register(ctx, owner, zone.layer, target.mark(&drop), tokens, false, target.under_modal());
take_drop(ctx, owner, ordinal, op, payload_type).then_some(drop)
}
@@ -682,6 +707,10 @@ impl<P: 'static, T: DropTarget<P>> DropTarget<dyn Any + Send + Sync> for Typed<'
fn mark(&self, drop: &T::Drop) -> DropMark {
self.0.mark(drop)
}
fn under_modal(&self) -> bool {
self.0.under_modal()
}
}
/// The operation of the drop of `source`'s payload of type `P` that the previous frame took.
@@ -701,10 +730,14 @@ pub(crate) fn ended<P: 'static>(ctx: &Context, source: Id) -> Option<DropOperati
/// window holds the drop, it waits there for one more frame so that window's pass takes it.
pub(crate) fn end_pass(ctx: &Context) {
let viewport = pass::current_viewport(ctx);
// A modal holds input: targets on layers below it, and on the window, never accept.
let scopes: Vec<Option<Id>> =
PerViewport::<Targets>::with(ctx, viewport, |targets| targets.this.iter().map(|registration| registration.scope).collect());
let open: Vec<bool> = scopes.iter().map(|scope| overlay::key_layer(ctx, scope.as_slice(), None) != LayerKeys::Blocked).collect();
// A modal holds input: targets on layers below it, and on the window, never accept, except OS files on a target
// that takes them there.
let os = carries_os_files(ctx);
let scopes: Vec<(Option<Id>, bool)> = PerViewport::<Targets>::with(ctx, viewport, |targets| {
targets.this.iter().map(|registration| (registration.scope, os && registration.under_modal)).collect()
});
let open: Vec<bool> =
scopes.iter().map(|(scope, through)| *through || overlay::key_layer(ctx, scope.as_slice(), None) != LayerKeys::Blocked).collect();
let winner = PerViewport::<Targets>::with(ctx, viewport, |targets| {
// The topmost layer in the order the overlay host handed egui wins, then the target registered last: a target
// shown later lies over an enclosing one.
@@ -98,6 +98,12 @@ pub(crate) fn layer_at_without(ctx: &Context, point: Pos2, skipped: LayerId) ->
ctx.memory(|memory| if memory.is_above_modal_layer(found) { Some(found) } else { memory.top_modal_layer() })
}
/// The topmost layer under a screen position whether or not a modal layer holds the input, without the modal's
/// scrim `scrim`: where a target that takes OS files under a modal lies.
pub(crate) fn layer_at_under_modal(ctx: &Context, point: Pos2, scrim: Option<LayerId>) -> Option<LayerId> {
crate::runtime::sampler::layer_at(ctx, point).or_else(|| layers_under(ctx, point, scrim).first().copied())
}
/// The shown, interactable layers with their screen rects, bottom first, without `skipped`.
fn interactive_layers(ctx: &Context, skipped: Option<LayerId>) -> Vec<(LayerId, Rect)> {
let stack: Vec<LayerId> =
+80 -11
View File
@@ -3,8 +3,8 @@
use craft_behavior::{DropOperation, PartKey, PressPhase};
use craft_host::target::ShellInfo;
use craft_ui::contract::{
Common, Component, Cx, DragPreview, DropMark, DropTarget, MeasureCx, OsFiles, PartsCx, Pointer as _, Props as _, Role, Show as _,
WindowDropTarget,
Common, Component, Container, ContainerProps, Cx, DragPreview, DropMark, DropTarget, MeasureCx, Mount, OsFiles, Overlay, PartsCx, Pointer as _,
Props as _, Role, Show as _, ShowContainer as _, WindowDropTarget,
};
use craft_ui::runtime::InstallSpec;
use craft_ui::runtime::drag;
@@ -251,8 +251,8 @@ fn names(files: &OsFiles) -> Vec<String> {
files.files.iter().map(|file| file.name.clone()).collect()
}
/// A drop of files on the row under the pointer.
struct OnRow;
/// A drop of files on the row under the pointer; it takes them under a modal layer when it holds `true`.
struct OnRow(bool);
impl DropTarget<OsFiles> for OnRow {
type Drop = (usize, Vec<String>);
@@ -264,10 +264,14 @@ impl DropTarget<OsFiles> for OnRow {
fn mark(&self, (row, _): &Self::Drop) -> DropMark {
DropMark::Into(Rect::from_min_size(LIST.min + Vec2::new(0.0, *row as f32 * ROW), Vec2::new(LIST.width(), ROW)))
}
fn under_modal(&self) -> bool {
self.0
}
}
/// A drop of files anywhere in the window.
struct OnWindow;
/// A drop of files anywhere in the window; it takes them under a modal layer when it holds `true`.
struct OnWindow(bool);
impl WindowDropTarget<OsFiles> for OnWindow {
type Drop = Vec<String>;
@@ -279,6 +283,10 @@ impl WindowDropTarget<OsFiles> for OnWindow {
fn mark(&self, _: &Self::Drop) -> DropMark {
DropMark::Into(WINDOW)
}
fn under_modal(&self) -> bool {
self.0
}
}
/// A list of rows that takes files on a row, with the window as the target of every other drop.
@@ -287,6 +295,8 @@ struct FileList<'a> {
#[common]
common: Common,
landed: &'a RefCell<Landed>,
/// The targets take files under a modal layer.
under_modal: bool,
}
impl Component for FileList<'_> {
@@ -303,10 +313,10 @@ impl Component for FileList<'_> {
}
fn update(&mut self, cx: &mut Cx<'_, Self>) {
if let Some(row) = cx.drop_target(&OnRow) {
if let Some(row) = cx.drop_target(&OnRow(self.under_modal)) {
self.landed.borrow_mut().rows.push(row);
}
if let Some(files) = cx.window_drop_target(&OnWindow) {
if let Some(files) = cx.window_drop_target(&OnWindow(self.under_modal)) {
self.landed.borrow_mut().window.push(files);
}
}
@@ -321,7 +331,7 @@ fn files_that_leave_the_window_end_the_drag_without_a_drop() {
let landed = RefCell::new(Landed::default());
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| {
FileList { common: Common::new(), landed: &landed }.id_salt("files").show_at(ui, LIST);
FileList { common: Common::new(), landed: &landed, under_modal: false }.id_salt("files").show_at(ui, LIST);
};
frames.idle(2, &mut show);
frames.hover_files(&["a.png"]);
@@ -340,7 +350,7 @@ fn files_hovering_right_after_a_drop_drag_and_drop_on_their_own() {
let landed = RefCell::new(Landed::default());
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| {
FileList { common: Common::new(), landed: &landed }.id_salt("files").show_at(ui, LIST);
FileList { common: Common::new(), landed: &landed, under_modal: false }.id_salt("files").show_at(ui, LIST);
};
let row = |n: usize| LIST.min + Vec2::new(LIST.width() / 2.0, (n as f32 + 0.5) * ROW);
frames.idle(2, &mut show);
@@ -358,6 +368,65 @@ fn files_hovering_right_after_a_drop_drag_and_drop_on_their_own() {
assert!(drag::active::<OsFiles>(&frames.ctx).is_none(), "the last drop ends the drag");
}
/// A modal layer with an empty body, in the middle of the window.
#[derive(Component)]
#[component(container, unregistered)]
struct Modal {
#[common]
common: Common,
#[container]
props: ContainerProps,
}
impl Component for Modal {
const ROLE: Role = Role::GROUP;
type Style = craft_ui_test_kit::contract::PlainStyle;
type Output = Response;
fn style(_: &StyleCx<'_>, _: ModalStyleKey) -> craft_ui_test_kit::contract::PlainStyle {
craft_ui_test_kit::contract::PlainStyle::min(Vec2::new(120.0, 60.0))
}
fn finish(self, cx: Cx<'_, Self>) -> Response {
cx.response.clone()
}
}
impl Container for Modal {}
/// What a file list whose targets take files `under_modal` took of a file dropped on its first row and one dropped
/// beside it, while a modal layer is open.
fn landed_under_a_modal(under_modal: bool) -> Landed {
let landed = RefCell::new(Landed::default());
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| {
FileList { common: Common::new(), landed: &landed, under_modal }.id_salt("files").show_at(ui, LIST);
let props = ContainerProps::new().mount(Mount::Layer(Overlay::modal()));
Modal { common: Common::new(), props }.id_salt("alert").show(ui, |_, _| {});
};
frames.idle(2, &mut show);
for (file, at) in [("a.png", LIST.min + Vec2::new(LIST.width() / 2.0, ROW / 2.0)), ("b.png", Pos2::new(LIST.center().x, 500.0))] {
frames.hover_files(&[file]);
frames.frame(vec![moved(at)], &mut show);
frames.frame(vec![], &mut show);
frames.drop_files(&[file]);
frames.frame(vec![], &mut show);
frames.frame(vec![], &mut show);
}
landed.into_inner()
}
/// Tb-8 (4): a modal refuses OS files on the layers below it and on the window, unless the target says it takes them
/// there (`DropTarget::under_modal`), as VectorCraft placed files dropped under its alert.
#[trial]
fn files_dropped_under_a_modal_land_only_on_targets_that_take_them_there() {
let refused = landed_under_a_modal(false);
assert!(refused.rows.is_empty() && refused.window.is_empty(), "by default the modal refuses the drops");
let taken = landed_under_a_modal(true);
assert_eq!(taken.rows, vec![(0, vec!["a.png".to_owned()])], "the row below the modal takes its file");
assert_eq!(taken.window, vec![vec!["b.png".to_owned()]], "the window takes the file dropped beside the row");
}
/// Where the window's inner rect sits on the desktop in the desktop-pointer trial.
const WINDOW_AT: Pos2 = Pos2::new(100.0, 50.0);
@@ -372,7 +441,7 @@ fn files_over_the_window_are_targeted_at_the_desktop_pointer_while_the_frame_has
windows.root_info(ViewportInfo { inner_rect: Some(Rect::from_min_size(WINDOW_AT, WINDOW.size())), ..ViewportInfo::default() });
let frame = |events: Vec<egui::Event>, hovered: Vec<egui::HoveredFile>, dropped: Vec<String>| {
let _ = windows.frame_with_files(events, vec![], FilesOver::Root, hovered, dropped, |ui| {
FileList { common: Common::new(), landed: &landed }.id_salt("files").show_at(ui, LIST);
FileList { common: Common::new(), landed: &landed, under_modal: false }.id_salt("files").show_at(ui, LIST);
});
windows.ctx.has_requested_repaint()
};