Makes Windows DPI aware of scale changes (#8058)

* make dpi aware of scale changes

* Resize on startup if there is any dpi change

* remove workarounds and use SDL_HINT_WINDOWS_DPI_SCALING
This commit is contained in:
Dexer 2023-03-26 06:28:43 -03:00 committed by GitHub
parent 84a40e88cb
commit f0786c4e42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 12 deletions

View File

@ -420,7 +420,7 @@ class WindowBase(EventDispatcher):
def _get_size(self):
r = self._rotation
w, h = self._size
if self._density != 1:
if platform == 'win' or self._density != 1:
w, h = self._win._get_gl_size()
if self.softinput_mode == 'resize':
h -= self.keyboard_height
@ -519,7 +519,7 @@ class WindowBase(EventDispatcher):
# make some property read-only
def _get_width(self):
_size = self._size
if self._density != 1:
if platform == 'win' or self._density != 1:
_size = self._win._get_gl_size()
r = self._rotation
if r == 0 or r == 180:
@ -536,7 +536,7 @@ class WindowBase(EventDispatcher):
'''Rotated window height'''
r = self._rotation
_size = self._size
if self._density != 1:
if platform == 'win' or self._density != 1:
_size = self._win._get_gl_size()
kb = self.keyboard_height if self.softinput_mode == 'resize' else 0
if r == 0 or r == 180:
@ -739,13 +739,13 @@ class WindowBase(EventDispatcher):
'''
def _get_effective_size(self):
'''On density=1 and non-ios displays, return :attr:`system_size`,
else return scaled / rotated :attr:`size`.
'''On density=1 and non-ios / non-Windows displays,
return :attr:`system_size`, else return scaled / rotated :attr:`size`.
Used by MouseMotionEvent.update_graphics() and WindowBase.on_motion().
'''
w, h = self.system_size
if platform == 'ios' or self._density != 1:
if platform in ('ios', 'win') or self._density != 1:
w, h = self.size
return w, h

View File

@ -109,6 +109,10 @@ cdef class _WindowSDL2Storage:
SDL_SetHintWithPriority(b'SDL_ANDROID_TRAP_BACK_BUTTON', b'1',
SDL_HINT_OVERRIDE)
# makes dpi aware of scale changes
if platform == "win":
SDL_SetHint(SDL_HINT_WINDOWS_DPI_SCALING, b"1")
if SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0:
self.die()

View File

@ -981,8 +981,6 @@ class _WindowsSysDPIWatch:
from ctypes import windll
if msg == WM_DPICHANGED:
ow, oh = self.window.size
old_dpi = self.window.dpi
def clock_callback(*args):
if x_dpi != y_dpi:
@ -991,10 +989,6 @@ class _WindowsSysDPIWatch:
self.window.dpi = x_dpi
# maintain the same window size
ratio = x_dpi / old_dpi
self.window.size = ratio * ow, ratio * oh
x_dpi = wParam & 0xFFFF
y_dpi = wParam >> 16
Clock.schedule_once(clock_callback, -1)

View File

@ -480,6 +480,8 @@ cdef extern from "SDL.h":
cdef char *SDL_HINT_VIDEO_WIN_D3DCOMPILER
cdef char *SDL_HINT_ACCELEROMETER_AS_JOYSTICK
cdef char *SDL_HINT_ANDROID_TRAP_BACK_BUTTON
cdef char *SDL_HINT_WINDOWS_DPI_AWARENESS
cdef char *SDL_HINT_WINDOWS_DPI_SCALING
cdef int SDL_QUERY = -1
cdef int SDL_IGNORE = 0