From 44e2888979e8e7366be1d5b0647afe63d8d9131d Mon Sep 17 00:00:00 2001 From: Ed_ Date: Fri, 19 Jun 2026 22:15:05 -0400 Subject: [PATCH] refactor(gui_2): migrate L1222 _show_menus is_max to Result[T] (Phase 3) TIER-2 READ conductor/code_styleguides/error_handling.md end-to-end before Phase 3. Adds _show_menus_is_max_result(app, hwnd) -> Result[bool] helper that wraps the win32gui.GetWindowPlacement try/except from App._show_menus. The data field carries the is_max value (True iff window is maximized, False on failure) so the legacy wrapper can use it without an additional instance attribute. App._show_menus becomes a thin wrapper that drains errors to _last_request_errors when GetWindowPlacement fails. Audit: BROAD_CATCH count 20 -> 19, COMPLIANT count 17 -> 18. Tests: 2/2 pass. --- src/gui_2.py | 35 ++++++++++++++++++++++++++--- tests/test_gui_2_result.py | 46 +++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/src/gui_2.py b/src/gui_2.py index 9b59e2db..8247c34a 100644 --- a/src/gui_2.py +++ b/src/gui_2.py @@ -1211,9 +1211,11 @@ class App: win32gui.SendMessage(hwnd, win32con.WM_NCLBUTTONDOWN, win32con.HTCAPTION, 0) imgui.push_style_color(imgui.Col_.button, imgui.ImVec4(0, 0, 0, 0)) - #Note(Ed): Exception(Thirdparty) - try: is_max = win32gui.GetWindowPlacement(hwnd)[1] == win32con.SW_SHOWMAXIMIZED - except Exception: is_max = False + result = _show_menus_is_max_result(self, hwnd) + if not result.ok: + if not hasattr(self, '_last_request_errors'): self._last_request_errors = [] + self._last_request_errors.append(("_show_menus.is_max", result.errors[0])) + is_max = result.data # Explicitly set Y to 0 and match button height to bar height for perfect alignment imgui.set_cursor_pos((right_x, 0)) if imgui.button("_", (btn_w, bar_h)): win32gui.ShowWindow(hwnd, win32con.SW_MINIMIZE) @@ -7554,6 +7556,33 @@ def _show_menus_hwnd_result(app: "App") -> Result[int]: original=e, )]) +def _show_menus_is_max_result(app: "App", hwnd) -> Result[bool]: + """Drain-aware variant of L1222 _show_menus GetWindowPlacement try/except. + + Extracts the win32gui.GetWindowPlacement try/except from App._show_menus + into a Result-returning helper. On success, returns Result(data=is_max) + where is_max is True iff the window is currently maximized. On + failure, returns Result(data=False, errors=[ErrorInfo]) - the data + defaults to False (not maximized) matching the original except + branch behavior. + + [C: src/gui_2.py:App._show_menus (L1222 legacy wrapper)] + """ + global win32gui, win32con + try: + if win32gui is None: + import win32gui + import win32con + is_max = win32gui.GetWindowPlacement(hwnd)[1] == win32con.SW_SHOWMAXIMIZED + return Result(data=is_max) + except Exception as e: + return Result(data=False, errors=[ErrorInfo( + kind=ErrorKind.INTERNAL, + message=f"GetWindowPlacement failed for hwnd={hwnd}: {e}", + source="gui_2._show_menus_is_max_result", + original=e, + )]) + #endregion: Phase 3 Render-Loop Result Helpers #endregion: MMA diff --git a/tests/test_gui_2_result.py b/tests/test_gui_2_result.py index 30946753..034852a9 100644 --- a/tests/test_gui_2_result.py +++ b/tests/test_gui_2_result.py @@ -419,4 +419,48 @@ def test_phase_3_l1197_show_menus_hwnd_result_failure(): assert result.errors, "Expected at least one error on failure" err = result.errors[0] assert err.source == "gui_2._show_menus_hwnd_result" - assert result.data == 0 \ No newline at end of file + assert result.data == 0 + + +def test_phase_3_l1222_show_menus_is_max_result_success(): + """ + L1222 _show_menus_is_max_result returns Result.ok=True with is_max=True. + + The helper wraps the win32gui.GetWindowPlacement try/except in + App._show_menus. On success, returns Result(data=is_max) where + is_max is True iff the window is currently maximized. + """ + from src import gui_2 + from unittest.mock import MagicMock, patch + app = MagicMock() + mock_hwnd = 12345 + SW_SHOWMAXIMIZED = 3 + mock_placement = ("first", SW_SHOWMAXIMIZED) + with patch.object(gui_2, "win32gui") as mock_w32, \ + patch.object(gui_2, "win32con") as mock_w32c: + mock_w32.GetWindowPlacement.return_value = mock_placement + mock_w32c.SW_SHOWMAXIMIZED = SW_SHOWMAXIMIZED + result = gui_2._show_menus_is_max_result(app, mock_hwnd) + assert result.ok, f"Expected ok=True on success, got errors: {result.errors}" + assert result.data is True + + +def test_phase_3_l1222_show_menus_is_max_result_failure(): + """ + L1222 _show_menus_is_max_result returns Result.ok=False with is_max=False on failure. + + When GetWindowPlacement raises, the helper returns Result(data=False, + errors=[ErrorInfo]) - the data defaults to False (not maximized) + matching the original except branch behavior. + """ + from src import gui_2 + from unittest.mock import MagicMock, patch + app = MagicMock() + with patch.object(gui_2, "win32gui") as mock_w32: + mock_w32.GetWindowPlacement.side_effect = RuntimeError("win32 failed") + result = gui_2._show_menus_is_max_result(app, 12345) + assert not result.ok, f"Expected ok=False on failure, got data: {result.data}" + assert result.errors, "Expected at least one error on failure" + err = result.errors[0] + assert err.source == "gui_2._show_menus_is_max_result" + assert result.data is False \ No newline at end of file