Skip to content

Commit ae04fea

Browse files
leaanthonyclaude
andcommitted
[v2] fix: simplify DragAndDrop fix - remove OnDomReady suppression
The previous fix was over-engineered. It suppressed OnDomReady events on all platforms to prevent multiple calls during file drops. However, users may have legitimate use cases for OnDomReady being called on navigation/reload. This simplified fix: - Keeps the JavaScript setup() that runs on each DOM load to register drag handlers early (preventing browser navigation on file drops) - Keeps the Windows AllowExternalDrag fix for EnableFileDrop+DisableWebViewDrop - Removes the OnDomReady suppression on macOS, Linux, and Windows The core insight is that by preventing browser navigation on file drops via JavaScript, the page won't reload, so OnDomReady won't fire extra times in the problematic scenario anyway. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7ddb49b commit ae04fea

7 files changed

Lines changed: 10 additions & 40 deletions

File tree

v2/internal/frontend/desktop/darwin/WailsContext.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050

5151
@property bool devtoolsEnabled;
5252
@property bool defaultContextMenuEnabled;
53-
@property bool domReadySent;
5453

5554
@property (retain) WKUserContentController* userContentController;
5655

v2/internal/frontend/desktop/darwin/WailsContext.m

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,7 @@ - (void)webView:(nonnull WKWebView *)webView stopURLSchemeTask:(nonnull id<WKURL
473473
}
474474

475475
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
476-
// Only send DomReady once to prevent OnDomReady callback from being called
477-
// multiple times (e.g., on navigation or page reload)
478-
if (!self.domReadySent) {
479-
self.domReadySent = YES;
480-
processMessage("DomReady");
481-
}
476+
processMessage("DomReady");
482477
}
483478

484479
- (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {

v2/internal/frontend/desktop/linux/window.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -469,20 +469,11 @@ gboolean UnFullscreen(gpointer data)
469469
return G_SOURCE_REMOVE;
470470
}
471471

472-
// Flag to track if DomReady has been sent - ensures OnDomReady callback is only called once
473-
static gboolean domReadySent = FALSE;
474-
475472
static void webviewLoadChanged(WebKitWebView *web_view, WebKitLoadEvent load_event, gpointer data)
476473
{
477474
if (load_event == WEBKIT_LOAD_FINISHED)
478475
{
479-
// Only send DomReady once to prevent OnDomReady callback from being called
480-
// multiple times (e.g., on navigation or page reload)
481-
if (!domReadySent)
482-
{
483-
domReadySent = TRUE;
484-
processMessage("DomReady");
485-
}
476+
processMessage("DomReady");
486477
}
487478
}
488479

v2/internal/frontend/desktop/windows/frontend.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,10 @@ func (f *Frontend) ExecJS(js string) {
907907
}
908908

909909
func (f *Frontend) navigationCompleted(sender *edge.ICoreWebView2, args *edge.ICoreWebView2NavigationCompletedEventArgs) {
910+
if f.frontendOptions.OnDomReady != nil {
911+
go f.frontendOptions.OnDomReady(f.ctx)
912+
}
913+
910914
if f.frontendOptions.Frameless && f.frontendOptions.DisableResize == false {
911915
f.ExecJS("window.wails.flags.enableResize = true;")
912916
}
@@ -920,12 +924,6 @@ func (f *Frontend) navigationCompleted(sender *edge.ICoreWebView2, args *edge.IC
920924
}
921925
f.hasStarted = true
922926

923-
// Only call OnDomReady on the first navigation completion to prevent
924-
// multiple calls when page reloads or navigates (e.g., during file drops)
925-
if f.frontendOptions.OnDomReady != nil {
926-
go f.frontendOptions.OnDomReady(f.ctx)
927-
}
928-
929927
// Hack to make it visible: https://github.com/MicrosoftEdge/WebView2Feedback/issues/1077#issuecomment-825375026
930928
err := f.chromium.Hide()
931929
if err != nil {

v2/internal/frontend/runtime/desktop/draganddrop.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {EventsOn, EventsOff} from "./events";
1414

1515
const flags = {
1616
registered: false,
17-
handlersSetup: false,
1817
defaultUseDropTarget: true,
1918
useDropTarget: true,
2019
nextDeactivate: null,
@@ -247,9 +246,6 @@ export function OnFileDrop(callback, useDropTarget) {
247246
const uDTPT = typeof useDropTarget;
248247
flags.useDropTarget = uDTPT === "undefined" || uDTPT !== "boolean" ? flags.defaultUseDropTarget : useDropTarget;
249248

250-
// Ensure handlers are set up (idempotent - safe to call multiple times)
251-
setup();
252-
253249
let cb = callback;
254250
if (flags.useDropTarget) {
255251
cb = function (x, y, paths) {
@@ -277,16 +273,13 @@ export function OnFileDropOff() {
277273
}
278274

279275
/**
280-
* setup installs the drag and drop handlers early to prevent browser navigation
276+
* setup installs the drag and drop handlers to prevent browser navigation
281277
* when files are dropped. This is called from the runtime initialization to ensure
282278
* handlers are in place before the user has a chance to drop files.
283279
* The actual file processing only happens when enableWailsDragAndDrop flag is true.
280+
* This runs on every DOM load to ensure handlers are always registered.
284281
*/
285282
export function setup() {
286-
if (flags.handlersSetup) {
287-
return;
288-
}
289-
flags.handlersSetup = true;
290283
window.addEventListener('dragover', onDragOver);
291284
window.addEventListener('dragleave', onDragLeave);
292285
window.addEventListener('drop', onDrop);

v2/internal/frontend/runtime/runtime_debug_desktop.js

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2/internal/frontend/runtime/runtime_prod_desktop.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)