forked from Support/sp-hydra-veil-gui
66 lines
2.6 KiB
Python
Executable file
66 lines
2.6 KiB
Python
Executable file
import importlib
|
|
import time
|
|
import traceback
|
|
|
|
from gui.v2.infrastructure.page_registry import PAGE_REGISTRY
|
|
|
|
|
|
class Navigator:
|
|
def __init__(self, page_stack, custom_window):
|
|
self.page_stack = page_stack
|
|
self.custom_window = custom_window
|
|
self._instances = {}
|
|
|
|
def navigate(self, name):
|
|
page = self._instances.get(name)
|
|
if page is None:
|
|
if name not in PAGE_REGISTRY:
|
|
self._warn_not_migrated(name)
|
|
return
|
|
module_path, class_name = PAGE_REGISTRY[name]
|
|
module = importlib.import_module(module_path)
|
|
cls = getattr(module, class_name)
|
|
page = cls(self.page_stack, self.custom_window)
|
|
self.page_stack.addWidget(page)
|
|
self._instances[name] = page
|
|
self.page_stack.setCurrentIndex(self.page_stack.indexOf(page))
|
|
|
|
def preload(self, name):
|
|
if name in self._instances:
|
|
print(f"[preload] {name:30s} SKIP (already cached)", flush=True)
|
|
return False
|
|
if name not in PAGE_REGISTRY:
|
|
print(f"[preload] {name:30s} SKIP (not in registry)", flush=True)
|
|
return False
|
|
module_path, class_name = PAGE_REGISTRY[name]
|
|
print(f"[preload] {name:30s} LOAD {module_path}.{class_name}", flush=True)
|
|
t0 = time.perf_counter()
|
|
try:
|
|
module = importlib.import_module(module_path)
|
|
cls = getattr(module, class_name)
|
|
page = cls(self.page_stack, self.custom_window)
|
|
self.page_stack.addWidget(page)
|
|
self._instances[name] = page
|
|
dt_ms = (time.perf_counter() - t0) * 1000.0
|
|
print(f"[preload] {name:30s} OK ({dt_ms:6.1f} ms, "
|
|
f"cached={len(self._instances)}, stack_size={self.page_stack.count()})",
|
|
flush=True)
|
|
return True
|
|
except Exception as e:
|
|
dt_ms = (time.perf_counter() - t0) * 1000.0
|
|
print(f"[preload] {name:30s} FAIL ({dt_ms:6.1f} ms) "
|
|
f"{type(e).__name__}: {e}", flush=True)
|
|
traceback.print_exc()
|
|
from core.errors.logger import logger as core_logger
|
|
core_logger.warning(f"Background preload failed for '{name}': "
|
|
f"{type(e).__name__}: {e}")
|
|
return False
|
|
|
|
def get_cached(self, name):
|
|
return self._instances.get(name)
|
|
|
|
def _warn_not_migrated(self, name):
|
|
try:
|
|
self.custom_window.update_status(f"Page '{name}' not migrated yet.")
|
|
except Exception:
|
|
pass
|