-
Notifications
You must be signed in to change notification settings - Fork 52
/
webview_ui.py
executable file
·304 lines (242 loc) · 8.05 KB
/
webview_ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python3
import multiprocessing as mp
def splash_screen(queue: mp.Queue):
import tkinter as tk
from tkinter.font import Font
from PIL import Image, ImageTk
from arknights_mower.utils.path import get_path
root = tk.Tk()
container = tk.Frame(root)
logo_path = get_path("@internal/logo.png")
img = Image.open(logo_path)
img = ImageTk.PhotoImage(img)
canvas = tk.Canvas(container, width=256, height=256)
canvas.create_image(128, 128, image=img)
canvas.pack()
title_font = Font(size=24)
title_label = tk.Label(
container,
text="arknights-mower",
font=title_font,
)
title_label.pack()
loading_label = tk.Label(container)
loading_label.pack()
container.pack(expand=1)
root.overrideredirect(True)
window_width = 500
window_height = 400
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = int(screen_width / 2 - window_width / 2)
y = int(screen_height / 2 - window_height / 2)
root.geometry(f"{window_width}x{window_height}+{x}+{y}")
def recv_msg():
try:
msg = queue.get(False)
if msg["type"] == "text":
loading_label.config(text=msg["data"] + "……")
root.after(100, recv_msg)
elif msg["type"] == "dialog":
from tkinter import messagebox
root.withdraw()
messagebox.showerror("arknights-mower", msg["data"])
root.destroy()
except Exception:
pass
root.after(100, recv_msg)
root.mainloop()
def start_tray(queue: mp.Queue, global_space, port, url):
from PIL import Image
from pystray import Icon, Menu, MenuItem
from arknights_mower.utils.path import get_path
logo_path = get_path("@internal/logo.png")
img = Image.open(logo_path)
title = f"mower@{port}({global_space})" if global_space else f"mower@{port}"
def open_browser():
import webbrowser
webbrowser.open(url)
icon = Icon(
name="arknights-mower",
icon=img,
menu=Menu(
MenuItem(
text=title,
action=None,
enabled=False,
),
Menu.SEPARATOR,
MenuItem(
text="打开/关闭窗口",
action=lambda: queue.put("toggle"),
default=True,
),
MenuItem(
text="在浏览器中打开网页面板",
action=open_browser,
),
Menu.SEPARATOR,
MenuItem(
text="退出",
action=lambda: queue.put("exit"),
),
),
title=title,
)
icon.run()
def webview_window(child_conn, global_space, host, port, url, tray):
import sys
from threading import Thread
import webview
webview.settings["ALLOW_DOWNLOADS"] = True
from arknights_mower.__init__ import __version__
from arknights_mower.utils import config, path
path.global_space = global_space
global width
global height
config.load_conf()
width = config.conf.webview.width
height = config.conf.webview.height
def window_size(w, h):
global width
global height
width = w
height = h
window = webview.create_window(
f"arknights-mower {__version__} (http://{host}:{port})",
url,
text_select=True,
confirm_close=not tray,
width=width,
height=height,
)
window.events.resized += window_size
def recv_msg():
while True:
msg = child_conn.recv()
if msg == "exit":
window.confirm_close = False
window.destroy()
return
if msg == "file":
result = window.create_file_dialog(
dialog_type=webview.OPEN_DIALOG,
)
elif msg == "folder":
result = window.create_file_dialog(
dialog_type=webview.FOLDER_DIALOG,
)
if result is None:
result = ""
elif not isinstance(result, str):
if len(result) == 0:
result = ""
else:
result = result[0]
child_conn.send(result)
Thread(target=recv_msg, daemon=True).start()
try:
webview.start()
config.load_conf()
config.conf.webview.width = width
config.conf.webview.height = height
config.save_conf()
sys.exit()
except Exception:
import webbrowser
webbrowser.open(url)
if __name__ == "__main__":
mp.freeze_support()
splash_queue = mp.Queue()
splash_process = mp.Process(target=splash_screen, args=(splash_queue,), daemon=True)
splash_process.start()
splash_queue.put({"type": "text", "data": "加载配置文件"})
import sys
from arknights_mower.utils import path
if len(sys.argv) == 2:
path.global_space = sys.argv[1]
from arknights_mower.utils import config
conf = config.conf
tray = conf.webview.tray
token = conf.webview.token
host = "0.0.0.0" if token else "127.0.0.1"
splash_queue.put({"type": "text", "data": "检测端口占用"})
from arknights_mower.utils.network import get_new_port, is_port_in_use
if token:
port = conf.webview.port
if is_port_in_use(port):
splash_queue.put(
{"type": "dialog", "data": f"端口{port}已被占用,无法启动!"}
)
sys.exit()
else:
port = get_new_port()
url = f"http://127.0.0.1:{port}"
if token:
url += f"?token={token}"
splash_queue.put({"type": "text", "data": "加载Flask依赖"})
from server import app
splash_queue.put({"type": "text", "data": "启动Flask网页服务器"})
from threading import Thread
from time import sleep
if token:
app.token = token
flask_thread = Thread(
target=app.run,
kwargs={"host": host, "port": port},
daemon=True,
)
flask_thread.start()
while not is_port_in_use(port):
sleep(0.1)
url = f"http://127.0.0.1:{port}"
if token:
url += f"?token={token}"
if tray:
splash_queue.put({"type": "text", "data": "加载托盘图标"})
tray_queue = mp.Queue()
tray_process = mp.Process(
target=start_tray,
args=(tray_queue, path.global_space, port, url),
daemon=True,
)
tray_process.start()
splash_queue.put({"type": "text", "data": "创建主窗口"})
config.parent_conn, child_conn = mp.Pipe()
config.webview_process = mp.Process(
target=webview_window,
args=(child_conn, path.global_space, host, port, url, tray),
daemon=True,
)
config.webview_process.start()
splash_process.terminate()
if tray:
while True:
msg = tray_queue.get()
if msg == "toggle":
if config.webview_process.is_alive():
config.parent_conn.send("exit")
if config.webview_process.join(3) is None:
config.webview_process.terminate()
else:
config.parent_conn, child_conn = mp.Pipe()
config.webview_process = mp.Process(
target=webview_window,
args=(
child_conn,
path.global_space,
host,
port,
url,
tray,
),
daemon=True,
)
config.webview_process.start()
elif msg == "exit":
config.parent_conn.send("exit")
if config.webview_process.join(3) is None:
config.webview_process.terminate()
break
else:
config.webview_process.join()