Add persistent notifications for priority 10 messages
Some checks failed
build / build-pip (push) Failing after 1m13s
build / build-win64 (push) Has been cancelled
build / build-macos (push) Has been cancelled

- Implement custom PersistentNotification widget with flashing background
- Add settings for persistent priority 10 notifications and sound control
- Modify notification logic to show persistent pop-ups for priority 10
- Allow closing all persistent notifications via tray icon click
- Add AGENTS.md with type checking guidelines
- Configure pyright to suppress PyQt6 false positives
- Update UI in settings dialog for new options
- Add notification sound file
This commit is contained in:
kdusek
2025-11-26 15:10:50 +01:00
parent 4e4fd9cdc9
commit 2108568f50
10 changed files with 673 additions and 125 deletions

View File

@@ -0,0 +1,145 @@
import logging
from PyQt6 import QtCore, QtGui, QtWidgets
logger = logging.getLogger("gotify-tray")
class PersistentNotification(QtWidgets.QWidget):
close_all_requested = QtCore.pyqtSignal()
def __init__(
self,
title: str,
message: str,
icon: QtGui.QIcon | QtWidgets.QSystemTrayIcon.MessageIcon,
y_offset: int = 0,
flash: bool = False,
parent=None,
):
super().__init__(parent)
self.y_offset = y_offset
self.flash = flash
self.flash_state = False
self.original_stylesheet = ""
self.setWindowFlags(
QtCore.Qt.WindowType.Window
| QtCore.Qt.WindowType.FramelessWindowHint
| QtCore.Qt.WindowType.WindowStaysOnTopHint
)
self.setAttribute(QtCore.Qt.WidgetAttribute.WA_OpaquePaintEvent, True)
self.setAttribute(QtCore.Qt.WidgetAttribute.WA_ShowWithoutActivating)
# Layout
layout = QtWidgets.QHBoxLayout(self)
layout.setContentsMargins(10, 10, 10, 10)
layout.setSpacing(10)
# Icon
self.icon_label = QtWidgets.QLabel()
if isinstance(icon, QtGui.QIcon):
self.icon_label.setPixmap(icon.pixmap(32, 32))
else:
# For MessageIcon
if icon == QtWidgets.QSystemTrayIcon.MessageIcon.Information:
standard_icon = QtWidgets.QStyle.StandardPixmap.SP_MessageBoxInformation
elif icon == QtWidgets.QSystemTrayIcon.MessageIcon.Warning:
standard_icon = QtWidgets.QStyle.StandardPixmap.SP_MessageBoxWarning
elif icon == QtWidgets.QSystemTrayIcon.MessageIcon.Critical:
standard_icon = QtWidgets.QStyle.StandardPixmap.SP_MessageBoxCritical
else:
standard_icon = QtWidgets.QStyle.StandardPixmap.SP_MessageBoxInformation
system_icon = self.style().standardIcon(standard_icon)
self.icon_label.setPixmap(system_icon.pixmap(32, 32))
layout.addWidget(self.icon_label)
# Text
text_layout = QtWidgets.QVBoxLayout()
self.title_label = QtWidgets.QLabel(title)
self.title_label.setStyleSheet("font-weight: bold;")
text_layout.addWidget(self.title_label)
self.message_label = QtWidgets.QLabel(message)
self.message_label.setWordWrap(True)
text_layout.addWidget(self.message_label)
layout.addLayout(text_layout, 1)
# Close button
self.close_button = QtWidgets.QPushButton("×")
self.close_button.setFixedSize(20, 20)
self.close_button.clicked.connect(self._on_close)
layout.addWidget(self.close_button)
# Style
self.setAutoFillBackground(True)
self.setBackgroundRole(QtGui.QPalette.ColorRole.Window)
palette = self.palette()
palette.setColor(QtGui.QPalette.ColorRole.Window, QtGui.QColor("white"))
palette.setColor(QtGui.QPalette.ColorRole.WindowText, QtGui.QColor("red"))
self.setPalette(palette)
self.setStyleSheet("""
PersistentNotification {
border-radius: 10px;
border: 1px solid rgba(100, 100, 100, 200);
}
QPushButton {
background-color: transparent;
color: white;
border: none;
font-size: 16px;
}
QPushButton:hover {
background-color: rgba(100, 100, 100, 100);
}
""")
if self.flash:
self.flash_timer = QtCore.QTimer(self)
self.flash_timer.timeout.connect(self._toggle_flash)
# Size and position
self.adjustSize()
self.setFixedWidth(300)
self._position()
# Timer for fade out if not clicked, but since persistent, maybe not needed
# But to avoid staying forever if forgotten, perhaps auto-close after long time
# But user wants until clicked, so no timer.
def _position(self):
screen = QtWidgets.QApplication.primaryScreen().availableGeometry()
# Stack from bottom right
self.move(
screen.width() - self.width() - 10,
screen.height() - self.height() - 50 - self.y_offset,
)
def _toggle_flash(self):
self.flash_state = not self.flash_state
palette = self.palette()
if self.flash_state:
palette.setColor(QtGui.QPalette.ColorRole.Window, QtGui.QColor("red"))
palette.setColor(QtGui.QPalette.ColorRole.WindowText, QtGui.QColor("white"))
else:
palette.setColor(QtGui.QPalette.ColorRole.Window, QtGui.QColor("white"))
palette.setColor(QtGui.QPalette.ColorRole.WindowText, QtGui.QColor("red"))
self.setPalette(palette)
self.update()
self.repaint()
def mousePressEvent(self, event):
self._on_close()
def _on_close(self):
if self.flash:
self.flash_timer.stop()
self.close_all_requested.emit()
self.close()
def showEvent(self, event):
super().showEvent(event)
self.raise_()
if self.flash:
self.flash_timer.start(1000)

