Skip to content

Commit 5a292e3

Browse files
committed
fix(sync): Implement webview state restoration on readiness
- Add a new command 'webviewReady' to notify when the webview is ready to receive state updates. - Implement the `restoreState` method in SyncManager to handle state restoration for webviews, enhancing synchronization across different visualizations. - Update draw functions to log and restore state upon receiving the 'webviewReady' command, improving user experience during panel interactions.
1 parent f996642 commit 5a292e3

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/matImage/matProvider.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,9 @@ export async function drawMatImage(
304304
SyncManager.syncView(panelName, message.state);
305305
} else if (message.command === 'pixelHighlight') {
306306
SyncManager.syncPixelHighlight(panelName, message.pixelX, message.pixelY);
307+
} else if (message.command === 'webviewReady') {
308+
console.log(`[DEBUG-TRACE] webviewReady received for ${panelName}, restoring state`);
309+
SyncManager.restoreState(panelName);
307310
} else if (message.command === 'reload') {
308311
const reloadStartTime = Date.now();
309312
console.log(`[DEBUG-TRACE] reload message received for ${variableName} at ${reloadStartTime}`);
@@ -768,6 +771,9 @@ export async function drawMatxImage(
768771
SyncManager.syncView(panelName, message.state);
769772
} else if (message.command === 'pixelHighlight') {
770773
SyncManager.syncPixelHighlight(panelName, message.pixelX, message.pixelY);
774+
} else if (message.command === 'webviewReady') {
775+
console.log(`[DEBUG-TRACE] webviewReady received for Matx ${panelName}, restoring state`);
776+
SyncManager.restoreState(panelName);
771777
} else if (message.command === 'reload') {
772778
// Check if debug session is still active before reloading
773779
const currentSession = vscode.debug.activeDebugSession;
@@ -966,6 +972,9 @@ export async function draw2DStdArrayImage(
966972
SyncManager.syncView(panelName, message.state);
967973
} else if (message.command === 'pixelHighlight') {
968974
SyncManager.syncPixelHighlight(panelName, message.pixelX, message.pixelY);
975+
} else if (message.command === 'webviewReady') {
976+
console.log(`[DEBUG-TRACE] webviewReady received for 2D array ${panelName}, restoring state`);
977+
SyncManager.restoreState(panelName);
969978
} else if (message.command === 'reload') {
970979
// Check if debug session is still active before reloading
971980
const currentSession = vscode.debug.activeDebugSession;
@@ -1152,6 +1161,9 @@ export async function draw3DArrayImage(
11521161
SyncManager.syncView(panelName, message.state);
11531162
} else if (message.command === 'pixelHighlight') {
11541163
SyncManager.syncPixelHighlight(panelName, message.pixelX, message.pixelY);
1164+
} else if (message.command === 'webviewReady') {
1165+
console.log(`[DEBUG-TRACE] webviewReady received for 3D array ${panelName}, restoring state`);
1166+
SyncManager.restoreState(panelName);
11551167
} else if (message.command === 'reload') {
11561168
const reloadStartTime = Date.now();
11571169
console.log(`[DEBUG-TRACE] 3D array reload message received for ${panelName} at ${reloadStartTime}`);

src/matImage/matWebview.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,9 @@ export function getWebviewContentForMat(
592592
isInitialized = true;
593593
requestRender();
594594
updateJetColorbarVisibility();
595+
596+
// Notify extension that webview is ready to receive sync state
597+
vscode.postMessage({ command: 'webviewReady' });
595598
}
596599
597600
function clampByte(v) {

src/utils/syncManager.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,45 @@ export class SyncManager {
208208
}
209209
}
210210

211+
/**
212+
* Restore the synchronized state for a specific variable's webview.
213+
* This is called when a webview signals it's ready to receive state updates.
214+
* @param variableName The variable name to restore state for
215+
*/
216+
static restoreState(variableName: string) {
217+
const panel = this.panels.get(variableName);
218+
if (!panel) {
219+
console.log(`[SyncManager] restoreState: panel for ${variableName} not found`);
220+
return;
221+
}
222+
223+
const groupId = this.variableToGroup.get(variableName);
224+
if (groupId) {
225+
const state = this.groupStates.get(groupId);
226+
if (state) {
227+
console.log(`[SyncManager] restoreState: sending group state to ${variableName}`);
228+
panel.webview.postMessage({
229+
command: 'setView',
230+
state: state
231+
});
232+
this.variableHasSynced.add(variableName);
233+
return;
234+
}
235+
}
236+
237+
// Not in a group, but might have a saved state from previous panel
238+
const savedState = this.variableStates.get(variableName);
239+
if (savedState) {
240+
console.log(`[SyncManager] restoreState: sending saved state to ${variableName}`);
241+
panel.webview.postMessage({
242+
command: 'setView',
243+
state: savedState
244+
});
245+
} else {
246+
console.log(`[SyncManager] restoreState: no state found for ${variableName}`);
247+
}
248+
}
249+
211250
static getGroupIndex(varName: string): number | undefined {
212251
const groupId = this.variableToGroup.get(varName);
213252
return groupId ? this.groupToIndex.get(groupId) : undefined;

0 commit comments

Comments
 (0)