Skip to content

Commit 583e7c4

Browse files
authored
Make canvas.set_logical_size lazy again (#148)
1 parent 630709a commit 583e7c4

File tree

3 files changed

+2
-76
lines changed

3 files changed

+2
-76
lines changed

rendercanvas/_size.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,6 @@ def _resolve_total_pixel_ratio_and_logical_size(self):
4141

4242
self["changed"] = True
4343

44-
def set_logical_size(self, width: float, height: float):
45-
"""Called by the canvas when the logical size is set.
46-
47-
This calculates the expected physical size (with the current pixel ratio),
48-
to get the corrected logical size. But this *only* sets the logical_size
49-
and total_pixel_ratio. The backend will, likely before the next draw,
50-
adjust the size and call set_physical_size(), which re-sets the logical size.
51-
"""
52-
# Calculate adjusted logical size
53-
ratio = self["native_pixel_ratio"] * self["canvas_pixel_ratio"]
54-
pwidth = max(1, round(float(width) * ratio + 0.01))
55-
pheight = max(1, round(float(height) * ratio + 0.01))
56-
lwidth, lheight = pwidth / ratio, pheight / ratio
57-
58-
# Update logical size and total ratio. You could see it as a temporary zoom factor being applied.
59-
# Note that The backend will soon call set_physical_size().
60-
self["logical_size"] = lwidth, lheight
61-
self["total_pixel_ratio"] = self["physical_size"][0] / lwidth
62-
63-
self["changed"] = True
64-
6544
def set_zoom(self, zoom: float):
6645
"""Set the zoom factor, i.e. the canvas pixel ratio."""
6746
self["canvas_pixel_ratio"] = float(zoom)

rendercanvas/base.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,6 @@ def set_logical_size(self, width: float, height: float) -> None:
593593
width, height = float(width), float(height)
594594
if width < 0 or height < 0:
595595
raise ValueError("Canvas width and height must not be negative")
596-
# Already adjust our logical size, so e.g. layout engines can get to work
597-
self._size_info.set_logical_size(width, height)
598596
# Tell the backend to adjust the size. It will likely set the new physical size before the next draw.
599597
self._rc_set_logical_size(width, height)
600598

tests/test_size.py

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -31,57 +31,6 @@ def test_size_info_basic():
3131
assert si["changed"] is True
3232

3333

34-
def test_size_info_logical():
35-
si = SizeInfo()
36-
assert si["physical_size"] == (1, 1)
37-
assert si["logical_size"] == (1.0, 1.0)
38-
assert si["total_pixel_ratio"] == 1.0
39-
assert si["changed"] is False
40-
41-
# Base class setting logical size
42-
si.set_logical_size(100, 100)
43-
44-
assert si["physical_size"] == (1, 1) # don't touch!
45-
assert si["logical_size"] == (100.0, 100.0)
46-
assert si["total_pixel_ratio"] == 0.01
47-
assert si["changed"] is True
48-
49-
# Again
50-
si.set_logical_size(640, 480)
51-
52-
assert si["physical_size"] == (1, 1) # don't touch!
53-
assert si["logical_size"] == (640.0, 480.0)
54-
assert si["total_pixel_ratio"] == 1 / 640
55-
56-
# Now backend adjusts its actual size
57-
si.set_physical_size(1280, 960, 2.0)
58-
59-
assert si["physical_size"] == (1280, 960)
60-
assert si["logical_size"] == (640.0, 480.0)
61-
assert si["total_pixel_ratio"] == 2.0
62-
63-
# Base class sets logical size again
64-
si.set_logical_size(100.2, 100.2)
65-
66-
assert si["physical_size"] == (1280, 960) # don't touch!
67-
assert si["logical_size"] == (100.0, 100.0) # took ratio into account
68-
assert si["total_pixel_ratio"] == 1280 / 100
69-
70-
# And again
71-
si.set_logical_size(101.8, 101.8)
72-
73-
assert si["physical_size"] == (1280, 960) # don't touch!
74-
assert si["logical_size"] == (102.0, 102.0) # took ratio into account
75-
assert si["total_pixel_ratio"] == 1280 / 102
76-
77-
# And backend adjusts its actual size
78-
si.set_physical_size(204, 204, 2.0)
79-
80-
assert si["physical_size"] == (204, 204)
81-
assert si["logical_size"] == (102, 102.0)
82-
assert si["total_pixel_ratio"] == 2.0
83-
84-
8534
def test_size_info_zoom():
8635
si = SizeInfo()
8736
si.set_physical_size(1200, 1200, 2.0)
@@ -145,9 +94,9 @@ def test_canvas_sizing():
14594

14695
c = MyRenderCanvas()
14796

148-
assert c.get_logical_size() == (640.0, 480.0)
97+
assert c.get_logical_size() == (1.0, 1.0)
14998
assert c.get_physical_size() == (1, 1)
150-
assert c.get_pixel_ratio() == 1 / 640
99+
assert c.get_pixel_ratio() == 1
151100

152101
c.apply_size()
153102

0 commit comments

Comments
 (0)