ui-contract: a drag of OS files runs in the window under them: the main window's pass, which the OS sends none of a child window's files, ended the session, so files dropped on a native panel window never reached DockAreaOutput::window_files; the test kit's Windows now sends files to one window (FilesOver), as the OS does

This commit is contained in:
bflatastic
2026-11-23 19:42:19 +07:00
parent 24e30ffacb
commit 51ad138dd2
4 changed files with 34 additions and 14 deletions
+9 -2
View File
@@ -422,12 +422,16 @@ pub(crate) fn begin(ctx: &Context) {
}
/// The session of files the OS drags over the window (8.4): files held over the window start one, the dropped files
/// replace its payload in the frame of the drop, and files that leave without a drop end it.
/// replace its payload in the frame of the drop, and files that leave without a drop end it. The OS sends the files
/// only to the window under the pointer, so the session runs in that window: a window the files move to takes it, and
/// only a pass of its own window, which no longer sees them, ends it.
fn os_files(ctx: &Context) {
let seen = files::seen(ctx);
let frame = pass::root_frame_nr(ctx);
let viewport = pass::current_viewport(ctx);
let hovering = !seen.hovered.is_empty();
// A session whose window closed has no pass left to end it.
let closed = source_viewport(ctx).is_some_and(|window| !pass::live_viewports(ctx).contains(&window));
board(ctx, |board| match &mut board.session {
None if hovering => {
board.session = Some(Session {
@@ -455,7 +459,10 @@ fn os_files(ctx: &Context) {
let files = seen.dropped.into_iter().map(|file| with_hovered_type(file, hovered)).collect();
session.payload = Arc::new(OsFiles { files });
session.dropped = Some(frame);
} else if !hovering && session.dropped != Some(frame) {
session.viewport = viewport;
} else if hovering {
session.viewport = viewport;
} else if (session.viewport == viewport || closed) && session.dropped != Some(frame) {
board.session = None;
}
}
+2 -2
View File
@@ -13,7 +13,7 @@ use craft_ui::style::{ComponentStyle, StyleCx};
use craft_ui::testing::pointer::{moved, press, release};
use craft_ui::testing::windows::FakeSystem;
use craft_ui::testing::{Frames, trial};
use craft_ui_test_kit::windows::Windows;
use craft_ui_test_kit::windows::{FilesOver, Windows};
use egui::{Pos2, Rect, Response, Shape, Ui, Vec2, ViewportBuilder, ViewportId, ViewportInfo};
use std::cell::RefCell;
use std::sync::Arc;
@@ -371,7 +371,7 @@ fn files_over_the_window_are_targeted_at_the_desktop_pointer_while_the_frame_has
let windows = Windows::with(InstallSpec::new(ShellInfo::default(), system.clone()));
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![], hovered, dropped, |ui| {
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);
});
windows.ctx.has_requested_repaint()
@@ -20,7 +20,7 @@ use craft_ui::testing::input::key_press;
use craft_ui::testing::pointer::{moved, press, release, secondary};
use craft_ui::testing::trial;
use craft_ui::testing::windows::{FakeSystem, HeldButtons};
use craft_ui_test_kit::windows::{AppKitWindow, Out, Windows};
use craft_ui_test_kit::windows::{AppKitWindow, FilesOver, Out, Windows};
use egui::{Event, HoveredFile, Key, Modifiers, Pos2, Rect, Ui, Vec2, ViewportBuilder, ViewportCommand, ViewportEvent, ViewportId, ViewportInfo};
use std::sync::Arc;
@@ -1346,7 +1346,8 @@ impl DockPanels<u32> for Veiled {
}
/// Files dropped on a native float's own OS window reach `DockAreaOutput::window_files` once, unless a target inside
/// that window took them.
/// that window took them. The main window, whose pass runs first, sees none of them, as the OS sends them only to the
/// window under the pointer: it must not end the drag of files over the float's window.
#[trial]
fn a_file_dropped_on_a_native_float_reaches_window_files_once() {
let reached = |veiled: bool| {
@@ -1356,7 +1357,7 @@ fn a_file_dropped_on_a_native_float_reaches_window_files_once() {
let mut workspace = Arc::new(Workspace::new(Layout::new(None, vec![float()])));
let mut frame = |hovered: Vec<HoveredFile>, dropped: Vec<String>| {
let mut files = 0;
windows.frame_with_files(vec![], vec![], hovered, dropped, |ui| {
windows.frame_with_files(vec![], vec![], FilesOver::Child, hovered, dropped, |ui| {
let config = DockConfig::default();
let shown = if veiled {
DockArea::new("Native dock", Bind::bound(&mut workspace), &Specs::with(Permissions::ALL), &mut Veiled, config).show(ui)
+19 -7
View File
@@ -141,6 +141,15 @@ fn patch_window(ids: egui::ViewportIdPair, builder: ViewportBuilder) {
});
}
/// The window OS files hover over and drop on in [`Windows::frame_with_files`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FilesOver {
/// The root viewport's window.
Root,
/// The child viewport's window.
Child,
}
/// A dropped file that exists only as a path.
#[derive(Debug)]
struct DroppedPath(std::path::PathBuf);
@@ -305,25 +314,28 @@ impl Windows {
/// One frame: `root` events go to the root viewport, `child` events to the child's next pass. Returns the
/// window commands of the frame.
pub fn frame(&self, root: Vec<Event>, child: Vec<Event>, ui: impl FnMut(&mut Ui)) -> Out {
self.frame_with_files(root, child, Vec::new(), Vec::new(), ui)
self.frame_with_files(root, child, FilesOver::Root, Vec::new(), Vec::new(), ui)
}
/// [`frame`](Self::frame) where the child's next pass also gets the OS files hovering over its window and the
/// files dropped on it.
/// [`frame`](Self::frame) where the next pass of the window `over` also gets the OS files hovering over it and the
/// files dropped on it. The other window gets none, as the OS sends them only to the window under the pointer.
pub fn frame_with_files(
&self,
root: Vec<Event>,
child: Vec<Event>,
over: FilesOver,
hovered: Vec<HoveredFile>,
dropped: Vec<String>,
ui: impl FnMut(&mut Ui),
) -> Out {
CHILD_EVENTS.with(|events| *events.borrow_mut() = child);
// The root pass starts before the child's and ends an OS-file session it sees no files for, so it sees them too.
let ((root_hovered, root_dropped), child_files) = match over {
FilesOver::Root => ((hovered, dropped), Default::default()),
FilesOver::Child => (Default::default(), (hovered, dropped)),
};
let root_dropped: Vec<egui::DroppedFileHandle> =
dropped.iter().map(|name| Arc::new(DroppedPath(name.into())) as egui::DroppedFileHandle).collect();
let root_hovered = hovered.clone();
CHILD_FILES.with(|files| *files.borrow_mut() = (hovered, dropped));
root_dropped.into_iter().map(|name| Arc::new(DroppedPath(name.into())) as egui::DroppedFileHandle).collect();
CHILD_FILES.with(|files| *files.borrow_mut() = child_files);
TREES.with(|kept| kept.borrow_mut().clear());
set_input(&self.ctx, FrameInput::new(RootEnv::default()));
let mut input = RawInput {