Skip to content

Commit 3d90a51

Browse files
committed
fix(scroll-frame): handle empty canvas bbox during wheel scroll
Also guard scroll handling when the canvas has no bbox yet.
1 parent 13fa66d commit 3d90a51

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

ardupilot_methodic_configurator/frontend_tkinter_scroll_frame.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@ def on_frame_configure(self, _event) -> None: # noqa: ANN001
7171
"""Reset the scroll region to encompass the inner frame."""
7272
# Whenever the size of the frame changes, alter the scroll region respectively.
7373
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
74-
# Calculate the bounding box for the scroll region, starting from the second row
75-
# bbox = self.canvas.bbox("all")
76-
# if bbox:
77-
# # Adjust the bounding box to start from the second row
78-
# bbox = (bbox[0], bbox[1] + self.canvas.winfo_reqheight(), bbox[2], bbox[3])
79-
# self.canvas.configure(scrollregion=bbox)
8074

8175
def on_canvas_configure(self, event: tk.Event) -> None:
8276
"""Reset the canvas window to encompass inner frame when required."""
@@ -86,7 +80,8 @@ def on_canvas_configure(self, event: tk.Event) -> None:
8680

8781
def on_mouse_wheel(self, event: tk.Event) -> None: # cross platform scroll wheel event
8882
canvas_height = self.canvas.winfo_height()
89-
rows_height = self.canvas.bbox("all")[3]
83+
bbox = self.canvas.bbox("all")
84+
rows_height = bbox[3] if bbox is not None else 0
9085

9186
if rows_height > canvas_height: # only scroll if the rows overflow the frame
9287
if platform_system() == "Windows":

tests/test_frontend_tkinter_scroll_frame.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,25 @@ def test_user_does_not_scroll_when_all_content_is_visible(self, scrollable_frame
154154
# Verify no scrolling occurred
155155
scrollable_frame.canvas.yview_scroll.assert_not_called()
156156

157+
def test_user_does_not_crash_when_canvas_has_no_items(self, scrollable_frame) -> None:
158+
"""
159+
User does not hit a crash when scrolling before any canvas items exist.
160+
161+
GIVEN: A ScrollFrame whose canvas reports no bounding box yet
162+
WHEN: The user scrolls
163+
THEN: No scrolling occurs and no exception is raised
164+
"""
165+
scrollable_frame.canvas.winfo_height.return_value = 100
166+
scrollable_frame.canvas.bbox.return_value = None
167+
168+
event = MagicMock()
169+
event.delta = 120
170+
171+
with patch("ardupilot_methodic_configurator.frontend_tkinter_scroll_frame.platform_system", return_value="Windows"):
172+
scrollable_frame.on_mouse_wheel(event)
173+
174+
scrollable_frame.canvas.yview_scroll.assert_not_called()
175+
157176
def test_user_mouse_events_are_properly_managed_on_enter_leave(self, scrollable_frame) -> None:
158177
"""
159178
User mouse events are properly bound and unbound as cursor enters/leaves the scroll area.

0 commit comments

Comments
 (0)