Skip to content

gpui: Deliver files dragged in from other applications - #591

Merged
huacnlee merged 5 commits into
longbridge:mainfrom
UncertaintyDeterminesYou4ndMe:gpui-file-drop
Nov 4, 2026
Merged

huacnlee merged 5 commits into
longbridge:mainfrom
UncertaintyDeterminesYou4ndMe:gpui-file-drop

Conversation

@UncertaintyDeterminesYou4ndMe

@UncertaintyDeterminesYou4ndMe UncertaintyDeterminesYou4ndMe commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

The GPUI backend never delivered files dragged in from other applications: onFileDragEnter, onFileDragLeave and onFileDrop worked on AppKit, GTK and Win32 but nothing reached a view in a GPUI window. GPUI turns such a drag into an internal drag of ExternalPaths that only its own elements hear, and the surface's hitbox (BlockMouse) keeps the mouse from the elements behind it.

While a drag is active, AppView now lays a layer over the surface. Its drag moves and its drop drive a new file_drop.rs, which follows docs/dev/file-drop.md the way the AppKit backend does:

  • The destination is the closest view under the pointer that listens to onFileDrop, within its modal (input::file_drop_target, as on macOS). Disabled views have no file-drop events, so they are skipped.
  • Moving between destinations sends onFileDragLeave to the old one and onFileDragEnter to the new one. Dropping, leaving the window, and the destination's removal or loss of its onFileDrop listener all clear it. A drag ending is detected when the window redraws without an active drag, since GPUI sends no element event for FileDropEvent::Exited.
  • Only absolute paths are delivered, as file: URLs in their order, using the clipboard's file_url.

The first report after a drag enters is the platform's next move, because the layer arrives with the drag's frame.

The docs list GPUI next to AppKit, GTK and Win32 (website/elements/view.md and its zh-CN page, docs/dev/file-drop.md), and docs/dev/gpui.md has a row for the module.

How to Test

  • (cd crates/gpui && cargo test -p nsh-platform-gpui --features runtime-shaders,shot --lib): 200 passed on macOS. The new files_dropped_from_outside_reach_the_view_under_them step of tests::windows covers these cases, with Entered / Pending / Submit / Exited events dispatched to a test window:
    • entering an inner destination;
    • moving to its parent (leave, then enter);
    • dropping there (the relative path is left out, then leave);
    • leaving the window;
    • dropping where no view listens.
  • I checked that the test fails when the drag-ended clear is disabled.
  • file_drop::tests::only_absolute_paths_become_file_urls covers the URL conversion: spaces and non-ASCII characters are escaped.
  • cargo clippy --all-targets --features nsh-platform-gpui/dev,nsh-platform-gpui/runtime-shaders in crates/gpui is clean.
  • cargo +nightly fmt --all changes nothing in these files.
  • A real Finder drag onto a text field's container in an embedded GPUI app, Apache Maka's desktop with this change vendored, attached the file. Only macOS was tried; Linux and Windows were not run.

Checklist

  • I have read the CONTRIBUTING document and followed the guidelines.
  • Reviewed the changes in this PR and confirmed AI generated code (if any) is accurate.
  • Passed cargo test --workspace, cargo +nightly fmt --all and clippy (including the Catalyst and Android runs for platform code).
  • Changes to what apps use keep what released versions shipped and come with a case in tests/compat/ (cargo xtask compat).
  • Ran the related examples/gallery demos on iOS and Android (if the change is visible or platform-specific).

Only the GPUI workspace's tests, fmt and clippy were run (above): the change is confined to crates/gpui, which the main workspace does not build. No API apps use changes. AI-assisted: Claude Code wrote the change and this description.

GPUI makes such a drag an internal one that only its own elements hear,
and the surface's hitbox blocks the mouse for the elements behind it.
While a drag is active, the app view lays a layer over the surface
whose drag moves and drop drive onFileDragEnter, onFileDragLeave and
onFileDrop as the native backends do.
UncertaintyDeterminesYou4ndMe added a commit to apache/maka that referenced this pull request Oct 28, 2026
…does

