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 103 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 @@
DPI scaling on Windows is now improved and related bugs are fixed.
10 changes: 8 additions & 2 deletions examples/window/window/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ def do_right(self, widget, **kwargs):
self.main_window.position = (2000, 500)

def do_left_current_screen(self, widget, **kwargs):
self.main_window.screen_position = (0, 100)
self.main_window.screen_position = (
self.main_window.screen.origin[0],
self.main_window.screen_position[1],
)

def do_right_current_screen(self, widget, **kwargs):
self.main_window.screen_position = (1080, 100)
self.main_window.screen_position = (
self.main_window.screen.size[0] - self.main_window.size[0],
self.main_window.screen_position[1],
)

def do_small(self, widget, **kwargs):
self.main_window.size = (400, 300)
Expand Down
4 changes: 2 additions & 2 deletions iOS/tests_backend/probe.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio

import toga
from toga_iOS.libs import NSRunLoop, UIScreen
from toga_iOS.libs import NSRunLoop


class BaseProbe:
Expand All @@ -21,5 +21,5 @@ async def redraw(self, message=None, delay=0):

def assert_image_size(self, image_size, size, screen):
# Retina displays render images at a higher resolution than their reported size.
scale = int(UIScreen.mainScreen.scale)
scale = int(screen._impl.native.scale)
assert image_size == (size[0] * scale, size[1] * scale)
124 changes: 124 additions & 0 deletions testbed/tests/app/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,127 @@ async def test_app_icon(app, app_probe):
app.icon = toga.Icon.APP_ICON
await app_probe.redraw("Revert app icon to default")
app_probe.assert_app_icon(None)


# This test is windows specific
if toga.platform.current_platform == "windows":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest.mark.skipif is a cleaner way of doing this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks!


async def test_system_dpi_change(
monkeypatch, app, app_probe, main_window, main_window_probe
):
main_window.toolbar.add(app.cmd1, app.cmd2)
main_window.content.add(
toga.Button(text="Testing for system DPI change response")
)
await main_window_probe.redraw(
"Main Window is ready for testing system DPI change response"
)
# Store original values
original_sizes = dict()
original_sizes[main_window._impl.native.MainMenuStrip] = (
main_window._impl.scale_out(
main_window._impl.native.MainMenuStrip.Size.Width
),
main_window._impl.scale_out(
main_window._impl.native.MainMenuStrip.Size.Height
),
)
original_sizes[main_window._impl.toolbar_native] = (
main_window._impl.scale_out(main_window._impl.toolbar_native.Size.Width),
main_window._impl.scale_out(main_window._impl.toolbar_native.Size.Height),
)
for widget in app.widgets:
original_sizes[widget] = (
widget.window._impl.scale_out(widget._impl.native.Size.Width),
widget.window._impl.scale_out(widget._impl.native.Size.Height),
)

from toga_winforms.libs import shcore

GetScaleFactorForMonitor_original = getattr(shcore, "GetScaleFactorForMonitor")

for dpi_change_event in {
app._impl.winforms_DisplaySettingsChanged,
main_window._impl.winforms_LocationChanged,
main_window._impl.winforms_Resize,
}:
for pScale_value_mock in [1.0, 1.25, 1.5, 1.75, 2.0]:

def GetScaleFactorForMonitor_mock(hMonitor, pScale):
pScale.value = int(pScale_value_mock * 100)

monkeypatch.setattr(
"toga_winforms.libs.shcore.GetScaleFactorForMonitor",
GetScaleFactorForMonitor_mock,
)
# Trigger DPI change event
dpi_change_event(None, None)
await main_window_probe.redraw(
"Triggering DPI change event for testing property changes"
)
# Check that the screen dpi scale returns the mocked value
assert app.screens[0]._impl.dpi_scale == pScale_value_mock

# Check MenuBar Font Scaling
assert (
main_window._impl.native.MainMenuStrip.Font.Size
== main_window._impl.scale_font(
main_window._impl.original_menubar_font
).Size
)
assert (
main_window._impl.native.MainMenuStrip.Size.Width,
main_window._impl.native.MainMenuStrip.Size.Height,
) == (
main_window._impl.scale_in(
original_sizes[main_window._impl.native.MainMenuStrip][0]
),
main_window._impl.scale_in(
original_sizes[main_window._impl.native.MainMenuStrip][1]
),
)
# Check ToolBar Font Scaling and Size
assert (
main_window._impl.toolbar_native.Font.Size
== main_window._impl.scale_font(
main_window._impl.original_toolbar_font
).Size
)
assert (
main_window._impl.toolbar_native.Size.Width,
main_window._impl.toolbar_native.Size.Height,
) == (
main_window._impl.scale_in(
original_sizes[main_window._impl.toolbar_native][0]
),
main_window._impl.scale_in(
original_sizes[main_window._impl.toolbar_native][1]
),
)

