Skip to content

Commit 62ce710

Browse files
committed
Add --start-in-tray parameter
1 parent ef3855b commit 62ce710

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/main.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,37 @@ def _wnd_proc(hwnd, msg, wparam, lparam):
216216
0, 0, hinstance, None,
217217
)
218218

219+
def _load_icon(self) -> int:
220+
221+
if getattr(sys, "frozen", False):
222+
hinstance = ctypes.windll.kernel32.GetModuleHandleW(None)
223+
hicon = ctypes.windll.user32.LoadIconW(
224+
hinstance,
225+
ctypes.cast(1, ctypes.wintypes.LPCWSTR),
226+
)
227+
if hicon:
228+
return hicon
229+
230+
exe_path = sys.executable
231+
if exe_path and os.path.isfile(exe_path):
232+
hicon_large = ctypes.wintypes.HICON(0)
233+
hicon_small = ctypes.wintypes.HICON(0)
234+
n = ctypes.windll.shell32.ExtractIconExW(
235+
ctypes.c_wchar_p(exe_path),
236+
0,
237+
ctypes.byref(hicon_large),
238+
ctypes.byref(hicon_small),
239+
1,
240+
)
241+
if n > 0:
242+
hicon = hicon_small.value or hicon_large.value
243+
if hicon:
244+
return hicon
245+
246+
return ctypes.windll.user32.LoadIconW(None, IDI_APPLICATION)
247+
219248
def _add_tray_icon(self) -> None:
220-
hicon = ctypes.windll.user32.LoadIconW(None, IDI_APPLICATION)
249+
hicon = self._load_icon()
221250

222251
nid = NOTIFYICONDATA()
223252
nid.cbSize = ctypes.sizeof(NOTIFYICONDATA)
@@ -352,6 +381,7 @@ def __init__(self):
352381
self.no_blacklist = False
353382
self.auto_blacklist = False
354383
self.quiet = False
384+
self.start_in_tray = False
355385

356386

357387
class IBlacklistManager(ABC):
@@ -1500,6 +1530,7 @@ def load_from_args(args) -> ProxyConfig:
15001530
config.no_blacklist = args.no_blacklist
15011531
config.auto_blacklist = args.autoblacklist
15021532
config.quiet = args.quiet
1533+
config.start_in_tray = args.start_in_tray
15031534
return config
15041535

15051536

@@ -1688,6 +1719,11 @@ def parse_args():
16881719
parser.add_argument(
16891720
"-q", "--quiet", action="store_true", help="Remove UI output"
16901721
)
1722+
parser.add_argument(
1723+
"--start-in-tray",
1724+
action="store_true",
1725+
help="Start minimized to tray (Windows only)",
1726+
)
16911727

16921728
autostart_group = parser.add_mutually_exclusive_group()
16931729
autostart_group.add_argument(
@@ -1744,6 +1780,8 @@ async def run(cls):
17441780
if sys.platform == "win32" and not config.quiet:
17451781
tray = WindowsTrayIcon(tooltip=f"NoDPI v{__version__}")
17461782
tray.start()
1783+
if config.start_in_tray:
1784+
tray.hide_to_tray()
17471785

17481786
proxy = ProxyServer(config, blacklist_manager, statistics, logger)
17491787

0 commit comments

Comments
 (0)