Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
150 lines (122 loc) · 5.43 KB
/
Copy pathmain.py
File metadata and controls
150 lines (122 loc) · 5.43 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
# main.py
import sys
import time
from src.app.logging_utils import get_logger, setup_logging
if __name__ == "__main__":
if "--mcp-stdio" in sys.argv or "--mcp-server" in sys.argv:
# Packaged builds have no python and no src/ tree. Stdout is the MCP protocol.
from src.web.mcp_server import main as mcp_stdio_main
sys.exit(mcp_stdio_main())
setup_logging()
logger = get_logger("main")
t_app = time.perf_counter()
logger.info("Application starting")
if "--gpu-probe" in sys.argv:
# Heavy ONNX/cv2 import only for the isolated GPU probe child process.
from src.core.clip_embedding import gpu_probe_cli_main
sys.exit(gpu_probe_cli_main())
if "--bootstrap-runtime" in sys.argv:
from src.storage.migration_runner import ensure_config_schema_v2_bootstrap
ensure_config_schema_v2_bootstrap()
from src.services.bundled_runtime_service import bootstrap_bundled_runtime
result = bootstrap_bundled_runtime()
if result.get("ok"):
sys.exit(0)
skipped = str(result.get("skipped") or "")
if skipped in {"nothing_to_import", "no_manifest"}:
sys.exit(0 if skipped == "nothing_to_import" else 2)
logger.error("Bundled runtime bootstrap failed: %s", result.get("error") or result)
sys.exit(1)
from PySide6.QtCore import QEvent, QObject, Qt
from PySide6.QtWidgets import QApplication
from src.app.single_instance import SingleInstanceServer, try_activate_existing_instance
# The picture host is a native window for VLC. Without this, Qt also turns
# the transport beside it into a native window that keeps the first frame:
# seeks still land, but the slider and times never move on screen.
QApplication.setAttribute(Qt.ApplicationAttribute.AA_DontCreateNativeWidgetSiblings, True)
app = QApplication(sys.argv)
from ui.dialogs.error_hook import install_uncaught_exception_hook
from ui.widgets.rounded_windows import install_rounded_frames
from ui.widgets.tooltip_utils import install_wrapped_tooltips
install_uncaught_exception_hook()
install_wrapped_tooltips()
install_rounded_frames(app)
if try_activate_existing_instance():
logger.info("Another instance is running; activating existing window")
sys.exit(0)
single_instance_server = SingleInstanceServer(parent=app)
app._videoseek_single_instance = single_instance_server
from src.app.instance_lock import configured_api_enabled, remove_instance_lock, write_instance_lock
try:
write_instance_lock(api_enabled=configured_api_enabled())
except Exception:
logger.exception("Could not write the instance lock")
# 设置全局字体
font = app.font()
font.setFamily("Microsoft YaHei UI")
app.setFont(font)
from src.storage.migration_runner import ensure_config_schema_v2_bootstrap
ensure_config_schema_v2_bootstrap()
logger.info("Loading plugins")
t_plugins = time.perf_counter()
from src.app.plugins import load_plugins
try:
loaded = load_plugins()
if loaded.loaded_modules:
logger.info("Loaded plugins: %s", ", ".join(loaded.loaded_modules))
except Exception:
logger.exception("Failed to load plugins")
raise
logger.info("Loading plugins done in %.2fs", time.perf_counter() - t_plugins)
logger.info("Loading main window")
t_import = time.perf_counter()
from ui.windows.gui import MainWindow
logger.info("import MainWindow done in %.2fs", time.perf_counter() - t_import)
t_ctor = time.perf_counter()
window = MainWindow()
logger.info("MainWindow() done in %.2fs", time.perf_counter() - t_ctor)
single_instance_server.set_activate_handler(window._show_main_window_from_tray)
class _FirstPaintProbe(QObject):
def __init__(self, started_at: float):
super().__init__()
self._started_at = started_at
self._done = False
def eventFilter(self, obj, event):
if not self._done and event.type() == QEvent.Type.Paint:
self._done = True
logger.info(
"first paint done (%.2fs since show, %.2fs since app start)",
time.perf_counter() - self._started_at,
time.perf_counter() - t_app,
)
obj.removeEventFilter(self)
return False
t_show = time.perf_counter()
window.show()
logger.info("window.show() returned in %.2fs", time.perf_counter() - t_show)
if hasattr(window, "start_bundled_runtime_bootstrap"):
window.start_bundled_runtime_bootstrap()
paint_probe = _FirstPaintProbe(t_show)
window.installEventFilter(paint_probe)
# Keep a reference so the probe is not GC'd before first paint.
window._startup_first_paint_probe = paint_probe
t_migrate = time.perf_counter()
window.begin_startup_migration()
logger.info(
"begin_startup_migration returned in %.2fs (%.2fs since show)",
time.perf_counter() - t_migrate,
time.perf_counter() - t_show,
)
logger.info(
"entering app.exec() (%.2fs since app start, %.2fs since show)",
time.perf_counter() - t_app,
time.perf_counter() - t_show,
)
exit_code = app.exec()
try:
single_instance_server.close()
except Exception:
pass
remove_instance_lock()
logger.info("Application exiting with code %s", exit_code)
sys.exit(exit_code)