mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-30 10:42:37 +02:00
Add more type annotations
Application moved to application.py to avoid circular imports
This commit is contained in:
parent
bdc08d79cd
commit
616ba55db7
5 changed files with 54 additions and 45 deletions
|
@ -6,41 +6,14 @@ from typing import Optional
|
|||
|
||||
import click
|
||||
import uuid
|
||||
from PySide6 import QtCore
|
||||
from PySide6.QtCore import QEvent
|
||||
from PySide6.QtWidgets import QApplication
|
||||
|
||||
from .application import Application
|
||||
from .common import GuiCommon
|
||||
from .main_window import MainWindow
|
||||
from .systray import SysTray
|
||||
from ..global_common import GlobalCommon
|
||||
|
||||
|
||||
class Application(QApplication):
|
||||
document_selected = QtCore.Signal(str)
|
||||
new_window = QtCore.Signal()
|
||||
application_activated = QtCore.Signal()
|
||||
|
||||
def __init__(self):
|
||||
super(Application, self).__init__()
|
||||
self.setQuitOnLastWindowClosed(False)
|
||||
self.original_event = self.event
|
||||
|
||||
def monkeypatch_event(event: QEvent):
|
||||
# In macOS, handle the file open event
|
||||
if event.type() == QtCore.QEvent.FileOpen:
|
||||
# Skip file open events in dev mode
|
||||
if not hasattr(sys, "dangerzone_dev"):
|
||||
self.document_selected.emit(event.file())
|
||||
return True
|
||||
elif event.type() == QtCore.QEvent.ApplicationActivate:
|
||||
self.application_activated.emit()
|
||||
return True
|
||||
return self.original_event(event)
|
||||
|
||||
self.event = monkeypatch_event
|
||||
|
||||
|
||||
@click.command()
|
||||
@click.argument("filename", required=False)
|
||||
def gui_main(filename):
|
||||
|
@ -50,7 +23,7 @@ def gui_main(filename):
|
|||
|
||||
# Strip ANSI colors from stdout output, to prevent terminal colors from breaking
|
||||
# the macOS GUI app
|
||||
from strip_ansi import strip_ansi
|
||||
from strip_ansi import strip_ansi # type: ignore
|
||||
|
||||
class StdoutFilter:
|
||||
def __init__(self, stream):
|
||||
|
@ -95,7 +68,7 @@ def gui_main(filename):
|
|||
len(windows) == 1
|
||||
and windows[list(windows.keys())[0]].common.input_filename is None
|
||||
):
|
||||
window = windows[list(windows.keys())[0]]
|
||||
window: MainWindow = windows[list(windows.keys())[0]]
|
||||
else:
|
||||
window_id = uuid.uuid4().hex
|
||||
window = MainWindow(global_common, gui_common, window_id)
|
||||
|
|
30
dangerzone/gui/application.py
Normal file
30
dangerzone/gui/application.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
import sys
|
||||
|
||||
from PySide6 import QtCore
|
||||
from PySide6.QtCore import QEvent
|
||||
from PySide6.QtWidgets import QApplication
|
||||
|
||||
|
||||
class Application(QApplication):
|
||||
document_selected = QtCore.Signal(str)
|
||||
new_window = QtCore.Signal()
|
||||
application_activated = QtCore.Signal()
|
||||
|
||||
def __init__(self):
|
||||
super(Application, self).__init__()
|
||||
self.setQuitOnLastWindowClosed(False)
|
||||
self.original_event = self.event
|
||||
|
||||
def monkeypatch_event(event: QEvent):
|
||||
# In macOS, handle the file open event
|
||||
if event.type() == QtCore.QEvent.FileOpen:
|
||||
# Skip file open events in dev mode
|
||||
if not hasattr(sys, "dangerzone_dev"):
|
||||
self.document_selected.emit(event.file())
|
||||
return True
|
||||
elif event.type() == QtCore.QEvent.ApplicationActivate:
|
||||
self.application_activated.emit()
|
||||
return True
|
||||
return self.original_event(event)
|
||||
|
||||
self.event = monkeypatch_event
|
|
@ -6,13 +6,16 @@ import pipes
|
|||
from PySide6 import QtCore, QtGui, QtWidgets
|
||||
from colorama import Fore
|
||||
|
||||
from . import Application
|
||||
from ..global_common import GlobalCommon
|
||||
|
||||
if platform.system() == "Darwin":
|
||||
import plistlib
|
||||
|
||||
elif platform.system() == "Linux":
|
||||
import grp
|
||||
import getpass
|
||||
from xdg.DesktopEntry import DesktopEntry
|
||||
from xdg.DesktopEntry import DesktopEntry # type: ignore
|
||||
|
||||
from ..settings import Settings
|
||||
|
||||
|
@ -22,7 +25,7 @@ class GuiCommon(object):
|
|||
The GuiCommon class is a singleton of shared functionality for the GUI
|
||||
"""
|
||||
|
||||
def __init__(self, app, global_common):
|
||||
def __init__(self, app: Application, global_common: GlobalCommon):
|
||||
# Qt app
|
||||
self.app = app
|
||||
|
||||
|
@ -45,7 +48,7 @@ class GuiCommon(object):
|
|||
path = self.global_common.get_resource_path("icon.png")
|
||||
return QtGui.QIcon(path)
|
||||
|
||||
def open_pdf_viewer(self, filename):
|
||||
def open_pdf_viewer(self, filename: str):
|
||||
if platform.system() == "Darwin":
|
||||
# Open in Preview
|
||||
args = ["open", "-a", "Preview.app", filename]
|
||||
|
@ -75,7 +78,8 @@ class GuiCommon(object):
|
|||
print(Fore.YELLOW + "> " + Fore.CYAN + args_str)
|
||||
subprocess.Popen(args)
|
||||
|
||||
def _find_pdf_viewers(self):
|
||||
@staticmethod
|
||||
def _find_pdf_viewers():
|
||||
pdf_viewers = {}
|
||||
if platform.system() == "Linux":
|
||||
# Find all .desktop files
|
||||
|
|
|
@ -7,14 +7,16 @@ import shutil
|
|||
from PySide6 import QtCore, QtGui, QtWidgets
|
||||
from colorama import Style, Fore
|
||||
|
||||
from . import GuiCommon
|
||||
from ..common import Common
|
||||
from ..container import convert
|
||||
from ..global_common import GlobalCommon
|
||||
|
||||
|
||||
class MainWindow(QtWidgets.QMainWindow):
|
||||
delete_window = QtCore.Signal(str)
|
||||
|
||||
def __init__(self, global_common, gui_common, window_id):
|
||||
def __init__(self, global_common: GlobalCommon, gui_common: GuiCommon, window_id: str):
|
||||
super(MainWindow, self).__init__()
|
||||
self.global_common = global_common
|
||||
self.gui_common = gui_common
|
||||
|
@ -90,7 +92,7 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||
class InstallContainerThread(QtCore.QThread):
|
||||
finished = QtCore.Signal()
|
||||
|
||||
def __init__(self, global_common):
|
||||
def __init__(self, global_common: GlobalCommon):
|
||||
super(InstallContainerThread, self).__init__()
|
||||
self.global_common = global_common
|
||||
|
||||
|
@ -111,7 +113,7 @@ class WaitingWidget(QtWidgets.QWidget):
|
|||
# - "install_container"
|
||||
finished = QtCore.Signal()
|
||||
|
||||
def __init__(self, global_common, gui_common):
|
||||
def __init__(self, global_common: GlobalCommon, gui_common: GuiCommon):
|
||||
super(WaitingWidget, self).__init__()
|
||||
self.global_common = global_common
|
||||
self.gui_common = gui_common
|
||||
|
@ -145,7 +147,7 @@ class WaitingWidget(QtWidgets.QWidget):
|
|||
self.check_state()
|
||||
|
||||
def check_state(self):
|
||||
state = None
|
||||
state: str
|
||||
|
||||
# Can we find the container runtime binary binary
|
||||
if platform.system() == "Linux":
|
||||
|
@ -200,7 +202,7 @@ class WaitingWidget(QtWidgets.QWidget):
|
|||
class ContentWidget(QtWidgets.QWidget):
|
||||
close_window = QtCore.Signal()
|
||||
|
||||
def __init__(self, global_common, gui_common, common):
|
||||
def __init__(self, global_common: GlobalCommon, gui_common: GuiCommon, common: Common):
|
||||
super(ContentWidget, self).__init__()
|
||||
|
||||
self.global_common = global_common
|
||||
|
@ -255,7 +257,7 @@ class ContentWidget(QtWidgets.QWidget):
|
|||
class DocSelectionWidget(QtWidgets.QWidget):
|
||||
document_selected = QtCore.Signal()
|
||||
|
||||
def __init__(self, common):
|
||||
def __init__(self, common: Common):
|
||||
super(DocSelectionWidget, self).__init__()
|
||||
self.common = common
|
||||
|
||||
|
@ -298,7 +300,7 @@ class SettingsWidget(QtWidgets.QWidget):
|
|||
start_clicked = QtCore.Signal()
|
||||
close_window = QtCore.Signal()
|
||||
|
||||
def __init__(self, global_common, gui_common, common):
|
||||
def __init__(self, global_common: GlobalCommon, gui_common: GuiCommon, common: Common):
|
||||
super(SettingsWidget, self).__init__()
|
||||
self.global_common = global_common
|
||||
self.gui_common = gui_common
|
||||
|
@ -489,7 +491,7 @@ class ConvertThread(QtCore.QThread):
|
|||
is_finished = QtCore.Signal(bool)
|
||||
update = QtCore.Signal(bool, str, int)
|
||||
|
||||
def __init__(self, global_common, common):
|
||||
def __init__(self, global_common: GlobalCommon, common: Common):
|
||||
super(ConvertThread, self).__init__()
|
||||
self.global_common = global_common
|
||||
self.common = common
|
||||
|
@ -536,7 +538,7 @@ class ConvertThread(QtCore.QThread):
|
|||
class ConvertWidget(QtWidgets.QWidget):
|
||||
close_window = QtCore.Signal()
|
||||
|
||||
def __init__(self, global_common, gui_common, common):
|
||||
def __init__(self, global_common: GlobalCommon, gui_common: GuiCommon, common: Common):
|
||||
super(ConvertWidget, self).__init__()
|
||||
self.global_common = global_common
|
||||
self.gui_common = gui_common
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
from PySide6 import QtWidgets
|
||||
|
||||
from dangerzone.global_common import GlobalCommon
|
||||
from dangerzone.gui import GuiCommon
|
||||
from dangerzone.gui import GuiCommon, Application
|
||||
|
||||
|
||||
class SysTray(QtWidgets.QSystemTrayIcon):
|
||||
def __init__(
|
||||
self, global_common: GlobalCommon, gui_common: GuiCommon, app
|
||||
self, global_common: GlobalCommon, gui_common: GuiCommon, app: Application
|
||||
):
|
||||
super(SysTray, self).__init__()
|
||||
self.global_common = global_common
|
||||
|
|
Loading…
Reference in a new issue