Skip to content

Commit

Permalink
Hide console on start
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxBQb committed Aug 21, 2021
1 parent 582f9ae commit 7fc5f76
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
3 changes: 2 additions & 1 deletion bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@

def request_admin_rights() -> bool:
# msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx
from win32con import SW_HIDE
if shell.IsUserAnAdmin():
return False
hinstance = shell.ShellExecuteW(
None, 'runas', sys.executable, sys.argv[0], None, 1
None, 'runas', sys.executable, sys.argv[0], None, SW_HIDE
)
if hinstance <= 32:
raise RuntimeError(ERRORS.get(hinstance, f"{hinstance}: Unknown error"))
Expand Down
53 changes: 47 additions & 6 deletions tray.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from asyncio import to_thread
import inject
import win32gui
from win32con import SW_HIDE, SW_SHOW
import win32console
from PIL import Image
from configobj import ConfigObj
from pystray import Menu, MenuItem, Icon
Expand All @@ -11,6 +14,25 @@
from utils import explore


def make_toggle(out_func=None, default_value=False):
def decorator(func):
def wrapper(self):
value = [default_value]

def get_value(item):
return value[0]

def toggle():
value[0] ^= True
func(self, value[0])

return toggle, get_value
return wrapper
if out_func:
return decorator(out_func)
return decorator


class Tray:
config = inject.attr(ConfigObj)
rules_file_manager = inject.attr(RulesFileManager)
Expand All @@ -20,17 +42,24 @@ class Tray:

def __init__(self):
self.tray = None
self.console_hwnd: int = None
self.console_shown = False

def setup(self):
self.close_manager.add_exit_handler(self.close)
self.console_hwnd = win32console.GetConsoleWindow()
win32gui.ShowWindow(self.console_hwnd, SW_HIDE)

def run(self):
self.tray = Icon(
app.__product_name__,
Image.open(app.__icon__),
menu=self.build_menu()
)
self.tray.run()
try:
self.tray = Icon(
app.__product_name__,
Image.open(app.__icon__),
menu=self.build_menu()
)
self.tray.run()
except Exception as e:
pass

async def run_async(self):
try:
Expand Down Expand Up @@ -66,6 +95,11 @@ def ref(text: str):
f'{app.__product_name__} v{app.__version__}',
None, enabled=False),
Menu.SEPARATOR,
MenuItem(
ref("Show console"),
*self.toggle_console()
),
Menu.SEPARATOR,
MenuItem(
ref('Open'),
Menu(
Expand All @@ -89,3 +123,10 @@ def ref(text: str):
MenuItem(ref('Exit'),
callback(self.close_manager.close)),
)

@make_toggle
def toggle_console(self, value):
win32gui.ShowWindow(
self.console_hwnd,
SW_SHOW if value else SW_HIDE
)

0 comments on commit 7fc5f76

Please sign in to comment.