Skip to content

Commit

Permalink
优化启动流程
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhaoZuohong committed Nov 21, 2023
1 parent 082b8d1 commit 7c5c372
Showing 1 changed file with 97 additions and 82 deletions.
179 changes: 97 additions & 82 deletions webview_ui.py
Original file line number Diff line number Diff line change
@@ -1,121 +1,136 @@
#!/usr/bin/env python3
import multiprocessing

import webview
import server
from server import app

import multiprocessing
if __name__ == "__main__":
multiprocessing.freeze_support()

from arknights_mower.utils.conf import load_conf, save_conf
from arknights_mower.utils.path import get_path
from arknights_mower.__init__ import __version__
import tkinter as tk
from tkinter.font import Font
from arknights_mower.__init__ import __version__

from threading import Thread
from PIL import Image, ImageTk
from pystray import Icon, Menu, MenuItem
class SplashScreen:
def __init__(self):
self.root = tk.Tk()

import socket
import tkinter as tk
from tkinter.font import Font
from tkinter import messagebox
from time import sleep
import sys
self.canvas = tk.Canvas(self.root, width=256, height=256)
self.canvas.pack()

self.title_font = Font(size=24)
self.title_label = tk.Label(
self.root, text=f"arknights-mower {__version__}", font=self.title_font
)
self.title_label.pack()

quit_app = False
display = True
window = None
self.loading_label = tk.Label(self.root)
self.loading_label.pack()
self.root.overrideredirect(True)

def load_img(self, img):
self.img = ImageTk.PhotoImage(img)
self.canvas.create_image(128, 128, image=self.img)
self.root.update()

def on_resized(w, h):
global width
global height
def show_text(self, text):
self.loading_label.config(text=text + "……")
self.center()

width = w
height = h
def center(self):
self.root.eval("tk::PlaceWindow . center")
self.root.update()

def hide(self):
self.root.withdraw()

def toggle_window():
global window
global display
window.hide() if display else window.show()
display = not display
def stop(self):
self.root.destroy()

splash = SplashScreen()

def on_closing():
if quit_app:
return True
Thread(target=toggle_window).start()
return False
splash.show_text("加载图标")

from PIL import Image, ImageTk
from arknights_mower.utils.path import get_path

def destroy_window():
global quit_app
global window
quit_app = True
window.destroy()
logo_path = get_path("@internal/logo.png")
tray_img = Image.open(logo_path)
splash.load_img(tray_img)

splash.show_text("加载依赖")

def is_port_in_use(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(("localhost", port)) == 0
quit_app = False
display = True
window = None

def on_resized(w, h):
global width
global height

logo_path = get_path("@internal/logo.png")
tray_img = Image.open(logo_path)
width = w
height = h

def toggle_window():
global window
global display
window.hide() if display else window.show()
display = not display

class SplashScreen:
def __init__(self):
self.root = tk.Tk()
def on_closing():
if quit_app:
return True
Thread(target=toggle_window).start()
return False

self.canvas = tk.Canvas(self.root, width=256, height=256)
self.canvas.pack()
self.img = ImageTk.PhotoImage(tray_img)
self.canvas.create_image(128, 128, image=self.img)
def destroy_window():
global quit_app
global window
quit_app = True
window.destroy()

self.title_font = Font(size=24)
self.title_label = tk.Label(
self.root, text=f"arknights-mower {__version__}", font=self.title_font
)
self.title_label.pack()
splash.show_text("加载配置文件")

self.loading_label = tk.Label(self.root)
self.loading_label.pack()
self.root.overrideredirect(True)
from arknights_mower.utils.conf import load_conf, save_conf

def show_text(self, text):
self.loading_label.config(text=text)
self.center()
conf = load_conf()

def center(self):
self.root.eval("tk::PlaceWindow . center")
self.root.update()
port = conf["webview"]["port"]
token = conf["webview"]["token"]
host = "0.0.0.0" if token else "127.0.0.1"

def stop(self):
self.root.destroy()
global width
global height

width = conf["webview"]["width"]
height = conf["webview"]["height"]

if __name__ == "__main__":
multiprocessing.freeze_support()
splash.show_text("检测端口占用")

splash = SplashScreen()
splash.show_text("加载配置文件")
import socket

conf = load_conf()
def is_port_in_use(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(("localhost", port)) == 0

port = conf["webview"]["port"]
token = conf["webview"]["token"]
host = "0.0.0.0" if token else "127.0.0.1"
from tkinter import messagebox
import sys

if is_port_in_use(port):
splash.hide()
messagebox.showerror(
"arknights-mower",
f"端口{port}已被占用,无法启动!",
)
splash.stop()
sys.exit()

splash.show_text("启动flask网页服务器")
splash.show_text("加载Flask依赖")

import server
from server import app

splash.show_text("启动Flask网页服务器")

from threading import Thread

app.token = token
Thread(
Expand All @@ -124,14 +139,10 @@ def stop(self):
daemon=True,
).start()

global width
global height

width = conf["webview"]["width"]
height = conf["webview"]["height"]

splash.show_text("加载托盘图标")

from pystray import Icon, Menu, MenuItem

icon = Icon(
"arknights-mower",
icon=tray_img,
Expand All @@ -151,6 +162,8 @@ def stop(self):

splash.show_text("准备主窗口")

import webview

window = webview.create_window(
f"arknights-mower {__version__} (http://{host}:{port})",
f"http://127.0.0.1:{port}?token={token}",
Expand All @@ -162,6 +175,8 @@ def stop(self):
window.events.resized += on_resized
window.events.closing += on_closing

from time import sleep

while not is_port_in_use(port):
sleep(0.1)

Expand Down

0 comments on commit 7c5c372

Please sign in to comment.