Skip to content

Commit

Permalink
add notification handle
Browse files Browse the repository at this point in the history
  • Loading branch information
ginazhouhuiwu committed Jun 13, 2024
1 parent 614c2fe commit e18b73d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
3 changes: 2 additions & 1 deletion examples/24_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ def main() -> None:
@gui_button.on_click
def _(_) -> None:
"""Reset the scene when the button is clicked."""
server.gui.add_notification(
notif = server.gui.add_notification(
title="Notification",
body="You have clicked a button!",
withCloseButton=True,
loading=False,
autoClose=2000,
)
notif.show()

@clear_button.on_click
def _(_) -> None:
Expand Down
13 changes: 7 additions & 6 deletions src/viser/_gui_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
GuiInputHandle,
GuiMarkdownHandle,
GuiModalHandle,
GuiNotificationHandle,
GuiPlotlyHandle,
GuiTabGroupHandle,
GuiUploadButtonHandle,
Expand Down Expand Up @@ -876,7 +877,7 @@ def add_notification(
withCloseButton: bool,
loading: bool,
autoClose: int | bool = False,
) -> None:
) -> GuiNotificationHandle:
"""Add a notification, which can be toggled on/off in the GUI.
Args: (https://mantine.dev/x/notifications/)
Expand All @@ -889,15 +890,15 @@ def add_notification(
Returns:
A handle that can be used to interact with the GUI element.
"""

self._websock_interface.queue_message(
_messages.NotificationMessage(
return GuiNotificationHandle(
gui_api=self,
notification=_messages.NotificationMessage(
title=title,
body=body,
autoClose=autoClose,
withCloseButton=withCloseButton,
loading=loading,
)
autoClose=autoClose,
),
)

def clear_notification(self) -> None:
Expand Down
22 changes: 20 additions & 2 deletions src/viser/_gui_handles.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@

from ._icons import svg_from_icon
from ._icons_enum import IconName
from ._messages import GuiCloseModalMessage, GuiRemoveMessage, GuiUpdateMessage, Message
from ._messages import (
GuiCloseModalMessage,
GuiRemoveMessage,
GuiUpdateMessage,
Message,
NotificationMessage,
)
from ._scene_api import _encode_image_base64
from .infra import ClientId

Expand All @@ -42,7 +48,8 @@ class GuiContainerProtocol(Protocol):


class SupportsRemoveProtocol(Protocol):
def remove(self) -> None: ...
def remove(self) -> None:
...


@dataclasses.dataclass
Expand Down Expand Up @@ -298,6 +305,17 @@ def disabled(self, disabled: bool) -> None:
assert not disabled, "Button groups cannot be disabled."


@dataclasses.dataclass
class GuiNotificationHandle:
"""Handle for a notification in our visualizer."""

gui_api: GuiApi
notification: NotificationMessage

def show(self):
self.gui_api._websock_interface.queue_message(self.notification)


@dataclasses.dataclass
class GuiDropdownHandle(GuiInputHandle[StringType], Generic[StringType]):
"""Handle for a dropdown-style GUI input in our visualizer.
Expand Down

0 comments on commit e18b73d

Please sign in to comment.