From ac464788e16dce0bfb199be23c1349ed2d7520a4 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 30 May 2026 13:43:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor(gui):=20=E6=8A=BD=20open=5Fscan=5Fdial?= =?UTF-8?q?og=20=E5=88=B0=20components/wifi=5Fadb=5Fscan=5Fdialog=20(5476?= =?UTF-8?q?=20=E2=86=92=205274)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gui/components/wifi_adb_scan_dialog.py | 231 +++++++++++++++++++++++++ gui/gui_app.py | 204 +--------------------- 2 files changed, 232 insertions(+), 203 deletions(-) create mode 100644 gui/components/wifi_adb_scan_dialog.py diff --git a/gui/components/wifi_adb_scan_dialog.py b/gui/components/wifi_adb_scan_dialog.py new file mode 100644 index 0000000..da979d4 --- /dev/null +++ b/gui/components/wifi_adb_scan_dialog.py @@ -0,0 +1,231 @@ +"""WiFi ADB 局域网扫描对话框。 + +从 ``gui/gui_app.py`` 中抽出。原本是 ``main()`` 内的闭包 ``open_scan_dialog``, +其签名已经是显式参数形式,所以迁移到模块顶层只需要把 import 提上来即可。 + +被 ``open_wifi_adb_dialog`` 通过 "扫描局域网" 按钮调用。 +""" + +from __future__ import annotations + +import threading +import tkinter as tk +from tkinter import ttk +from tkinter.constants import BOTH, LEFT, RIGHT, W, X +from typing import Callable + +from gui.gui_core_pkg import ( + discover_adb_devices, + get_local_subnet, + parse_port_spec, +) + + +def open_scan_dialog( + parent: tk.Misc, + adb_exe_local: str, + ip_var: tk.StringVar, + on_line_local: Callable[[str], None], + refresh_cb: Callable[[], None], +) -> None: + """打开"扫描局域网"对话框:自动推子网或手动输入 → 后台扫描 → 列表展示。""" + scan_dlg = tk.Toplevel(parent) + scan_dlg.title("扫描局域网 ADB 设备") + scan_dlg.geometry("640x440") + scan_dlg.minsize(520, 360) + scan_dlg.transient(parent) + + # 顶部:子网输入 + 自动推按钮 + 开始扫描 + top_frame = ttk.LabelFrame(scan_dlg, text="扫描范围", padding=10) + top_frame.pack(fill=X, padx=10, pady=10) + + ttk.Label(top_frame, text="子网/范围:").grid(row=0, column=0, sticky=W) + subnet_var = tk.StringVar(value=get_local_subnet() or "") + subnet_entry = ttk.Entry(top_frame, textvariable=subnet_var, width=32) + subnet_entry.grid(row=0, column=1, padx=(6, 6), sticky="we") + top_frame.grid_columnconfigure(1, weight=1) + + def auto_fill(): + sub = get_local_subnet() + if sub: + subnet_var.set(sub) + on_line_local(f"[扫描] 自动推导子网: {sub}") + else: + on_line_local("[扫描] 无法推导本机子网,请手动输入") + + ttk.Button(top_frame, text="自动推", command=auto_fill).grid( + row=0, column=2, padx=(0, 6) + ) + scan_btn = ttk.Button(top_frame, text="开始扫描") + scan_btn.grid(row=0, column=3) + + # 第二行:端口规格 + ttk.Label(top_frame, text="端口:").grid(row=1, column=0, sticky=W, pady=(6, 0)) + scan_port_var = tk.StringVar(value="5555") + ttk.Entry(top_frame, textvariable=scan_port_var, width=32).grid( + row=1, column=1, columnspan=3, padx=(6, 0), pady=(6, 0), sticky="we" + ) + + ttk.Label( + top_frame, + text="子网示例: 192.168.5.0/24 | 192.168.5.1-100 | 192.168.5.5", + font=("Arial", 8), + foreground="gray", + ).grid(row=2, column=0, columnspan=4, sticky=W, pady=(4, 0)) + ttk.Label( + top_frame, + text="端口示例: 5555 | 5555,5556 | 37000-40000 (Android 11+ 无线调试随机端口在 37000-44999)", + font=("Arial", 8), + foreground="gray", + ).grid(row=3, column=0, columnspan=4, sticky=W, pady=(2, 0)) + + # 中间:进度条 + 状态 + progress_frame = ttk.Frame(scan_dlg) + progress_frame.pack(fill=X, padx=10) + progress_var = tk.DoubleVar(value=0.0) + progress_bar = ttk.Progressbar( + progress_frame, mode="determinate", variable=progress_var, maximum=100 + ) + progress_bar.pack(fill=X, pady=(0, 4)) + status_var = tk.StringVar(value="待扫描") + ttk.Label(progress_frame, textvariable=status_var, font=("Arial", 9)).pack( + anchor=W + ) + + # 列表:IP / 状态 / 型号 / 备注 + list_frame = ttk.LabelFrame(scan_dlg, text="发现的设备", padding=10) + list_frame.pack(fill=BOTH, expand=True, padx=10, pady=(8, 0)) + + cols = ("target", "state", "model", "message") + tree = ttk.Treeview( + list_frame, 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 connect 输出") + tree.column("target", width=140, anchor=W) + tree.column("state", width=80, anchor=W) + tree.column("model", width=100, anchor=W) + tree.column("message", width=240, anchor=W) + tree.pack(side=LEFT, fill=BOTH, expand=True) + sb = ttk.Scrollbar(list_frame, orient="vertical", command=tree.yview) + sb.pack(side=LEFT, fill="y") + tree.configure(yscrollcommand=sb.set) + + # 底部按钮 + bottom_frame = ttk.Frame(scan_dlg) + bottom_frame.pack(fill=X, padx=10, pady=10) + + def use_selected(): + sel = tree.selection() + if not sel: + on_line_local("[扫描] 请先在列表中选中一个设备") + return + item = tree.item(sel[0]) + target = item["values"][0] + state = item["values"][1] + if state == "unauthorized": + on_line_local( + f"[扫描] {target} 未授权,请先在手机上确认 ADB 调试授权再选" + ) + return + # 把 IP 部分填回主对话框 + ip_part = str(target).split(":")[0] + ip_var.set(ip_part) + on_line_local(f"[扫描] 已选中 {target},可在主对话框点'连接'或它已在 adb devices 中") + try: + refresh_cb() + except Exception: + pass + scan_dlg.destroy() + + use_btn = ttk.Button( + bottom_frame, text="使用选中设备", command=use_selected, state="disabled" + ) + use_btn.pack(side=LEFT) + ttk.Button(bottom_frame, text="关闭", command=scan_dlg.destroy).pack( + side=RIGHT + ) + + def on_select(_event=None): + sel = tree.selection() + use_btn.config(state="normal" if sel else "disabled") + + tree.bind("<>", on_select) + + # 扫描线程 + scanning = {"flag": False} + + def do_scan(): + if scanning["flag"]: + on_line_local("[扫描] 上次扫描尚未完成,请稍候") + return + spec = subnet_var.get().strip() + if not spec: + on_line_local("[扫描] 请输入子网/IP 范围") + return + scanning["flag"] = True + scan_btn.config(state="disabled") + tree.delete(*tree.get_children()) + progress_var.set(0) + + def on_progress(done, total): + try: + pct = (done * 100.0) / max(total, 1) + progress_var.set(pct) + status_var.set(f"端口扫描 {done}/{total}") + scan_dlg.update_idletasks() + except Exception: + pass + + def on_phase(text): + try: + status_var.set(text) + scan_dlg.update_idletasks() + except Exception: + pass + + def worker(): + try: + port_list = parse_port_spec(scan_port_var.get().strip()) + devs = discover_adb_devices( + adb_exe_local, + subnet_spec=spec, + ports=port_list, + on_progress=on_progress, + on_phase=on_phase, + ) + except Exception as e: + on_line_local(f"[扫描] 异常: {e}") + devs = [] + + # 回主线程更新 UI + def render(): + for d in devs: + tree.insert( + "", + "end", + values=(d.target, d.adb_state, d.model, d.message[:120]), + ) + if devs: + status_var.set(f"扫描完成,发现 {len(devs)} 台设备") + on_line_local( + f"[扫描] 完成:{len(devs)} 台候选 — " + + ", ".join(f"{d.target}({d.adb_state})" for d in devs[:5]) + ) + else: + status_var.set("扫描完成,未发现设备") + on_line_local("[扫描] 未发现开放 5555 的设备") + progress_var.set(100) + scan_btn.config(state="normal") + scanning["flag"] = False + + try: + scan_dlg.after(0, render) + except Exception: + pass + + threading.Thread(target=worker, daemon=True).start() + + scan_btn.config(command=do_scan) diff --git a/gui/gui_app.py b/gui/gui_app.py index ef6c007..8779688 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -45,6 +45,7 @@ from gui.gui_core_pkg import ( from gui.gui_core_pkg.wifi_adb_scan import verify_adb_target, query_device_model from douyin.core.path_config import get_logs_dir from gui.components.mapping_table import create_mapping_tab +from gui.components.wifi_adb_scan_dialog import open_scan_dialog # 导入日志颜色配置模块 try: @@ -536,209 +537,6 @@ def main(): scan_connect_btn.config(command=_on_scan_one_click) # WiFi ADB 功能 - def open_scan_dialog(parent, adb_exe_local, ip_var, on_line_local, refresh_cb): - """打开"扫描局域网"对话框:自动推子网或手动输入 → 后台扫描 → 列表展示。""" - scan_dlg = tk.Toplevel(parent) - scan_dlg.title("扫描局域网 ADB 设备") - scan_dlg.geometry("640x440") - scan_dlg.minsize(520, 360) - scan_dlg.transient(parent) - - # 顶部:子网输入 + 自动推按钮 + 开始扫描 - top_frame = ttk.LabelFrame(scan_dlg, text="扫描范围", padding=10) - top_frame.pack(fill=X, padx=10, pady=10) - - ttk.Label(top_frame, text="子网/范围:").grid(row=0, column=0, sticky=W) - subnet_var = tk.StringVar(value=get_local_subnet() or "") - subnet_entry = ttk.Entry(top_frame, textvariable=subnet_var, width=32) - subnet_entry.grid(row=0, column=1, padx=(6, 6), sticky="we") - top_frame.grid_columnconfigure(1, weight=1) - - def auto_fill(): - sub = get_local_subnet() - if sub: - subnet_var.set(sub) - on_line_local(f"[扫描] 自动推导子网: {sub}") - else: - on_line_local("[扫描] 无法推导本机子网,请手动输入") - - ttk.Button(top_frame, text="自动推", command=auto_fill).grid( - row=0, column=2, padx=(0, 6) - ) - scan_btn = ttk.Button(top_frame, text="开始扫描") - scan_btn.grid(row=0, column=3) - - # 第二行:端口规格 - ttk.Label(top_frame, text="端口:").grid(row=1, column=0, sticky=W, pady=(6, 0)) - scan_port_var = tk.StringVar(value="5555") - ttk.Entry(top_frame, textvariable=scan_port_var, width=32).grid( - row=1, column=1, columnspan=3, padx=(6, 0), pady=(6, 0), sticky="we" - ) - - ttk.Label( - top_frame, - text="子网示例: 192.168.5.0/24 | 192.168.5.1-100 | 192.168.5.5", - font=("Arial", 8), - foreground="gray", - ).grid(row=2, column=0, columnspan=4, sticky=W, pady=(4, 0)) - ttk.Label( - top_frame, - text="端口示例: 5555 | 5555,5556 | 37000-40000 (Android 11+ 无线调试随机端口在 37000-44999)", - font=("Arial", 8), - foreground="gray", - ).grid(row=3, column=0, columnspan=4, sticky=W, pady=(2, 0)) - - # 中间:进度条 + 状态 - progress_frame = ttk.Frame(scan_dlg) - progress_frame.pack(fill=X, padx=10) - progress_var = tk.DoubleVar(value=0.0) - progress_bar = ttk.Progressbar( - progress_frame, mode="determinate", variable=progress_var, maximum=100 - ) - progress_bar.pack(fill=X, pady=(0, 4)) - status_var = tk.StringVar(value="待扫描") - ttk.Label(progress_frame, textvariable=status_var, font=("Arial", 9)).pack( - anchor=W - ) - - # 列表:IP / 状态 / 型号 / 备注 - list_frame = ttk.LabelFrame(scan_dlg, text="发现的设备", padding=10) - list_frame.pack(fill=BOTH, expand=True, padx=10, pady=(8, 0)) - - cols = ("target", "state", "model", "message") - tree = ttk.Treeview( - list_frame, 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 connect 输出") - tree.column("target", width=140, anchor=W) - tree.column("state", width=80, anchor=W) - tree.column("model", width=100, anchor=W) - tree.column("message", width=240, anchor=W) - tree.pack(side=LEFT, fill=BOTH, expand=True) - sb = ttk.Scrollbar(list_frame, orient="vertical", command=tree.yview) - sb.pack(side=LEFT, fill="y") - tree.configure(yscrollcommand=sb.set) - - # 底部按钮 - bottom_frame = ttk.Frame(scan_dlg) - bottom_frame.pack(fill=X, padx=10, pady=10) - - def use_selected(): - sel = tree.selection() - if not sel: - on_line_local("[扫描] 请先在列表中选中一个设备") - return - item = tree.item(sel[0]) - target = item["values"][0] - state = item["values"][1] - if state == "unauthorized": - on_line_local( - f"[扫描] {target} 未授权,请先在手机上确认 ADB 调试授权再选" - ) - return - # 把 IP 部分填回主对话框 - ip_part = str(target).split(":")[0] - ip_var.set(ip_part) - on_line_local(f"[扫描] 已选中 {target},可在主对话框点'连接'或它已在 adb devices 中") - try: - refresh_cb() - except Exception: - pass - scan_dlg.destroy() - - use_btn = ttk.Button( - bottom_frame, text="使用选中设备", command=use_selected, state="disabled" - ) - use_btn.pack(side=LEFT) - ttk.Button(bottom_frame, text="关闭", command=scan_dlg.destroy).pack( - side=RIGHT - ) - - def on_select(_event=None): - sel = tree.selection() - use_btn.config(state="normal" if sel else "disabled") - - tree.bind("<>", on_select) - - # 扫描线程 - scanning = {"flag": False} - - def do_scan(): - if scanning["flag"]: - on_line_local("[扫描] 上次扫描尚未完成,请稍候") - return - spec = subnet_var.get().strip() - if not spec: - on_line_local("[扫描] 请输入子网/IP 范围") - return - scanning["flag"] = True - scan_btn.config(state="disabled") - tree.delete(*tree.get_children()) - progress_var.set(0) - - def on_progress(done, total): - try: - pct = (done * 100.0) / max(total, 1) - progress_var.set(pct) - status_var.set(f"端口扫描 {done}/{total}") - scan_dlg.update_idletasks() - except Exception: - pass - - def on_phase(text): - try: - status_var.set(text) - scan_dlg.update_idletasks() - except Exception: - pass - - def worker(): - try: - from gui.gui_core_pkg import parse_port_spec - port_list = parse_port_spec(scan_port_var.get().strip()) - devs = discover_adb_devices( - adb_exe_local, - subnet_spec=spec, - ports=port_list, - on_progress=on_progress, - on_phase=on_phase, - ) - except Exception as e: - on_line_local(f"[扫描] 异常: {e}") - devs = [] - - # 回主线程更新 UI - def render(): - for d in devs: - tree.insert( - "", - "end", - values=(d.target, d.adb_state, d.model, d.message[:120]), - ) - if devs: - status_var.set(f"扫描完成,发现 {len(devs)} 台设备") - on_line_local( - f"[扫描] 完成:{len(devs)} 台候选 — " - + ", ".join(f"{d.target}({d.adb_state})" for d in devs[:5]) - ) - else: - status_var.set("扫描完成,未发现设备") - on_line_local("[扫描] 未发现开放 5555 的设备") - progress_var.set(100) - scan_btn.config(state="normal") - scanning["flag"] = False - - try: - scan_dlg.after(0, render) - except Exception: - pass - - threading.Thread(target=worker, daemon=True).start() - - scan_btn.config(command=do_scan) def open_wifi_adb_dialog(): """打开WiFi ADB设置对话框"""