Skip to content

Commit 4c68e20

Browse files
committed
fix: convert physical pixels to CSS pixels before passing to windows webview
1 parent f1037c8 commit 4c68e20

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

v3/UNRELEASED_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ After processing, the content will be moved to the main changelog and this file
2525
## Fixed
2626
- Fixed linux desktop.tmpl protocol range, by removing `<.Info.Protocol>` to `<.Protocol>` by @Tolfx in #4510
2727
- Fixed redefinition error for liquid glass demo in [#4542](https://github.com/wailsapp/wails/pull/4542) by @Etesam913
28+
- Fixed DnD dropzone detection failing at non-100% display scaling on Windows [#4632](https://github.com/wailsapp/wails/pull/4632) by @yulesxoxo
2829

2930
## Deprecated
3031
<!-- Soon-to-be removed features -->

v3/internal/runtime/desktop/@wailsio/runtime/src/window.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,8 @@ class Window {
539539
* Gathers information about the drop target element and sends it back to the Go backend.
540540
*
541541
* @param filenames - An array of file paths (strings) that were dropped.
542-
* @param x - The x-coordinate of the drop event.
543-
* @param y - The y-coordinate of the drop event.
542+
* @param x - The x-coordinate of the drop event, in logical (CSS) pixels relative to the webview.
543+
* @param y - The y-coordinate of the drop event, in logical (CSS) pixels relative to the webview.
544544
*/
545545
HandlePlatformFileDrop(filenames: string[], x: number, y: number): void {
546546
const element = document.elementFromPoint(x, y);

v3/pkg/application/webview_window_windows.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package application
55
import (
66
"errors"
77
"fmt"
8+
"math"
89
"net/url"
910
"strconv"
1011
"strings"
@@ -605,12 +606,25 @@ func (w *windowsWebviewWindow) convertWindowToWebviewCoordinates(windowX, window
605606
globalApplication.debug("[DragDropDebug] convertWindowToWebviewCoordinates: Calculated offset", "offsetX", offsetX, "offsetY", offsetY)
606607

607608
// Convert window-relative coordinates to webview-relative coordinates
608-
webviewX := windowX - offsetX
609-
webviewY := windowY - offsetY
609+
webviewPhysicalX := windowX - offsetX
610+
webviewPhysicalY := windowY - offsetY
610611

611-
globalApplication.debug("[DragDropDebug] convertWindowToWebviewCoordinates: Final webview coordinates", "webviewX", webviewX, "webviewY", webviewY)
612+
globalApplication.debug("[DragDropDebug] convertWindowToWebviewCoordinates: Webview coordinates before DPI Scaling", "webviewPhysicalX", webviewPhysicalX, "webviewPhysicalY", webviewPhysicalY)
612613

613-
return webviewX, webviewY
614+
// Get DPI for this window
615+
dpi := w32.GetDpiForWindow(w.hwnd)
616+
// Convert to scale factor: 96 DPI == 1.0 (100%)
617+
scaleFactor := float64(dpi) / 96.0
618+
globalApplication.debug("[DragDropDebug] convertWindowToWebviewCoordinates: DPI info", "dpi", dpi, "scaleFactor", scaleFactor)
619+
620+
// Convert physical pixels -> logical/CSS pixels by dividing by the scale factor
621+
// Use rounding to avoid truncation artefacts
622+
webviewLogicalX := int(math.Round(float64(webviewPhysicalX) / scaleFactor))
623+
webviewLogicalY := int(math.Round(float64(webviewPhysicalY) / scaleFactor))
624+
globalApplication.debug("[DragDropDebug] convertWindowToWebviewCoordinates: Final webview coordinates (logical/CSS pixels)",
625+
"webviewLogicalX", webviewLogicalX, "webviewLogicalY", webviewLogicalY)
626+
627+
return webviewLogicalX, webviewLogicalY
614628
}
615629

616630
func (w *windowsWebviewWindow) physicalBounds() Rect {

0 commit comments

Comments
 (0)