Replaces the drop-only path with longbridge/nsh#591: the drag's moves
pick the destination within its modal and send onFileDragEnter and
onFileDragLeave; dropping, leaving the window, removal and disablement
clear it. Also moves a doc comment that the drop handler had split from
focus_next.

Generated-by: Claude Code

@huacnlee huacnlee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed by ChatGPT on behalf of @huacnlee.


#[test]
fn only_absolute_paths_become_file_urls() {
let paths = ["/tmp/a b.txt", "relative.txt", "/tmp/é"].map(std::path::PathBuf::from);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Use platform-appropriate absolute paths in the tests

/tmp/... is not absolute on Windows, so uris() filters out both fixtures and this assertion fails; the new drag/drop integration test in tests.rs uses the same Unix-only paths. Use platform-appropriate paths and expected URLs in both tests. This is a static finding; Windows tests were not executed here.

Reviewed by ChatGPT on behalf of @huacnlee.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in dee4e9a: both tests now take their paths from test_path (C:\tmp\… on Windows, /tmp/… elsewhere) and their expected URLs from test_url. The production path already handled Windows (clipboard::file_url). Re-ran the GPUI tests on macOS (200 passed); Windows was not run locally.

A path without a drive is not absolute on Windows, so the fixtures
under /tmp were left out there.
@huacnlee

huacnlee commented Nov 2, 2026

Copy link
Copy Markdown
Member

Review of dee4e9ad52 (merged with current main locally, clean auto-merge; nothing pushed).

What I ran (Linux, x86_64)

  • cargo test -p nsh-platform-gpui --features runtime-shaders,shot --lib in crates/gpui: 201 passed, 0 failed (incl. files_dropped_from_outside_reach_the_view_under_them and only_absolute_paths_become_file_urls).
  • cargo clippy -p nsh-platform-gpui --all-targets --features dev,runtime-shaders: clean.
  • cargo +nightly fmt --all --check: clean.
  • Gallery / real drop: not verified on Linux. This host has no headless drag source (no X11/Wayland XDND injector) and the visual harness was unavailable, so an external file drop could not be simulated; only the macOS Finder test in the PR description covers a real drag.

API / scope: no public API change; everything is pub(crate) inside nsh-platform-gpui, the JS-facing events (onFileDragEnter/Leave/Drop) already exist on other backends, so no nsh-compat case is needed. Scope is tight (one module + hooks in mount/app + docs). Security: only absolute paths are forwarded, encoded via the existing clipboard::file_url, same contract as AppKit/GTK/Win32; no file is read by the backend. Fine.

Findings

  • P2 crates/gpui/nsh-platform-gpui/src/file_drop.rs:27-42 — set_target holds surface.file_drop.target.borrow_mut() while calling surface.event(...). Today dispatch_event only queues to the engine so it is safe, but any future synchronous path back into file_drop::active/invalidate (e.g. a mount flush during dispatch) would panic with BorrowMutError. Suggest take()/compute under the borrow, drop it, then send the events.
  • P3 crates/gpui/nsh-platform-gpui/src/app.rs:838-849 — the overlay is added for any active GPUI drag, not only ExternalPaths. For an embedding app that drags its own GPUI items across the surface, the overlay's hitbox sits above the surface's, so surface hover/cursor may stop updating during that drag. Consider checking the active drag's type (cx.active_drag_cursor_style / active_drag.value.downcast_ref::<ExternalPaths>()) if GPUI exposes it, or document the limitation.
  • P3 crates/gpui/nsh-platform-gpui/src/app.rs:819-828 — the deferred clear is scheduled on every render while !dragging && active; harmless (idempotent) but could be guarded to schedule once.
  • P3 (behaviour note) onFileDragEnter arrives only on the first move after the drag enters (described in the PR); acceptable, worth one line in docs/dev/file-drop.md if not already there.

Verdict: Logic, tests and docs look right; no blockers. P2 is a cheap robustness fix. Owner should still try a real drag on Linux (Wayland) and Windows in the gallery before release.

— 🤖 Argus (reviewer) · AI agent acting for @huacnlee

Store the new destination before the leave and enter events go out, schedule the clear after a drag ends only once, and note the GPUI enter timing and evidence in the file-drop design notes.
@UncertaintyDeterminesYou4ndMe

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Addressed in 6da4a83:

  • P2 (set_target borrow): fixed. The new destination is stored and the borrow released before the leave/enter events go out.
  • P3 (deferred clear on every render): fixed. file_drop::needs_clear marks the clear as scheduled, so later redraws before it runs schedule no other; clear resets the mark.
  • P3 (enter on the first move): added to docs/dev/file-drop.md under Contract (it was only in docs/dev/gpui.md), along with a GPUI paragraph under Current evidence.
  • P3 (overlay for any drag): left as is, with a comment. GPUI keeps App::active_drag pub(crate) and exposes only has_active_drag() / active_drag_cursor_style(), so the layer cannot check the drag's type. It does not block the surface either: the layer's hitbox is HitboxBehavior::Normal, and Window::hit_test stops only at a BlockMouse hitbox, so the surface's hitbox under it stays in the hit list and remains hovered. Its listeners are on_drag_move::<ExternalPaths> / on_drop::<ExternalPaths>, so another type of drag reaches neither.

Real drags on Linux (Wayland) and Windows are still unverified on our side as well.

@huacnlee

huacnlee commented Nov 4, 2026

Copy link
Copy Markdown
Member

Re-review of 6da4a830, covering only the new commit since dee4e9ad52. This is not a merge recommendation.

Verdict: all four prior findings are resolved, and I found no new bugs. Two minor P3 notes are below.

Prior findings

  • P2, set_target borrow: fixed. file_drop.rs:28-35 swaps the target inside a scope, so the RefCell borrow ends before any leave/enter event goes out. invalidate and needs_clear also drop their borrows before they do anything else, so a synchronous re-entry can no longer panic with BorrowMutError.
  • P3, overlay for any drag: left as is, and the author's reasoning holds. I checked it against the locked gpui-fast 0.1.9:
    • active_drag is pub(crate), so the layer cannot check the drag's type.
    • on_drag_move and on_drop both match on TypeId (elements/div.rs:371, :2952).
    • The layer's hitbox uses the default Normal behavior, and both hit_test paths (window.rs:1180, fast/composition) stop only at BlockMouse, so the surface's hitbox (render.rs:772) stays hovered.
  • P3, repeated deferred clear: fixed. needs_clear (file_drop.rs:53, called from app.rs:823) sets clearing, and clear resets it at file_drop.rs:69. I found no path that leaves the flag stuck while the window is alive, because the deferred closure always calls clear.
  • P3, enter timing docs: fixed. It is now under Contract in docs/dev/file-drop.md.

New notes (neither blocks anything)

  • P3 file_drop.rs:53: needs_clear reads like a pure query but also marks the clear as scheduled. The doc comment says so, but a name like take_needs_clear would show it at the call site in render.
  • P3 tests.rs:760: no test covers the new behavior directly. The existing drag test still passes, but nothing asserts that a clear is scheduled only once or that the target is already stored when events go out.

What I ran
Linux x86_64, on 6da4a830 merged locally with origin/main (b7a69186). The merge was clean and nothing was pushed.

  • cargo test -p nsh-platform-gpui --features runtime-shaders,shot --lib in crates/gpui: 202 passed, 0 failed.
  • cargo clippy -p nsh-platform-gpui --all-targets --features dev,runtime-shaders: no lints; the only warning is an unused workspace.package.publish manifest field.
  • cargo fmt -p nsh-platform-gpui --check, stable and nightly: clean.

Not verified

  • Real external drags: none were run on any platform, because this host cannot simulate one. Linux (Wayland/X11) and Windows remain unexercised, as the author also notes.
  • The overlay and hover reasoning above comes from reading the code, not from a running app with an in-app GPUI drag.
  • A drop over a native child view: on the fast/composition hit-test path, a native surface ends the hit test at its bounds. I did not establish whether the layer's hitbox is reached first there, so this is worth trying in the real-drag pass.

API / naming: no public API change. The new items are private or pub(crate), so no nsh-compat case is needed.

🤖 Noop (reviewer) · AI agent acting for @huacnlee

Rename needs_clear to take_needs_clear, since it marks the clear as scheduled, and test that a drag that ended schedules one clear.
@UncertaintyDeterminesYou4ndMe

Copy link
Copy Markdown
Contributor Author

Addressed both notes in a39843d:

  • needs_clear is now take_needs_clear.
  • The file drop test now checks that a destination yields one scheduled clear: a second take_needs_clear answers no, and after clear there is nothing left to clear. Removing the line that marks the clear as scheduled makes it fail.

On the drop over a native child view: in gpui-fast's fast/composition hit test, a point inside a native surface's bounds ends the test before the GPUI hitboxes behind it, so from reading the code the drop layer does not hear a drag over a web view (and the platform may hand that drag to the web view's own native view first). We haven't run it; we'll check with a real drag in the gallery.

@UncertaintyDeterminesYou4ndMe

Copy link
Copy Markdown
Contributor Author

Ran the native-child case with a real Finder drag on macOS (nsh-gpui-host from a39843df, a view with onFileDrop holding a WebView):

  • Drop on the view outside the web view: enter, drop with the file URL, leave.
  • Drag from the view onto the web view and back: leave on entering the web view, enter on coming back; the drop on the view afterwards arrives as usual.
  • Drop on the web view: the outer view gets no onFileDrop. The web view takes the drag itself and tries to navigate to the file, which the web view's navigation policy refuses (web view navigation to file:///… is not granted), so nothing is opened.

So a drop over a native child goes to that child, not to the surrounding onFileDrop view; enter/leave stay paired. Making the web view refuse file drags so the drop reaches the view around it would be a web-view change, separate from this PR. Linux and Windows remain unexercised.

@huacnlee

huacnlee commented Nov 4, 2026

Copy link
Copy Markdown
Member

Re-review of a39843df, covering only the new commit since 6da4a830, plus the native child report above. This is not a merge recommendation.

Verdict: both notes from my last review are resolved, and I found no new bugs. One minor documentation note is below.

Prior notes

  • P3, needs_clear name: fixed. It is now take_needs_clear at the definition, the call site (app.rs:823) and the doc comment.
  • P3, missing test: fixed, and the test catches the bug. I removed state.clearing.set(true) locally, and tests::windows failed at tests.rs:870 on assertion failed: !crate::file_drop::take_needs_clear(surface), as you described. It passes again with the line restored.

New note (does not block anything)

  • P3 docs/dev/file-drop.md:19: the Contract says the destination is "the closest eligible ancestor of the hit view". Your macOS run shows a native child such as a web view is an exception: it takes the drag itself, and the surrounding onFileDrop view gets leave and no drop. Since this differs from what a reader of the contract would expect, it is worth a sentence there. Changing how the web view handles file drags can stay separate, as you say.

What I ran
macOS arm64, on a39843df merged locally with origin/main (b7a69186). The merge was clean and nothing was pushed.

  • cargo test -p nsh-platform-gpui --features runtime-shaders,shot --lib in crates/gpui: 201 passed, 0 failed.
  • cargo clippy -p nsh-platform-gpui --all-targets --features dev,runtime-shaders: no lints.
  • cargo fmt -p nsh-platform-gpui --check, stable: no diffs.

Not verified

  • I did not reproduce your Finder drag: this host cannot drive a real external drag, so the native child behavior above rests on your report. From my reading of the code it fits what you described for the hit test, but I did not confirm it at runtime.
  • Real drags on Linux (Wayland/X11) and Windows remain unexercised.

🤖 Noop (reviewer) · AI agent acting for @huacnlee

@huacnlee
huacnlee merged commit 3c3e7df into longbridge:main Nov 4, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants