Skip to content

Commit aa54034

Browse files
committed
fix(tray): enhance tray context menu with Komorebi commands
Added commands to start, stop, and reload the Komorebi application in the system tray context menu. The commands are configurable through the main configuration file, allowing for greater flexibility. Updated error handling during configuration loading to improve robustness. This change enhances user experience by integrating more functionality directly into the tray interface.
1 parent 86a5126 commit aa54034

File tree

4 files changed

+81
-24
lines changed

4 files changed

+81
-24
lines changed

src/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
from cx_Freeze import setup, Executable
33

4-
version = "1.0.0"
4+
version = "1.0.1"
55
#base = "console"
66
base = "gui"
77
build_options = {

src/config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
watch_stylesheet: true
22
watch_config: true
33

4+
5+
# This komorebi setting will be used for tray context menu.
6+
komorebi:
7+
start_command: "komorebic start --whkd"
8+
stop_command: "komorebic stop --whkd"
9+
reload_command: "komorebic reload-configuration"
10+
11+
# This is the main configuration file for Yet Another Status Bar (YASB)
412
bars:
513
status-bar:
614
enabled: true

src/core/tray.py

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import logging
22
import webbrowser
3-
import os
3+
import os
44
import shutil
55
import sys
66
from pathlib import Path
77
import subprocess
88
import winshell
99
from PyQt6.QtWidgets import QSystemTrayIcon, QMenu
1010
from PyQt6.QtGui import QIcon
11-
from PyQt6.QtCore import QCoreApplication, QSize
11+
from PyQt6.QtCore import QCoreApplication, QSize, Qt
1212
from core.bar_manager import BarManager
1313
from settings import GITHUB_URL, SCRIPT_PATH, APP_NAME, DEFAULT_CONFIG_DIRECTORY
14+
from core.config import get_config
1415

1516
OS_STARTUP_FOLDER = os.path.join(os.environ['APPDATA'], r'Microsoft\Windows\Start Menu\Programs\Startup')
1617
VBS_PATH = os.path.join(SCRIPT_PATH, 'yasb.vbs')
17-
18+
1819
# Check if exe file exists
1920
INSTALLATION_PATH = os.path.abspath(os.path.join(__file__, "../../.."))
2021
EXE_PATH = os.path.join(INSTALLATION_PATH, 'yasb.exe')
2122
SHORTCUT_FILENAME = "yasb.lnk"
2223
AUTOSTART_FILE = EXE_PATH if os.path.exists(EXE_PATH) else VBS_PATH
2324
WORKING_DIRECTORY = INSTALLATION_PATH if os.path.exists(EXE_PATH) else SCRIPT_PATH
24-
25-
class TrayIcon(QSystemTrayIcon):
2625

26+
class TrayIcon(QSystemTrayIcon):
2727
def __init__(self, bar_manager: BarManager):
2828
super().__init__()
2929
self._bar_manager = bar_manager
@@ -32,7 +32,16 @@ def __init__(self, bar_manager: BarManager):
3232
self._load_favicon()
3333
self._load_context_menu()
3434
self.setToolTip(f"{APP_NAME}")
35-
35+
try:
36+
config = get_config(show_error_dialog=True)
37+
except Exception as e:
38+
logging.error(f"Error loading config: {e}")
39+
return
40+
if config['komorebi']:
41+
self.komorebi_start = config['komorebi']["start_command"]
42+
self.komorebi_stop = config['komorebi']["stop_command"]
43+
self.komorebi_reload = config['komorebi']["reload_command"]
44+
3645
def _load_favicon(self):
3746
# Get the current directory of the script
3847
parent_directory = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -41,7 +50,7 @@ def _load_favicon(self):
4150

4251
def _load_context_menu(self):
4352
menu = QMenu()
44-
53+
menu.setWindowModality(Qt.WindowModality.WindowModal)
4554
style_sheet = """
4655
QMenu {
4756
background-color: #26292b;
@@ -51,41 +60,52 @@ def _load_context_menu(self):
5160
margin:0;
5261
border-radius:6px
5362
}
54-
5563
QMenu::item {
5664
margin:0 4px;
57-
padding: 6px 16px;
58-
border-radius:4px
65+
padding: 8px 16px;
66+
border-radius:4px;
67+
font-size: 11px;
5968
}
60-
6169
QMenu::item:selected {
6270
background-color: #373b3e;
6371
}
64-
6572
QMenu::separator {
6673
height: 1px;
6774
background: #373b3e;
6875
margin:5px 0;
6976
}
77+
QMenu::right-arrow {
78+
width: 8px;
79+
height: 8px;
80+
padding-right: 18px;
81+
}
7082
"""
71-
7283
menu.setStyleSheet(style_sheet)
73-
84+
7485
github_action = menu.addAction("Visit GitHub")
7586
github_action.triggered.connect(self._open_docs_in_browser)
87+
7688
open_config_action = menu.addAction("Open Config")
7789
open_config_action.triggered.connect(self._open_config)
78-
reload_action = menu.addAction("Reload App")
90+
91+
reload_action = menu.addAction("Reload YASB")
7992
reload_action.triggered.connect(self._reload_application)
80-
menu.addSeparator()
8193

94+
menu.addSeparator()
95+
8296
if self.is_komorebi_installed():
83-
start_komorebi = menu.addAction("Start Komorebi")
97+
komorebi_menu = menu.addMenu("Komorebi")
98+
start_komorebi = komorebi_menu.addAction("Start Komorebi")
8499
start_komorebi.triggered.connect(self._start_komorebi)
85-
stop_komorebi = menu.addAction("Stop Komorebi")
100+
101+
stop_komorebi = komorebi_menu.addAction("Stop Komorebi")
86102
stop_komorebi.triggered.connect(self._stop_komorebi)
103+
104+
reload_komorebi = komorebi_menu.addAction("Reload Komorebi")
105+
reload_komorebi.triggered.connect(self._reload_komorebi)
106+
87107
menu.addSeparator()
88-
108+
89109
if self.is_autostart_enabled():
90110
disable_startup_action = menu.addAction("Disable Autostart")
91111
disable_startup_action.triggered.connect(self._disable_startup)
@@ -94,9 +114,10 @@ def _load_context_menu(self):
94114
enable_startup_action.triggered.connect(self._enable_startup)
95115

96116
menu.addSeparator()
117+
97118
exit_action = menu.addAction("Exit")
98119
exit_action.triggered.connect(self._exit_application)
99-
120+
100121
self.setContextMenu(menu)
101122

102123
def is_autostart_enabled(self):
@@ -141,16 +162,22 @@ def _open_config(self):
141162

142163
def _start_komorebi(self):
143164
try:
144-
subprocess.run("komorebic start --whkd", stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, shell=True)
165+
subprocess.run(self.komorebi_start, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, shell=True)
145166
except Exception as e:
146167
logging.error(f"Failed to start komorebi: {e}")
147168

148169
def _stop_komorebi(self):
149170
try:
150-
subprocess.run("komorebic stop --whkd", stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, shell=True)
171+
subprocess.run(self.komorebi_stop, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, shell=True)
151172
except Exception as e:
152173
logging.error(f"Failed to stop komorebi: {e}")
153174

175+
def _reload_komorebi(self):
176+
try:
177+
subprocess.run(self.komorebi_reload, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, shell=True)
178+
except Exception as e:
179+
logging.error(f"Failed to reload komorebi: {e}")
180+
154181
def _reload_application(self):
155182
logging.info("Reloading Application...")
156183
os.execl(sys.executable, sys.executable, *sys.argv)
@@ -161,4 +188,4 @@ def _exit_application(self):
161188
QCoreApplication.exit(0)
162189

163190
def _open_docs_in_browser(self):
164-
webbrowser.open(self._docs_url)
191+
webbrowser.open(self._docs_url)

src/core/validation/config.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@
99
'type': 'boolean',
1010
'default': True,
1111
},
12+
'komorebi': {
13+
'type': 'dict',
14+
'schema': {
15+
'start_command': {
16+
'type': 'string',
17+
'required': False,
18+
},
19+
'stop_command': {
20+
'type': 'string',
21+
'required': False,
22+
},
23+
'reload_command': {
24+
'type': 'string',
25+
'required': False,
26+
}
27+
},
28+
'default': {
29+
'start_command': 'komorebic start --whkd',
30+
'stop_command': 'komorebic stop --whkd',
31+
'reload_command': 'komorebic reload-configuration'
32+
}
33+
},
1234
'bars': {
1335
'type': 'dict',
1436
'keysrules': {

0 commit comments

Comments
 (0)