Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved DPI Scaling on Windows and Fixed related Bugs #2155

Open
wants to merge 107 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
184d5d4
Fixed bug in call of SetProcessDpiAwarenessContext
proneon267 Oct 15, 2023
986804f
Added a changelog.
proneon267 Oct 15, 2023
5967db9
Fixed winforms scaling bugs.
proneon267 Oct 16, 2023
a9f740b
Updated changelog.
proneon267 Oct 16, 2023
c1bdf9d
Added event handler to detect dpi change when live.
proneon267 Oct 18, 2023
196bf77
Merge branch 'patch-20' of https://github.com/proneon267/toga into pa…
proneon267 Oct 18, 2023
29a55d3
Merge branch 'main' into patch-20
proneon267 Oct 18, 2023
ff23b17
Empty commit for CI/CD
proneon267 Oct 18, 2023
afccfc6
Updated changelog
proneon267 Oct 18, 2023
94dce84
Added support for font scaling based on DPI change
proneon267 Oct 21, 2023
65c6bd7
Empty commit
proneon267 Oct 21, 2023
21d1f56
Miscellaneous fixes
proneon267 Oct 21, 2023
fcfe409
Merge branch 'beeware:main' into patch-20
proneon267 Oct 21, 2023
0129035
Merge branch 'beeware:main' into patch-20
proneon267 Oct 23, 2023
ebd1c29
Fixed Hwnd Related Bugs.
proneon267 Oct 23, 2023
4e4addc
Fixed menubar clipping bug.
proneon267 Oct 23, 2023
7aeeb7c
Miscellaneous fixes
proneon267 Oct 23, 2023
0ff2ec6
Empty commit for CI
proneon267 Oct 23, 2023
1012ac5
Merge branch 'beeware:main' into patch-20
proneon267 Oct 25, 2023
1393b7d
Empty commit for CI
proneon267 Oct 26, 2023
796db51
Added Support for Scaling Stack Trace Dialogs.
proneon267 Oct 27, 2023
ec7617d
Miscellaneous fixes
proneon267 Oct 27, 2023
a4ff196
Miscellaneous fixes
proneon267 Oct 27, 2023
9e3685a
Miscellaneous fixes
proneon267 Oct 27, 2023
20b6793
Merge branch 'main' into patch-20
proneon267 Nov 1, 2023
4619553
Merge branch 'main' into patch-20
proneon267 Nov 3, 2023
6f8019c
Empty commit for CI
proneon267 Nov 3, 2023
407156e
Added support for scaling window toolbar
proneon267 Nov 3, 2023
550839c
Added tests
proneon267 Nov 5, 2023
392487a
Miscellaneous fixes
proneon267 Nov 5, 2023
2d59078
Fixed tests
proneon267 Nov 6, 2023
d54bc4d
Merge branch 'beeware:main' into patch-20
proneon267 Nov 7, 2023
c45aa2e
Modified scaling code
proneon267 Nov 7, 2023
1714095
Merge branch 'patch-20' of https://github.com/proneon267/toga into pa…
proneon267 Nov 7, 2023
5c512cb
Fixed Stack Trace Dialog Scaling issues
proneon267 Nov 9, 2023
cbdc69a
Miscellaneous Fixes
proneon267 Nov 9, 2023
2ae714f
Merge branch 'main' into patch-20
proneon267 Nov 9, 2023
a285a53
Merge branch 'patch-20' of https://github.com/proneon267/toga into HEAD
proneon267 Nov 9, 2023
c5fb1b0
Miscellaneous Fixes
proneon267 Nov 9, 2023
469f287
Merge branch 'main' into patch-20
proneon267 Nov 9, 2023
9929c3a
Merge branch 'patch-20' of https://github.com/proneon267/toga into HEAD
proneon267 Nov 9, 2023
ab754c2
Merge branch 'main' into patch-20
proneon267 Nov 9, 2023
a670a2a
Miscellaneous Fixes
proneon267 Nov 9, 2023
eeeeaa3
Merge branch 'patch-20' of https://github.com/proneon267/toga into dp…
proneon267 Nov 9, 2023
d2e5948
Fixed tests
proneon267 Nov 9, 2023
6aa68d7
Empty commit for CI
proneon267 Nov 9, 2023
083d9da
Added scaling support for moving between screens.
proneon267 Nov 9, 2023
f1ddd6f
Empty commit for CI
proneon267 Nov 9, 2023
0325388
Empty commit for CI
proneon267 Nov 9, 2023
7527b39
Miscellaneous Fixes
proneon267 Nov 11, 2023
4b7791b
Miscellaneous Fixes
proneon267 Nov 11, 2023
995819b
Merge branch 'beeware:main' into patch-20
proneon267 Nov 11, 2023
8c422e8
Miscellaneous Fixes
proneon267 Nov 11, 2023
0eb366a
Merge branch 'main' into patch-20
proneon267 Nov 12, 2023
c7b2f19
Merge branch 'beeware:main' into patch-20
proneon267 Nov 14, 2023
30b088b
Merge branch 'beeware:main' into patch-20
proneon267 Nov 15, 2023
3e7bdea
Merge branch 'main' into patch-20
proneon267 Nov 25, 2023
ee76a14
Miscellaneous Fixes
proneon267 Nov 29, 2023
d92d929
Miscellaneous Fixes
proneon267 Nov 29, 2023
48cd2bf
Merge branch 'beeware:main' into patch-20
proneon267 Dec 2, 2023
0a9e732
Merge branch 'beeware:main' into patch-20
proneon267 Dec 10, 2023
de079fa
Merge branch 'main' into patch-20
proneon267 Dec 17, 2023
81a04ae
Miscellaneous Fixes
proneon267 Dec 17, 2023
279458f
Merge branch 'beeware:main' into patch-20
proneon267 Dec 18, 2023
2e16e16
Merge branch 'beeware:main' into patch-20
proneon267 Dec 22, 2023
acf13d3
Miscellaneous Fixes
proneon267 Dec 22, 2023
c97df03
Merge branch 'beeware:main' into patch-20
proneon267 Jan 13, 2024
b8db567
Merge branch 'beeware:main' into patch-20
proneon267 Jan 14, 2024
4086055
Merge branch 'beeware:main' into patch-20
proneon267 Jan 17, 2024
85bcb6b
Merge branch 'beeware:main' into patch-20
proneon267 Jan 17, 2024
4d77f3f
Merge branch 'beeware:main' into patch-20
proneon267 Jan 20, 2024
7842aff
Empty commit for CI
proneon267 Jan 21, 2024
4f4f988
Merge branch 'beeware:main' into patch-20
proneon267 Jan 25, 2024
c529e5f
Merge branch 'beeware:main' into patch-20
proneon267 Feb 3, 2024
46eb4a9
Misc Fixes
proneon267 Feb 3, 2024
3090935
Removed dialog scaling
proneon267 Feb 6, 2024
a99b83a
Misc Fixes
proneon267 Feb 6, 2024
71d06b5
Misc Fixes
proneon267 Feb 6, 2024
fd718a2
Merge branch 'beeware:main' into patch-20
proneon267 Feb 9, 2024
d0b7f0e
Corrected windows implementation
proneon267 Feb 10, 2024
6ceb39d
Updated to latest main branch
proneon267 Feb 11, 2024
03062b9
Corrected winforms screens dpi scaling
proneon267 Feb 11, 2024
69f83cc
Corrected winforms tests_backend to detect dpi scale
proneon267 Feb 11, 2024
65e6144
Corrected winforms dpi scaling
proneon267 Feb 11, 2024
b6451f5
Corrected winforms tests_backend dpi scaling
proneon267 Feb 11, 2024
8cf5eef
Corrected winforms tests_backend dpi scaling
proneon267 Feb 11, 2024
6a0b6d7
Empty commit for CI
proneon267 Feb 11, 2024
18bc25d
Empty commit for CI
proneon267 Feb 11, 2024
87c88af
Merge branch 'main' into patch-20
proneon267 Mar 21, 2024
6713490
updated to latest main branch
proneon267 Mar 21, 2024
c873366
updated to latest main branch
proneon267 Mar 21, 2024
fac093d
Merge branch 'beeware:main' into patch-20
proneon267 Apr 2, 2024
3084c42
Merge remote-tracking branch 'remotes/origin/main' into patch-20
mhsmith Apr 3, 2024
31e54c9
Fix StackTraceDialog scaling
mhsmith Apr 3, 2024
c793d64
Fix various scaling bugs
mhsmith Apr 4, 2024
c44fef0
Merge branch 'beeware:main' into patch-20
proneon267 Apr 14, 2024
64221ab
Fixed reported dpi scaling bugs
proneon267 Apr 14, 2024
12a70d1
Fixed tests
proneon267 Apr 15, 2024
2cd6213
Empty commit for CI
proneon267 Apr 15, 2024
7c8a9b8
Empty commit for CI
proneon267 Apr 15, 2024
69054c1
Fixed remaining errors
proneon267 Apr 26, 2024
5e58f35
Fixed test
proneon267 Apr 28, 2024
ae20798
Merge branch 'main' into patch-20
proneon267 May 7, 2024
ff6223c
Modified DPI change test
proneon267 May 15, 2024
1313d7e
Fixed DPI change test
proneon267 May 15, 2024
a53a820
Merge branch 'beeware:main' into patch-20
proneon267 May 15, 2024
f3db6ff
Merge branch 'beeware:main' into patch-20
proneon267 Jun 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/2155.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bugs affecting DPI scaling on Windows were fixed.
73 changes: 47 additions & 26 deletions winforms/src/toga_winforms/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import re
import sys
import threading
from ctypes import windll
from ctypes import c_bool, c_void_p, windll

