Compare commits

..

1 Commits

Author SHA1 Message Date
Raoul Snyman 1471b73403 Update WebAppify to the latest tech
ci/woodpecker/push/woodpecker Pipeline was successful Details
ci/woodpecker/pr/woodpecker Pipeline was successful Details
- Migrate to Hatch
- Migrate to Woodpecker CI
- Migrate to only Python 3
- Add .editorconfig
- Expand .gitignore
2022-06-10 12:30:10 -07:00
2 changed files with 36 additions and 8 deletions

8
.editorconfig Normal file
View File

@ -0,0 +1,8 @@
root = true
[*]
max_line_length = 120
[*.py]
indent_style = space
indent_size = 4

View File

@ -21,6 +21,7 @@ This will create a window with the website, using the icon provided.
If your site needs Flash Player, you'll need the appropriate Flash Player plugin installed system-wide. For QtWebKit
you will need the NPAPI plugin, and for QtWebEngine you will need the PPAPI plugin.
"""
import logging
import sys
import platform
@ -31,8 +32,25 @@ SETTINGS = [
QtWebEngineWidgets.QWebEngineSettings.JavascriptCanAccessClipboard,
QtWebEngineWidgets.QWebEngineSettings.LocalContentCanAccessRemoteUrls
]
LOG_LEVELS = {
QtWebEngineWidgets.QWebEnginePage.InfoMessageLevel: logging.INFO,
QtWebEngineWidgets.QWebEnginePage.WarningMessageLevel: logging.WARNING,
QtWebEngineWidgets.QWebEnginePage.ErrorMessageLevel: logging.ERROR
}
# WebView = QtWebEngineWidgets.QWebEngineView
log = logging.getLogger(__name__)
class WebPage(QtWebEngineWidgets.QWebEnginePage):
"""
A custom QWebEnginePage which logs JS console messages to the Python logging system
"""
def javaScriptConsoleMessage(self, level, message, line_number, source_id):
"""
Custom logger to log console messages to the Python logging system
"""
log.log(LOG_LEVELS[level], f'{source_id}:{line_number} {message}')
print(message)
class WebWindow(QtWidgets.QWidget):
@ -53,7 +71,8 @@ class WebWindow(QtWidgets.QWidget):
self.setContentsMargins(0, 0, 0, 0)
self.layout = QtWidgets.QVBoxLayout(self)
self.layout.setContentsMargins(0, 0, 0, 0)
self.webview = WebView(self)
self.webview = QtWebEngineWidgets.QWebEngineView(self)
self.webview.setPage(WebPage(self.webview))
for setting in SETTINGS:
self.webview.settings().setAttribute(setting, True)
self.webview.setUrl(QtCore.QUrl(url))
@ -64,11 +83,11 @@ class WebWindow(QtWidgets.QWidget):
"""
Show a balloon message to inform the user that the app is minimized
"""
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. '
'To close the program, choose <b>Quit</b> in the context menu of the system '
'tray icon.', QtWidgets.QSystemTrayIcon.Information, 5000)
self.has_shown_warning = True
self._has_shown_warning = True
def _update_tray_menu(self):
"""
@ -102,11 +121,11 @@ class WebWindow(QtWidgets.QWidget):
"""
# Create the actions for the menu
self.restore_action = QtWidgets.QAction('&Restore', self)
self.restore_action.triggered.connect(self._restoreWindow)
self.restore_action.triggered.connect(self._restore_window)
self.minimize_action = QtWidgets.QAction('Mi&nimize', self)
self.minimize_action.triggered.connect(self.close)
self.maximize_action = QtWidgets.QAction('Ma&ximize', self)
self.maximize_action.triggered.connect(self._maximizeWindow)
self.maximize_action.triggered.connect(self._maximize_window)
self.quit_action = QtWidgets.QAction('&Quit', self)
self.quit_action.triggered.connect(self.app.quit)
# Create the menu and add the actions
@ -192,7 +211,7 @@ class WebApp(QtWidgets.QApplication):
"""
A generic application to open a web page in a desktop app
"""
def __init__(self, title, url, icon, canMinimizeToTray=False):
def __init__(self, title, url, icon, can_minimize_to_tray=False, canMinimizeToTray=False):
"""
Create an application which loads a URL into a window
"""
@ -202,7 +221,8 @@ class WebApp(QtWidgets.QApplication):
self.title = title
self.url = url
self.icon = icon
self.can_minimize_to_tray = QtWidgets.QSystemTrayIcon.isSystemTrayAvailable() and can_minimize_to_tray
self.can_minimize_to_tray = QtWidgets.QSystemTrayIcon.isSystemTrayAvailable() and \
(can_minimize_to_tray or canMinimizeToTray)
if self.can_minimize_to_tray:
self.setQuitOnLastWindowClosed(False)
self.setWindowIcon(QtGui.QIcon(self.icon))