Skip to content

Commit

Permalink
Ensure Winforms uses DPI scaled sizes.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Feb 11, 2024
1 parent 777f272 commit 45de2c9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
5 changes: 3 additions & 2 deletions android/src/toga_android/screens.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ def get_origin(self):
return (0, 0)

def get_size(self):
return tuple(
map(self.scale_out, (self.native.getWidth(), self.native.getHeight()))
return (
self.scale_out(self.native.getWidth()),
self.scale_out(self.native.getHeight()),
)

def get_image_data(self):
Expand Down
2 changes: 1 addition & 1 deletion winforms/src/toga_winforms/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Container(Scalable):
def __init__(self, native_parent):
self.init_scale(native_parent)
super().__init__()
self.native_parent = native_parent
self.native_width = self.native_height = 0
self.content = None
Expand Down
21 changes: 15 additions & 6 deletions winforms/src/toga_winforms/screens.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

from toga.screens import Screen as ScreenInterface

from .widgets.base import Scalable

class Screen:

class Screen(Scalable):
_instances = {}

def __new__(cls, native):
Expand All @@ -27,17 +29,24 @@ def get_name(self):
return self.native.DeviceName

def get_origin(self):
return self.native.Bounds.X, self.native.Bounds.Y
return (
self.scale_out(self.native.Bounds.X),
self.scale_out(self.native.Bounds.Y),
)

def get_size(self):
return self.native.Bounds.Width, self.native.Bounds.Height
return (
self.scale_out(self.native.Bounds.Width),
self.scale_out(self.native.Bounds.Height),
)

def get_image_data(self):
bitmap = Bitmap(*self.get_size())
native_size = self.native.Bounds.Width, self.native.Bounds.Height
bitmap = Bitmap(native_size)
graphics = Graphics.FromImage(bitmap)
source_point = Point(*self.get_origin())
source_point = Point(self.native.Bounds.X, self.native.Bounds.Y)
destination_point = Point(0, 0)
copy_size = Size(*self.get_size())
copy_size = Size(native_size)
graphics.CopyFromScreen(source_point, destination_point, copy_size)
stream = MemoryStream()
bitmap.Save(stream, Imaging.ImageFormat.Png)
Expand Down
9 changes: 7 additions & 2 deletions winforms/src/toga_winforms/widgets/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from abc import ABC, abstractmethod
from decimal import ROUND_HALF_EVEN, Decimal

from System import IntPtr
from System.Drawing import (
Color,
Graphics,
Point,
Size,
SystemColors,
Expand All @@ -16,8 +18,11 @@
class Scalable:
SCALE_DEFAULT_ROUNDING = ROUND_HALF_EVEN

def init_scale(self, native):
self.dpi_scale = native.CreateGraphics().DpiX / 96
def __init__(self):
super().__init__()
self.dpi_scale = (
Graphics.FromHdc(Graphics.FromHwnd(IntPtr.Zero).GetHdc()).DpiX / 96
)

# Convert CSS pixels to native pixels
def scale_in(self, value, rounding=SCALE_DEFAULT_ROUNDING):
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 @@ -25,7 +25,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

0 comments on commit 45de2c9

Please sign in to comment.