import System.Windows.Forms as WinForms
from Microsoft.Win32 import SystemEvents
from System import Environment, Threading
from System.Media import SystemSounds
from System.Net import SecurityProtocolType, ServicePointManager
Expand All @@ -16,6 +17,7 @@
from .keys import toga_to_winforms_key
from .libs.proactor import WinformsProactorEventLoop
from .libs.wrapper import WeakrefCallable
from .widgets.base import Scalable
from .window import Window


Expand All @@ -32,9 +34,43 @@ def winforms_FormClosing(self, sender, event):
event.Cancel = True


class App:
class App(Scalable):
_MAIN_WINDOW_CLASS = MainWindow

# ------------------- Set the DPI Awareness mode for the process -------------------
# This needs to be done at the earliest and doing this in __init__() or
# in create() doesn't work
mhsmith marked this conversation as resolved.
Show resolved Hide resolved
#
# Check the version of windows and make sure we are setting the DPI mode
# with the most up to date API
mhsmith marked this conversation as resolved.
Show resolved Hide resolved
# Windows Versioning Check Sources : https://www.lifewire.com/windows-version-numbers-2625171
# and https://docs.microsoft.com/en-us/windows/release-information/
win_version = Environment.OSVersion.Version
if win_version.Major >= 6: # Checks for Windows Vista or later
# Represents Windows 8.1 up to Windows 10 before Build 1703 which should use
# SetProcessDpiAwareness(True)
if (win_version.Major == 6 and win_version.Minor == 3) or (
win_version.Major == 10 and win_version.Build < 15063
):
windll.shcore.SetProcessDpiAwareness(True)
print(
"WARNING: Your Windows version doesn't support DPI-independent rendering. "
"We recommend you upgrade to at least Windows 10 Build 1703."
)
# Represents Windows 10 Build 1703 and beyond which should use
# SetProcessDpiAwarenessContext(-4) for DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
# Valid values: https://learn.microsoft.com/en-us/windows/win32/hidpi/dpi-awareness-context
elif win_version.Major == 10 and win_version.Build >= 15063:
mhsmith marked this conversation as resolved.
Show resolved Hide resolved
windll.user32.SetProcessDpiAwarenessContext.restype = c_bool
windll.user32.SetProcessDpiAwarenessContext.argtypes = [c_void_p]
# SetProcessDpiAwarenessContext returns False on Failure
if not windll.user32.SetProcessDpiAwarenessContext(-4):
print("WARNING: Failed to set the DPI Awareness mode for the app.")
# Any other version of windows should use SetProcessDPIAware()
else:
windll.user32.SetProcessDPIAware()
# ----------------------------------------------------------------------------------

