Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GUI reset helper + fix init race condition #241

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/viser/_gui_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,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 @@ -462,6 +462,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 @@ -934,6 +941,7 @@ export type Message =
| SetSceneNodeClickableMessage
| SceneNodeClickMessage
| ResetSceneMessage
| ResetGuiMessage
| GuiAddFolderMessage
| GuiAddMarkdownMessage
| GuiAddPlotlyMessage
Expand Down
Loading