Skip to content

Commit

Permalink
Add GUI reset helper + fix init race condition (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi authored Jul 16, 2024
1 parent 2889a09 commit 2c7303e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/viser/_gui_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ def _set_container_id(self, container_id: str) -> None:
"""Set container ID associated with the current thread."""
self._target_container_from_thread_id[threading.get_ident()] = container_id

def reset(self) -> None:
"""Reset the GUI."""
self._websock_interface.queue_message(_messages.ResetGuiMessage())

def set_panel_label(self, label: str | None) -> None:
"""Set the main label that appears in the GUI panel.
Expand Down
5 changes: 5 additions & 0 deletions src/viser/_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,11 @@ class ResetSceneMessage(Message):
"""Reset scene."""


@dataclasses.dataclass
class ResetGuiMessage(Message):
"""Reset GUI."""


@tag_class("GuiAddComponentMessage")
@dataclasses.dataclass
class GuiAddFolderMessage(Message):
Expand Down
1 change: 1 addition & 0 deletions src/viser/_viser.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ def _(conn: infra.WebsockClientConnection) -> None:
self.request_share_url()

self.scene.reset()
self.gui.reset()
self.gui.set_panel_label(label)

def get_host(self) -> str:
Expand Down
5 changes: 5 additions & 0 deletions src/viser/client/src/ControlPanel/GuiState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,15 @@ export function useGuiState(initialServer: string) {
}),
resetGui: () =>
set((state) => {
// This feels brittle, could be cleaned up...
state.theme = cleanGuiState.theme;
state.label = cleanGuiState.label;
state.shareUrl = null;
state.guiIdSetFromContainerId = {};
state.modals = [];
state.guiOrderFromId = {};
state.guiConfigFromId = {};
state.uploadsInProgress = {};
}),
updateUploadState: (state) =>
set((globalState) => {
Expand Down
6 changes: 6 additions & 0 deletions src/viser/client/src/MessageHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function useMessageHandler() {
const removeSceneNode = viewer.useSceneTree((state) => state.removeSceneNode);
const resetScene = viewer.useSceneTree((state) => state.resetScene);
const addSceneNode = viewer.useSceneTree((state) => state.addSceneNode);
const resetGui = viewer.useGui((state) => state.resetGui);
const setTheme = viewer.useGui((state) => state.setTheme);
const setShareUrl = viewer.useGui((state) => state.setShareUrl);
const addGui = viewer.useGui((state) => state.addGui);
Expand Down Expand Up @@ -892,6 +893,11 @@ function useMessageHandler() {
viewer.backgroundMaterialRef.current!.uniforms.enabled.value = false;
return;
}
// Reset the GUI state.
case "ResetGuiMessage": {
resetGui();
return;
}
// Update props of a GUI component
case "GuiUpdateMessage": {
updateGuiProps(message.id, message.updates);
Expand Down
8 changes: 8 additions & 0 deletions src/viser/client/src/WebsocketMessages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,13 @@ export interface SceneNodeClickMessage {
export interface ResetSceneMessage {
type: "ResetSceneMessage";
}
/** Reset GUI.
*
* (automatically generated)
*/
export interface ResetGuiMessage {
type: "ResetGuiMessage";
}
/** GuiAddFolderMessage(order: 'float', id: 'str', label: 'str', container_id: 'str', expand_by_default: 'bool', visible: 'bool')
*
* (automatically generated)
Expand Down Expand Up @@ -963,6 +970,7 @@ export type Message =
| SetSceneNodeClickableMessage
| SceneNodeClickMessage
| ResetSceneMessage
| ResetGuiMessage
| GuiAddFolderMessage
| GuiAddMarkdownMessage
| GuiAddProgressBarMessage
Expand Down

0 comments on commit 2c7303e

Please sign in to comment.