def __init__(self, interface):
self.interface = interface
self.interface._impl = self
Expand All @@ -61,33 +97,13 @@ def create(self):
self.app_context = WinForms.ApplicationContext()
self.app_dispatcher = Dispatcher.CurrentDispatcher

# Check the version of windows and make sure we are setting the DPI mode
# with the most up to date API
# Windows Versioning Check Sources : https://www.lifewire.com/windows-version-numbers-2625171
# and https://docs.microsoft.com/en-us/windows/release-information/
win_version = Environment.OSVersion.Version
if win_version.Major >= 6: # Checks for Windows Vista or later
# Represents Windows 8.1 up to Windows 10 before Build 1703 which should use
# SetProcessDpiAwareness(True)
if (win_version.Major == 6 and win_version.Minor == 3) or (
win_version.Major == 10 and win_version.Build < 15063
):
windll.shcore.SetProcessDpiAwareness(True)
print(
"WARNING: Your Windows version doesn't support DPI-independent rendering. "
"We recommend you upgrade to at least Windows 10 Build 1703."
)
# Represents Windows 10 Build 1703 and beyond which should use
# SetProcessDpiAwarenessContext(-2)
elif win_version.Major == 10 and win_version.Build >= 15063:
windll.user32.SetProcessDpiAwarenessContext(-2)
# Any other version of windows should use SetProcessDPIAware()
else:
windll.user32.SetProcessDPIAware()

