Browse Source

Added devtools as a side view

sisyphus
JezerM 3 years ago
parent
commit
543b7926d5
No known key found for this signature in database
GPG Key ID: 66BBC5D01388C6B5
  1. 56
      web-greeter/browser/browser.py
  2. 48
      web-greeter/browser/devtools.py
  3. 1
      web-greeter/browser/interceptor.py
  4. 12
      web-greeter/browser/window.py

56
web-greeter/browser/browser.py

@ -39,13 +39,13 @@ from typing import (
# 3rd-Party Libs # 3rd-Party Libs
from PyQt5.QtCore import QUrl, Qt, QCoreApplication, QFile 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.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.QtGui import QColor
from PyQt5.QtWebChannel import QWebChannel 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.url_scheme import QtUrlSchemeHandler
from browser.interceptor import QtUrlRequestInterceptor from browser.interceptor import QtUrlRequestInterceptor
@ -64,10 +64,10 @@ os.environ["QT_SCREEN_SCALE_FACTORS"] = "1"
os.environ["QT_SCALE_FACTOR"] = "1" os.environ["QT_SCALE_FACTOR"] = "1"
WINDOW_STATES = { WINDOW_STATES = {
'NORMAL': Qt.WindowNoState, 'NORMAL': Qt.WindowState.WindowNoState,
'MINIMIZED': Qt.WindowMinimized, 'MINIMIZED': Qt.WindowState.WindowMinimized,
'MAXIMIZED': Qt.WindowMaximized, 'MAXIMIZED': Qt.WindowState.WindowMaximized,
'FULLSCREEN': Qt.WindowFullScreen, 'FULLSCREEN': Qt.WindowState.WindowFullScreen,
} # type: Dict[str, Qt.WindowState] } # type: Dict[str, Qt.WindowState]
DISABLED_SETTINGS = [ DISABLED_SETTINGS = [
@ -90,20 +90,20 @@ class Application:
states = WINDOW_STATES states = WINDOW_STATES
def __init__(self): def __init__(self):
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling) QCoreApplication.setAttribute(Qt.ApplicationAttribute.AA_EnableHighDpiScaling)
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling) QApplication.setAttribute(Qt.ApplicationAttribute.AA_EnableHighDpiScaling)
self.app = QApplication([]) self.app = QApplication([])
self.window = MainWindow() self.window = MainWindow()
self.desktop = self.app.desktop() 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.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.setWindowFlags(
self.window.windowFlags() | Qt.MaximizeUsingFullscreenGeometryHint self.window.windowFlags() | Qt.WindowType.MaximizeUsingFullscreenGeometryHint
) )
state = self.states['NORMAL'] state = self.states['NORMAL']
@ -112,7 +112,7 @@ class Application:
except Exception: except Exception:
self.window.setWindowState(state) self.window.setWindowState(state)
self.window.setCursor(Qt.ArrowCursor) self.window.setCursor(Qt.CursorShape.ArrowCursor)
self.app.aboutToQuit.connect(self._before_exit) self.app.aboutToQuit.connect(self._before_exit)
@ -166,14 +166,16 @@ class Browser(Application):
self._initialize_page() self._initialize_page()
if web_greeter_config["config"]["greeter"]["debug_mode"]: if web_greeter_config["config"]["greeter"]["debug_mode"]:
self.devtools = DevTools() self._initialize_devtools()
if web_greeter_config["config"]["greeter"]["secure_mode"]: if web_greeter_config["config"]["greeter"]["secure_mode"]:
self.profile.setUrlRequestInterceptor(self.interceptor) self.profile.setUrlRequestInterceptor(self.interceptor)
self.page.setBackgroundColor(QColor(0, 0, 0)) self.page.setBackgroundColor(QColor(0, 0, 0))
self.window.setStyleSheet("""QMainWindow, QWebEngineView {
background: #000000;
}""")
self.view.show()
self.window.setCentralWidget(self.view) self.window.setCentralWidget(self.view)
logger.debug("Browser Window created") logger.debug("Browser Window created")
@ -190,6 +192,28 @@ class Browser(Application):
self.load_script(':/_greeter/js/bundle.js', 'Web Greeter Bundle') self.load_script(':/_greeter/js/bundle.js', 'Web Greeter Bundle')
self.load_theme() 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): def _initialize_page(self):
page_settings = self.page.settings().globalSettings() page_settings = self.page.settings().globalSettings()
@ -241,7 +265,7 @@ class Browser(Application):
# print(script_file, path) # 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_string = str(script_file.readAll(), 'utf-8')
script.setInjectionPoint(QWebEngineScript.DocumentCreation) script.setInjectionPoint(QWebEngineScript.DocumentCreation)

48
web-greeter/browser/devtools.py

@ -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 <http://www.gnu.org/licenses/>.
# 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")

1
web-greeter/browser/interceptor.py

@ -44,6 +44,7 @@ class QtUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
not_devtools = ( not_devtools = (
not url.startswith('http://127.0.0.1') and not url.startswith('ws://127.0.0.1') 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 block_request = not_devtools and not_data_uri and not_webg_uri and not_local_file

12
web-greeter/browser/window.py

@ -29,10 +29,18 @@ from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtGui import QKeyEvent from PyQt5.QtGui import QKeyEvent
import globals
class MainWindow(QMainWindow): class MainWindow(QMainWindow):
def keyPressEvent(self, keyEvent: QKeyEvent) -> None: def keyPressEvent(self, keyEvent: QKeyEvent) -> None:
super().keyPressEvent(keyEvent) 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 pass
elif (keyEvent.key() == Qt.Key.Key_MonBrightnessDown): elif (key == Qt.Key.Key_MonBrightnessDown):
pass pass
elif (key == Qt.Key.Key_I
and mod & Qt.KeyboardModifier.ControlModifier
and mod & Qt.KeyboardModifier.ShiftModifier):
globals.greeter.toggle_devtools()

Loading…
Cancel
Save