Files
douyin_guaji-main/docs/superpowers/specs/2026-05-30-split-gui-app-design.md
T

214 lines
9.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 拆分 gui/gui_app.py 设计文档
## 背景
`gui/gui_app.py` 当前 5476 行,几乎全部代码都嵌在单个 `main()` 函数里,
内含 50+ 个闭包,捕获大量 widgets 和 tkinter Var。文件已经被多轮抽取过:
- `5649 → 5537`:`ThreadSafeLogWriter` → `components/log_writer`
- `5539 → 5370`:`_open_realtime_viewer_impl` → `components/realtime_viewer`
- `6339 → 5634`:`refresh_stats` 字符串格式化 → `components/stats_formatter`
继续按相同思路把内聚闭包抽到 `gui/components/*`,主文件继续瘦身。
## 目标
迭代式抽取 `gui/gui_app.py` 中的内聚闭包,主文件越变越小,
**不改变任何 GUI 运行行为**。
### 成功标准
1. `gui/gui_app.py` 行数显著下降(目标:≤ 3000 行;继续视边际收益决定是否再抽)
2. `pytest` 全绿
3. 每次抽取产生一个独立 commit,保持小步可回滚
4. GUI 启动 + 主要交互路径无回归(手工 smoke:启动 GUI、打开 WiFi 对话框、加载配置、截图、查看日志)
### 非目标
- 不重构 GUI 整体架构(不引入 MVC/MVVM)
- 不改变 `gui_app.py` 对外暴露的入口(`main()`、`format_gui_log_line()`)
- 不优化性能、不修 bug(除非抽取过程中发现)
- 不动 `gui/web_gui.py`(属于下一轮独立 spec)
## 抽取候选清单
按「行数 × 边界清晰度」排序(行数粗略统计):
| 优先级 | 闭包 | 行范围 | 行数 | 目标文件 | 主要依赖 |
|---|---|---|---|---|---|
| P1 | `open_wifi_adb_dialog` | 743-1588 | 845 | `components/wifi_adb_dialog.py` | ADB 工具 + 几个回调 |
| P2 | `load_config_to_ui` + `save_ui_to_config` | 2748-3402 | 654 | `components/config_io.py` | 100+ tkinter Var(需聚合)|
| P3 | `refresh_reservations` | 4530-4805 | 275 | `components/reservations_view.py` | 1-2 个 Treeview + 路径 |
| P4 | `refresh_high_value_rooms` | 4805-5085 | ~280 | `components/high_value_view.py` | 1 个 Treeview + 路径 |
| P5 | `do_screenshot` + `_bind_screenshot_button` | 3402-3651 | 249 | `components/screenshot_action.py` | ADB exe + 路径 + 状态 Label |
| P6 | `refresh_actions_log` + 搜索栏 | 4892-5070 | ~178 | `components/actions_log_view.py` | Text widget + 路径 |
**估算**:完整抽完 P1-P6 约减 2480 行 → 主文件 ~3000 行。
若 P2 风险评估后觉得不值得做,可跳过,仍能减约 1830 行 → ~3650 行。
## 抽取模式(每轮通用)
每个候选段按以下步骤处理:
### 1. 摸清依赖
读完整段代码,列出:
- 捕获的 `main()` 局部变量(widgets / Vars / 路径 / 状态)
- 调用的 `main()` 内其它闭包(回调)
- 调用的 module-level 函数 / 项目其它模块函数
### 2. 设计签名
把闭包改造为顶层函数:
```python
# components/<feature>.py
def <feature_action>(
parent: tk.Misc, # 父窗口或容器
*,
deps: <FeatureDeps>, # 依赖注入容器
on_line: Callable[[str], None], # 日志回调
...
) -> <return-type>:
"""..."""
```
依赖容器优先用 `dataclass`(命名参数太多时):
```python
@dataclass
class <FeatureDeps>:
adb_exe: str
ip_var: tk.StringVar
refresh_devices: Callable[[], None]
# ...
```
### 3. 抽取 + 注入
- 在 `gui/components/<feature>.py` 写新函数
- 在 `gui_app.py` 原位置:删旧闭包,import 新函数,传依赖调用
### 4. 验证
```bash
pytest # 全部通过
python -m py_compile gui/gui_app.py gui/components/<feature>.py
# 手工 smoke:启动 GUI 走一遍受影响功能
```
### 5. 提交
按 AGENTS.md 规则:
- `git status` 检查只改了预期文件
- `git add` 具体路径,不用 `-A`
- commit 消息格式参考历史:`refactor(gui): 抽 <feature> 到 components/<file> (<旧行数> → <新行数>)`
- `git push`
## 风险
| 风险 | 缓解 |
|---|---|
| 闭包捕获 Var 漏传 → 配置项静默丢失 | P2 用 `dataclass` 聚合 Var 引用;先写完整测试再改 |
| GUI 交互回归(按钮回调失效等) | 每轮手工 smoke;优先抽边界清晰的 P1/P5 验证流程 |
| `mainloop` / `after()` 调度跨闭包失效 | 显式把 `root` 或 `tk_widget.after` 当依赖传入 |
| import 循环(components → gui_app) | components/* 不允许 import gui_app;只接收依赖参数 |
## 停止条件
满足任一即停手,向用户报告本轮成果:
1. `gui_app.py` ≤ 3000 行
2. 下一个候选段需要传 ≥ 50 个独立依赖(信号:应该先把 widgets 聚成一个 `UiState`,那是另一个 spec)
3. 抽取后 `gui_app.py` 行数下降 < 50(边际收益太小)
4. 某轮抽取后 pytest 失败且修复需要改动多个非目标文件(信号:风险扩散,停下复盘)
## 实现顺序
按 P1 → P2 → P3 → P4 → P5 → P6,每段独立 commit。
P2 视 P1 完成后情况评估(如果 P1 已经把主文件压到很小,P2 可推迟)。
## 测试策略
- **现有 pytest**:每轮必须保持全绿(不需要为抽取新增单测,因为是纯重构)
- **手工 smoke**(每轮都要走一遍受影响路径):
- P1:启动 → 打开 "WiFi ADB" 对话框 → 扫描设备 → 连接
- P2:启动 → 修改若干配置项 → 关闭 → 重启 → 检查配置已保存
- P3/P4:启动 → 切到「预约」/「高价值房间」Tab → 验证刷新
- P5:启动 → 截图按钮 → 检查图片落盘
- P6:启动 → 切到「操作日志」Tab → 搜索
## Out of Scope
- `gui/web_gui.py`(1810 行)—— 下一轮独立 spec
- `douyin/core/*` 几个 1000+ 行业务文件 —— 下一轮独立 spec
- 引入新的 GUI 框架或异步模式
- 单元测试覆盖率提升
## Outcome(2026-05-30 实施完成)
**起始**:5476 行
**结束**:3052 行
**降幅**:2424 行(-44.3%)
### 抽出的模块(19 个新文件,按抽取顺序)
| # | commit | 模块 | 主文件行数变化 |
|---|---|---|---|
| 1 | `ac46478` | `components/wifi_adb_scan_dialog.py` | 5476 → 5274 (-202) |
| 2 | `6bffcae` | `components/wifi_adb_dialog.py` | 5274 → 4913 (-361) |
| 3 | `5329561` | `components/reservations_view.py` | 4913 → 4656 (-257) |
| 4 | `a07ef00` | `components/high_value_view.py` | 4656 → 4635 (-21) |
| 5 | `8ea6765` | `components/screenshot_action.py` | 4635 → 4463 (-172) |
| 6 | `44b0914` | `components/actions_log_view.py` | 4463 → 4337 (-126) |
| 7 | `4993fad` | `components/wifi_adb_pick_dialog.py` | 4337 → 4268 (-69) |
| 8 | `b338004` | `components/wifi_adb_reconnect.py` | 4268 → 4148 (-120) |
| 9 | `c5df7a0` | `components/ocr_results_view.py` | 4148 → 4117 (-31) |
| 10 | `a7ef92f` | `components/log_pump.py` | 4117 → 4044 (-73) |
| 11 | `59ebe8c` | `components/win_record_view.py` | 4044 → 3891 (-153) |
| 12 | `92d5242` | `components/preview_view.py` | 3891 → 3775 (-116) |
| 13 | `68d1224` | `components/wifi_adb_one_click_scan.py` | 3775 → 3715 (-60) |
| 14 | `64c210d` | `components/run_log_view.py` | 3715 → 3669 (-46) |
| 15 | `97935d9` | `components/text_search.py` | 3669 → 3590 (-79) |
| 16 | `2a05be2` | `components/logging_config_io.py` | 3590 → 3563 (-27) |
| 17 | `f6d382b` | `components/run_params_builder.py` | 3563 → 3538 (-25) |
| 18 | `6bac014` | `components/config_io.py` | 3538 → 3052 (-486) |
### 关键决策
- **P2 配置序列化**(`load_config_to_ui` + `save_ui_to_config`,640 行,122 个 Var)
原本预计跳过,但通过 dict-of-vars 模式(共用一份 `vars_dict` 和 `texts_dict`,
只构造一次)实际抽完节省了 486 行,是单次最大收益。同时需要更新
`tools/diagnostics/check_web_gui_config_sync.py` 让它从 `config_io.py` 读
save/load 函数体。
- 用同样的 dict-of-vars 模式抽了 `save_logging_config`(18 Var)、`get_params`
(30 Var)、`config_io.py`(122 Var),证明此模式对中大型 Var 段都有效。
### 剩余无法继续的部分
- 顶层闭包都已 ≤32 行,继续抽边际收益 < 50 行(触发停止条件第 3 条)。
- 主文件剩余 3052 行的主体是大量 widget 创建代码(LabelFrame/Spinbox/Entry/
Treeview 一片片),不是闭包。这部分要继续瘦身需要按 Tab 拆 `setup_xxx_tab(...)`,
属于不同的重构模式(改变 main() 调用结构,风险更高),应作为下一轮独立 spec。
- 两组日志搜索小闭包(运行日志 + 行为日志各 5 个,共 ~70 行)用 `nonlocal`
维护 matches state,做成 search-state 容器抽完节省 < 50 行,性价比低。
### 验证
- 每轮抽取后 `pytest` 全绿(1374 passed),包括 `test_gui_app_smoke.py` 启动 smoke 和
`test_gui_webui_sync.py` GUI/WebUI 配置 key 同步检查。
- 两次 pytest 失败:
- `preview_view` 抽取:`_preview_view` 在 `update_image_list` 之前被引用
→ 调整 `_preview_view` 创建位置即修复。
- `config_io` 抽取:`enable_ocr_mode1_var` 因含数字未被 `\b\w+_var\b` 正则替换
→ 单点修复;`test_gui_webui_sync.py` 因 sync 工具锚点失效
→ 让工具改读 `config_io.py`。
- 未做手工 GUI smoke(建议用户启动一次 GUI 走一遍 WiFi 对话框 / 预约 Tab / 截图按钮
/ 操作日志 / 中奖记录 Tab / 修改并保存配置,确认无回归)。
### 停止原因
主文件已从 5476 → 3052(-44%),剩余候选段都 ≤ 50 行(触发停止条件第 3 条)。
继续瘦身需要按 Tab 拆 widget 创建代码,是不同的重构模式,作为下一轮独立 spec。