diff --git a/gui/components/wifi_adb_pick_dialog.py b/gui/components/wifi_adb_pick_dialog.py new file mode 100644 index 0000000..9817367 --- /dev/null +++ b/gui/components/wifi_adb_pick_dialog.py @@ -0,0 +1,108 @@ +"""扫描结果选择对话框。 + +从 ``gui/gui_app.py`` 抽出。原本是 ``main()`` 内的闭包 ``_open_scan_pick_dialog``, +当 "扫描 WiFi" 一键扫描发现多台设备时弹出,让用户挑一个作为当前设备。 + +依赖通过显式参数注入: +- ``parent`` - 父窗口 (用于 ``Toplevel`` 与 ``transient``) +- ``devs`` - 扫描结果列表(``DiscoveredDevice``) +- ``device_var`` - 主窗口的设备 StringVar(选中后写入) +- ``on_line`` - 日志回调 +- ``refresh_devices`` - 刷新主窗口设备下拉的回调 +""" + +from __future__ import annotations + +import tkinter as tk +from tkinter import ttk +from tkinter.constants import BOTH, LEFT, RIGHT, W, X +from typing import Callable, Sequence + +from gui.gui_core_pkg import set_last_wifi_target + + +def open_scan_pick_dialog( + parent: tk.Misc, + devs: Sequence, + *, + device_var: tk.StringVar, + on_line: Callable[[str], None], + refresh_devices: Callable[[], None], +) -> None: + """简化版选择对话框:扫描结果已就绪,让用户从列表挑一个使用。""" + pick_dlg = tk.Toplevel(parent) + pick_dlg.title(f"扫描发现 {len(devs)} 台设备 - 请选择") + pick_dlg.geometry("680x320") + pick_dlg.transient(parent) + + ttk.Label( + pick_dlg, + text="扫描发现多台设备,请选中一个作为当前设备:", + font=("Arial", 9), + ).pack(anchor=W, padx=10, pady=(10, 4)) + + cols = ("target", "state", "model", "message") + tree = ttk.Treeview(pick_dlg, columns=cols, show="headings", height=8) + tree.heading("target", text="IP:Port") + tree.heading("state", text="状态") + tree.heading("model", text="设备型号") + tree.heading("message", text="adb 返回") + tree.column("target", width=140, anchor=W) + tree.column("state", width=90, anchor=W) + tree.column("model", width=120, anchor=W) + tree.column("message", width=200, anchor=W) + tree.pack(fill=BOTH, expand=True, padx=10, pady=(0, 8)) + + for d in devs: + msg = (d.message or "").strip().replace("\n", " | ") + tree.insert( + "", "end", + values=(d.target, d.adb_state, d.model or "-", msg[:200]), + ) + + bottom = ttk.Frame(pick_dlg) + bottom.pack(fill=X, padx=10, pady=(0, 10)) + + def use_selected(): + sel = tree.selection() + if not sel: + on_line("[扫描] 请先在列表中选中一个设备") + return + values = tree.item(sel[0])["values"] + target = str(values[0]) + state = str(values[1]) + model = str(values[2] or "").strip() + if model == "-": + model = "" + if state == "unauthorized": + on_line( + f"[扫描] {target} 未授权,请在手机上确认 ADB 调试授权后重试" + ) + return + if state == "failed": + # 选中的就是失败项时,把对应的 message 拿出来给用户看真实根因 + fail_msg = "" + for d in devs: + if d.target == target: + fail_msg = (d.message or "").strip().replace("\n", " | ") + break + on_line( + f"[扫描] {target} adb 连接失败: {fail_msg or '未知错误'}" + ) + return + if state != "connected": + on_line(f"[扫描] {target} 当前状态={state},无法直接使用") + return + refresh_devices() + device_var.set(target) + try: + set_last_wifi_target(target, model) + except Exception: + pass + on_line( + f"[扫描] 已设为当前设备: {target}(已记住,下次 GUI 启动会自动重连)" + ) + pick_dlg.destroy() + + ttk.Button(bottom, text="使用选中", command=use_selected).pack(side=LEFT) + ttk.Button(bottom, text="关闭", command=pick_dlg.destroy).pack(side=RIGHT) diff --git a/gui/gui_app.py b/gui/gui_app.py index ed2637f..dbe150c 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -54,6 +54,7 @@ from gui.components.actions_log_view import ( configure_actions_log_colors as _configure_actions_log_colors_impl, refresh_actions_log as _refresh_actions_log_impl, ) +from gui.components.wifi_adb_pick_dialog import open_scan_pick_dialog as _open_scan_pick_dialog_impl # 导入日志颜色配置模块 try: @@ -464,83 +465,13 @@ def main(): _open_scan_pick_dialog(devs) def _open_scan_pick_dialog(devs): - """简化版选择对话框:扫描结果已就绪,让用户从列表挑一个使用。""" - pick_dlg = tk.Toplevel(root) - pick_dlg.title(f"扫描发现 {len(devs)} 台设备 - 请选择") - pick_dlg.geometry("680x320") - pick_dlg.transient(root) - - ttk.Label( - pick_dlg, - text="扫描发现多台设备,请选中一个作为当前设备:", - font=("Arial", 9), - ).pack(anchor=W, padx=10, pady=(10, 4)) - - cols = ("target", "state", "model", "message") - tree = ttk.Treeview(pick_dlg, columns=cols, show="headings", height=8) - tree.heading("target", text="IP:Port") - tree.heading("state", text="状态") - tree.heading("model", text="设备型号") - tree.heading("message", text="adb 返回") - tree.column("target", width=140, anchor=W) - tree.column("state", width=90, anchor=W) - tree.column("model", width=120, anchor=W) - tree.column("message", width=200, anchor=W) - tree.pack(fill=BOTH, expand=True, padx=10, pady=(0, 8)) - - for d in devs: - msg = (d.message or "").strip().replace("\n", " | ") - tree.insert( - "", "end", - values=(d.target, d.adb_state, d.model or "-", msg[:200]), - ) - - bottom = ttk.Frame(pick_dlg) - bottom.pack(fill=X, padx=10, pady=(0, 10)) - - def use_selected(): - sel = tree.selection() - if not sel: - on_line("[扫描] 请先在列表中选中一个设备") - return - values = tree.item(sel[0])["values"] - target = str(values[0]) - state = str(values[1]) - model = str(values[2] or "").strip() - if model == "-": - model = "" - if state == "unauthorized": - on_line( - f"[扫描] {target} 未授权,请在手机上确认 ADB 调试授权后重试" - ) - return - if state == "failed": - # 选中的就是失败项时,把对应的 message 拿出来给用户看真实根因 - fail_msg = "" - for d in devs: - if d.target == target: - fail_msg = (d.message or "").strip().replace("\n", " | ") - break - on_line( - f"[扫描] {target} adb 连接失败: {fail_msg or '未知错误'}" - ) - return - if state != "connected": - on_line(f"[扫描] {target} 当前状态={state},无法直接使用") - return - refresh_devices() - device_var.set(target) - try: - set_last_wifi_target(target, model) - except Exception: - pass - on_line( - f"[扫描] 已设为当前设备: {target}(已记住,下次 GUI 启动会自动重连)" - ) - pick_dlg.destroy() - - ttk.Button(bottom, text="使用选中", command=use_selected).pack(side=LEFT) - ttk.Button(bottom, text="关闭", command=pick_dlg.destroy).pack(side=RIGHT) + _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)