Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 调整部分样式与布局;对 json 字符串进行校验 #32

Merged
merged 4 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/MaaDebugger/utils/input_checker/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import json
from pathlib import Path
from typing import Optional


def hwnd(data: str) -> Optional[str]:
if not data:
return

try:
int(data, 16)
except:
return "Please input hexadecimal numbers."


def json_style_str(data: str) -> Optional[str]:
if not data:
return

try:
json.loads(data)
except json.decoder.JSONDecodeError as e:
return f"JSONDecodeError: {e}"
except Exception as e:
return str(e)


def paths_exist(data: str) -> Optional[str]:
if not data:
return

not_exist_paths = []
not_pipe_paths = []
msg = ""

paths = [Path(p) for p in data.split("\n") if p]
for p in paths:
if p.exists():
pipe_dir = Path(p, "pipeline")
if not pipe_dir.is_dir():
not_pipe_paths.append(str(p))
else:
not_exist_paths.append(str(p))

if not_exist_paths:
if len(not_pipe_paths) == 1:
msg += f"Path not exist: {not_exist_paths}"
else:
msg += f"Paths not exist: {not_exist_paths}"

if not_pipe_paths:
msg += f" Pipeline dir not in: {not_pipe_paths}"

if msg:
return msg
134 changes: 73 additions & 61 deletions src/MaaDebugger/webpage/index_page/master_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
from nicegui import app, binding, ui

from ...maafw import maafw
from ...utils import input_checker as ic
from ...webpage.components.status_indicator import Status, StatusIndicator

binding.MAX_PROPAGATION_TIME = 1
STORAGE = app.storage.general


class GlobalStatus:
Expand Down Expand Up @@ -41,9 +43,7 @@ async def connect_control():
adb = ui.tab("Adb")
win32 = ui.tab("Win32")

with ui.tab_panels(tabs, value="Adb").bind_value(
app.storage.general, "controller_type"
):
with ui.tab_panels(tabs, value="Adb").bind_value(STORAGE, "controller_type"):
with ui.tab_panel(adb):
with ui.row(align_items="center").classes("w-full"):
await connect_adb_control()
Expand All @@ -53,53 +53,55 @@ async def connect_control():


async def connect_adb_control():
StatusIndicator(GlobalStatus, "ctrl_connecting")
with ui.row(align_items="center"):
with ui.row(align_items="baseline"):
StatusIndicator(GlobalStatus, "ctrl_connecting")
adb_path_input = (
ui.input(
"ADB Path",
placeholder="eg: C:/adb.exe",
)
.props("size=60")
.bind_value(app.storage.general, "adb_path")
.bind_value(STORAGE, "adb_path")
)
adb_address_input = (
ui.input(
"ADB Address",
placeholder="eg: 127.0.0.1:5555",
)
.props("size=30")
.bind_value(app.storage.general, "adb_address")
.props("size=20")
.bind_value(STORAGE, "adb_address")
)

adb_config_input = (
ui.input(
"Extras",
placeholder="eg: {}",
validation=ic.json_style_str,
)
.props("size=30")
.bind_value(app.storage.general, "adb_config")
.props("size=20")
.bind_value(STORAGE, "adb_config")
)
ui.button(
"Connect",
on_click=lambda: on_click_connect(),
)

ui.button(
icon="wifi_find",
"FIND",
on_click=lambda: on_click_detect(),
)

device_select = ui.select(
{}, on_change=lambda e: on_change_device_select(e)
{},
label="Devices",
on_change=lambda e: on_change_device_select(e),
).bind_visibility_from(
GlobalStatus,
"ctrl_detecting",
backward=lambda s: s == Status.SUCCEEDED,
)

if "adb_config" not in app.storage.general or not app.storage.general["adb_config"]:
app.storage.general["adb_config"] = "{}"
if "adb_config" not in STORAGE or not STORAGE["adb_config"]:
STORAGE["adb_config"] = "{}"

StatusIndicator(GlobalStatus, "ctrl_detecting").label().bind_visibility_from(
GlobalStatus,
Expand Down Expand Up @@ -162,8 +164,6 @@ def on_change_device_select(e: ui.select):


async def connect_win32_control():
StatusIndicator(GlobalStatus, "ctrl_connecting")

SCREENCAP_DICT = {
MaaWin32ScreencapMethodEnum.GDI: "Screencap_GDI",
MaaWin32ScreencapMethodEnum.DXGI_DesktopDup: "Screencap_DXGI_DesktopDup",
Expand All @@ -175,18 +175,24 @@ async def connect_win32_control():
MaaWin32InputMethodEnum.Seize: "Input_Seize",
}

with ui.row(align_items="center"):
with ui.row(align_items="baseline"):
StatusIndicator(GlobalStatus, "ctrl_connecting")
hwnd_input = (
ui.input("HWND").props("size=30").bind_value(app.storage.general, "hwnd")
ui.input("HWND", placeholder="0x11451", validation=ic.hwnd)
.props("size=30")
.bind_value(STORAGE, "hwnd")
.on("keydown.enter", lambda: on_click_connect())
)
screencap_select = ui.select(
SCREENCAP_DICT, value=MaaWin32ScreencapMethodEnum.DXGI_DesktopDup
).bind_value(app.storage.general, "win32_screencap")

SCREENCAP_DICT,
label="Screencap Method",
value=MaaWin32ScreencapMethodEnum.DXGI_DesktopDup,
).bind_value(STORAGE, "win32_screencap")
input_select = ui.select(
INPUT_DICT,
label="Input Method",
value=MaaWin32InputMethodEnum.Seize,
).bind_value(app.storage.general, "win32_input")
).bind_value(STORAGE, "win32_input")

