Skip to content

Commit

Permalink
refactor(NotificationWidget): remove all the logic and make it solely…
Browse files Browse the repository at this point in the history
… a view
  • Loading branch information
sassanh committed May 5, 2024
1 parent b17e16d commit c788b01
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 87 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.11.4

- refactor(NotificationWidget): remove all the logic and make it solely a view

## Version 0.11.3

- feat(Menu): action items can now return not only the application class, but also
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.11.3"
version = "0.11.4"
description = "GUI sdk for Ubo Pod"
authors = ["Sassan Haradji <[email protected]>"]
license = "Apache-2.0"
Expand Down
85 changes: 2 additions & 83 deletions ubo_gui/notification/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,89 +3,22 @@
from __future__ import annotations

import pathlib
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 (
AliasProperty,
BooleanProperty,
ColorProperty,
StringProperty,
)

from ubo_gui.constants import DANGER_COLOR, INFO_COLOR
from ubo_gui.menu.types import ActionItem
from ubo_gui.page import PAGE_MAX_ITEMS, PageWidget

if TYPE_CHECKING:
from ubo_gui.menu.types import Item

from kivy.properties import ColorProperty, StringProperty

class NotificationAction(Immutable):
"""A notification action."""

title: str
callback: Callable
from ubo_gui.page import PageWidget


class NotificationWidget(PageWidget):
"""renders a notification."""

__events__ = ('on_dismiss', 'on_info')

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

def dispatch_info() -> None:
self.dispatch('on_info')

return ActionItem(
icon='󰋼',
action=dispatch_info,
label='',
is_short=True,
background_color=INFO_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."""

def dispatch_dismiss() -> None:
self.dispatch('on_dismiss')

self.dismiss_action = ActionItem(
icon='󰆴',
action=dispatch_dismiss,
label='',
is_short=True,
background_color=DANGER_COLOR,
)
super().__init__(
*args,
items=[],
**kwargs,
)

def go_down(self: NotificationWidget) -> None:
"""Scroll down the notification list."""
self.ids.slider.animated_value -= dp(50)
Expand All @@ -94,20 +27,6 @@ def go_up(self: NotificationWidget) -> None:
"""Scroll up the notification list."""
self.ids.slider.animated_value += dp(50)

def get_item(self: NotificationWidget, index: int) -> Item | None:
"""Get the page item at the given index."""
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
10 changes: 7 additions & 3 deletions ubo_gui/notification/notification_widget.kv
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
spacing: dp(UBO_GUI_MENU_ITEM_GAP)

ItemWidget:
item: root.info_action
item: root.items[0]
size_hint: 1, None

ItemWidget:
item: root.dismiss_action
item: root.items[1]
size_hint: 1, None

ItemWidget:
item: root.items[2]
size_hint: 1, None

StencilView:
Expand Down Expand Up @@ -72,7 +76,7 @@

AnchorLayout:
size_hint: (None, 1) if scrollable_widget.height - container.height else (None, None)
width: dp(UBO_GUI_SHORT_WIDTH - 2)
width: dp(UBO_GUI_SHORT_WIDTH)

AnimatedSlider:
id: slider
Expand Down

0 comments on commit c788b01

Please sign in to comment.