From d8b16c38ea97352d89588a359c325f2088181197 Mon Sep 17 00:00:00 2001 From: Sassan Haradji Date: Mon, 15 Apr 2024 04:02:01 +0400 Subject: [PATCH] feat: info action for the `NotificationWidget` --- CHANGELOG.md | 4 ++ pyproject.toml | 2 +- ubo_gui/notification/__init__.py | 59 +++++++++++++++------ ubo_gui/notification/notification_widget.kv | 9 +++- ubo_gui/prompt/__init__.py | 4 +- 5 files changed, 56 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8eb431..200bd0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Version 0.10.6 + +- feat: info action for the `NotificationWidget` + ## Version 0.10.5 - refactor: enable `markup` for labels diff --git a/pyproject.toml b/pyproject.toml index bff477d..a7afd7c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ubo-gui" -version = "0.10.5" +version = "0.10.6" description = "GUI sdk for Ubo Pod" authors = ["Sassan Haradji "] license = "Apache-2.0" diff --git a/ubo_gui/notification/__init__.py b/ubo_gui/notification/__init__.py index 1ed380e..f119c57 100644 --- a/ubo_gui/notification/__init__.py +++ b/ubo_gui/notification/__init__.py @@ -3,13 +3,17 @@ from __future__ import annotations import pathlib -import warnings from typing import TYPE_CHECKING, Callable from immutable import Immutable from kivy.lang.builder import Builder from kivy.metrics import dp -from kivy.properties import ColorProperty, ObjectProperty, StringProperty +from kivy.properties import ( + AliasProperty, + BooleanProperty, + ColorProperty, + StringProperty, +) from ubo_gui.constants import DANGER_COLOR from ubo_gui.menu.types import ActionItem @@ -29,31 +33,48 @@ class NotificationAction(Immutable): class NotificationWidget(PageWidget): """renders a notification.""" - __events__ = ('on_dismiss',) + __events__ = ('on_dismiss', 'on_info') - notification = ObjectProperty() notification_title: str = StringProperty() content: str = StringProperty() + has_extra_information: bool = BooleanProperty(defaultvalue=False) icon: str = StringProperty() color = ColorProperty() + def get_info_action(self: NotificationWidget) -> ActionItem | None: + """Return the info action if `info` is available.""" + if not self.has_extra_information: + return None + return ActionItem( + icon='󰋼', + action=lambda: self.dispatch('on_info'), + label='', + is_short=True, + background_color=DANGER_COLOR, + ) + + info_action: ActionItem = AliasProperty( + getter=get_info_action, + bind=['has_extra_information'], + cache=True, + ) + def __init__( self: NotificationWidget, *args: object, **kwargs: object, ) -> None: """Create a new `NotificationWidget` object.""" + self.dismiss_action = ActionItem( + icon='󰆴', + action=lambda: self.dispatch('on_dismiss') and None, + label='', + is_short=True, + background_color=DANGER_COLOR, + ) super().__init__( *args, - items=[ - ActionItem( - icon='󰆴', - action=lambda: self.dispatch('on_dismiss') and None, - label='', - is_short=True, - background_color=DANGER_COLOR, - ), - ], + items=[], **kwargs, ) @@ -67,14 +88,18 @@ def go_up(self: NotificationWidget) -> None: def get_item(self: NotificationWidget, index: int) -> Item | None: """Get the page item at the given index.""" - if index != PAGE_MAX_ITEMS - 1: - warnings.warn('index must be 2', ResourceWarning, stacklevel=1) - return None - return self.items[index - 2] + if index == PAGE_MAX_ITEMS - 2: + return self.info_action + if index == PAGE_MAX_ITEMS - 1: + return self.dismiss_action + return None def on_dismiss(self: PageWidget) -> None: """Signal when the notification is dismissed.""" + def on_info(self: PageWidget) -> None: + """Signal when the info action is selected.""" + Builder.load_file( pathlib.Path(__file__) diff --git a/ubo_gui/notification/notification_widget.kv b/ubo_gui/notification/notification_widget.kv index 8357465..43385ab 100644 --- a/ubo_gui/notification/notification_widget.kv +++ b/ubo_gui/notification/notification_widget.kv @@ -6,12 +6,17 @@ spacing: dp(4) BoxLayout: - orientation: 'vertical' size_hint: None, 1 width: dp(UBO_GUI_SHORT_WIDTH) + orientation: 'vertical' + spacing: dp(7) + + ItemWidget: + item: root.info_action + size_hint: 1, None ItemWidget: - item: root.items[0] if len(root.items) > 0 else None + item: root.dismiss_action size_hint: 1, None Widget: diff --git a/ubo_gui/prompt/__init__.py b/ubo_gui/prompt/__init__.py index 7f99d2f..9d6aac2 100644 --- a/ubo_gui/prompt/__init__.py +++ b/ubo_gui/prompt/__init__.py @@ -52,7 +52,7 @@ class PromptWidget(PageWidget, ABC, metaclass=PromptWidgetMetaClass): def get_first_item(self: PromptWidget) -> ActionItem | None: """Return the first item of the prompt.""" - if self.first_option_label is None: + if not self.first_option_label: return None return ActionItem( label=self.first_option_label, @@ -65,7 +65,7 @@ def get_first_item(self: PromptWidget) -> ActionItem | None: def get_second_item(self: PromptWidget) -> ActionItem | None: """Return the second item of the prompt.""" - if self.second_option_label is None: + if not self.second_option_label: return None return ActionItem( label=self.second_option_label,