11import webview as wv
22import os
33import sys
4+ import threading
5+ import pystray
6+ from PIL import Image
47
58import src .back .util .print as print
69from src .back .system .contact import API
710
8- def startUp ( debugMode ) :
9- # def loadCSS(window ):
10- # whereCss = os.path.join(os.path.dirname(__file__), "cacaStyle.css")
11- # with open(whereCss, "r") as f:
12- # css = f.read ()
13- # window.load_css(css )
11+ class WindowManager :
12+ def __init__ ( self ):
13+ self . window = None
14+ self . debugMode = False
15+ self . projectRoot = self . getProjectRoot ()
16+ self . iconPath = self . getIconPath ( )
1417
15- API .checkForUpdates (API ())
18+ def getProjectRoot (self ):
19+ if getattr (sys , "frozen" , False ):
20+ return sys ._MEIPASS
21+ return os .path .dirname (
22+ os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))
23+ )
1624
17- here = os .path .join (os .path .dirname (__file__ ), "main" , "index.html" )
18- print .debug (f"URL for HTTP is at { here } " )
25+ def getIconPath (self ):
26+ iconExt = ".ico" if sys .platform == "win32" else ".png"
27+ return os .path .join (
28+ self .projectRoot , "assets" , "icon" , f"SendYourFiles{ iconExt } "
29+ )
1930
20- if debugMode :
21- print .success ("Debug mode is enabled. I don't know why you'd want this." )
31+ def createWindow (self ):
32+ if self .window :
33+ self .showWindow ()
34+ return
2235
23- # NO idea what this is, probably guessing PyInstaller's OneFile thingy.
24- if getattr (sys , "frozen" , False ):
25- projectRoot = sys ._MEIPASS
26- else :
27- projectRoot = os .path .dirname (
28- os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))
36+ htmlPath = os .path .join (os .path .dirname (__file__ ), "main" , "index.html" )
37+ print .debug (f"Creating window with URL: { htmlPath } " )
38+
39+ self .window = wv .create_window (
40+ title = "Send Your Files" ,
41+ url = htmlPath ,
42+ js_api = API (),
43+ width = 800 ,
44+ height = 600 ,
2945 )
3046
31- iconExt = ".ico" if sys .platform == "win32" else ".png"
32- iconPath = os .path .join (projectRoot , "assets" , "icon" , f"SendYourFiles{ iconExt } " )
47+ self .window .events .closing += self .onWindowClosing
48+
49+ def onWindowClosing (self ):
50+ print .debug ("X clicked! Hiding to tray and clearing RAM..." )
51+ self .window .load_html (" " ) # less ram usage :3 (hopefully)
52+ self .window .hide ()
53+ return False
3354
34- print .debug (f"Using icon at: { iconPath } " )
55+ def showWindow (self , icon = None , item = None ):
56+ if self .window :
57+ print .debug ("Refreshing UI and showing window..." )
58+ htmlPath = os .path .join (os .path .dirname (__file__ ), "main" , "index.html" )
59+ self .window .load_url (htmlPath )
60+ self .window .show ()
61+ else :
62+ self .createWindow ()
3563
36- wv . create_window (
37- title = "Send Your Files" ,
38- url = here ,
39- js_api = API (),
64+ def quitApp ( self , icon , item ):
65+ print . warning ( "Shutting down application..." )
66+ icon . stop ()
67+ os . _exit ( 0 )
4068
41- width = 800 ,
42- height = 600 ,
43- )
69+ def setupTray (self ):
70+ try :
71+ image = Image .open (self .iconPath )
72+ menu = pystray .Menu (
73+ pystray .MenuItem ("Open" , self .showWindow , default = True ),
74+ pystray .MenuItem ("Quit" , self .quitApp ),
75+ )
4476
45- # window.events.loaded += loadCSS
46- wv .start (
47- http_server = True ,
48- private_mode = True ,
77+ self .trayIcon = pystray .Icon (
78+ "SendYourFiles" , image , "Send Your Files" , menu
79+ )
80+ self .trayIcon .run ()
81+ except Exception as e :
82+ print .error (f"Failed to setup tray: { e } " )
4983
50- debug = debugMode ,
51- icon = iconPath ,
52- )
84+ def start (self , debugMode ):
85+ self .debugMode = debugMode
86+ if self .debugMode :
87+ print .success ("Debug mode is active." )
88+
89+ trayThread = threading .Thread (target = self .setupTray , daemon = True )
90+ trayThread .start ()
91+
92+ self .createWindow ()
93+ wv .start (
94+ http_server = True ,
95+ private_mode = True ,
96+ debug = self .debugMode ,
97+ icon = self .iconPath ,
98+ )
99+
100+
101+ def startUp (debugMode ):
102+ manager = WindowManager ()
103+ manager .start (debugMode )
0 commit comments