-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesktop.py
More file actions
248 lines (203 loc) · 8.74 KB
/
Copy pathdesktop.py
File metadata and controls
248 lines (203 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import os
import sys
import threading
import time
import webbrowser
from http.client import HTTPConnection, HTTPException
from pathlib import Path
from urllib.error import URLError
from urllib.request import Request, urlopen
# Allow attaching Chrome DevTools at http://localhost:9222 when ALERT_ALERT_DEBUG=1.
if os.environ.get("ALERT_ALERT_DEBUG"):
os.environ.setdefault("QTWEBENGINE_REMOTE_DEBUGGING", "9222")
from PySide6.QtCore import QUrl
from PySide6.QtGui import QAction, QIcon, QKeySequence
from PySide6.QtWidgets import QApplication, QFileDialog, QMainWindow, QMessageBox, QStatusBar
from PySide6.QtWebEngineCore import QWebEngineDownloadRequest, QWebEnginePage, QWebEngineProfile
from PySide6.QtWebEngineWidgets import QWebEngineView
from app import DEFAULT_APP_PORT, INTERNAL_DIR, find_available_port, get_output_dir, start_server
APP_TITLE = "Alert! Alert!"
APP_HOST = "localhost"
APP_PORT = find_available_port(APP_HOST, DEFAULT_APP_PORT)
APP_URL = f"http://{APP_HOST}:{APP_PORT}"
INTERNAL_NAV_HOSTS = {APP_HOST, "", "localhost"}
def wait_for_server(timeout_seconds=60):
deadline = time.monotonic() + timeout_seconds
while time.monotonic() < deadline:
connection = None
try:
connection = HTTPConnection(APP_HOST, APP_PORT, timeout=1.5)
connection.request("GET", "/api/health", headers={
"Cache-Control": "no-cache",
"Connection": "close",
})
response = connection.getresponse()
response.read()
if 200 <= response.status < 400:
return True
except (OSError, HTTPException, TimeoutError, URLError):
pass
finally:
if connection is not None:
try:
connection.close()
except Exception:
pass
QThreadSleeper.sleep(150)
return False
class QThreadSleeper:
@staticmethod
def sleep(milliseconds):
loop = QApplication.instance()
if loop:
loop.processEvents()
threading.Event().wait(milliseconds / 1000)
class DesktopPage(QWebEnginePage):
def acceptNavigationRequest(self, url, nav_type, is_main_frame):
if url.host() not in INTERNAL_NAV_HOSTS and url.scheme() in {"http", "https"}:
webbrowser.open(url.toString())
return False
return super().acceptNavigationRequest(url, nav_type, is_main_frame)
class DesktopWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle(APP_TITLE)
self.setMinimumSize(1100, 720)
icon_path = INTERNAL_DIR / "static" / "favicon.ico"
if icon_path.exists():
self.setWindowIcon(QIcon(str(icon_path)))
self.view = QWebEngineView(self)
# Use the DEFAULT profile with memory-cache + no-persistent-cookies, so
# stale state from prior installs can't leak across launches.
# NOTE: this open-source QtWebEngine build ships no proprietary codecs, so
# the <video> preview cannot decode H.264/AAC regardless of which profile
# is used. Clips in an unplayable codec are transcoded to a WebM preview
# proxy server-side (see ensure_preview_proxy in app.py); the profile
# choice does not affect codec support.
self.profile = QWebEngineProfile.defaultProfile()
self.profile.setHttpCacheType(QWebEngineProfile.MemoryHttpCache)
self.profile.setPersistentCookiesPolicy(QWebEngineProfile.NoPersistentCookies)
self.page = DesktopPage(self.profile, self.view)
self.view.setPage(self.page)
self.setCentralWidget(self.view)
self._build_menubar()
self._build_statusbar()
self.view.page().profile().downloadRequested.connect(self.handle_download_requested)
self.view.loadFinished.connect(self._on_loaded)
def _build_menubar(self):
mb = self.menuBar()
app_menu = mb.addMenu("&App")
deps_action = QAction("Dependency Setup", self)
deps_action.setStatusTip("Check FFmpeg, ffprobe, and yt-dlp")
deps_action.triggered.connect(self._open_dep_settings)
app_menu.addAction(deps_action)
reload_action = QAction("Reload App", self)
reload_action.setShortcut(QKeySequence("F5"))
reload_action.triggered.connect(self.view.reload)
app_menu.addAction(reload_action)
restart_onboarding = QAction("Restart Onboarding", self)
restart_onboarding.setStatusTip("Replay the welcome tour")
restart_onboarding.triggered.connect(self._restart_onboarding)
app_menu.addAction(restart_onboarding)
reset_action = QAction("Reset Settings", self)
reset_action.triggered.connect(self._reset_settings)
app_menu.addAction(reset_action)
app_menu.addSeparator()
open_folder = QAction("Open Output Folder", self)
open_folder.setShortcut(QKeySequence("Ctrl+Shift+O"))
open_folder.setStatusTip("Open the folder where finished alerts are saved")
open_folder.triggered.connect(self._open_output_folder)
app_menu.addAction(open_folder)
app_menu.addSeparator()
about_action = QAction("About", self)
about_action.triggered.connect(self._show_about)
app_menu.addAction(about_action)
exit_action = QAction("Exit", self)
exit_action.setShortcut(QKeySequence("Ctrl+Q"))
exit_action.triggered.connect(self.close)
app_menu.addAction(exit_action)
def _open_output_folder(self):
folder = get_output_dir()
folder.mkdir(parents=True, exist_ok=True)
os.startfile(str(folder))
def _reset_settings(self):
self.view.page().runJavaScript("App.resetSettings()")
def _restart_onboarding(self):
self.view.page().runJavaScript(
"if (typeof App !== 'undefined' && App.restartOnboarding) App.restartOnboarding();"
)
self.statusBar().showMessage("Onboarding restarted.", 3000)
def _open_dep_settings(self):
self.view.page().runJavaScript(
"if (typeof App !== 'undefined' && App.openSettingsPanel) App.openSettingsPanel('dependency-settings-panel');"
)
self.statusBar().showMessage("Opened dependency setup.", 3000)
def _show_about(self):
QMessageBox.about(
self,
"About Alert! Alert!",
"<b>Alert! Alert!</b><br><br>"
"A desktop tool for trimming, cropping, and exporting<br>"
"short alert clips quickly.<br><br>"
"Built with Flask + PySide6.",
)
def _build_statusbar(self):
status = QStatusBar(self)
status.showMessage("Starting local app server...")
self.setStatusBar(status)
def load_app(self):
self.view.setUrl(QUrl(APP_URL))
def _on_loaded(self, ok):
if ok:
self.statusBar().showMessage("Ready", 3000)
else:
self.statusBar().showMessage("Failed to load app UI.")
def handle_download_requested(self, download: QWebEngineDownloadRequest):
suggested_name = download.downloadFileName() or "alert-alert-download.bin"
ext = Path(suggested_name).suffix.lower()
if ext in {".mp3", ".wav", ".aac", ".flac", ".ogg"}:
file_filter = "Audio (*.mp3 *.wav *.aac *.flac *.ogg);;All Files (*.*)"
else:
file_filter = "Videos (*.mp4 *.mov *.mkv *.webm);;All Files (*.*)"
target_path, _ = QFileDialog.getSaveFileName(
self,
"Save Download",
str(Path.home() / suggested_name),
file_filter,
)
if not target_path:
download.cancel()
return
download.setDownloadDirectory(str(Path(target_path).parent))
download.setDownloadFileName(Path(target_path).name)
download.accept()
self.statusBar().showMessage(f"Downloading to {target_path}")
def closeEvent(self, event):
try:
request = Request(f"{APP_URL}/api/shutdown", method="POST")
urlopen(request, timeout=1)
except Exception:
pass
event.accept()
def launch_server():
start_server(host=APP_HOST, port=APP_PORT, open_browser=False)
def main():
qt_app = QApplication(sys.argv)
qt_app.setApplicationName(APP_TITLE)
qt_app.setOrganizationName("deutschmark")
server_thread = threading.Thread(target=launch_server, daemon=True)
server_thread.start()
window = DesktopWindow()
window.show()
if wait_for_server():
window.load_app()
else:
QMessageBox.critical(
window,
"Startup Failed",
"The local app server did not start in time. Check the console output for details.",
)
return 1
return qt_app.exec()
if __name__ == "__main__":
raise SystemExit(main())