Skip to content

Commit 587878b

Browse files
committed
Fix zoom update in Plots view
1 parent 34f518b commit 587878b

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/vs/workbench/contrib/positronPlots/browser/components/plotsContainer.tsx

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { WebviewPlotThumbnail } from './webviewPlotThumbnail.js';
2121
import { usePositronPlotsContext } from '../positronPlotsContext.js';
2222
import { WebviewPlotClient } from '../webviewPlotClient.js';
2323
import { PlotClientInstance } from '../../../../services/languageRuntime/common/languageRuntimePlotClient.js';
24-
import { DarkFilter, IPositronPlotClient, IPositronPlotsService, PlotRenderFormat } from '../../../../services/positronPlots/common/positronPlots.js';
24+
import { DarkFilter, IPositronPlotClient, IPositronPlotsService, isZoomablePlotClient, PlotRenderFormat, ZoomLevel } from '../../../../services/positronPlots/common/positronPlots.js';
2525
import { StaticPlotClient } from '../../../../services/positronPlots/common/staticPlotClient.js';
2626
import { PlotSizingPolicyIntrinsic } from '../../../../services/positronPlots/common/sizingPolicyIntrinsic.js';
2727
import { PlotSizingPolicyAuto } from '../../../../services/positronPlots/common/sizingPolicyAuto.js';
@@ -58,6 +58,7 @@ export const PlotsContainer = (props: PlotContainerProps) => {
5858
const positronPlotsContext = usePositronPlotsContext();
5959
const plotHistoryRef = React.createRef<HTMLDivElement>();
6060
const containerRef = useRef<HTMLDivElement>(undefined!);
61+
const [zoom, setZoom] = React.useState<ZoomLevel>(ZoomLevel.Fit);
6162

6263
// We generally prefer showing the plot history on the bottom (making the
6364
// plot wider), but if the plot container is too wide, we show it on the
@@ -164,6 +165,29 @@ export const PlotsContainer = (props: PlotContainerProps) => {
164165
};
165166
}, [plotWidth, plotHeight, props.positronPlotsService]);
166167

168+
useEffect(() => {
169+
// Create the disposable store for cleanup.
170+
const disposableStore = new DisposableStore();
171+
172+
// Get the current plot instance using the selected instance ID from the
173+
// PositronPlotsContext.
174+
const currentPlotInstance = positronPlotsContext.positronPlotInstances.find(
175+
(plotInstance) => plotInstance.id === positronPlotsContext.selectedInstanceId
176+
);
177+
if (currentPlotInstance && isZoomablePlotClient(currentPlotInstance)) {
178+
// Listen to the plot instance for zoom level changes.
179+
disposableStore.add(currentPlotInstance.onDidChangeZoomLevel((zoomLevel) => {
180+
setZoom(zoomLevel);
181+
}));
182+
// Set the initial zoom level.
183+
setZoom(currentPlotInstance.zoomLevel);
184+
}
185+
return () => {
186+
// Dispose of the disposable store when the component unmounts.
187+
disposableStore.dispose();
188+
}
189+
}, [positronPlotsContext.positronPlotInstances, positronPlotsContext.selectedInstanceId]);
190+
167191
/**
168192
* Renders either a DynamicPlotInstance (resizable plot), a
169193
* StaticPlotInstance (static plot image), or a WebviewPlotInstance
@@ -179,12 +203,12 @@ export const PlotsContainer = (props: PlotContainerProps) => {
179203
height={plotHeight}
180204
plotClient={plotInstance}
181205
width={plotWidth}
182-
zoom={plotInstance.zoomLevel} />;
206+
zoom={zoom} />;
183207
} else if (plotInstance instanceof StaticPlotClient) {
184208
return <StaticPlotInstance
185209
key={plotInstance.id}
186210
plotClient={plotInstance}
187-
zoom={plotInstance.zoomLevel} />;
211+
zoom={zoom} />;
188212
} else if (plotInstance instanceof WebviewPlotClient) {
189213
return <WebviewPlotInstance
190214
key={plotInstance.id}

0 commit comments

Comments
 (0)