From b0152dcac5b7e7e3112089949856019c65ce2e23 Mon Sep 17 00:00:00 2001
From: bruvzg <7645683+bruvzg@users.noreply.github.com>
Date: Thu, 1 Oct 2020 22:52:20 +0300
Subject: [PATCH] Fix `screen_get_dpi` on macOS for non fractional display
scales and restore documentation.
---
doc/classes/DisplayServer.xml | 12 ++++++++++++
platform/osx/display_server_osx.mm | 13 ++++++++++---
2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/doc/classes/DisplayServer.xml b/doc/classes/DisplayServer.xml
index 28194587ab98..53776b775230 100644
--- a/doc/classes/DisplayServer.xml
+++ b/doc/classes/DisplayServer.xml
@@ -521,6 +521,18 @@
+ Returns the dots per inch density of the specified screen. If [code]screen[/code] is [/code]SCREEN_OF_MAIN_WINDOW[/code] (the default value), a screen with the main window will be used.
+ [b]Note:[/b] On macOS, returned value is inaccurate if fractional display scaling mode is used.
+ [b]Note:[/b] On Android devices, the actual screen densities are grouped into six generalized densities:
+ [codeblock]
+ ldpi - 120 dpi
+ mdpi - 160 dpi
+ hdpi - 240 dpi
+ xhdpi - 320 dpi
+ xxhdpi - 480 dpi
+ xxxhdpi - 640 dpi
+ [/codeblock]
+ [b]Note:[/b] This method is implemented on Android, Linux, macOS and Windows. Returns [code]72[/code] on unsupported platforms.
diff --git a/platform/osx/display_server_osx.mm b/platform/osx/display_server_osx.mm
index 49f0e7bfa3c5..cd5b71890c75 100644
--- a/platform/osx/display_server_osx.mm
+++ b/platform/osx/display_server_osx.mm
@@ -2246,11 +2246,18 @@ static void displays_arrangement_changed(CGDirectDisplayID display_id, CGDisplay
NSArray *screenArray = [NSScreen screens];
if ((NSUInteger)p_screen < [screenArray count]) {
NSDictionary *description = [[screenArray objectAtIndex:p_screen] deviceDescription];
- NSSize displayDPI = [[description objectForKey:NSDeviceResolution] sizeValue];
- return (displayDPI.width + displayDPI.height) / 2;
+
+ const NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
+ const CGSize displayPhysicalSize = CGDisplayScreenSize([[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);
+ float scale = [[screenArray objectAtIndex:p_screen] backingScaleFactor];
+
+ float den2 = (displayPhysicalSize.width / 25.4f) * (displayPhysicalSize.width / 25.4f) + (displayPhysicalSize.height / 25.4f) * (displayPhysicalSize.height / 25.4f);
+ if (den2 > 0.0f) {
+ return ceil(sqrt(displayPixelSize.width * displayPixelSize.width + displayPixelSize.height * displayPixelSize.height) / sqrt(den2) * scale);
+ }
}
- return 96;
+ return 72;
}
float DisplayServerOSX::screen_get_scale(int p_screen) const {