Skip to content

Commit 7fc5f76

Browse files
committed
Hide console on start
1 parent 582f9ae commit 7fc5f76

2 files changed

Lines changed: 49 additions & 7 deletions

File tree

bootstrap.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929

3030
def request_admin_rights() -> bool:
3131
# msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx
32+
from win32con import SW_HIDE
3233
if shell.IsUserAnAdmin():
3334
return False
3435
hinstance = shell.ShellExecuteW(
35-
None, 'runas', sys.executable, sys.argv[0], None, 1
36+
None, 'runas', sys.executable, sys.argv[0], None, SW_HIDE
3637
)
3738
if hinstance <= 32:
3839
raise RuntimeError(ERRORS.get(hinstance, f"{hinstance}: Unknown error"))

tray.py

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from asyncio import to_thread
22
import inject
3+
import win32gui
4+
from win32con import SW_HIDE, SW_SHOW
5+
import win32console
36
from PIL import Image
47
from configobj import ConfigObj
58
from pystray import Menu, MenuItem, Icon
@@ -11,6 +14,25 @@
1114
from 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+
1436
class 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

Comments
 (0)