Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix backend drag and drop on Linux #3949

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

makew0rld
Copy link

@makew0rld makew0rld commented Dec 14, 2024

Description

This PR fixes #3686 by copying a dead PR, #3704.

Type of change

Please select the option that is relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration using wails doctor.

  • Windows: works
  • macOS: works
  • Linux: works

If you checked Linux, please specify the distro and version: Arch Linux latest

Test Configuration

# Wails
Version         | v2.9.2
Package Manager | pacman

# System
┌───────────────────────────────────────────────────────────────────────────────────────┐
| OS           | Arch Linux                                                             |
| Version      | Unknown                                                                |
| ID           | arch                                                                   |
| Go Version   | go1.23.3                                                               |
| Platform     | linux                                                                  |
| Architecture | amd64                                                                  |
| CPU          | 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz                         |
| GPU          | TigerLake-LP GT2 [Iris Xe Graphics] (Intel Corporation) - Driver: i915 |
| Memory       | 15GB                                                                   |
└───────────────────────────────────────────────────────────────────────────────────────┘

# Dependencies
┌─────────────────────────────────────────────────────────────────────┐
| Dependency | Package Name | Status    | Version                     |
| *docker    | docker       | Installed | 1:27.3.1-1                  |
| gcc        | gcc          | Installed | 14.2.1+r134+gab884fffe3fc-1 |
| libgtk-3   | gtk3         | Installed | 1:3.24.43-4                 |
| libwebkit  | webkit2gtk   | Installed | 2.46.4-1                    |
| npm        | npm          | Installed | 10.9.0-1                    |
| pkg-config | pkgconf      | Installed | 2.1.1-1                     |
└────────────────────── * - Optional Dependency ──────────────────────┘

# Diagnosis
 SUCCESS  Your system is ready for Wails development!

Checklist:

  • I have updated website/src/pages/changelog.mdx with details of this PR
  • My code follows the general coding style of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

I don't fully understand this PR (hopefully you do instead) but the basic reason why it works is because it registers event listeners that call e.PreventDefault() so the default file drop behaviour is stopped.

This requires doc updates because callers need to know that backend file drop handlers should be registered in OnDomReady, not OnStartup.

Summary by CodeRabbit

  • New Features

    • Enhanced drag-and-drop functionality with improved event listener management.
    • Added support for file drop events, ensuring listeners are registered only once during initialization.
  • Bug Fixes

    • Improved handling of drag-and-drop operations to ensure callbacks are invoked only on valid drop targets.

Copy link
Contributor

coderabbitai bot commented Dec 14, 2024

Walkthrough

The changes in this pull request focus on enhancing the drag-and-drop functionality within the application. Key modifications include the introduction of an EventsOnce listener that initializes drag-and-drop event handlers when the "wails:init-file-drop" event is triggered. This ensures that event listeners are registered only once, improving efficiency. Additionally, the OnFileDrop function has been updated to emit an event at the start of the file drop process while maintaining its original logic.

Changes

File Path Change Summary
v2/internal/frontend/runtime/desktop/draganddrop.js Added EventsOnce import and listener for "wails:init-file-drop" to manage drag-and-drop events.
v2/internal/frontend/runtime/runtime_debug_desktop.js Introduced EventsOnce listener for drag-and-drop initialization based on the enableWailsDragAndDrop flag.
v2/pkg/runtime/draganddrop.go Modified OnFileDrop to emit "wails:init-file-drop" event at the start of the drop process.

Assessment against linked issues

Objective Addressed Explanation
Ensure file view does not appear when dropping files (3686)

Possibly related PRs

Suggested reviewers

  • atterpac

🐰 In the land of code where rabbits play,
Drag-and-drop now works in a better way!
Events registered just once, oh so neat,
Our UI stays intact, a real treat!
With each file dropped, a new event sings,
Hopping along, we embrace these changes! 🐇✨

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. This feature will be included in our Pro Plan when released.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 42e7750 and 4acbbe8.

📒 Files selected for processing (3)
  • v2/internal/frontend/runtime/desktop/draganddrop.js (2 hunks)
  • v2/internal/frontend/runtime/runtime_debug_desktop.js (1 hunks)
  • v2/pkg/runtime/draganddrop.go (1 hunks)
🔇 Additional comments (2)
v2/pkg/runtime/draganddrop.go (1)

14-16: LGTM! Event emission coordinates frontend initialization.

The addition of EventsEmit for "wails:init-file-drop" ensures proper initialization sequence between backend and frontend.

Let's verify the event handling coordination:

✅ Verification successful

Event coordination between backend and frontend is properly implemented

The verification confirms proper event handling coordination:

  • Backend emits "wails:init-file-drop" event in draganddrop.go
  • Frontend listens for this event using EventsOnce in both draganddrop.js and runtime_debug_desktop.js
  • Upon receiving the event, frontend registers drag-and-drop event listeners (dragover, dragleave, drop) only if the feature flag is enabled
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify event handling coordination between backend and frontend
# Test: Search for event handler registration. Expect: Event emission in backend and handler in frontend.

# Check backend event emission
rg -A 2 "wails:init-file-drop"

# Check frontend event handling
ast-grep --pattern 'EventsOnce("wails:init-file-drop", $_)'

Length of output: 2254

v2/internal/frontend/runtime/desktop/draganddrop.js (1)

247-254: LGTM! Well-structured event handling initialization.

The implementation:

  • Uses EventsOnce for one-time registration
  • Properly checks enableWailsDragAndDrop flag
  • Reuses existing handler functions

Comment on lines +568 to +574
EventsOnce("wails:init-file-drop", () => {
if (window.wails.flags.enableWailsDragAndDrop) {
window.addEventListener("dragover", onDragOver);
window.addEventListener("dragleave", onDragLeave);
window.addEventListener("drop", onDrop);
}
});
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Remove duplicate event handler registration.

This event handler registration is identical to the one in draganddrop.js. Having duplicate handlers could cause issues and makes maintenance more difficult.

Apply this diff to remove the duplicate code:

-  EventsOnce("wails:init-file-drop", () => {
-    if (window.wails.flags.enableWailsDragAndDrop) {
-      window.addEventListener("dragover", onDragOver);
-      window.addEventListener("dragleave", onDragLeave);
-      window.addEventListener("drop", onDrop);
-    }
-  });

@leaanthony
Copy link
Member

@atterpac - any thoughts on this?

@makew0rld
Copy link
Author

Any chance this could be merged soon? Thanks.

@makew0rld
Copy link
Author

makew0rld commented Jan 5, 2025

Ok I am seeing one issue with this PR so far, which is that it doesn't seem to work properly with a built binary (wails build) even though wails dev works fine.

I don't get why this would happen. Maybe related to #3563?

It would be good if someone else could confirm this.

@leaanthony
Copy link
Member

I can try to look at this this week at some point. Perhaps @atterpac could look at it if he has time.

@atterpac
Copy link
Member

atterpac commented Jan 5, 2025

I can try to look at this this week at some point. Perhaps @atterpac could look at it if he has time.

I can look into it

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.

File drag and drop displays file in Linux WebKit
3 participants