Skip to content

Commit

Permalink
fix: keep system tray menu alive
Browse files Browse the repository at this point in the history
Signed-off-by: Sylvain Leclerc <[email protected]>
  • Loading branch information
sylvlecl committed Sep 30, 2024
1 parent ea8ec2c commit f77e700
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions antarest/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import platform
import time
import webbrowser
from dataclasses import dataclass
from multiprocessing import Process
from pathlib import Path
from threading import Thread
Expand Down Expand Up @@ -54,34 +55,45 @@ def open_app() -> None:
webbrowser.open("http://localhost:8080")


def create_systray_app() -> QApplication:
@dataclass(frozen=True)
class AntaresSystrayApp:
"""
Used to keep ownership of root Qt objects.
QMenu can only be owned by QWidgets, but we don't have one.
"""
app: QApplication
menu: QMenu


def create_systray_app() -> AntaresSystrayApp:
"""
Creates the small application that allows to open
the browser or shutdown the server.
"""
app = QApplication([])
app.setQuitOnLastWindowClosed(False)

# Adding an icon
icon = QIcon(str(RESOURCE_PATH / "webapp" / "logo16.png"))
# Adding item on the menu bar
tray = QSystemTrayIcon(icon, app)
tray.setVisible(True)
tray = QSystemTrayIcon()
tray.setToolTip("AntaresWebServer")
tray.setIcon(icon)

# Creating the options
menu = QMenu()
open_app_action = QAction("Open application")
menu.addAction(open_app_action)
open_app_action = menu.addAction("Open application")
open_app_action.triggered.connect(open_app)
# To quit the app
quit_action = QAction("Quit")
quit_action = menu.addAction("Quit")
quit_action.triggered.connect(app.quit)
menu.addAction(quit_action)

# Adding options to the System Tray
tray.setContextMenu(menu)
app.processEvents()
tray.setToolTip("AntaresWebServer")

return app
tray.setVisible(True)

return AntaresSystrayApp(app, menu)


def monitor_server_process(server, app) -> None:
Expand Down Expand Up @@ -134,12 +146,12 @@ def main() -> None:

arguments = parse_arguments()
notification_popup("Antares Web Server starting...")
app = create_systray_app()
systray_app = create_systray_app()
server = start_server(arguments.config_file)
setup_exit_application_on_server_end(server, app)
setup_exit_application_on_server_end(server, systray_app.app)
wait_for_server_start()
notification_popup("Antares Web Server started, you can manage the application within the systray app")
app.exec_()
systray_app.app.exec_()
server.kill()


Expand Down

0 comments on commit f77e700

Please sign in to comment.