Skip to content

Commit

Permalink
Update circuits widget sizing behavior
Browse files Browse the repository at this point in the history
This change fixes a few quirks of the `Circuit` widget, specifically:

 - if the width of the widget is greater than the width of the cell output area, scroll bars are added, and
 - setting a custom zoom level by changing the value in the zoom control disables the resize handling so that VS Code doesn't incorrectly trigger a recalculation of the default zoom and undo the manual change.

Confirmed with manual testing that window resizing in VS Code circuit display is still hadled properly.
  • Loading branch information
swernli committed Sep 16, 2024
1 parent 7b532fe commit c62e18a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
12 changes: 11 additions & 1 deletion npm/qsharp/ux/circuit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function ZoomableCircuit(props: { circuit: qviz.Circuit }) {
<div>
<div>
{rendering ? null : (
<ZoomControl zoom={zoomLevel} onChange={setZoomLevel} />
<ZoomControl zoom={zoomLevel} onChange={setCustomZoomLevel} />
)}
</div>
<div>
Expand All @@ -103,6 +103,16 @@ function ZoomableCircuit(props: { circuit: qviz.Circuit }) {
}
}

/**
* Set a custom zoom level. This will disable the automatic resizing by removing the resize event listener,
* which will prevent the zoom level from being recalculated immediately based on the new window width.
* Resize events will still get handled the next time the window is redrawn or other properies are updated.
*/
function setCustomZoomLevel(zoom: number) {
window.removeEventListener("resize", onResize);
setZoomLevel(zoom);
}

/**
* Update the width of the SVG element based on the current zoom level.
*/
Expand Down
1 change: 1 addition & 0 deletions widgets/src/qsharp_widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,4 @@ class Circuit(anywidget.AnyWidget):
def __init__(self, circuit):
super().__init__()
self.circuit_json = circuit.json()
self.layout.overflow = "visible scroll"

0 comments on commit c62e18a

Please sign in to comment.