Repository navigation
Description
Summary
With nativeapi 0.6.2 DropTarget on Windows, hovering a dragged file over the app window (mouse not released, Drop never fires) completely freezes the drag-source process (explorer.exe) until the drag is force-aborted. The app-side Dart callback is never observed to run.
We verified the native→Dart event pipeline is non-blocking at every hop, so the freeze is not in the Dart dispatch chain. After a line-by-line comparison against two mature Flutter drag-drop implementations (desktop_drop 0.9.2, super_drag_and_drop / super_native_extensions), suspicion narrows to two things nativeapi does differently from both: ungated IDropTargetHelper calls on every DragOver and registering on the top-level window HWND instead of the FlutterView child window.
Environment
- nativeapi 0.6.2 + cnativeapi 0.6.2 (MSVC native build), Windows 11 build 10.0.26300 x64
- Flutter 3.50.1 stable, Windows desktop (custom frameless window with DWM attribute surgery:
DWMWA_NCRENDERING_POLICY,DWMWA_BORDER_COLOR,DWMWA_WINDOW_CORNER_PREFERENCE, fullscreen ncrendering suppression)
Minimal repro
final target = na.DropTarget.create(na.WindowManager.instance.getCurrent());
target!.addListener((event) { /* any lightweight Dart callback */ });
// first listener triggers lazy RegisterDragDrop- Drag an
.mp4from Explorer, hover over the app window without releasing the mouse. - Observed:
explorer.exe(the drag source) freezes immediately until the drag is force-aborted; no Dart callback observed. - Control: same app, same window, with desktop_drop 0.9.2 as the backend — all six drag checks pass.
What we ruled out
- RegisterDragDrop conflict — reproduced with desktop_drop fully removed (single-variable isolation).
- OleInitialize failure /
GetNativeObjectnull — registration demonstrably succeeds (hover events fire); those failures can only yield "no drag response", never a frozen source. - Dart dispatch blocking — the pipeline is post-only end to end:
DropTargetCom::DragEnter/DragOver→Impl::Entered/Moved(same-threadEmit) →event_emitter.hEmit(dispatch outside the listener lock) → C trampoline → DartNativeCallable.listener(post semantics, never blocks native). - Source-data reads too heavy — nativeapi reads are lighter than desktop_drop's, which works fine.
Remaining synchronous work inside the callbacks (the suspects)
DragEnter/DragOver call IDropTargetHelper unconditionally whenever helper_ exists — no activation gate (cnativeapi-0.6.2/cxx_impl/src/platform/windows/drop_target_windows.cpp:111-152):
HRESULT STDMETHODCALLTYPE DragOver(DWORD /*key_state*/, POINTL point, DWORD* effect) override {
const DWORD allowed = *effect;
*effect = handler_ ? handler_->Moved(point, allowed) : DROPEFFECT_NONE;
if (helper_) { // ← ungated: called on EVERY move
POINT pt = {point.x, point.y};
helper_->DragOver(&pt, *effect); // shell drag-image manager
}
return S_OK;
}Registration uses the top-level window (:237-259):
bool DropTarget::Impl::Register() {
HWND hwnd = static_cast<HWND>(window->GetNativeObject()); // top-level HWND
...
// OLE finds the target by walking up from the window under the cursor, so
// registering the top-level window also covers child windows (a Flutter view).
if (FAILED(RegisterDragDrop(hwnd, com))) { ... }
}Contrast with the two mature implementations:
| nativeapi 0.6.2 | desktop_drop 0.9.2 | super_native_extensions | |
|---|---|---|---|
| Registered HWND | top-level window | FlutterView child window | FlutterView child window |
| Helper gating | none (if (helper_) always) |
drag_image_active_ gate set only after a successful DragEnter |
helper used, but still on the FlutterView HWND |
| Pre-register hygiene | — | explicit error on failure | RevokeDragDrop first + warn if HWND already has a target |
Ranked suspicion
- Ungated
IDropTargetHelper+ top-level HWND — the shell drag-image manager's internal round-trip wedging for this window shape (top-level + DWM-modified, no redirection bitmap) would leaveDragOvernever returning; since the source'sDoDragDropwaits synchronously for target callbacks, the source STA freezes →explorer.exehangs. - Top-level-window registration itself (same divergence axis; one instrumented run can adjudicate both).
- Late
ScopedOleInitialize(runtime first-listener) — minor, not the blocker.
A definitive adjudication needs a short instrumented run upstream (A/B: helper calls on/off; top-level vs child-window HWND; DragOver throttle on/off). We are not pursuing it further in our app (drop_target archived as disabled), but are happy to test a patched build.
Suggestions
- Register on the FlutterView child window by default (or expose a target-window parameter) — both mature packages do this; top-level registration is unverified against drag-image/DWM-modified windows.
- Gate
IDropTargetHelpercalls on a successfulDragEnter(desktop_drop'sdrag_image_active_pattern); todayDragOvercalls the helper on every move even ifDragEnternever succeeded. - Move
ScopedOleInitializeto plugin/engine setup time (one-shot), matching both mature packages. RevokeDragDrop+ warn before registering (super_native_extensions pattern), with actionable guidance on failure (e.g.E_OUTOFMEMORY→ hintOleInitializevsCoInitializeEx).- Optional: throttle
Movedevents (per-move C-event construction + handle lease + post under high-frequency moves adds queue pressure). - Add a per-callback-stage tracing switch (
QueryPerformanceCounter/OutputDebugString: Entered → Emit → post → helper per-stage timing) — the biggest cost of diagnosing this was the lack of it.
中文版(Chinese version)
标题:Windows: DropTarget 悬停事件使拖拽源(explorer.exe)完全冻结——事件管道非阻塞,疑点收敛于 IDropTargetHelper + 顶层窗口注册
摘要:nativeapi 0.6.2 的 DropTarget(Windows)在拖拽源文件悬停于窗口上方(未释放鼠标、Drop 未触发)时,使拖拽源进程 explorer.exe 完全冻结,需强制中止拖拽。应用侧 Dart 回调未观察到执行。
复现:DropTarget.create(...) + addListener(首 listener 触发惰性 RegisterDragDrop)→ 从 explorer 拖 mp4 悬停窗口 → explorer.exe 立即冻结。对照:同 app 同窗口 desktop_drop 0.9.2 后端六项全过。
源码定位(0.6.2):事件管道逐跳核验为非阻塞(drop_target_windows.cpp → drop_target.cpp 同线程 Emit → event_emitter.h 锁外派发 → drop_target_c.cpp trampoline → Dart NativeCallable.listener post 语义)。回调内仅剩的同步跨进程工作:QueryGetData×2(轻)与 IDropTargetHelper::DragEnter/DragOver(drop_target_windows.cpp:117-120、127-130——后者每 move 无条件调用、无激活门控;hwnd 为顶层窗口)。注册面:RegisterDragDrop 用顶层窗口(:238-256),而 desktop_drop 与 super_native_extensions 均注册 FlutterView 子窗口;desktop_drop 对 helper 带激活门控,SNE 注册前先 Revoke 并警告。
嫌疑排序:① 顶层窗口 + 无门控 IDropTargetHelper(shell 拖影链路在该窗口形态下楔死内部往返 → 目标回调不返回 → 源端 DoDragDrop 同步等待卡死);② 顶层窗口注册本身;③ OLE 初始化时机偏晚。原始三候选(RegisterDragDrop 冲突 / OleInitialize 失败 / GetNativeObject null)均被证据排除——三者只能导致「无拖放」,无法冻结源进程。
建议:子窗口注册(或参数化 target window)、helper 激活门控、OleInitialize 前移到装配期、注册前 Revoke+警告、Moved 节流、阶段插桩开关。
Activity
Merge pull request #58 from libnativeapi/fix/issue-57
lijy91 commentedon Nov 29, 2026
Fixed in libnativeapi/nativeapi-core#102 and synced to main in #58.
Root Cause
Under Flutter's
UIThreadPolicy::RunOnSeparateThread, adding aDropTargetlistener ranOleInitialize,new DropTargetCom, andRegisterDragDropon the Dart UI isolate thread, which has no Win32 message loop. BecauseDropTargetComlived in an unpumped STA apartment, OLE's cross-apartmentDragEnterRPC fromexplorer.exedeadlocked indefinitely.Fix
OleInitialize,DropTargetComcreation,RegisterDragDrop, and teardown (RevokeDragDrop/Release/OleUninitialize) onto theHWND's owning Win32 thread, and handledDRAGDROP_E_ALREADYREGISTEREDby revoking and retrying once.IDropTargetHelpercalls behindhelper_active_(only set whenhelper_->DragEntersucceeds) and skipped redundanthelper_->DragOvercalls when position and effect are unchanged.DragEnterand throttledDropTargetMovedEventemission to ~60 Hz (16 ms) while returningDROPEFFECTimmediately on everyDragOverfor smooth drag feedback.DropTargetregistration from a thread without a Win32 message loop.This is now on
mainand will be included in the next release.