Skip to content

Listen to screen DPI changes #864

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

Merged
merged 3 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)