140 lines
4.7 KiB
Python
140 lines
4.7 KiB
Python
"""GUI 顶部设备选择栏(设备下拉 + 刷新设备 + 扫描 WiFi + WiFi ADB 按钮 + Y偏移)。
|
||
|
||
从 ``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, Optional
|
||
|
||
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],
|
||
y_var: Optional[tk.IntVar] = None,
|
||
) -> TopDeviceBarHandles:
|
||
"""构建顶部设备选择栏 + WiFi 工具按钮 + (可选)Y偏移 Spinbox。
|
||
|
||
若提供 ``y_var``,会在所有按钮之后追加 "Y偏移:" 标签 + Spinbox(范围 -200..200)。
|
||
"""
|
||
# 用 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))
|
||
|
||
# 可选的 Y偏移 Spinbox(放在所有按钮之后)
|
||
if y_var is not None:
|
||
ttk.Label(top, text="Y偏移:").pack(side=LEFT)
|
||
ttk.Spinbox(top, from_=-200, to=200, textvariable=y_var, width=6).pack(
|
||
side=LEFT, padx=(0, 12)
|
||
)
|
||
|
||
return TopDeviceBarHandles(
|
||
device_var=device_var,
|
||
refresh_devices=refresh_devices,
|
||
get_adb_exe=lambda: adb_exe_box[0],
|
||
)
|