Skip to content

Commit

Permalink
feat: info action for the NotificationWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
sassanh committed Apr 15, 2024
1 parent c1eef89 commit d8b16c3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Version 0.10.6

- feat: info action for the `NotificationWidget`

## Version 0.10.5

- refactor: enable `markup` for labels
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "Apache-2.0"
Expand Down
59 changes: 42 additions & 17 deletions ubo_gui/notification/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)

Expand All @@ -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__)
Expand Down
9 changes: 7 additions & 2 deletions ubo_gui/notification/notification_widget.kv
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions ubo_gui/prompt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit d8b16c3

Please sign in to comment.