From 9642b7b0f857e33be226aee00c9971d426405fe1 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 30 May 2026 17:45:01 +0800 Subject: [PATCH] =?UTF-8?q?refactor(gui):=20=E6=8A=BD=E9=A1=B6=E9=83=A8?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E6=A0=8F=E5=88=B0=20components/top=5Fdevice?= =?UTF-8?q?=5Fbar=20(607=20=E2=86=92=20530)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gui/components/top_device_bar.py | 128 +++++++++++++++++++++++++++++++ gui/gui_app.py | 85 +------------------- 2 files changed, 132 insertions(+), 81 deletions(-) create mode 100644 gui/components/top_device_bar.py diff --git a/gui/components/top_device_bar.py b/gui/components/top_device_bar.py new file mode 100644 index 0000000..9938e89 --- /dev/null +++ b/gui/components/top_device_bar.py @@ -0,0 +1,128 @@ +"""GUI 顶部设备选择栏(设备下拉 + 刷新设备 + 扫描 WiFi + WiFi ADB 按钮)。 + +从 ``gui/gui_app.py`` 抽出。包括 5 个互相耦合的闭包: +- ``refresh_devices``: 刷新 adb 设备列表 +- ``_try_reconnect_last_wifi``: 启动后延迟尝试重连上次设备 +- ``_on_scan_one_click``: 一键扫描局域网 +- ``_handle_scan_result``: 扫描结果处理 +- ``_open_scan_pick_dialog``: 多设备选择弹窗 + +返回 ``TopDeviceBarHandles`` 暴露 ``device_var`` / ``adb_exe`` / ``refresh_devices``。 +""" + +from __future__ import annotations + +import tkinter as tk +from dataclasses import dataclass +from tkinter import ttk +from tkinter.constants import LEFT, X +from typing import Callable + +from gui.gui_core import find_adb_executable, list_adb_devices +from gui.components.wifi_adb_dialog import open_wifi_adb_dialog +from gui.components.wifi_adb_pick_dialog import open_scan_pick_dialog +from gui.components.wifi_adb_reconnect import try_reconnect_last_wifi +from gui.components.wifi_adb_one_click_scan import ( + on_scan_one_click, + _handle_scan_result, +) + + +@dataclass +class TopDeviceBarHandles: + """``main()`` 后续仍要用的 handles。""" + + device_var: tk.StringVar + refresh_devices: Callable[[], None] + # adb_exe 是动态值(refresh_devices 会更新),用 getter 暴露 + get_adb_exe: Callable[[], str] + + +def setup_top_device_bar( + top: tk.Misc, + *, + root: tk.Misc, + on_line: Callable[[str], None], +) -> TopDeviceBarHandles: + """构建顶部设备选择栏 + WiFi 工具按钮。""" + # 用 list 持有 adb_exe 让闭包可以原地更新(替代 nonlocal) + adb_exe_box = [find_adb_executable()] + devices = list_adb_devices(adb_exe_box[0]) + device_var = tk.StringVar(value=(devices[0] if devices else "")) + + ttk.Label(top, text="设备:").pack(side=LEFT) + device_combo = ttk.Combobox(top, textvariable=device_var, values=devices, width=28) + device_combo.pack(side=LEFT, padx=(0, 10)) + + def refresh_devices() -> None: + adb_exe_box[0] = find_adb_executable() + devs = list_adb_devices(adb_exe_box[0]) + device_combo["values"] = devs + if devs and not device_var.get(): + device_var.set(devs[0]) + if not adb_exe_box[0]: + on_line("[设备] 未找到 adb.exe(请确认已安装 platform-tools)") + elif not devs: + on_line("[设备] adb 可用,但未发现 device 状态设备(可能未授权/未连接)") + + refresh_devices_btn = ttk.Button(top, text="刷新设备", command=refresh_devices) + refresh_devices_btn.pack(side=LEFT, padx=(0, 8)) + + # 一键扫描局域网 ADB 设备并自动连接(同 WiFi 下设备已开 5555 / 已授权时无需 USB) + scan_connect_btn = ttk.Button(top, text="扫描 WiFi") + scan_connect_btn.pack(side=LEFT, padx=(0, 20)) + _scan_busy = {"flag": False} + + def _try_reconnect_last_wifi() -> None: + try_reconnect_last_wifi( + root, + adb_exe=adb_exe_box[0], + device_var=device_var, + on_line=on_line, + refresh_devices=refresh_devices, + ) + + # 启动时延迟一点,等界面创建完成再尝试重连 + root.after(300, _try_reconnect_last_wifi) + + def _open_scan_pick_dialog(devs): + open_scan_pick_dialog( + root, + devs, + device_var=device_var, + on_line=on_line, + refresh_devices=refresh_devices, + ) + + def _on_scan_one_click() -> None: + on_scan_one_click( + adb_exe=adb_exe_box[0], + scan_btn=scan_connect_btn, + scan_busy=_scan_busy, + root=root, + on_line=on_line, + device_var=device_var, + refresh_devices=refresh_devices, + open_pick_dialog=_open_scan_pick_dialog, + ) + + scan_connect_btn.config(command=_on_scan_one_click) + + # WiFi ADB 功能 + wifi_adb_btn = ttk.Button( + top, + text="WiFi ADB", + command=lambda: open_wifi_adb_dialog( + root, + adb_exe=adb_exe_box[0], + on_line=on_line, + refresh_devices=refresh_devices, + ), + ) + wifi_adb_btn.pack(side=LEFT, padx=(0, 20)) + + return TopDeviceBarHandles( + device_var=device_var, + refresh_devices=refresh_devices, + get_adb_exe=lambda: adb_exe_box[0], + ) diff --git a/gui/gui_app.py b/gui/gui_app.py index 9458b79..e78c240 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -100,6 +100,7 @@ from gui.components.keywords_tab import setup_keywords_tab from gui.components.main_notebook import setup_main_notebook from gui.components.action_buttons import setup_action_buttons from gui.components.config_vars import build_config_vars +from gui.components.top_device_bar import setup_top_device_bar from gui.components.periodic_refreshers import ( setup_periodic_refreshers, setup_close_handler, @@ -211,24 +212,9 @@ def main(): top.pack(fill=X, padx=12, pady=10) adb_exe = find_adb_executable() - devices = list_adb_devices(adb_exe) - device_var = tk.StringVar(value=(devices[0] if devices else "")) - - ttk.Label(top, text="设备:").pack(side=LEFT) - device_combo = ttk.Combobox(top, textvariable=device_var, values=devices, width=28) - device_combo.pack(side=LEFT, padx=(0, 10)) - - def refresh_devices(): - nonlocal adb_exe - adb_exe = find_adb_executable() - devs = list_adb_devices(adb_exe) - device_combo["values"] = devs - if devs and not device_var.get(): - device_var.set(devs[0]) - if not adb_exe: - on_line("[设备] 未找到 adb.exe(请确认已安装 platform-tools)") - elif not devs: - on_line("[设备] adb 可用,但未发现 device 状态设备(可能未授权/未连接)") + _top_bar = setup_top_device_bar(top, root=root, on_line=on_line) + device_var = _top_bar.device_var + refresh_devices = _top_bar.refresh_devices def save_logging_config(): _save_logging_config_impl( @@ -237,69 +223,6 @@ def main(): write_config=write_config, on_line=on_line, ) - refresh_devices_btn = ttk.Button(top, text="刷新设备", command=refresh_devices) - refresh_devices_btn.pack(side=LEFT, padx=(0, 8)) - - # 一键扫描局域网 ADB 设备并自动连接(同 WiFi 下设备已开 5555 / 已授权时无需 USB) - scan_connect_btn = ttk.Button(top, text="扫描 WiFi") - scan_connect_btn.pack(side=LEFT, padx=(0, 20)) - _scan_busy = {"flag": False} - - def _try_reconnect_last_wifi(): - _try_reconnect_last_wifi_impl( - root, - adb_exe=adb_exe, - device_var=device_var, - on_line=on_line, - refresh_devices=refresh_devices, - ) - # 启动时延迟一点,等界面创建完成再尝试重连 - root.after(300, _try_reconnect_last_wifi) - - def _on_scan_one_click(): - _on_scan_one_click_impl( - adb_exe=adb_exe, - scan_btn=scan_connect_btn, - scan_busy=_scan_busy, - root=root, - on_line=on_line, - device_var=device_var, - refresh_devices=refresh_devices, - open_pick_dialog=_open_scan_pick_dialog, - ) - - def _handle_scan_result(devs): - _handle_scan_result_impl( - devs, - on_line=on_line, - device_var=device_var, - refresh_devices=refresh_devices, - open_pick_dialog=_open_scan_pick_dialog, - ) - - def _open_scan_pick_dialog(devs): - _open_scan_pick_dialog_impl( - root, - devs, - device_var=device_var, - on_line=on_line, - refresh_devices=refresh_devices, - ) - - scan_connect_btn.config(command=_on_scan_one_click) - - # WiFi ADB 功能 - wifi_adb_btn = ttk.Button( - top, - text="WiFi ADB", - command=lambda: _open_wifi_adb_dialog( - root, - adb_exe=adb_exe, - on_line=on_line, - refresh_devices=refresh_devices, - ), - ) - wifi_adb_btn.pack(side=LEFT, padx=(0, 20)) # 配置区(提前读取配置,供后续使用) cfg = read_config()