-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use scene node props pattern for notifications * Add missing handle imports * Nits
- Loading branch information
Showing
7 changed files
with
106 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,47 @@ | ||
from __future__ import annotations | ||
|
||
import dataclasses | ||
from typing import Literal | ||
from typing import TYPE_CHECKING, Any, Literal | ||
|
||
from ._gui_api import Color | ||
from ._messages import NotificationMessage, RemoveNotificationMessage | ||
from ._messages import NotificationMessage, NotificationProps, RemoveNotificationMessage | ||
from .infra._infra import WebsockClientConnection | ||
|
||
|
||
@dataclasses.dataclass | ||
class _NotificationHandleState: | ||
websock_interface: WebsockClientConnection | ||
id: str | ||
title: str | ||
body: str | ||
loading: bool | ||
with_close_button: bool | ||
auto_close: int | Literal[False] | ||
color: Color | None | ||
props: NotificationProps | ||
|
||
|
||
@dataclasses.dataclass | ||
class NotificationHandle: | ||
class NotificationHandle(NotificationProps): | ||
"""Handle for a notification in our visualizer.""" | ||
|
||
_impl: _NotificationHandleState | ||
|
||
def _sync_with_client(self, first: bool = False) -> None: | ||
m = NotificationMessage( | ||
"show" if first else "update", | ||
self._impl.id, | ||
self._impl.title, | ||
self._impl.body, | ||
self._impl.loading, | ||
self._impl.with_close_button, | ||
self._impl.auto_close, | ||
self._impl.color, | ||
) | ||
self._impl.websock_interface.queue_message(m) | ||
|
||
@property | ||
def title(self) -> str: | ||
"""Title to display on the notification.""" | ||
return self._impl.title | ||
|
||
@title.setter | ||
def title(self, title: str) -> None: | ||
if title == self._impl.title: | ||
return | ||
|
||
self._impl.title = title | ||
self._sync_with_client() | ||
|
||
@property | ||
def body(self) -> str: | ||
"""Message to display on the notification body.""" | ||
return self._impl.body | ||
|
||
@body.setter | ||
def body(self, body: str) -> None: | ||
if body == self._impl.body: | ||
return | ||
|
||
self._impl.body = body | ||
self._sync_with_client() | ||
|
||
@property | ||
def loading(self) -> bool: | ||
"""Whether the notification shows loading icon.""" | ||
return self._impl.loading | ||
|
||
@loading.setter | ||
def loading(self, loading: bool) -> None: | ||
if loading == self._impl.loading: | ||
return | ||
|
||
self._impl.loading = loading | ||
self._sync_with_client() | ||
|
||
@property | ||
def with_close_button(self) -> bool: | ||
"""Whether the notification can be manually closed.""" | ||
return self._impl.with_close_button | ||
|
||
@with_close_button.setter | ||
def with_close_button(self, with_close_button: bool) -> None: | ||
if with_close_button == self._impl.with_close_button: | ||
return | ||
|
||
self._impl.with_close_button = with_close_button | ||
self._sync_with_client() | ||
|
||
@property | ||
def auto_close(self) -> int | Literal[False]: | ||
"""Time in ms before the notification automatically closes; | ||
otherwise False such that the notification never closes on its own.""" | ||
return self._impl.auto_close | ||
|
||
@auto_close.setter | ||
def auto_close(self, auto_close: int | Literal[False]) -> None: | ||
if auto_close == self._impl.auto_close: | ||
return | ||
def __init__(self, impl: _NotificationHandleState) -> None: | ||
self._impl = impl | ||
|
||
self._impl.auto_close = auto_close | ||
self._sync_with_client() | ||
# Support property-style read/write. Similar to `_OverridableScenePropApi`. | ||
if not TYPE_CHECKING: | ||
|
||
@property | ||
def color(self) -> Color | None: | ||
"""Color of the notification.""" | ||
return self._impl.color | ||
def __setattr__(self, name: str, value: Any) -> None: | ||
if name in NotificationProps.__annotations__: | ||
setattr(self._impl.props, name, value) | ||
self._sync_with_client("update") | ||
else: | ||
return object.__setattr__(self, name, value) | ||
|
||
@color.setter | ||
def color(self, color: Color | None) -> None: | ||
if color == self._impl.color: | ||
return | ||
def __getattr__(self, name: str) -> Any: | ||
if name in NotificationProps.__annotations__: | ||
return getattr(self._impl.props, name) | ||
else: | ||
raise AttributeError( | ||
f"'{self.__class__.__name__}' object has no attribute '{name}'" | ||
) | ||
|
||
self._impl.color = color | ||
self._sync_with_client() | ||
def _sync_with_client(self, mode: Literal["show", "update"]) -> None: | ||
msg = NotificationMessage(mode, self._impl.id, self._impl.props) | ||
self._impl.websock_interface.queue_message(msg) | ||
|
||
def remove(self) -> None: | ||
self._impl.websock_interface.queue_message( | ||
RemoveNotificationMessage(self._impl.id) | ||
) | ||
msg = RemoveNotificationMessage(self._impl.id) | ||
self._impl.websock_interface.queue_message(msg) |
Oops, something went wrong.