11from asyncio import to_thread
22import inject
3+ import win32gui
4+ from win32con import SW_HIDE , SW_SHOW
5+ import win32console
36from PIL import Image
47from configobj import ConfigObj
58from pystray import Menu , MenuItem , Icon
1114from utils import explore
1215
1316
17+ def make_toggle (out_func = None , default_value = False ):
18+ def decorator (func ):
19+ def wrapper (self ):
20+ value = [default_value ]
21+
22+ def get_value (item ):
23+ return value [0 ]
24+
25+ def toggle ():
26+ value [0 ] ^= True
27+ func (self , value [0 ])
28+
29+ return toggle , get_value
30+ return wrapper
31+ if out_func :
32+ return decorator (out_func )
33+ return decorator
34+
35+
1436class Tray :
1537 config = inject .attr (ConfigObj )
1638 rules_file_manager = inject .attr (RulesFileManager )
@@ -20,17 +42,24 @@ class Tray:
2042
2143 def __init__ (self ):
2244 self .tray = None
45+ self .console_hwnd : int = None
46+ self .console_shown = False
2347
2448 def setup (self ):
2549 self .close_manager .add_exit_handler (self .close )
50+ self .console_hwnd = win32console .GetConsoleWindow ()
51+ win32gui .ShowWindow (self .console_hwnd , SW_HIDE )
2652
2753 def run (self ):
28- self .tray = Icon (
29- app .__product_name__ ,
30- Image .open (app .__icon__ ),
31- menu = self .build_menu ()
32- )
33- self .tray .run ()
54+ try :
55+ self .tray = Icon (
56+ app .__product_name__ ,
57+ Image .open (app .__icon__ ),
58+ menu = self .build_menu ()
59+ )
60+ self .tray .run ()
61+ except Exception as e :
62+ pass
3463
3564 async def run_async (self ):
3665 try :
@@ -66,6 +95,11 @@ def ref(text: str):
6695 f'{ app .__product_name__ } v{ app .__version__ } ' ,
6796 None , enabled = False ),
6897 Menu .SEPARATOR ,
98+ MenuItem (
99+ ref ("Show console" ),
100+ * self .toggle_console ()
101+ ),
102+ Menu .SEPARATOR ,
69103 MenuItem (
70104 ref ('Open' ),
71105 Menu (
@@ -89,3 +123,10 @@ def ref(text: str):
89123 MenuItem (ref ('Exit' ),
90124 callback (self .close_manager .close )),
91125 )
126+
127+ @make_toggle
128+ def toggle_console (self , value ):
129+ win32gui .ShowWindow (
130+ self .console_hwnd ,
131+ SW_SHOW if value else SW_HIDE
132+ )
0 commit comments