Merge pull request #37 from firstlookmedia/36_macos_new_window

When you activate dangerzone in macOS and no windows are open, open a…
This commit is contained in:
Micah Lee 2020-02-28 17:35:44 -08:00 committed by GitHub
commit d0b8a33f33
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 6 deletions

View file

@ -5,6 +5,7 @@ import signal
import platform
import click
import time
import uuid
from .global_common import GlobalCommon
from .main_window import MainWindow
@ -20,6 +21,7 @@ dangerzone_version = "0.0.3"
class Application(QtWidgets.QApplication):
document_selected = QtCore.pyqtSignal(str)
application_activated = QtCore.pyqtSignal()
def __init__(self):
QtWidgets.QApplication.__init__(self, sys.argv)
@ -29,6 +31,9 @@ class Application(QtWidgets.QApplication):
if event.type() == QtCore.QEvent.FileOpen:
self.document_selected.emit(event.file())
return True
elif event.type() == QtCore.QEvent.ApplicationActivate:
self.application_activated.emit()
return True
return QtWidgets.QApplication.event(self, event)
@ -66,15 +71,25 @@ def main(filename):
docker_installer.start()
return
windows = []
closed_windows = {}
windows = {}
def delete_window(window_id):
closed_windows[window_id] = windows[window_id]
del windows[window_id]
# Open a document in a window
def select_document(filename=None):
if len(windows) == 1 and windows[0].common.document_filename == None:
window = windows[0]
if (
len(windows) == 1
and windows[list(windows.keys())[0]].common.document_filename == None
):
window = windows[list(windows.keys())[0]]
else:
window = MainWindow(global_common)
windows.append(window)
window_id = uuid.uuid4().hex
window = MainWindow(global_common, window_id)
window.delete_window.connect(delete_window)
windows[window_id] = window
if filename:
# Validate filename
@ -100,7 +115,15 @@ def main(filename):
if not select_document(filename):
return True
# Open a new window, if all windows are closed
def application_activated():
if len(windows) == 0:
select_document()
# If we get a file open event, open it
app.document_selected.connect(select_document)
# If the application is activated and all windows are closed, open a new one
app.application_activated.connect(application_activated)
sys.exit(app.exec_())

View file

@ -10,9 +10,12 @@ from .common import Common
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, global_common):
delete_window = QtCore.pyqtSignal(str)
def __init__(self, global_common, window_id):
super(MainWindow, self).__init__()
self.global_common = global_common
self.window_id = window_id
self.common = Common()
self.setWindowTitle("dangerzone")
@ -83,5 +86,7 @@ class MainWindow(QtWidgets.QMainWindow):
def closeEvent(self, e):
e.accept()
self.delete_window.emit(self.window_id)
if platform.system() != "Darwin":
self.global_common.app.quit()