Skip to content

Commit

Permalink
Listen to screen DPI changes (#864)
Browse files Browse the repository at this point in the history
Co-authored-by: Corran Webster <cwebster@enthought.com>
  • Loading branch information
jwiggins and corranwebster authored Mar 11, 2022
1 parent 718c863 commit 13111f6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
17 changes: 13 additions & 4 deletions enable/qt4/base_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# Enthought library imports.
from enable.abstract_window import AbstractWindow
from enable.events import KeyEvent, MouseEvent, DragEvent
from traits.api import Instance
from traits.api import Instance, Property

# Local imports.
from .constants import (
Expand Down Expand Up @@ -351,6 +351,9 @@ class _Window(AbstractWindow):

control = Instance(QtGui.QWidget)

# Use a Property since this value can change dynamically
base_pixel_scale = Property(observe="control")

def __init__(self, parent, wid=-1, pos=None, size=None, **traits):
AbstractWindow.__init__(self, **traits)

Expand All @@ -360,15 +363,21 @@ def __init__(self, parent, wid=-1, pos=None, size=None, **traits):
parent = parent.parentWidget()
self.control = self._create_control(parent, self)

if self.high_resolution and hasattr(self.control, "devicePixelRatio"):
self.base_pixel_scale = self.control.devicePixelRatio()

if pos is not None:
self.control.move(*pos)

if size is not None:
self.control.resize(*size)

def _get_base_pixel_scale(self):
if self.control is None:
return 1.0

if self.high_resolution and hasattr(self.control, "devicePixelRatio"):
return self.control.devicePixelRatio()

return 1.0

# ------------------------------------------------------------------------
# Implementations of abstract methods in AbstractWindow
# ------------------------------------------------------------------------
Expand Down
26 changes: 23 additions & 3 deletions enable/tests/test_component_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
""" Test the interaction between traitsui and enable's ComponentEditor.
"""
import unittest
from unittest import mock

from traits.api import Any, HasTraits
from traitsui.api import Item, View

from enable.component import Component
from enable.component_editor import ComponentEditor
from enable.tests._testing import get_dialog_size, skip_if_null
from enable.tests._testing import (
get_dialog_size, skip_if_null, skip_if_not_qt, skip_if_not_wx
)

ITEM_WIDTH, ITEM_HEIGHT = 700, 200

Expand Down Expand Up @@ -74,8 +77,8 @@ def test_initial_component_with_item_size(self):
self.assertGreater(size[0], ITEM_WIDTH - 1)
self.assertGreater(size[1], ITEM_HEIGHT - 1)

@skip_if_null
def test_component_hidpi_size_stability(self):
@skip_if_not_wx
def test_component_hidpi_size_stability_wx(self):
# Issue #634: HiDPI doubles size of components when a component is
# replaced
dialog = _ComponentDialogWithSize(thing=Component())
Expand All @@ -89,3 +92,20 @@ def test_component_hidpi_size_stability(self):
new_bounds = dialog.thing.bounds

self.assertListEqual(initial_bounds, new_bounds)

@skip_if_not_qt
def test_component_hidpi_size_stability_qt(self):
# Issue #634: HiDPI doubles size of components when a component is
# replaced
dialog = _ComponentDialogWithSize(thing=Component())
dialog.edit_traits()

initial_bounds = dialog.thing.bounds
# Force the window into HiDPI mode
with mock.patch.object(
dialog.thing.window.control, "devicePixelRatio", return_value=2
) as mock_method:
dialog.thing = Component()
new_bounds = dialog.thing.bounds

self.assertListEqual(initial_bounds, new_bounds)

0 comments on commit 13111f6

Please sign in to comment.