Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Messaging toast refactor #625

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/seedsigner/gui/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class GUIConstants:

BACKGROUND_COLOR = "black"
WARNING_COLOR = "#FFD60A"
DIRE_WARNING_COLOR = "#FF453A"
DIRE_WARNING_COLOR = "#FF5700"
SUCCESS_COLOR = "#30D158"
INFO_COLOR = "#0084FF"
ERROR_COLOR = "#AD2013"
ACCENT_COLOR = "#FF9F0A"
BITCOIN_ORANGE = "#FF9416"
TESTNET_COLOR = "#00F100"
Expand Down
128 changes: 113 additions & 15 deletions src/seedsigner/gui/toast.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class ToastOverlay(BaseComponent):
icon_name: str = None
color: str = GUIConstants.NOTIFICATION_COLOR
font_color: str = GUIConstants.NOTIFICATION_COLOR
label_text: str = None
height: int = GUIConstants.ICON_TOAST_FONT_SIZE + 2*GUIConstants.EDGE_PADDING
font_size: int = 19
Expand All @@ -19,33 +20,42 @@ class ToastOverlay(BaseComponent):
def __post_init__(self):
super().__post_init__()

self.icon = Icon(
image_draw=self.image_draw,
canvas=self.canvas,
screen_x=self.outline_thickness + 2*GUIConstants.EDGE_PADDING, # Push the icon further from the left edge than strictly necessary
icon_name=self.icon_name,
icon_size=GUIConstants.ICON_TOAST_FONT_SIZE,
icon_color=self.color
)
self.icon.screen_y = self.canvas_height - self.height + int((self.height - self.icon.height)/2)

icon_delta_x = 0
if self.icon_name:
self.icon = Icon(
image_draw=self.image_draw,
canvas=self.canvas,
screen_x=self.outline_thickness + GUIConstants.EDGE_PADDING, # Push the icon further from the left edge than strictly necessary
icon_name=self.icon_name,
icon_size=GUIConstants.ICON_TOAST_FONT_SIZE,
icon_color=self.color
)
icon_delta_x = self.icon.width + self.icon.screen_x

self.label = TextArea(
image_draw=self.image_draw,
canvas=self.canvas,
text=self.label_text,
font_size=self.font_size,
font_color=self.color,
font_color=self.font_color,
edge_padding=0,
is_text_centered=False,
auto_line_break=True,
width=self.canvas_width - self.icon.screen_x - self.icon.width - GUIConstants.COMPONENT_PADDING - self.outline_thickness,
screen_x=self.icon.screen_x + self.icon.width + GUIConstants.COMPONENT_PADDING,
width=self.canvas_width - icon_delta_x - 2 * GUIConstants.COMPONENT_PADDING - 2 * self.outline_thickness,
screen_x=icon_delta_x + GUIConstants.COMPONENT_PADDING,
allow_text_overflow=False,
height_ignores_below_baseline=True,
)

if self.label.height > GUIConstants.ICON_FONT_SIZE:
self.height = self.label.height + GUIConstants.EDGE_PADDING * 2

# Vertically center the message within the toast (for single- or multi-line
# Vertically center the message and icon within the toast (for single- or multi-line
# messages).
self.label.screen_y = self.canvas_height - self.height + self.outline_thickness + int((self.height - 2*self.outline_thickness - self.label.height)/2)

if self.icon_name:
self.icon.screen_y = self.canvas_height - self.height + int((self.height - self.icon.height)/2)


def render(self):
Expand All @@ -59,7 +69,9 @@ def render(self):
)

# Draw the toast visual elements
self.icon.render()
if self.icon_name:
self.icon.render()

self.label.render()

self.renderer.show_image()
Expand Down Expand Up @@ -123,6 +135,7 @@ def toggle_renderer_lock(self):
def run(self):
logger.info(f"{self.__class__.__name__}: started")
start = time.time()
time.sleep(0.2)
while time.time() - start < self.activation_delay:
if self.hw_inputs.has_any_input():
# User has pressed a button, cancel the toast
Expand Down Expand Up @@ -230,3 +243,88 @@ def instantiate_toast(self) -> ToastOverlay:
icon_name=SeedSignerIconConstants.MICROSD,
label_text=self.message,
)


"""****************************************************************************
Messaging toasts
****************************************************************************"""