# These are required for properly setting up DPI mode
self.native.EnableVisualStyles()
self.native.SetCompatibleTextRenderingDefault(False)
mhsmith marked this conversation as resolved.
Show resolved Hide resolved

# Register the DisplaySettingsChanged event handler
SystemEvents.DisplaySettingsChanged += self.winforms_DisplaySettingsChanged

# Ensure that TLS1.2 and TLS1.3 are enabled for HTTPS connections.
# For some reason, some Windows installs have these protocols
# turned off by default. SSL3, TLS1.0 and TLS1.1 are *not* enabled
Expand Down Expand Up @@ -332,6 +348,11 @@ def hide_cursor(self):
WinForms.Cursor.Hide()
self._cursor_visible = False

def winforms_DisplaySettingsChanged(self, sender, event):
self.update_scale()
for window in self.interface.windows:
window.content.refresh()


class DocumentApp(App):
def _create_app_commands(self):
Expand Down
1 change: 0 additions & 1 deletion winforms/src/toga_winforms/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class Container(Scalable):
def __init__(self, native_parent):
self.init_scale(native_parent)
self.native_parent = native_parent
self.native_width = self.native_height = 0
self.content = None
Expand Down
28 changes: 23 additions & 5 deletions winforms/src/toga_winforms/widgets/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from abc import ABC, abstractmethod
from ctypes import byref, c_void_p, windll, wintypes
from decimal import ROUND_HALF_EVEN, ROUND_UP, Decimal

from System.Drawing import (
Expand All @@ -7,6 +8,7 @@
Size,
SystemColors,
)
from System.Windows.Forms import Screen
from travertino.size import at_least

from toga.colors import TRANSPARENT
Expand All @@ -16,19 +18,36 @@
class Scalable:
SCALE_DEFAULT_ROUNDING = ROUND_HALF_EVEN

def init_scale(self, native):
self.dpi_scale = native.CreateGraphics().DpiX / 96
def update_scale(self):
screen = Screen.PrimaryScreen
screen_rect = wintypes.RECT(
screen.Bounds.Left,
screen.Bounds.Top,
screen.Bounds.Right,
screen.Bounds.Bottom,
)
windll.user32.MonitorFromRect.restype = c_void_p
windll.user32.MonitorFromRect.argtypes = [wintypes.RECT, wintypes.DWORD]
# MONITOR_DEFAULTTONEAREST = 2
hMonitor = windll.user32.MonitorFromRect(screen_rect, 2)
pScale = wintypes.UINT()
windll.shcore.GetScaleFactorForMonitor(c_void_p(hMonitor), byref(pScale))
Scalable.dpi_scale = pScale.value / 100

# Convert CSS pixels to native pixels
def scale_in(self, value, rounding=SCALE_DEFAULT_ROUNDING):
return self.scale_round(value * self.dpi_scale, rounding)
if not hasattr(Scalable, "dpi_scale"):
self.update_scale()
return self.scale_round(value * Scalable.dpi_scale, rounding)

# Convert native pixels to CSS pixels
def scale_out(self, value, rounding=SCALE_DEFAULT_ROUNDING):
if not hasattr(Scalable, "dpi_scale"):
self.update_scale()
if isinstance(value, at_least):
return at_least(self.scale_out(value.value, rounding))
else:
return self.scale_round(value / self.dpi_scale, rounding)
return self.scale_round(value / Scalable.dpi_scale, rounding)

def scale_round(self, value, rounding):
if rounding is None:
Expand All @@ -49,7 +68,6 @@ def __init__(self, interface):
self._container = None
self.native = None
self.create()
self.init_scale(self.native)
self.interface.style.reapply()

@abstractmethod
Expand Down
1 change: 0 additions & 1 deletion winforms/src/toga_winforms/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def __init__(self, interface, title, position, size):
self.native = WinForms.Form()
self.native.FormClosing += WeakrefCallable(self.winforms_FormClosing)
super().__init__(self.native)
self.init_scale(self.native)

self.native.MinimizeBox = self.interface.minimizable
self.native.MaximizeBox = self.interface.resizable
Expand Down