2022-07-02 03:03:18 +12:00
|
|
|
from logging import getLogger
|
2023-01-11 03:35:24 +13:00
|
|
|
from typing import List
|
2022-01-03 10:52:43 +13:00
|
|
|
|
2023-01-28 09:05:39 +13:00
|
|
|
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QSettings
|
2021-04-12 03:21:39 +12:00
|
|
|
from PyQt5.QtGui import QIcon
|
2023-01-11 09:31:10 +13:00
|
|
|
from PyQt5.QtWidgets import QSystemTrayIcon, QMenu, QAction, QApplication
|
2021-04-23 00:34:06 +12:00
|
|
|
|
2023-09-20 10:39:15 +12:00
|
|
|
from rare.models.options import options
|
2023-01-28 09:05:39 +13:00
|
|
|
from rare.shared import RareCore
|
2022-07-02 03:03:18 +12:00
|
|
|
|
|
|
|
logger = getLogger("TrayIcon")
|
2022-01-03 10:52:43 +13:00
|
|
|
|
2021-04-08 00:46:27 +12:00
|
|
|
|
|
|
|
class TrayIcon(QSystemTrayIcon):
|
2022-10-24 02:06:54 +13:00
|
|
|
# none:
|
|
|
|
show_app: pyqtSignal = pyqtSignal()
|
|
|
|
# int: exit code
|
|
|
|
exit_app: pyqtSignal = pyqtSignal(int)
|
|
|
|
|
2022-12-30 11:14:51 +13:00
|
|
|
def __init__(self, parent=None):
|
2022-09-04 10:14:43 +12:00
|
|
|
super(TrayIcon, self).__init__(parent=parent)
|
2023-01-11 09:31:10 +13:00
|
|
|
self.__parent = parent
|
2023-01-11 03:35:24 +13:00
|
|
|
self.rcore = RareCore.instance()
|
2023-01-28 09:05:39 +13:00
|
|
|
self.core = RareCore.instance().core()
|
|
|
|
self.settings = QSettings()
|
2022-02-26 06:43:27 +13:00
|
|
|
|
2021-12-10 09:59:07 +13:00
|
|
|
self.setIcon(QIcon(":/images/Rare.png"))
|
2021-04-08 00:46:27 +12:00
|
|
|
self.setVisible(True)
|
2023-01-11 09:31:10 +13:00
|
|
|
self.setToolTip(QApplication.applicationName())
|
2021-04-08 00:46:27 +12:00
|
|
|
|
|
|
|
self.menu = QMenu()
|
|
|
|
|
2023-01-11 09:31:10 +13:00
|
|
|
self.show_action = QAction(QApplication.applicationName())
|
2022-10-24 02:06:54 +13:00
|
|
|
self.show_action.triggered.connect(self.show_app)
|
|
|
|
self.menu.addAction(self.show_action)
|
2021-04-08 00:46:27 +12:00
|
|
|
|
|
|
|
self.menu.addSeparator()
|
2022-01-03 10:52:43 +13:00
|
|
|
self.text_action = QAction("Quick launch")
|
|
|
|
self.text_action.setEnabled(False)
|
|
|
|
self.menu.addAction(self.text_action)
|
|
|
|
|
2023-01-11 09:31:10 +13:00
|
|
|
# We need to reference this separator to add game actions before it
|
|
|
|
self.separator = self.menu.addSeparator()
|
2022-10-24 02:06:54 +13:00
|
|
|
self.exit_action = QAction(self.tr("Quit"))
|
|
|
|
self.exit_action.triggered.connect(lambda: self.exit_app.emit(0))
|
2022-01-03 10:52:43 +13:00
|
|
|
self.menu.addAction(self.exit_action)
|
2023-01-11 09:31:10 +13:00
|
|
|
|
|
|
|
self.game_actions: List[QAction] = []
|
|
|
|
self.update_actions()
|
|
|
|
|
2021-04-08 00:46:27 +12:00
|
|
|
self.setContextMenu(self.menu)
|
2022-04-27 06:50:40 +12:00
|
|
|
|
2023-01-28 09:05:39 +13:00
|
|
|
self.signals = RareCore.instance().signals()
|
2022-12-25 15:21:23 +13:00
|
|
|
self.signals.game.uninstalled.connect(self.remove_button)
|
2023-01-28 09:05:39 +13:00
|
|
|
self.signals.application.notify.connect(self.notify)
|
2023-01-11 09:31:10 +13:00
|
|
|
self.signals.application.update_tray.connect(self.update_actions)
|
|
|
|
|
|
|
|
def last_played(self) -> List:
|
|
|
|
last_played = [game for game in self.rcore.games if (game.metadata and game.is_installed)]
|
|
|
|
last_played.sort(key=lambda g: g.metadata.last_played, reverse=True)
|
2023-12-25 07:44:46 +13:00
|
|
|
return last_played[:5]
|
2023-01-11 09:31:10 +13:00
|
|
|
|
2024-01-20 04:17:31 +13:00
|
|
|
@pyqtSlot(str, str)
|
|
|
|
def notify(self, title: str, body: str):
|
2023-09-20 10:39:15 +12:00
|
|
|
if self.settings.value(*options.notification):
|
2024-01-25 04:58:26 +13:00
|
|
|
self.showMessage(f"{title} - {QApplication.applicationName()}", body, QSystemTrayIcon.Information, 4000)
|
2023-01-28 09:05:39 +13:00
|
|
|
|
2023-01-11 09:31:10 +13:00
|
|
|
@pyqtSlot()
|
|
|
|
def update_actions(self):
|
|
|
|
for action in self.game_actions:
|
|
|
|
action.deleteLater()
|
|
|
|
self.game_actions.clear()
|
|
|
|
for rgame in self.last_played():
|
|
|
|
a = QAction(rgame.app_title)
|
|
|
|
a.setProperty("app_name", rgame.app_name)
|
|
|
|
a.triggered.connect(
|
2023-01-21 13:15:06 +13:00
|
|
|
lambda: self.rcore.get_game(self.sender().property("app_name")).launch()
|
2023-01-11 09:31:10 +13:00
|
|
|
)
|
|
|
|
self.menu.insertAction(self.separator, a)
|
|
|
|
self.game_actions.append(a)
|
2022-04-27 06:50:40 +12:00
|
|
|
|
2023-01-11 09:31:10 +13:00
|
|
|
@pyqtSlot(str)
|
2022-04-27 06:50:40 +12:00
|
|
|
def remove_button(self, app_name: str):
|
|
|
|
if action := next((i for i in self.game_actions if i.property("app_name") == app_name), None):
|
2022-07-02 03:03:18 +12:00
|
|
|
self.game_actions.remove(action)
|
2022-04-27 06:50:40 +12:00
|
|
|
action.deleteLater()
|