Private
Public Access
0
0

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.
This commit is contained in:
2026-06-19 22:15:05 -04:00
parent f51abe0795
commit 44e2888979
2 changed files with 77 additions and 4 deletions
+32 -3
View File
@@ -1211,9 +1211,11 @@ class App:
win32gui.SendMessage(hwnd, win32con.WM_NCLBUTTONDOWN, win32con.HTCAPTION, 0) win32gui.SendMessage(hwnd, win32con.WM_NCLBUTTONDOWN, win32con.HTCAPTION, 0)
imgui.push_style_color(imgui.Col_.button, imgui.ImVec4(0, 0, 0, 0)) imgui.push_style_color(imgui.Col_.button, imgui.ImVec4(0, 0, 0, 0))
#Note(Ed): Exception(Thirdparty) result = _show_menus_is_max_result(self, hwnd)
try: is_max = win32gui.GetWindowPlacement(hwnd)[1] == win32con.SW_SHOWMAXIMIZED if not result.ok:
except Exception: is_max = False 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 # Explicitly set Y to 0 and match button height to bar height for perfect alignment
imgui.set_cursor_pos((right_x, 0)) imgui.set_cursor_pos((right_x, 0))
if imgui.button("_", (btn_w, bar_h)): win32gui.ShowWindow(hwnd, win32con.SW_MINIMIZE) 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, 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: Phase 3 Render-Loop Result Helpers
#endregion: MMA #endregion: MMA
+45 -1
View File
@@ -419,4 +419,48 @@ def test_phase_3_l1197_show_menus_hwnd_result_failure():
assert result.errors, "Expected at least one error on failure" assert result.errors, "Expected at least one error on failure"
err = result.errors[0] err = result.errors[0]
assert err.source == "gui_2._show_menus_hwnd_result" assert err.source == "gui_2._show_menus_hwnd_result"
assert result.data == 0 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