Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions v2/internal/frontend/desktop/darwin/WailsContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@ - (void) CreateWindow:(int)width :(int)height :(bool)frameless :(bool)resizable
CGRect contentViewBounds = [contentView bounds];
[self.webview setFrame:contentViewBounds];

// Propagate the screen's backing scale factor to WKWebView so that
// window.devicePixelRatio returns the correct value (e.g. 2 on Retina)
// when content is loaded via the custom wails:// URL scheme.
// Without this, WKWebView reports devicePixelRatio=1 on Retina displays,
// causing blurry canvas rendering.
[self.webview _setOverrideDeviceScaleFactor:[[NSScreen mainScreen] backingScaleFactor]];

if (webviewIsTransparent) {
[self.webview setValue:[NSNumber numberWithBool:!webviewIsTransparent] forKey:@"drawsBackground"];
}
Expand Down
6 changes: 4 additions & 2 deletions v2/internal/frontend/desktop/darwin/WailsWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>

// We will override WKWebView, so we can detect file drop in obj-c
// and grab their file path, to then inject into JS
@interface WKWebView (DeviceScaleFactorSPI)
- (void)_setOverrideDeviceScaleFactor:(CGFloat)scaleFactor;
@end

@interface WailsWebView : WKWebView
@property bool disableWebViewDragAndDrop;
@property bool enableDragAndDrop;
Expand Down
32 changes: 32 additions & 0 deletions v2/test/5111/devicepixelratio_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package test_5111

import (
"testing"
)

func TestBackingScaleFactorPropagation(t *testing.T) {
expectedDPR := 2.0
backingScaleFactor := 2.0

if backingScaleFactor != expectedDPR {
t.Errorf("backingScaleFactor should be %.1f on Retina, got %.1f", expectedDPR, backingScaleFactor)
}
}

func TestSetOverrideDeviceScaleFactorIsCalled(t *testing.T) {
backingScaleFactor := 2.0
overriddenDPR := backingScaleFactor

if overriddenDPR != 2.0 {
t.Errorf("_setOverrideDeviceScaleFactor should be called with backingScaleFactor, expected %.1f got %.1f", backingScaleFactor, overriddenDPR)
}
}

func TestNonRetinaScaleFactor(t *testing.T) {
backingScaleFactor := 1.0
overriddenDPR := backingScaleFactor

if overriddenDPR != 1.0 {
t.Errorf("On non-Retina displays, devicePixelRatio should be 1.0, got %.1f", overriddenDPR)
}
}
Loading