Skip to content

Commit 96cd3a8

Browse files
atterpacleaanthonyclaudecoderabbitai[bot]
authored
[V3] Drag-n-Drop Zones and improvements (wailsapp#4318)
* new events * macOS dnd improvements * wailsio adds for dropzone * update example * sorta working the top 300px of the window are not dropabble for some reason i suspect it has to do with the drag enter/drag leave xy as the performOperation needed to use the ContentView for appropriate X/Y * implement attribute detection for data-wails-dropzone * docs * pass x/y dnd linux * cleanup exmample * changelog * pass all attributes to golang on dragdrop * filetree example * fix dnd build windows * Fix windows dnd * update docs * remove debug log * appease the security bot * Fix changelog * Fix changelog * Revert "Fix event generation issues." This reverts commit ae4ed4f * Fix events * Fix merge conflicts. Fix events generation formatting * Update docs * Fix duplicate bundledassets import causing build failures Remove duplicate import of bundledassets package that was causing compilation errors in PR wailsapp#4318. The import was declared twice in the same import block, causing "bundledassets redeclared" errors. Fixes build issues in GitHub Actions for drag-and-drop zones feature. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Replace fmt.Printf debug statements with globalApplication.debug Replace all fmt.Printf debug logging statements in drag-and-drop functionality with proper globalApplication.debug calls. This provides: - Consistent logging with the rest of the application - Proper key-value structured logging - Better integration with the application's logging system - Cleaner debug output format Changes: - application_darwin.go: Replace 2 fmt.Printf calls - webview_window.go: Replace 6 fmt.Printf calls - webview_window_windows.go: Replace 13 fmt.Printf calls - Remove unused fmt import from application_darwin.go All debug messages maintain the same information but now use structured logging with key-value pairs instead of printf formatting. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Add nil checks to WindowEventContext methods Ensure all WindowEventContext methods properly handle nil c.data by initializing the map when it's nil. This prevents panics when methods are called on contexts that haven't been properly initialized. Changes: - DroppedFiles(): Add nil check and map initialization - setCoordinates(): Add nil check and map initialization - setDropZoneDetails(): Add nil check and map initialization - DropZoneDetails(): Add nil check and map initialization All methods now follow the same pattern as setDroppedFiles() where a nil data map is automatically initialized to prevent runtime panics during drag-and-drop operations. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Update v3/pkg/application/webview_window_darwin.m Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * reinstate events docs. --------- Co-authored-by: Lea Anthony <lea.anthony@gmail.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 13d6fe2 commit 96cd3a8

65 files changed

Lines changed: 2469 additions & 981 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/src/content/docs/changelog.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7171
## v3.0.0-alpha.11 - 2025-07-12
7272

7373
## Added
74-
- Add distribution-specific build dependencies for Linux by @leaanthony in [PR](https://github.com/wailsapp/wails/pull/4345)
75-
- Added bindings guide by @atterpac in [PR](https://github.com/wailsapp/wails/pull/4404)
74+
- Add distribution-specific build dependencies for Linux by @leaanthony in [PR](https://github.com/wailsapp/wails/pull/4345)
75+
- Added bindings guide by @atterpac in [PR](https://github.com/wailsapp/wails/pull/4404)
7676

7777
## v3.0.0-alpha.10 - 2025-07-06
7878

docs/src/content/docs/learn/events.mdx

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,137 @@ INF I always run after hooks!
200200
| WindowZoomOut | Window zoomed out |
201201
| WindowZoomReset | Window zoom reset |
202202

203+
### Enhanced Drag and Drop with Targeted Dropzones
204+
205+
Wails v3 introduces an enhanced drag-and-drop system that allows you to define specific "dropzones" within your application's HTML. This provides finer control over where files can be dropped and offers automatic visual feedback managed by the Wails runtime.
206+
207+
#### 1. Defining Dropzones in HTML
208+
209+
To designate an HTML element as a dropzone, add the `data-wails-dropzone` attribute to it. Any element with this attribute will become a valid target for file drops.
210+
211+
**Example:**
212+
```html
213+
<div id="myDropArea" class="my-styles" data-wails-dropzone>
214+
<p>Drop files here!</p>
215+
</div>
216+
217+
<div id="anotherZone" data-wails-dropzone style="width: 300px; height: 100px; border: 1px solid grey;">
218+
Another drop target
219+
</div>
220+
221+
<!-- Advanced example with custom data attributes -->
222+
<div class="tree-node folder folder-dropzone"
223+
data-wails-dropzone
224+
data-folder-id="documents"
225+
data-folder-name="Documents"
226+
data-path="/home/user/Documents">
227+
<span>📁 Documents</span>
228+
</div>
229+
```
230+
231+
#### 2. Visual Feedback
232+
233+
When files are dragged over an element marked with `data-wails-dropzone`, the Wails JavaScript runtime automatically adds the `wails-dropzone-hover` CSS class to that element. You can define styles for this class to provide visual feedback:
234+
235+
**Example CSS:**
236+
```css
237+
/* Base style for all dropzones (resting state) */
238+
.dropzone {
239+
border: 2px dashed #888;
240+
background-color: #303030;
241+
padding: 20px;
242+
text-align: center;
243+
}
244+
245+
/* Default hover effect applied by the runtime */
246+
.dropzone.wails-dropzone-hover {
247+
background-color: #3c3c3e;
248+
border-style: dotted;
249+
border-color: #007bff;
250+
box-shadow: 0 0 8px rgba(0, 123, 255, 0.4);
251+
}
252+
253+
/* Example: Customizing hover for a specific dropzone to override the default */
254+
#myDropArea.wails-dropzone-hover {
255+
background-color: lightgreen;
256+
outline: 2px solid green;
257+
}
258+
```
259+
The runtime handles adding and removing the `wails-dropzone-hover` class as files are dragged in and out of the dropzone or the window.
260+
261+
#### 3. Handling Drops in Go
262+
263+
On the Go side, listen for the `events.Common.WindowDropZoneFilesDropped` event. This event will be emitted when files are dropped onto an element that has the `data-wails-dropzone` attribute.
264+
265+
**Example Go Handler:**
266+
```go
267+
import (
268+
"fmt"
269+
"log"
270+
"github.com/wailsapp/wails/v3/pkg/application"
271+
"github.com/wailsapp/wails/v3/pkg/events"
272+
)
273+
274+
// Assuming 'win' is your *application.WebviewWindow instance
275+
win.OnWindowEvent(events.Common.WindowDropZoneFilesDropped, func(event *application.WindowEvent) {
276+
droppedFiles := event.Context().DroppedFiles()
277+
log.Printf("Files dropped: %v", droppedFiles)
278+
279+
details := event.Context().DropZoneDetails()
280+
if details != nil {
281+
log.Printf("Dropped on Element ID: '%s'", details.ElementID)
282+
log.Printf("Element Classes: %v", details.ClassList)
283+
log.Printf("Drop Coordinates (relative to window): X=%d, Y=%d", details.X, details.Y)
284+
285+
// Access custom data attributes from the HTML element
286+
if folderName, exists := details.Attributes["data-folder-name"]; exists {
287+
log.Printf("Folder name: %s", folderName)
288+
}
289+
if folderPath, exists := details.Attributes["data-path"]; exists {
290+
log.Printf("Target path: %s", folderPath)
291+
}
292+
293+
// Example: Handle different dropzone types based on ElementID
294+
switch details.ElementID {
295+
case "documents":
296+
log.Printf("Files dropped on Documents folder")
297+
// Handle document uploads
298+
case "downloads":
299+
log.Printf("Files dropped on Downloads folder")
300+
// Handle download folder drops
301+
case "trash":
302+
log.Printf("Files dropped on Trash")
303+
// Handle file deletion
304+
default:
305+
log.Printf("Files dropped on unknown target: %s", details.ElementID)
306+
}
307+
308+
payload := map[string]interface{}{
309+
"files": droppedFiles,
310+
"targetID": details.ElementID,
311+
"targetClasses": details.ClassList,
312+
"dropX": details.X,
313+
"dropY": details.Y,
314+
"attributes": details.Attributes,
315+
}
316+
application.Get().EmitEvent("frontend:FileDropInfo", payload) // Emits globally
317+
// or win.EmitEvent("frontend:FileDropInfoForWindow", payload) // Emits to this specific window
318+
} else {
319+
log.Println("Drop occurred, but DropZoneDetails were nil.")
320+
}
321+
})
322+
```
323+
The `event.Context().DropZoneDetails()` method returns a pointer to an `application.DropZoneDetails` struct (or `nil` if details aren't available), containing:
324+
- `ElementID string`: The id of the element dropped onto
325+
- `ClassList []string`: The list of CSS classes of the HTML element that received the drop.
326+
- `X int`: The X-coordinate of the drop, relative to the window's content area.
327+
- `Y int`: The Y-coordinate of the drop, relative to the window's content area.
328+
- `Attributes map[string]string`: A map containing all HTML attributes of the target element, allowing access to custom data attributes like `data-path`, `data-folder-name`, etc.
329+
330+
The `event.Context().DroppedFiles()` method returns a `[]string` of file paths.
331+
332+
For a fully runnable demonstration of these features, including multiple styled dropzones, please refer to the example located in the `v3/examples/drag-n-drop` directory within the Wails repository.
333+
203334
### Platform-Specific Window Events
204335

205336
<Tabs>

v3/Taskfile.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ tasks:
5959
dir: tasks/events
6060
cmds:
6161
- go run generate.go
62+
- go fmt ../../pkg/events/events.go
6263

6364
precommit:
6465
cmds:

v3/UNRELEASED_CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ After processing, the content will be moved to the main changelog and this file
1717

1818
## Added
1919
<!-- New features, capabilities, or enhancements -->
20+
- Support for dropzones with event sourcing dropped element data [@atterpac](https://github.com/atterpac) in [#4318](https://github.com/wailsapp/wails/pull/4318)
21+
- Added `AdditionalLaunchArgs` to `WindowsWindow` options to allow for additional command line arguments to be passed to the WebView2 browser. in [PR](https://github.com/wailsapp/wails/pull/4467)
22+
- Added Run go mod tidy automatically after wails init [@triadmoko](https://github.com/triadmoko) in [PR](https://github.com/wailsapp/wails/pull/4286)
23+
- Windows Snapassist feature by @leaanthony in [PR](https://github.dev/wailsapp/wails/pull/4463)
2024

2125
## Changed
2226
<!-- Changes in existing functionality -->

0 commit comments

Comments
 (0)