# Check Widget Font Scaling and Size
for widget in app.widgets:
assert (
widget._impl.native.Font.Size
== widget.window._impl.scale_font(
widget._impl.original_font
).Size
)
assert (
widget._impl.native.Size.Width,
widget._impl.native.Size.Height,
) == (
main_window._impl.scale_in(original_sizes[widget][0]),
main_window._impl.scale_in(original_sizes[widget][1]),
)

monkeypatch.setattr(
"toga_winforms.libs.shcore.GetScaleFactorForMonitor",
GetScaleFactorForMonitor_original,
)
await main_window_probe.redraw(
"Triggering DPI change event for restoring original state"
)
app._impl.winforms_DisplaySettingsChanged(None, None)
main_window.content.clear()
main_window.toolbar.clear()
20 changes: 20 additions & 0 deletions winforms/src/toga_winforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import toga

from .libs.user32 import (
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2,
SetProcessDpiAwarenessContext,
)

# Add a reference to the Winforms assembly
clr.AddReference("System.Windows.Forms")

Expand All @@ -16,4 +21,19 @@
"WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
)


# Enable DPI awareness. This must be done before calling any other UI-related code
# (https://learn.microsoft.com/en-us/dotnet/desktop/winforms/high-dpi-support-in-windows-forms).
import System.Windows.Forms as WinForms # noqa: E402

WinForms.Application.EnableVisualStyles()
WinForms.Application.SetCompatibleTextRenderingDefault(False)

if SetProcessDpiAwarenessContext is not None:
if not SetProcessDpiAwarenessContext(
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
): # pragma: no cover
print("WARNING: Failed to set the DPI Awareness mode for the app.")


__version__ = toga._package_version(__file__, __name__)
57 changes: 26 additions & 31 deletions winforms/src/toga_winforms/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import re
import sys
import threading
from ctypes import windll

import System.Windows.Forms as WinForms
from System import Environment, Threading
from Microsoft.Win32 import SystemEvents
from System import Threading
from System.ComponentModel import InvalidEnumArgumentException
from System.Media import SystemSounds
from System.Net import SecurityProtocolType, ServicePointManager
Expand All @@ -18,7 +18,7 @@
from .keys import toga_to_winforms_key, toga_to_winforms_shortcut
from .libs.proactor import WinformsProactorEventLoop
from .libs.wrapper import WeakrefCallable
from .screens import Screen as ScreenImpl
from .screens import Screen
from .window import Window


Expand All @@ -33,6 +33,13 @@ def winforms_FormClosing(self, sender, event):
self.interface.app.on_exit()
event.Cancel = True

def update_dpi(self):
super().update_dpi()
if (
getattr(self, "original_menubar_font", None) is not None
): # pragma: no branch
self.native.MainMenuStrip.Font = self.scale_font(self.original_menubar_font)


def winforms_thread_exception(sender, winforms_exc): # pragma: no cover
# The PythonException returned by Winforms doesn't give us
Expand Down Expand Up @@ -97,32 +104,10 @@ 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
): # pragma: no cover
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: # pragma: no cover
windll.user32.SetProcessDPIAware()

self.native.EnableVisualStyles()
self.native.SetCompatibleTextRenderingDefault(False)
# Register the DisplaySettingsChanged event handler
SystemEvents.DisplaySettingsChanged += WeakrefCallable(
self.winforms_DisplaySettingsChanged
)

# Ensure that TLS1.2 and TLS1.3 are enabled for HTTPS connections.
# For some reason, some Windows installs have these protocols
Expand Down Expand Up @@ -150,6 +135,14 @@ def create(self):
self.create_menus()
self.interface.main_window._impl.set_app(self)

######################################################################
# Native event handlers
######################################################################

def winforms_DisplaySettingsChanged(self, sender, event):
for window in self.interface.windows:
window._impl.update_dpi()

######################################################################
# Commands and menus
######################################################################
Expand Down Expand Up @@ -257,6 +250,8 @@ def create_menus(self):
cmd._impl.native.append(item)
submenu.DropDownItems.Add(item)

# Required for font scaling on DPI changes
window.original_menubar_font = menubar.Font
window.resize_content()

######################################################################
Expand Down Expand Up @@ -313,9 +308,9 @@ def set_main_window(self, window):
######################################################################

def get_screens(self):
primary_screen = ScreenImpl(WinForms.Screen.PrimaryScreen)
primary_screen = Screen(WinForms.Screen.PrimaryScreen)
screen_list = [primary_screen] + [
ScreenImpl(native=screen)
Screen(native=screen)
for screen in WinForms.Screen.AllScreens
if screen != primary_screen.native
]
Expand Down
11 changes: 10 additions & 1 deletion winforms/src/toga_winforms/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@

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

self.native_content = WinForms.Panel()
native_parent.Controls.Add(self.native_content)

self.native_content.CreateGraphics().Dispose()

@property
def dpi_scale(self):
window = self.content.interface.window
if window:
return window._impl.dpi_scale
else:
return 1

@property
def width(self):
return self.scale_out(self.native_width)
Expand Down