View File

@@ -31,7 +31,7 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
super(SettingsDialog, self).__init__()
self.setupUi(self)
self.setWindowTitle("Settings")
self.settings_changed = False
self.changes_applied = False
self.server_changed = False
@@ -41,29 +41,55 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
self.link_callbacks()
def initUI(self):
self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Apply).setEnabled(False)
self.buttonBox.button(
QtWidgets.QDialogButtonBox.StandardButton.Apply
).setEnabled(False)
# Notifications
self.spin_priority.setValue(settings.value("tray/notifications/priority", type=int))
self.spin_priority.setValue(
settings.value("tray/notifications/priority", type=int)
)
self.spin_duration.setValue(settings.value("tray/notifications/duration_ms", type=int))
self.spin_duration.setValue(
settings.value("tray/notifications/duration_ms", type=int)
)
if platform.system() == "Windows":
# The notification duration setting is ignored by windows
self.label_notification_duration.hide()
self.spin_duration.hide()
self.label_notification_duration_ms.hide()
self.cb_notify.setChecked(settings.value("message/check_missed/notify", type=bool))
self.cb_notify.setChecked(
settings.value("message/check_missed/notify", type=bool)
)
self.cb_notification_click.setChecked(settings.value("tray/notifications/click", type=bool))
self.cb_notification_click.setChecked(
settings.value("tray/notifications/click", type=bool)
)
self.cb_tray_icon_unread.setChecked(settings.value("tray/icon/unread", type=bool))
self.cb_tray_icon_unread.setChecked(
settings.value("tray/icon/unread", type=bool)
)
self.cb_priority10_persistent.setChecked(
settings.value("tray/notifications/priority10_persistent", type=bool)
)
self.cb_sound_only_priority10.setChecked(
settings.value("tray/notifications/sound_only_priority10", type=bool)
)
# Interface
self.cb_priority_colors.setChecked(settings.value("MessageWidget/priority_color", type=bool))
self.cb_image_urls.setChecked(settings.value("MessageWidget/image_urls", type=bool))
self.cb_priority_colors.setChecked(
settings.value("MessageWidget/priority_color", type=bool)
)
self.cb_image_urls.setChecked(
settings.value("MessageWidget/image_urls", type=bool)
)
self.cb_locale.setChecked(settings.value("locale", type=bool))
self.cb_sort_applications.setChecked(settings.value("ApplicationModel/sort", type=bool))
self.cb_sort_applications.setChecked(
settings.value("ApplicationModel/sort", type=bool)
)
# Logging
self.combo_logging.addItems(
@@ -81,18 +107,22 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
self.add_message_widget()
# Advanced
self.groupbox_image_popup.setChecked(settings.value("ImagePopup/enabled", type=bool))
self.groupbox_image_popup.setChecked(
settings.value("ImagePopup/enabled", type=bool)
)
self.spin_popup_w.setValue(settings.value("ImagePopup/w", type=int))
self.spin_popup_h.setValue(settings.value("ImagePopup/h", type=int))
self.label_cache.setText("0 MB")
self.compute_cache_size()
self.groupbox_watchdog.setChecked(settings.value("watchdog/enabled", type=bool))
self.spin_watchdog_interval.setValue(settings.value("watchdog/interval/s", type=int))
self.spin_watchdog_interval.setValue(
settings.value("watchdog/interval/s", type=int)
)
self.label_app_version.setText(__version__)
self.label_qt_version.setText(QtCore.QT_VERSION_STR)
self.label_app_icon.setPixmap(QtGui.QIcon(get_image("logo.ico")).pixmap(22,22))
self.label_qt_icon.setPixmap(QtGui.QIcon(get_image("qt.png")).pixmap(22,22))
self.label_app_icon.setPixmap(QtGui.QIcon(get_image("logo.ico")).pixmap(22, 22))
self.label_qt_icon.setPixmap(QtGui.QIcon(get_image("qt.png")).pixmap(22, 22))
def add_message_widget(self):
self.message_widget = MessageWidget(
@@ -113,18 +143,18 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
def compute_cache_size(self):
self.cache_size_task = CacheSizeTask()
self.cache_size_task.size.connect(lambda size: self.label_cache.setText(f"{round(size/1e6, 1)} MB"))
self.cache_size_task.size.connect(
lambda size: self.label_cache.setText(f"{round(size / 1e6, 1)} MB")
)
self.cache_size_task.start()
def set_value(self, key: str, value: Any, widget: QtWidgets.QWidget):
"""Set a Settings value, only if the widget's value_changed attribute has been set
"""
"""Set a Settings value, only if the widget's value_changed attribute has been set"""
if hasattr(widget, "value_changed"):
settings.setValue(key, value)
def connect_signal(self, signal: QtCore.pyqtBoundSignal, widget: QtWidgets.QWidget):
"""Connect to a signal and set the value_changed attribute for a widget on trigger
"""
"""Connect to a signal and set the value_changed attribute for a widget on trigger"""
signal.connect(lambda *args: self.setting_changed_callback(widget))
def change_server_info_callback(self):
@@ -132,13 +162,17 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
def setting_changed_callback(self, widget: QtWidgets.QWidget):
self.settings_changed = True
self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Apply).setEnabled(True)
self.buttonBox.button(
QtWidgets.QDialogButtonBox.StandardButton.Apply
).setEnabled(True)
setattr(widget, "value_changed", True)
def change_font_callback(self, name: str):
label: QtWidgets.QLabel = getattr(self.message_widget, "label_" + name)
font, accepted = QtWidgets.QFontDialog.getFont(label.font(), self, f"Select a {name} font")
font, accepted = QtWidgets.QFontDialog.getFont(
label.font(), self, f"Select a {name} font"
)
if accepted:
self.setting_changed_callback(label)
@@ -146,7 +180,10 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
def export_callback(self):
fname = QtWidgets.QFileDialog.getSaveFileName(
self, "Export Settings", settings.value("export/path", type=str), "*",
self,
"Export Settings",
settings.value("export/path", type=str),
"*",
)[0]
if fname and os.path.exists(os.path.dirname(fname)):
self.export_settings_task = ExportSettingsTask(fname)
@@ -162,7 +199,10 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
def import_callback(self):
fname = QtWidgets.QFileDialog.getOpenFileName(
self, "Import Settings", settings.value("export/path", type=str), "*",
self,
"Import Settings",
settings.value("export/path", type=str),
"*",
)[0]
if fname and os.path.exists(fname):
self.import_settings_task = ImportSettingsTask(fname)
@@ -202,60 +242,128 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
self.label_cache.setText("0 MB")
def link_callbacks(self):
self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Apply).clicked.connect(self.apply_settings)
self.buttonBox.button(
QtWidgets.QDialogButtonBox.StandardButton.Apply
).clicked.connect(self.apply_settings)
# Notifications
self.connect_signal(self.spin_priority.valueChanged, self.spin_priority)
self.connect_signal(self.spin_duration.valueChanged, self.spin_duration)
self.connect_signal(self.cb_notify.stateChanged, self.cb_notify)
self.connect_signal(self.cb_notification_click.stateChanged, self.cb_notification_click)
self.connect_signal(self.cb_tray_icon_unread.stateChanged, self.cb_tray_icon_unread)
self.connect_signal(
self.cb_notification_click.stateChanged, self.cb_notification_click
)
self.connect_signal(
self.cb_tray_icon_unread.stateChanged, self.cb_tray_icon_unread
)
self.connect_signal(
self.cb_priority10_persistent.stateChanged, self.cb_priority10_persistent
)
self.connect_signal(
self.cb_sound_only_priority10.stateChanged, self.cb_sound_only_priority10
)
# Interface
self.connect_signal(self.cb_priority_colors.stateChanged, self.cb_priority_colors)
self.connect_signal(
self.cb_priority_colors.stateChanged, self.cb_priority_colors
)
self.connect_signal(self.cb_image_urls.stateChanged, self.cb_image_urls)
self.connect_signal(self.cb_locale.stateChanged, self.cb_locale)
self.connect_signal(self.cb_sort_applications.stateChanged, self.cb_sort_applications)
self.connect_signal(
self.cb_sort_applications.stateChanged, self.cb_sort_applications
)
# Server info
self.pb_change_server_info.clicked.connect(self.change_server_info_callback)
# Logging
self.connect_signal(self.combo_logging.currentTextChanged, self.combo_logging)
self.pb_open_log.clicked.connect(lambda: open_file(logger.root.handlers[0].baseFilename))
self.pb_open_log.clicked.connect(
lambda: open_file(logger.root.handlers[0].baseFilename)
)
# Fonts
self.pb_reset_fonts.clicked.connect(self.reset_fonts_callback)
self.pb_font_message_title.clicked.connect(lambda: self.change_font_callback("title"))
self.pb_font_message_date.clicked.connect(lambda: self.change_font_callback("date"))
self.pb_font_message_content.clicked.connect(lambda: self.change_font_callback("message"))
self.pb_font_message_title.clicked.connect(
lambda: self.change_font_callback("title")
)
self.pb_font_message_date.clicked.connect(
lambda: self.change_font_callback("date")
)
self.pb_font_message_content.clicked.connect(
lambda: self.change_font_callback("message")
)
# Advanced
self.pb_export.clicked.connect(self.export_callback)
self.pb_import.clicked.connect(self.import_callback)
self.pb_reset.clicked.connect(self.reset_callback)
self.connect_signal(self.groupbox_image_popup.toggled, self.groupbox_image_popup)
self.connect_signal(
self.groupbox_image_popup.toggled, self.groupbox_image_popup
)
self.connect_signal(self.spin_popup_w.valueChanged, self.spin_popup_w)
self.connect_signal(self.spin_popup_h.valueChanged, self.spin_popup_h)
self.pb_clear_cache.clicked.connect(self.clear_cache_callback)
self.pb_open_cache_dir.clicked.connect(lambda: open_file(Cache().directory()))
self.connect_signal(self.groupbox_watchdog.toggled, self.groupbox_watchdog)
self.connect_signal(self.spin_watchdog_interval.valueChanged, self.spin_watchdog_interval)
self.connect_signal(
self.spin_watchdog_interval.valueChanged, self.spin_watchdog_interval
)
def apply_settings(self):
# Priority
self.set_value("tray/notifications/priority", self.spin_priority.value(), self.spin_priority)
self.set_value("tray/notifications/duration_ms", self.spin_duration.value(), self.spin_duration)
self.set_value("message/check_missed/notify", self.cb_notify.isChecked(), self.cb_notify)
self.set_value("tray/notifications/click", self.cb_notification_click.isChecked(), self.cb_notification_click)
self.set_value("tray/icon/unread", self.cb_tray_icon_unread.isChecked(), self.cb_tray_icon_unread)
self.set_value(
"tray/notifications/priority",
self.spin_priority.value(),
self.spin_priority,
)
self.set_value(
"tray/notifications/duration_ms",
self.spin_duration.value(),
self.spin_duration,
)
self.set_value(
"message/check_missed/notify", self.cb_notify.isChecked(), self.cb_notify
)
self.set_value(
"tray/notifications/click",
self.cb_notification_click.isChecked(),
self.cb_notification_click,
)
self.set_value(
"tray/icon/unread",
self.cb_tray_icon_unread.isChecked(),
self.cb_tray_icon_unread,
)
self.set_value(
"tray/notifications/priority10_persistent",
self.cb_priority10_persistent.isChecked(),
self.cb_priority10_persistent,
)
self.set_value(
"tray/notifications/sound_only_priority10",
self.cb_sound_only_priority10.isChecked(),
self.cb_sound_only_priority10,
)
# Interface
self.set_value("MessageWidget/priority_color", self.cb_priority_colors.isChecked(), self.cb_priority_colors)
self.set_value("MessageWidget/image_urls", self.cb_image_urls.isChecked(), self.cb_image_urls)
self.set_value(
"MessageWidget/priority_color",
self.cb_priority_colors.isChecked(),
self.cb_priority_colors,
)
self.set_value(
"MessageWidget/image_urls",
self.cb_image_urls.isChecked(),
self.cb_image_urls,
)
self.set_value("locale", self.cb_locale.isChecked(), self.cb_locale)
self.set_value("ApplicationModel/sort", self.cb_sort_applications.isChecked(), self.cb_sort_applications)
self.set_value(
"ApplicationModel/sort",
self.cb_sort_applications.isChecked(),
self.cb_sort_applications,
)
# Logging
selected_level = self.combo_logging.currentText()
@@ -267,18 +375,44 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
logger.setLevel(selected_level)
# Fonts
self.set_value("MessageWidget/font/title", self.message_widget.label_title.font().toString(), self.message_widget.label_title)
self.set_value("MessageWidget/font/date", self.message_widget.label_date.font().toString(), self.message_widget.label_date)
self.set_value("MessageWidget/font/message", self.message_widget.label_message.font().toString(), self.message_widget.label_message)
self.set_value(
"MessageWidget/font/title",
self.message_widget.label_title.font().toString(),
self.message_widget.label_title,
)
self.set_value(
"MessageWidget/font/date",
self.message_widget.label_date.font().toString(),
self.message_widget.label_date,
)
self.set_value(
"MessageWidget/font/message",
self.message_widget.label_message.font().toString(),
self.message_widget.label_message,
)
# Advanced
self.set_value("ImagePopup/enabled", self.groupbox_image_popup.isChecked(), self.groupbox_image_popup)
self.set_value(
"ImagePopup/enabled",
self.groupbox_image_popup.isChecked(),
self.groupbox_image_popup,
)
self.set_value("ImagePopup/w", self.spin_popup_w.value(), self.spin_popup_w)
self.set_value("ImagePopup/h", self.spin_popup_h.value(), self.spin_popup_h)
self.set_value("watchdog/enabled", self.groupbox_watchdog.isChecked(), self.groupbox_watchdog)
self.set_value("watchdog/interval/s", self.spin_watchdog_interval.value(), self.spin_watchdog_interval)
self.set_value(
"watchdog/enabled",
self.groupbox_watchdog.isChecked(),
self.groupbox_watchdog,
)
self.set_value(
"watchdog/interval/s",
self.spin_watchdog_interval.value(),
self.spin_watchdog_interval,
)
self.settings_changed = False
self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Apply).setEnabled(False)
self.buttonBox.button(
QtWidgets.QDialogButtonBox.StandardButton.Apply
).setEnabled(False)
self.changes_applied = True