Implement pipeline hot-rebuild with state preservation

- Add save_state/restore_state methods to CameraStage
- Add save_state/restore_state methods to DisplayStage
- Extend Pipeline._copy_stage_state() to preserve camera/display state
- Add save_state/restore_state methods to UIPanel for UI state preservation
- Update pipeline_runner to preserve UI state across preset changes

Camera state preserved:
- Position (x, y)
- Mode (feed, scroll, horizontal, etc.)
- Speed, zoom, canvas dimensions
- Internal timing state

Display state preserved:
- Initialization status
- Dimensions
- Reuse flag for display reinitialization

UI Panel state preserved:
- Stage enabled/disabled status
- Parameter values
- Selected stage and focused parameter
- Scroll position

This enables manual/event-driven rebuilds when inlet-outlet connections change,
while preserving all relevant state across pipeline mutations.
This commit is contained in:
2026-03-18 23:30:24 -07:00
parent e684666774
commit 14d622f0d6
5 changed files with 164 additions and 1 deletions

View File

@@ -13,6 +13,39 @@ class DisplayStage(Stage):
self.name = name
self.category = "display"
self.optional = False
self._initialized = False
self._init_width = 80
self._init_height = 24
def save_state(self) -> dict[str, Any]:
"""Save display state for restoration after pipeline rebuild.
Returns:
Dictionary containing display state that can be restored
"""
return {
"initialized": self._initialized,
"init_width": self._init_width,
"init_height": self._init_height,
"width": getattr(self._display, "width", 80),
"height": getattr(self._display, "height", 24),
}
def restore_state(self, state: dict[str, Any]) -> None:
"""Restore display state from saved state.
Args:
state: Dictionary containing display state from save_state()
"""
self._initialized = state.get("initialized", False)
self._init_width = state.get("init_width", 80)
self._init_height = state.get("init_height", 24)
# Restore display dimensions if the display supports it
if hasattr(self._display, "width"):
self._display.width = state.get("width", 80)
if hasattr(self._display, "height"):
self._display.height = state.get("height", 24)
@property
def capabilities(self) -> set[str]:
@@ -37,7 +70,17 @@ class DisplayStage(Stage):
def init(self, ctx: PipelineContext) -> bool:
w = ctx.params.viewport_width if ctx.params else 80
h = ctx.params.viewport_height if ctx.params else 24
result = self._display.init(w, h, reuse=False)
# Try to reuse display if already initialized
reuse = self._initialized
result = self._display.init(w, h, reuse=reuse)
# Update initialization state
if result is not False:
self._initialized = True
self._init_width = w
self._init_height = h
return result is not False
def process(self, data: Any, ctx: PipelineContext) -> Any: