Skip to content

Commit

Permalink
Toast refactor - Messaging
Browse files Browse the repository at this point in the history
Fixes / required changes:
* Toast refactor as of SeedSigner#606 guidelines
    - Add toast messaging categories
    - General sizing refactor
* Unbind icon color from text color
* Dynamically adjust toast height
  • Loading branch information
alvroble committed Nov 9, 2024
1 parent 8cd8a5b commit f13c30a
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 15 deletions.
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
125 changes: 111 additions & 14 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,29 +20,37 @@ 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.icon.screen_y = self.canvas_height - self.height + int((self.height - self.icon.height)/2)


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
# messages).
Expand All @@ -59,7 +68,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 +134,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 +242,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,
)

0 comments on commit f13c30a

Please sign in to comment.