Compare commits
No commits in common. "allow-notifications" and "master" have entirely different histories.
allow-noti
...
master
@ -55,8 +55,7 @@ class WebWindow(QtWidgets.QWidget):
|
|||||||
"""
|
"""
|
||||||
A window with a single web view and nothing else
|
A window with a single web view and nothing else
|
||||||
"""
|
"""
|
||||||
def __init__(self, app: 'WebApp', title: str, url: str, icon: str, can_minimize_to_tray: bool = False,
|
def __init__(self, app, title, url, icon, can_minimize_to_tray=False, canMinimizeToTray=False):
|
||||||
allow_notifications: bool = False):
|
|
||||||
"""
|
"""
|
||||||
Create the window
|
Create the window
|
||||||
"""
|
"""
|
||||||
@ -64,7 +63,7 @@ class WebWindow(QtWidgets.QWidget):
|
|||||||
self._has_shown_warning = False
|
self._has_shown_warning = False
|
||||||
self.app = app
|
self.app = app
|
||||||
self.icon = QtGui.QIcon(icon)
|
self.icon = QtGui.QIcon(icon)
|
||||||
self.can_minimize_to_tray = can_minimize_to_tray or allow_notifications
|
self.can_minimize_to_tray = can_minimize_to_tray or canMinimizeToTray
|
||||||
self.setWindowTitle(title)
|
self.setWindowTitle(title)
|
||||||
self.setWindowIcon(self.icon)
|
self.setWindowIcon(self.icon)
|
||||||
self.setContentsMargins(0, 0, 0, 0)
|
self.setContentsMargins(0, 0, 0, 0)
|
||||||
@ -72,27 +71,12 @@ class WebWindow(QtWidgets.QWidget):
|
|||||||
self.layout.setContentsMargins(0, 0, 0, 0)
|
self.layout.setContentsMargins(0, 0, 0, 0)
|
||||||
self.webview = QtWebEngineWidgets.QWebEngineView(self)
|
self.webview = QtWebEngineWidgets.QWebEngineView(self)
|
||||||
self.webview.setPage(WebPage(self.webview))
|
self.webview.setPage(WebPage(self.webview))
|
||||||
if allow_notifications:
|
|
||||||
self.webview.page().featurePermissionRequested.connect(self._handle_notifications_requested)
|
|
||||||
self.webview.page().profile().setNotificationPresenter(self._present_notification)
|
|
||||||
for setting in SETTINGS:
|
for setting in SETTINGS:
|
||||||
self.webview.settings().setAttribute(setting, True)
|
self.webview.settings().setAttribute(setting, True)
|
||||||
self.webview.setUrl(QtCore.QUrl(url))
|
self.webview.setUrl(QtCore.QUrl(url))
|
||||||
self.layout.addWidget(self.webview)
|
self.layout.addWidget(self.webview)
|
||||||
self.webview.titleChanged.connect(self.on_title_changed)
|
self.webview.titleChanged.connect(self.on_title_changed)
|
||||||
|
|
||||||
def _handle_notifications_requested(self, origin: QtCore.QUrl, feature: QtWebEngineCore.QWebEnginePage.Feature):
|
|
||||||
"""Handle when notifications are requested and implicitly allow them"""
|
|
||||||
if feature != QtWebEngineCore.QWebEnginePage.Feature.Notifications:
|
|
||||||
return
|
|
||||||
self.webview.page().setFeaturePermission(
|
|
||||||
origin, feature, QtWebEngineCore.QWebEnginePage.PermissionPolicy.PermissionGrantedByUser
|
|
||||||
)
|
|
||||||
|
|
||||||
def _present_notification(self, notification: QtWebEngineCore.QWebEngineNotification):
|
|
||||||
"""Show a notification from the browser"""
|
|
||||||
self.tray_icon.showMessage(notification.title(), notification.message())
|
|
||||||
|
|
||||||
def _show_warning(self):
|
def _show_warning(self):
|
||||||
"""
|
"""
|
||||||
Show a balloon message to inform the user that the app is minimized
|
Show a balloon message to inform the user that the app is minimized
|
||||||
@ -100,7 +84,7 @@ class WebWindow(QtWidgets.QWidget):
|
|||||||
if not self._has_shown_warning:
|
if not self._has_shown_warning:
|
||||||
self.tray_icon.showMessage(self.windowTitle(), 'This program will continue running in the system tray. '
|
self.tray_icon.showMessage(self.windowTitle(), 'This program will continue running in the system tray. '
|
||||||
'To close the program, choose <b>Quit</b> in the context menu of the system '
|
'To close the program, choose <b>Quit</b> in the context menu of the system '
|
||||||
'tray icon.')
|
'tray icon.', QtWidgets.QSystemTrayIcon.Information, 5000)
|
||||||
self._has_shown_warning = True
|
self._has_shown_warning = True
|
||||||
|
|
||||||
def _update_tray_menu(self):
|
def _update_tray_menu(self):
|
||||||
@ -160,7 +144,7 @@ class WebWindow(QtWidgets.QWidget):
|
|||||||
self.tray_icon.activated.connect(self.on_tray_icon_activated)
|
self.tray_icon.activated.connect(self.on_tray_icon_activated)
|
||||||
self.tray_icon.show()
|
self.tray_icon.show()
|
||||||
|
|
||||||
def closeEvent(self, event: QtCore.QEvent):
|
def closeEvent(self, event):
|
||||||
"""
|
"""
|
||||||
Override the close event to minimize to the tray
|
Override the close event to minimize to the tray
|
||||||
"""
|
"""
|
||||||
@ -178,21 +162,21 @@ class WebWindow(QtWidgets.QWidget):
|
|||||||
# Update the menu to match
|
# Update the menu to match
|
||||||
self._update_tray_menu()
|
self._update_tray_menu()
|
||||||
|
|
||||||
def showEvent(self, event: QtCore.QEvent):
|
def showEvent(self, event):
|
||||||
"""
|
"""
|
||||||
Override the show event to catch max/min/etc events and update the tray icon menu accordingly
|
Override the show event to catch max/min/etc events and update the tray icon menu accordingly
|
||||||
"""
|
"""
|
||||||
super(WebWindow, self).showEvent(event)
|
super(WebWindow, self).showEvent(event)
|
||||||
self._update_tray_menu()
|
self._update_tray_menu()
|
||||||
|
|
||||||
def hideEvent(self, event: QtCore.QEvent):
|
def hideEvent(self, event):
|
||||||
"""
|
"""
|
||||||
Override the hide event to catch max/min/etc events and update the tray icon menu accordingly
|
Override the hide event to catch max/min/etc events and update the tray icon menu accordingly
|
||||||
"""
|
"""
|
||||||
super(WebWindow, self).hideEvent(event)
|
super(WebWindow, self).hideEvent(event)
|
||||||
self._update_tray_menu()
|
self._update_tray_menu()
|
||||||
|
|
||||||
def changeEvent(self, event: QtCore.QEvent):
|
def changeEvent(self, event):
|
||||||
"""
|
"""
|
||||||
Catch the minimize event and close the form
|
Catch the minimize event and close the form
|
||||||
"""
|
"""
|
||||||
@ -201,7 +185,7 @@ class WebWindow(QtWidgets.QWidget):
|
|||||||
self.close()
|
self.close()
|
||||||
super(WebWindow, self).changeEvent(event)
|
super(WebWindow, self).changeEvent(event)
|
||||||
|
|
||||||
def on_title_changed(self, title: str):
|
def on_title_changed(self, title):
|
||||||
"""
|
"""
|
||||||
React to title changes
|
React to title changes
|
||||||
"""
|
"""
|
||||||
@ -210,11 +194,11 @@ class WebWindow(QtWidgets.QWidget):
|
|||||||
if self.can_minimize_to_tray:
|
if self.can_minimize_to_tray:
|
||||||
self.tray_icon.setToolTip(title)
|
self.tray_icon.setToolTip(title)
|
||||||
|
|
||||||
def on_tray_icon_activated(self, reason: QtWidgets.QSystemTrayIcon.ActivationReason):
|
def on_tray_icon_activated(self, reason):
|
||||||
"""
|
"""
|
||||||
React to the tray icon being activated
|
React to the tray icon being activated
|
||||||
"""
|
"""
|
||||||
if reason == QtWidgets.QSystemTrayIcon.ActivationReason.Trigger:
|
if reason == QtWidgets.QSystemTrayIcon.Trigger:
|
||||||
if self.isVisible():
|
if self.isVisible():
|
||||||
self.close()
|
self.close()
|
||||||
else:
|
else:
|
||||||
@ -225,8 +209,7 @@ class WebApp(QtWidgets.QApplication):
|
|||||||
"""
|
"""
|
||||||
A generic application to open a web page in a desktop app
|
A generic application to open a web page in a desktop app
|
||||||
"""
|
"""
|
||||||
def __init__(self, title: str, url: str, icon: str, can_minimize_to_tray: bool = False,
|
def __init__(self, title, url, icon, can_minimize_to_tray=False):
|
||||||
allow_notifications: bool = False):
|
|
||||||
"""
|
"""
|
||||||
Create an application which loads a URL into a window
|
Create an application which loads a URL into a window
|
||||||
"""
|
"""
|
||||||
@ -236,9 +219,7 @@ class WebApp(QtWidgets.QApplication):
|
|||||||
self.title = title
|
self.title = title
|
||||||
self.url = url
|
self.url = url
|
||||||
self.icon = icon
|
self.icon = icon
|
||||||
self.can_minimize_to_tray = QtWidgets.QSystemTrayIcon.isSystemTrayAvailable() and \
|
self.can_minimize_to_tray = QtWidgets.QSystemTrayIcon.isSystemTrayAvailable() and can_minimize_to_tray
|
||||||
(can_minimize_to_tray or allow_notifications)
|
|
||||||
self.allow_notifications = allow_notifications
|
|
||||||
if self.can_minimize_to_tray:
|
if self.can_minimize_to_tray:
|
||||||
self.setQuitOnLastWindowClosed(False)
|
self.setQuitOnLastWindowClosed(False)
|
||||||
self.setWindowIcon(QtGui.QIcon(self.icon))
|
self.setWindowIcon(QtGui.QIcon(self.icon))
|
||||||
@ -249,8 +230,7 @@ class WebApp(QtWidgets.QApplication):
|
|||||||
"""
|
"""
|
||||||
Set up the window and the tray icon, and run the app
|
Set up the window and the tray icon, and run the app
|
||||||
"""
|
"""
|
||||||
self.window = WebWindow(self, self.title, self.url, self.icon, self.can_minimize_to_tray,
|
self.window = WebWindow(self, self.title, self.url, self.icon, self.can_minimize_to_tray)
|
||||||
self.allow_notifications)
|
|
||||||
if self.can_minimize_to_tray:
|
if self.can_minimize_to_tray:
|
||||||
self.window.setup_tray_icon()
|
self.window.setup_tray_icon()
|
||||||
self.window.showMaximized()
|
self.window.showMaximized()
|
||||||
|
Loading…
Reference in New Issue
Block a user