ui.button(
"Connect",
Expand All @@ -197,15 +203,16 @@ async def connect_win32_control():
"Search Window Name", placeholder="Supports regex, eg: File Explorer"
)
.props("size=30")
.bind_value(app.storage.general, "window_name")
.bind_value(STORAGE, "window_name")
.on("keydown.enter", lambda: on_click_detect())
)
ui.button(
icon="wifi_find",
"FIND",
on_click=lambda: on_click_detect(),
)

hwnd_select = ui.select(
{}, on_change=lambda e: on_change_hwnd_select(e)
{}, label="Windows", on_change=lambda e: on_change_hwnd_select(e)
).bind_visibility_from(
GlobalStatus,
"ctrl_detecting",
Expand Down Expand Up @@ -294,20 +301,23 @@ async def on_click_refresh():
async def load_resource_control():
StatusIndicator(GlobalStatus, "res_loading")

dir_input = (
ui.textarea(
"Resource Directory",
placeholder="Separate with newline, eg: C:/M9A/assets/resource/base",
with ui.row(align_items="baseline").classes("w-3/4"):
dir_input = (
ui.textarea(
"Resource Directory",
placeholder="Separate with newline, eg: C:/M9A/assets/resource/base",
validation=ic.paths_exist,
)
.props("input-class=h-7")
.style("width: 500px;")
.bind_value(STORAGE, "resource_dir")
.tooltip("Directorise are separated by newline characters.")
)
.classes("w-1/2")
.props("input-class=h-7")
.bind_value(app.storage.general, "resource_dir")
)

ui.button(
"Load",
on_click=lambda: on_click_resource_load(dir_input.value),
)
ui.button(
"Load",
on_click=lambda: on_click_resource_load(dir_input.value),
)


async def on_click_resource_load(values: str):
Expand All @@ -317,7 +327,7 @@ async def on_click_resource_load(values: str):
GlobalStatus.res_loading = Status.FAILED
return

paths = [Path(p) for p in values.split("\n")]
paths = [Path(p) for p in values.split("\n") if p]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里去掉了空字符串

print(paths)
loaded = await maafw.load_resource(paths)
if not loaded:
Expand All @@ -330,32 +340,34 @@ async def on_click_resource_load(values: str):
async def run_task_control():
StatusIndicator(GlobalStatus, "task_running")

entry_input = (
ui.input(
"Task Entry",
placeholder="eg: StartUp",
with ui.row(align_items="baseline"):
entry_input = (
ui.input(
"Task Entry",
placeholder="eg: StartUp",
)
.props("size=30")
.bind_value(STORAGE, "task_entry")
)
.props("size=30")
.bind_value(app.storage.general, "task_entry")
)

if (
"task_pipeline_override" not in app.storage.general
or not app.storage.general["task_pipeline_override"]
):
app.storage.general["task_pipeline_override"] = "{}"
if (
"task_pipeline_override" not in STORAGE
or not STORAGE["task_pipeline_override"]
):
STORAGE["task_pipeline_override"] = "{}"

pipeline_override_input = (
ui.input(
"Pipeline Override",
placeholder="eg: {}",
pipeline_override_input = (
ui.input(
"Pipeline Override",
placeholder="eg: {}",
validation=ic.json_style_str,
)
.props("size=60")
.bind_value(STORAGE, "task_pipeline_override")
)
.props("size=60")
.bind_value(app.storage.general, "task_pipeline_override")
)

ui.button("Start", on_click=lambda: on_click_start())
ui.button("Stop", on_click=lambda: on_click_stop())
ui.button("Start", on_click=lambda: on_click_start())
ui.button("Stop", on_click=lambda: on_click_stop())

async def on_click_start():
GlobalStatus.task_running = Status.RUNNING
Expand All @@ -372,7 +384,7 @@ async def on_click_start():
GlobalStatus.task_running = Status.FAILED
return

await on_click_resource_load(app.storage.general["resource_dir"])
await on_click_resource_load(STORAGE["resource_dir"])

run = await maafw.run_task(entry_input.value, pipeline_override)
if not run:
Expand Down