Skip to content

Commit

Permalink
Fixed reported dpi scaling bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
proneon267 committed Apr 14, 2024
1 parent c44fef0 commit 64221ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
3 changes: 2 additions & 1 deletion winforms/src/toga_winforms/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def winforms_FormClosing(self, sender, event):

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


def winforms_thread_exception(sender, winforms_exc): # pragma: no cover
Expand Down
24 changes: 16 additions & 8 deletions winforms/src/toga_winforms/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def scale_font(self, native_font):
######################################################################

def winforms_Resize(self, sender, event):
self.update_dpi()
self.resize_content()

def winforms_FormClosing(self, sender, event):
Expand All @@ -76,7 +75,15 @@ def winforms_FormClosing(self, sender, event):
event.Cancel = True

def winforms_LocationChanged(self, sender, event):
self.update_dpi()
# Check if the window has moved from one screen to another and if the new
# screen has a different dpi scale than the previous screen then rescale
current_screen = self.get_current_screen()
if not hasattr(self, "_previous_screen"):
self._previous_screen = current_screen
if current_screen != self._previous_screen:
if self._dpi_scale != current_screen.dpi_scale:
self.update_dpi()
self._previous_screen = current_screen

######################################################################
# Window properties
Expand Down Expand Up @@ -145,6 +152,7 @@ def show(self):
self.interface.content.refresh()
if self.interface is not self.interface.app._main_window:
self.native.Icon = self.interface.app.icon._impl.native
self.update_dpi()
self.native.Show()

######################################################################
Expand Down Expand Up @@ -187,15 +195,15 @@ def resize_content(self):
)

def update_dpi(self):
new_scale = self.get_current_screen().dpi_scale
if new_scale == self._dpi_scale:
return

self._dpi_scale = new_scale
if self.toolbar_native is not None:
self._dpi_scale = self.get_current_screen().dpi_scale
if (self.toolbar_native is not None) and (
getattr(self, "original_toolbar_font", None) is not None
):
self.toolbar_native.Font = self.scale_font(self.original_toolbar_font)
for widget in self.interface.widgets:
widget._impl.scale_font()
widget.refresh()
self.resize_content()

######################################################################
# Window size
Expand Down

0 comments on commit 64221ab

Please sign in to comment.