injectorHub/ui/installer.py

310 lines
13 KiB
Python

import json
import os
from magictk import mtk, color_tmpl
from magictk import fontconfig
from ui.lang import l
from lib import open_ventoy
from lib import get_disk
from lib import install_pkg
from lib import downloader
from logger.logger import *
now_frame = None
master: mtk.Frame = None
UPGRADE = 0
def show_first_page():
global now_frame, master
first_page = mtk.Frame(master,)
mtk.Frame(first_page, w=10000).pack(fill="both", expand=True)
if (UPGRADE):
mtk.Label(first_page, text=l("UpgradeWelcome"),
anchor='center', font=(fontconfig.getfont(), 22), background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["primary_text"]).pack(fill='x')
mtk.Label(first_page, text="\n"+l("InstallInfo"),
anchor='center', font=(fontconfig.getfont(), 12), background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["secondary_text"]).pack(fill='x')
else:
mtk.Label(first_page, text=l("InstallWelcome"),
anchor='center', font=(fontconfig.getfont(), 22), background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["primary_text"]).pack(fill='x')
mtk.Label(first_page, text="\n"+l("InstallInfo"),
anchor='center', font=(fontconfig.getfont(), 12), background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["secondary_text"]).pack(fill='x')
mtk.Frame(first_page, height=30).pack()
buttonlist = mtk.Frame(first_page)
def install_callback(*args):
change_page_download()
mtk.ButtonFill(buttonlist, text=l("StartInstall"),
w=110, color_type="primary", func=install_callback).pack(side="left")
mtk.Frame(buttonlist, height=10).pack(side="left")
buttonlist.pack()
mtk.Frame(first_page, w=10000).pack(fill="both", expand=True)
first_page.pack(fill="both", expand=True)
now_frame = first_page
def change_page_download():
global now_frame, master
now_frame.pack_forget()
instvtoy_page = mtk.Frame(master,)
mtk.Frame(instvtoy_page, w=10000).pack(fill="both", expand=True)
mtk.Label(instvtoy_page, text="0/3 "+l("DownloadInstallTitle"),
anchor='center', font=(fontconfig.getfont(), 22), background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["primary_text"]).pack(fill='x')
mtk.Label(instvtoy_page, text="\n"+l("DownloadInstall"),
anchor='center', font=(fontconfig.getfont(), 12), background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["secondary_text"]).pack(fill='x')
mtk.Frame(instvtoy_page, height=30).pack()
prog_frame = mtk.Frame(instvtoy_page)
with open("res/installer_file.json", 'r') as file:
files = json.load(file)
download_l = []
if not (os.path.exists("software")):
os.mkdir("software")
if not (os.path.exists("software"+os.sep+"cache")):
os.mkdir("software"+os.sep+"cache")
funclst = []
for i in files:
def runf():
sframe = mtk.Frame(prog_frame, height=10)
progps = mtk.Label(
sframe, background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["secondary_text"], text=" 0%", width=6, font=(fontconfig.getfont(), 10))
progps.pack(side="left")
progbar = mtk.ProgressBar(sframe, w=300)
progbar.pack(side="left", anchor='w')
proglabel = mtk.Label(
sframe, background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["secondary_text"], text=" "+i["name"], width=18, font=(fontconfig.getfont(), 10))
proglabel.pack(side="left")
sframe.pack(side="top")
def progcallback(prog):
progbar.set_progress(prog)
progstr = str(int(prog*100))
outstr = " "*(3-len(progstr))+progstr+"%"
def setproganim(*args):
progps.configure(text=outstr)
return -1
prog_frame.root.anim.append(setproganim)
funclst.append(progcallback)
download_l.append({
"from": i["source"],
"to": "software"+os.sep+"cache"+os.sep+i["name"]+".7z",
"progbar": funclst[-1],
"name": i["name"]
})
runf()
def nextpage_callback(*args):
change_page_installvtoy()
open_ventoy.run()
buttonlist = mtk.Frame(instvtoy_page, height=30)
btns = mtk.ButtonFill(buttonlist, text=l("InstallVtoy"),
w=120, color_type="primary", func=nextpage_callback)
def callbacks():
btns.pack(side="left")
downloader.download_files(download_l, callbacks, prog_frame.root)
prog_frame.pack(fill='x', anchor="center")
mtk.Frame(instvtoy_page, height=30).pack()
buttonlist.pack()
mtk.Frame(instvtoy_page, w=10000).pack(fill="both", expand=True)
instvtoy_page.pack(fill="both", expand=True)
now_frame = instvtoy_page
def change_page_installvtoy():
global now_frame, master
now_frame.pack_forget()
instvtoy_page = mtk.Frame(master,)
mtk.Frame(instvtoy_page, w=10000).pack(fill="both", expand=True)
mtk.Label(instvtoy_page, text="1/3 "+l("InstallVtoyTitle"),
anchor='center', font=(fontconfig.getfont(), 22), background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["primary_text"]).pack(fill='x')
mtk.Label(instvtoy_page, text="\n"+l("InstallVtoyInfo"),
anchor='center', font=(fontconfig.getfont(), 12), background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["secondary_text"]).pack(fill='x')
mtk.Frame(instvtoy_page, height=30).pack()
buttonlist = mtk.Frame(instvtoy_page)
scan_obj_list = mtk.Frame(instvtoy_page, height=30)
added_list = False
selects = mtk.Select(scan_obj_list, text=l(
"ChooseVtoyDevice"), items=[], w=380)
devmenu = []
def finishvtoy_callback(*args):
if (selects.get() == -1):
return
info(f"[installer]choose disk info:{str(devmenu[selects.get()])}")
global devinfo
devinfo = devmenu[selects.get()]
change_page_installing()
def install_callback(*args):
nonlocal added_list, selects, devmenu
def show_all_dev(*args):
nonlocal devmenu
devmenu = get_disk.find_all_disk()
selects.change_menu([f"{i[0]} [{i[1]}]" for i in devmenu])
if (added_list == False):
mtk.ButtonLight(scan_obj_list, func=show_all_dev, text=l("ShowAllDevices"), w=100, color_type="info").pack(
side="left")
selects.pack(side="left")
mtk.Button(scan_obj_list, func=finishvtoy_callback, iconname="circle-check", iconsize=16, w=32).pack(
side="left")
added_list = True
devmenu = get_disk.find_vtoy_disk()
selects.change_menu([f"{i[0]} [{i[1]}]" for i in devmenu])
def restart_callback(*args):
open_ventoy.run()
mtk.ButtonFill(buttonlist, text=l("ScanVtoy"),
w=80, color_type="primary", func=install_callback).pack(side="left")
mtk.ButtonLight(buttonlist, text=l("RestartVtoy"),
w=120, color_type="primary", func=restart_callback).pack(side="left")
mtk.Frame(buttonlist, height=10).pack(side="left")
buttonlist.pack()
scan_obj_list.pack()
mtk.Frame(instvtoy_page, w=10000).pack(fill="both", expand=True)
instvtoy_page.pack(fill="both", expand=True)
now_frame = instvtoy_page
def change_page_installing():
global now_frame, master, devinfo
devpath = devinfo[1]
now_frame.pack_forget()
installinjector_page = mtk.Frame(master,)
mtk.Frame(installinjector_page, w=10000).pack(fill="both", expand=True)
mtk.Label(installinjector_page, text="2/3 "+l("InstallIjTitle"),
anchor='center', font=(fontconfig.getfont(), 22), background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["primary_text"]).pack(fill='x')
mtk.Label(installinjector_page, text="\n"+l("InstallIjInfo"),
anchor='center', font=(fontconfig.getfont(), 12), background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["secondary_text"]).pack(fill='x')
mtk.Frame(installinjector_page, height=30).pack()
prog_frame = mtk.Frame(installinjector_page, height=10)
progbar = mtk.ProgressBar(prog_frame, w=300)
progbar.pack()
prog_frame.pack(fill='x')
def barcallback(prog):
progbar.set_progress(prog)
finishbutton_frame = mtk.Frame(installinjector_page, height=30)
def finishbtncallback(*args):
change_page_finish()
finishbtn = mtk.ButtonFill(finishbutton_frame, text=l(
"FinishInstall"), w=120, color_type="success", func=finishbtncallback)
closeid = False
def anim_pick(*args):
nonlocal closeid
if (closeid == True):
closeid = False
finishbtn.pack()
return -1
prog_frame.root.anim.append(anim_pick)
def finishcallback():
nonlocal closeid
closeid = True
finishbutton_frame.pack(fill="both", expand=True)
mtk.Frame(installinjector_page, w=10000).pack(fill="both", expand=True)
installinjector_page.pack(fill="both", expand=True)
theards = install_pkg.PKG_Theard(devpath, barcallback, finishcallback)
theards.start()
now_frame = installinjector_page
def change_page_installing():
global now_frame, master, devinfo
devpath = devinfo[1]
now_frame.pack_forget()
installinjector_page = mtk.Frame(master,)
mtk.Frame(installinjector_page, w=10000).pack(fill="both", expand=True)
mtk.Label(installinjector_page, text="2/3 "+l("InstallIjTitle"),
anchor='center', font=(fontconfig.getfont(), 22), background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["primary_text"]).pack(fill='x')
mtk.Label(installinjector_page, text="\n"+l("InstallIjInfo"),
anchor='center', font=(fontconfig.getfont(), 12), background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["secondary_text"]).pack(fill='x')
mtk.Frame(installinjector_page, height=30).pack()
prog_frame = mtk.Frame(installinjector_page, height=10)
progbar = mtk.ProgressBar(prog_frame, w=300)
progbar.pack()
prog_frame.pack(fill='x')
def barcallback(prog):
progbar.set_progress(prog)
finishbutton_frame = mtk.Frame(installinjector_page, height=30)
def finishbtncallback(*args):
change_page_finish()
finishbtn = mtk.ButtonFill(finishbutton_frame, text=l(
"FinishInstall"), w=120, color_type="success", func=finishbtncallback)
closeid = False
def anim_pick(*args):
nonlocal closeid
if (closeid == True):
closeid = False
finishbtn.pack()
return -1
prog_frame.root.anim.append(anim_pick)
def finishcallback():
nonlocal closeid
closeid = True
mtk.Frame(installinjector_page, height=30).pack()
finishbutton_frame.pack(fill="both", expand=True)
mtk.Frame(installinjector_page, w=10000).pack(fill="both", expand=True)
installinjector_page.pack(fill="both", expand=True)
theards = install_pkg.PKG_Theard(devpath, barcallback, finishcallback)
theards.start()
now_frame = installinjector_page
def change_page_finish():
global now_frame, master
now_frame.pack_forget()
finish_page = mtk.Frame(master,)
mtk.Frame(finish_page, w=10000).pack(fill="both", expand=True)
mtk.Label(finish_page, text="3/3 "+l("InstallFinishTitle"),
anchor='center', font=(fontconfig.getfont(), 22), background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["primary_text"]).pack(fill='x')
mtk.Label(finish_page, text="\n"+l("InstallFinishInfo"),
anchor='center', font=(fontconfig.getfont(), 12), background=color_tmpl.default_color["background"], foreground=color_tmpl.default_color["secondary_text"]).pack(fill='x')
def closecallback(*args):
warn("restart from installer")
python_path = sys.executable
os.execl(python_path, python_path, __file__.replace(
"ui"+os.sep+"installer.py", "main.py"))
btnframe = mtk.Frame(finish_page, height=30)
mtk.ButtonFill(finish_page, text=l(
"InstallFinishBtn"), func=closecallback, color_type="danger").pack()
btnframe.pack()
mtk.Frame(finish_page, height=30).pack()
mtk.Frame(finish_page, w=10000).pack(fill="both", expand=True)
finish_page.pack(fill="both", expand=True)
now_frame = finish_page
def installer_ui(root: mtk.Frame):
global master
install_root_frame = mtk.Frame(root)
master = install_root_frame
show_first_page()
install_root_frame.pack(fill="both", expand=True)
return install_root_frame