progress on fixing up gui code

This commit is contained in:
2026-05-12 15:20:34 -04:00
parent fb45b44824
commit c4e1cca66b
6 changed files with 198 additions and 197 deletions
+25 -1
View File
@@ -131,7 +131,31 @@ max-locals = 8
max-args = 5
```
## 11. Structural Dependency Mapping (SDM)
### 11. ImGui Defer Patterns
To prevent `PopID` or `End` leaks in immediate-mode rendering, and to keep code flat (0-1 levels of nesting) for AI agents, use the following patterns:
- **The Context Manager Pattern (Mandatory for complex blocks):**
Wrap all `Begin/End` blocks in `imscope` context managers (from `src/imgui_scopes.py`).
```python
with imscope.window("My Window") as (exp, opened):
if exp:
imgui.text("Hello")
with imscope.tab_item("My Tab") as (exp, _):
if exp:
self._render_tab_content()
```
This adds only 1 space of indentation (project standard) and guarantees the corresponding `End` is called even on early returns or exceptions. **Crucial:** Always check the `exp` (expanded/visible) state before rendering content to avoid ID conflicts and performance overhead.
- **The Flat Dispatch Pattern (Recommended for the main loop):**
To avoid nesting multiple window checks, use a dispatch helper that encapsulates the state check and the scope.
```python
self._render_window_if_open("My Window", self._render_my_panel)
```
This keeps the main GUI loop as a flat sequence of declarative calls.
## 12. Structural Dependency Mapping (SDM)
To assist AI agents in evaluating refactoring impact across dynamic codebases, all major definitions SHOULD include terse SDM tags at the end of their docstrings.
+4
View File
@@ -54,6 +54,10 @@ This file tracks all major tracks for the project. Each track has its own detail
*Link: [./tracks/context_comp_presets_20260510/](./tracks/context_comp_presets_20260510/)*
*Goal: Implement Context Preset save/load with validation, and Context Preview before sending to agent.*
12. [ ] **Track: GUI Architecture Refinement & AI-Friendliness**
*Link: [./tracks/gui_architecture_refinement_20260512/](./tracks/gui_architecture_refinement_20260512/)*
*Goal: Reduce nesting and compactness of ImGui code in `gui_2.py`, and formalize ImGui Defer patterns.*
---
## Hot Reload Feature
@@ -0,0 +1,45 @@
# Plan: GUI Architecture Refinement & AI-Friendliness
**Track ID:** gui_architecture_refinement_20260512
**Status:** [~] Draft
## Objective
Reduce nesting and improve compactness of ImGui code in `gui_2.py` to make it more AI-friendly. Formalize the "defer/scope" patterns (inspired by Go's `defer` and Ryan Fleury's macros) in the project style guides to prevent `PopID` / `End` leaks.
## Background & Motivation
The main GUI render loop (`_gui_func__abusrd_try_scope`) has grown to over 600 lines with deep nesting. Raw `imgui.begin()` and `imgui.end()` calls are prone to leaks if an early return occurs or if the return value of `begin` is ignored. While `imscope` context managers solve the leak issue, they still introduce nesting. We need a way to keep the code extremely flat (0-1 levels of nesting) while maintaining safety.
## Proposed Solution
### 1. Update Style Guides (`python.md` & `workflow.md`)
Introduce a new section explicitly defining the "ImGui Defer Patterns":
- **The Context Manager Pattern:** Use `with imscope.window("Name"):` to automatically handle `End()`. This adds only 1 space of indentation (per project rules).
- **The Flat Dispatch Pattern:** To avoid nesting multiple windows, use dispatch helpers like `self._render_window_if_open(name, render_func)` which encapsulate the state-checking, `Begin`, `End`, and execution logic.
### 2. Implement Flat Dispatch Helper
Create a helper method in `App`:
```python
def _render_window_if_open(self, name: str, render_func: Callable[[], None], flag_condition: bool = True) -> None:
if not flag_condition or not self.show_windows.get(name, False): return
with imscope.window(name, self.show_windows[name]) as (exp, opened):
self.show_windows[name] = bool(opened)
if exp: render_func()
```
### 3. Refactor `gui_2.py`
- Extract inline hub definitions (e.g., "Operations Hub", "Discussion Hub", "AI Settings") from `_gui_func__abusrd_try_scope` into dedicated methods (`_render_operations_hub`, etc.).
- Replace the massive `if self.show_windows.get...` blocks in `_gui_func__abusrd_try_scope` with a flat sequence of `_render_window_if_open` calls.
- Rename `_gui_func__abusrd_try_scope` to a cleaner name (e.g., `_gui_func_body`) once stabilized.
## Implementation Steps
1. [x] Edit `conductor/code_styleguides/python.md` to add "ImGui Defer Patterns" under the "AI-Agent Specific Conventions" or "Anti-OOP" section.
2. [x] Edit `conductor/workflow.md` to reference the mandatory use of `imscope` or dispatch helpers for ImGui code.
3. [x] Add `_render_window_if_open` to `gui_2.py`.
4. [x] Extract `_render_operations_hub`, `_render_discussion_hub`, and `_render_ai_settings_hub` in `gui_2.py`.
5. [x] Flatten `_gui_func__abusrd_try_scope` using the new helper.
## Verification & Testing
- Ensure the app launches successfully without `PopID` errors.
- Verify that toggling windows via the menu still opens and closes them correctly.
- Run `uv run pytest tests/test_gui_startup_smoke.py` and `uv run pytest tests/test_gui_window_controls.py`.
+1
View File
@@ -9,6 +9,7 @@
- Use `./scripts/ai_style_formatter.py` for formatting validation
- **NO COMMENTS** unless explicitly requested
- Type hints required for all public functions
- **ImGui Defer Patterns:** Use `imscope` context managers or `_render_window_if_open` dispatch helpers to prevent resource leaks and keep the main loop flat. See `conductor/code_styleguides/python.md` for details.
### CRITICAL: Native Edit Tool Destroys Indentation