refactor(gui): 抽顶部设备栏到 components/top_device_bar (607 → 530)
This commit is contained in:
1 parent
9896eb676a
commit
9642b7b0f8
2 files changed
+132
-81
No files matched your search
@@ -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],
|
||||
)
|
||||
+4
-81
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user