class DefaultToast(BaseToastOverlayManagerThread):
def __init__(self, label_text="This is a notification toast", activation_delay=0, duration=3):
# Note: activation_delay is configurable so the screenshot generator can get the
# toast to immediately render.
self.label_text = label_text
super().__init__(
activation_delay=activation_delay, # seconds
duration=duration # seconds
)


def instantiate_toast(self) -> ToastOverlay:
return ToastOverlay(
label_text=self.label_text,
color=GUIConstants.BODY_FONT_COLOR,
font_color=GUIConstants.BODY_FONT_COLOR,
font_size=GUIConstants.BODY_FONT_SIZE,
)



class InfoToast(DefaultToast):
def instantiate_toast(self) -> ToastOverlay:
return ToastOverlay(
icon_name=SeedSignerIconConstants.WARNING,
label_text=self.label_text,
color=GUIConstants.INFO_COLOR,
font_color=GUIConstants.BODY_FONT_COLOR,
font_size=GUIConstants.BODY_FONT_SIZE,
)



class SuccessToast(DefaultToast):
def instantiate_toast(self) -> ToastOverlay:
return ToastOverlay(
icon_name=SeedSignerIconConstants.SUCCESS,
label_text=self.label_text,
color=GUIConstants.SUCCESS_COLOR,
font_color=GUIConstants.BODY_FONT_COLOR,
font_size=GUIConstants.BODY_FONT_SIZE,
)



class WarningToast(DefaultToast):
def instantiate_toast(self) -> ToastOverlay:
return ToastOverlay(
icon_name=SeedSignerIconConstants.WARNING,
label_text=self.label_text,
color=GUIConstants.WARNING_COLOR,
font_color=GUIConstants.BODY_FONT_COLOR,
font_size=GUIConstants.BODY_FONT_SIZE,
)



class DireWarningToast(DefaultToast):
def instantiate_toast(self) -> ToastOverlay:
return ToastOverlay(
icon_name=SeedSignerIconConstants.WARNING,
label_text=self.label_text,
color=GUIConstants.DIRE_WARNING_COLOR,
font_color=GUIConstants.BODY_FONT_COLOR,
font_size=GUIConstants.BODY_FONT_SIZE,
)



class ErrorToast(DefaultToast):
def instantiate_toast(self) -> ToastOverlay:
return ToastOverlay(
icon_name=SeedSignerIconConstants.ERROR,
label_text=self.label_text,
color=GUIConstants.ERROR_COLOR,
font_color=GUIConstants.BODY_FONT_COLOR,
font_size=GUIConstants.BODY_FONT_SIZE,
)
7 changes: 7 additions & 0 deletions tests/screenshot_generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from seedsigner.controller import Controller
from seedsigner.gui.renderer import Renderer
from seedsigner.gui.toast import BaseToastOverlayManagerThread, RemoveSDCardToastManagerThread, SDCardStateChangeToastManagerThread
from seedsigner.gui.toast import DefaultToast, InfoToast, SuccessToast, WarningToast, ErrorToast, DireWarningToast
from seedsigner.hardware.microsd import MicroSD
from seedsigner.models.decode_qr import DecodeQR
from seedsigner.models.qr_type import QRType
Expand Down Expand Up @@ -165,6 +166,12 @@ def add_op_return_to_psbt(psbt: PSBT, raw_payload_data: bytes):
(MainMenuView, {}, 'MainMenuView_SDCardStateChangeToast_removed', SDCardStateChangeToastManagerThread(action=MicroSD.ACTION__REMOVED)),
(MainMenuView, {}, 'MainMenuView_SDCardStateChangeToast_inserted', SDCardStateChangeToastManagerThread(action=MicroSD.ACTION__INSERTED)),
(MainMenuView, {}, 'MainMenuView_RemoveSDCardToast', RemoveSDCardToastManagerThread(activation_delay=0)),
(MainMenuView, {}, 'MainMenuView_DefaultToast', DefaultToast("This is a default text toast!")),
(MainMenuView, {}, 'MainMenuView_InfoToast', InfoToast("This is an info toast!")),
(MainMenuView, {}, 'MainMenuView_SuccessToast', SuccessToast("This is a success toast!")),
(MainMenuView, {}, 'MainMenuView_WarningToast', WarningToast("This is a warning toast!")),
(MainMenuView, {}, 'MainMenuView_DireWarningToast', DireWarningToast("This is a dire warning toast!")),
(MainMenuView, {}, 'MainMenuView_ErrorToast', ErrorToast("This is an error toast!")),
PowerOptionsView,
RestartView,
PowerOffView,
Expand Down
Loading