From 543b7926d51d39190024745cf052b5c97952cfc4 Mon Sep 17 00:00:00 2001 From: JezerM Date: Sun, 12 Sep 2021 00:10:12 -0600 Subject: [PATCH] Added devtools as a side view --- web-greeter/browser/browser.py | 56 +++++++++++++++++++++--------- web-greeter/browser/devtools.py | 48 ------------------------- web-greeter/browser/interceptor.py | 1 + web-greeter/browser/window.py | 12 +++++-- 4 files changed, 51 insertions(+), 66 deletions(-) delete mode 100644 web-greeter/browser/devtools.py diff --git a/web-greeter/browser/browser.py b/web-greeter/browser/browser.py index c0cffe0..fa94e74 100644 --- a/web-greeter/browser/browser.py +++ b/web-greeter/browser/browser.py @@ -39,13 +39,13 @@ from typing import ( # 3rd-Party Libs from PyQt5.QtCore import QUrl, Qt, QCoreApplication, QFile -from PyQt5.QtWidgets import QApplication, QDesktopWidget, QMainWindow +from PyQt5.QtWidgets import QApplication, QDesktopWidget, QDockWidget, QMainWindow from PyQt5.QtWebEngineCore import QWebEngineUrlScheme -from PyQt5.QtWebEngineWidgets import QWebEngineScript, QWebEngineProfile, QWebEngineSettings, QWebEngineView +from PyQt5.QtWebEngineWidgets import QWebEngineScript, QWebEngineProfile, QWebEngineSettings, QWebEngineView, QWebEnginePage from PyQt5.QtGui import QColor from PyQt5.QtWebChannel import QWebChannel -from browser.error_prompt import WebPage, errorPrompt +from browser.error_prompt import WebPage from browser.url_scheme import QtUrlSchemeHandler from browser.interceptor import QtUrlRequestInterceptor @@ -64,10 +64,10 @@ os.environ["QT_SCREEN_SCALE_FACTORS"] = "1" os.environ["QT_SCALE_FACTOR"] = "1" WINDOW_STATES = { - 'NORMAL': Qt.WindowNoState, - 'MINIMIZED': Qt.WindowMinimized, - 'MAXIMIZED': Qt.WindowMaximized, - 'FULLSCREEN': Qt.WindowFullScreen, + 'NORMAL': Qt.WindowState.WindowNoState, + 'MINIMIZED': Qt.WindowState.WindowMinimized, + 'MAXIMIZED': Qt.WindowState.WindowMaximized, + 'FULLSCREEN': Qt.WindowState.WindowFullScreen, } # type: Dict[str, Qt.WindowState] DISABLED_SETTINGS = [ @@ -90,20 +90,20 @@ class Application: states = WINDOW_STATES def __init__(self): - QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling) - QApplication.setAttribute(Qt.AA_EnableHighDpiScaling) + QCoreApplication.setAttribute(Qt.ApplicationAttribute.AA_EnableHighDpiScaling) + QApplication.setAttribute(Qt.ApplicationAttribute.AA_EnableHighDpiScaling) self.app = QApplication([]) self.window = MainWindow() self.desktop = self.app.desktop() - self.window.setAttribute(Qt.WA_DeleteOnClose) + self.window.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose) self.window.setWindowTitle("Web Greeter") - self.window.setWindowFlags(self.window.windowFlags() | Qt.FramelessWindowHint) + self.window.setWindowFlags(self.window.windowFlags() | Qt.WindowType.FramelessWindowHint) self.window.setWindowFlags( - self.window.windowFlags() | Qt.MaximizeUsingFullscreenGeometryHint + self.window.windowFlags() | Qt.WindowType.MaximizeUsingFullscreenGeometryHint ) state = self.states['NORMAL'] @@ -112,7 +112,7 @@ class Application: except Exception: self.window.setWindowState(state) - self.window.setCursor(Qt.ArrowCursor) + self.window.setCursor(Qt.CursorShape.ArrowCursor) self.app.aboutToQuit.connect(self._before_exit) @@ -166,14 +166,16 @@ class Browser(Application): self._initialize_page() if web_greeter_config["config"]["greeter"]["debug_mode"]: - self.devtools = DevTools() + self._initialize_devtools() if web_greeter_config["config"]["greeter"]["secure_mode"]: self.profile.setUrlRequestInterceptor(self.interceptor) self.page.setBackgroundColor(QColor(0, 0, 0)) + self.window.setStyleSheet("""QMainWindow, QWebEngineView { + background: #000000; + }""") - self.view.show() self.window.setCentralWidget(self.view) logger.debug("Browser Window created") @@ -190,6 +192,28 @@ class Browser(Application): self.load_script(':/_greeter/js/bundle.js', 'Web Greeter Bundle') self.load_theme() + def _initialize_devtools(self): + self.dev_view = QWebEngineView(parent=self.window) + self.dev_page = QWebEnginePage() + self.dev_view.setPage(self.dev_page) + self.page.setDevToolsPage(self.dev_page) + + self.qdock = QDockWidget() + self.qdock.setWidget(self.dev_view) + self.qdock.setMinimumWidth(int(self.window.width() / 2)) + + self.window.addDockWidget(Qt.DockWidgetArea.RightDockWidgetArea, self.qdock) + self.qdock.hide() + logger.debug("DevTools initialized") + + def toggle_devtools(self): + if not web_greeter_config["config"]["greeter"]["debug_mode"]: + return + if self.qdock.isVisible(): + self.qdock.hide() + else: + self.qdock.show() + def _initialize_page(self): page_settings = self.page.settings().globalSettings() @@ -241,7 +265,7 @@ class Browser(Application): # print(script_file, path) - if script_file.open(QFile.ReadOnly): + if script_file.open(QFile.OpenModeFlag.ReadOnly): script_string = str(script_file.readAll(), 'utf-8') script.setInjectionPoint(QWebEngineScript.DocumentCreation) diff --git a/web-greeter/browser/devtools.py b/web-greeter/browser/devtools.py deleted file mode 100644 index 540f14f..0000000 --- a/web-greeter/browser/devtools.py +++ /dev/null @@ -1,48 +0,0 @@ -# -*- coding: utf-8 -*- -# -# devtools.py -# -# Copyright © 2016-2017 Antergos -# -# This file is part of Web Greeter. -# -# Web Greeter is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3 of the License, or -# (at your option) any later version. -# -# Web Greeter is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# The following additional terms are in effect as per Section 7 of the license: -# -# The preservation of all legal notices and author attributions in -# the material or in the Appropriate Legal Notices displayed -# by works containing it is required. -# -# You should have received a copy of the GNU General Public License -# along with Web Greeter; If not, see . - -# 3rd-Party Libs -from PyQt5.QtCore import QUrl -from PyQt5.QtWebEngineWidgets import ( - QWebEngineView, - QWebEnginePage, -) - -from logger import logger - -class DevTools: - - def __init__(self): - super().__init__() - - self.view = QWebEngineView() - self.page = self.view.page() # type: QWebEnginePage - - self.view.load(QUrl('http://127.0.0.1:12345')) - self.view.show() - logger.debug("DevTools initialized") - diff --git a/web-greeter/browser/interceptor.py b/web-greeter/browser/interceptor.py index f5d7307..370998f 100644 --- a/web-greeter/browser/interceptor.py +++ b/web-greeter/browser/interceptor.py @@ -44,6 +44,7 @@ class QtUrlRequestInterceptor(QWebEngineUrlRequestInterceptor): not_devtools = ( not url.startswith('http://127.0.0.1') and not url.startswith('ws://127.0.0.1') + and not url.startswith('devtools') ) block_request = not_devtools and not_data_uri and not_webg_uri and not_local_file diff --git a/web-greeter/browser/window.py b/web-greeter/browser/window.py index 2fd5a35..0ef46dd 100644 --- a/web-greeter/browser/window.py +++ b/web-greeter/browser/window.py @@ -29,10 +29,18 @@ from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QMainWindow from PyQt5.QtGui import QKeyEvent +import globals + class MainWindow(QMainWindow): def keyPressEvent(self, keyEvent: QKeyEvent) -> None: super().keyPressEvent(keyEvent) - if (keyEvent.key() == Qt.Key.Key_MonBrightnessUp): + key = keyEvent.key() + mod = keyEvent.modifiers() # type: Qt.KeyboardModifiers + if (key == Qt.Key.Key_MonBrightnessUp): pass - elif (keyEvent.key() == Qt.Key.Key_MonBrightnessDown): + elif (key == Qt.Key.Key_MonBrightnessDown): pass + elif (key == Qt.Key.Key_I + and mod & Qt.KeyboardModifier.ControlModifier + and mod & Qt.KeyboardModifier.ShiftModifier): + globals.greeter.toggle_devtools()