-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add note action buttons events and effects (#258)
- Loading branch information
Showing
9 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import Any | ||
|
||
from pydantic import Field | ||
|
||
from canvas_generated.messages.effects_pb2 import EffectType | ||
from canvas_sdk.effects.base import _BaseEffect | ||
|
||
|
||
class ShowButtonEffect(_BaseEffect): | ||
""" | ||
An Effect that will decide an action button's properties. | ||
""" | ||
|
||
class Meta: | ||
effect_type = EffectType.SHOW_ACTION_BUTTON | ||
|
||
key: str = Field(min_length=1) | ||
title: str = Field(min_length=1) | ||
|
||
@property | ||
def values(self) -> dict[str, Any]: | ||
"""The ShowButtonEffect's values.""" | ||
return {"key": self.key, "title": self.title} | ||
|
||
@property | ||
def effect_payload(self) -> dict[str, Any]: | ||
"""The payload of the effect.""" | ||
return {"data": self.values} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from abc import abstractmethod | ||
from enum import StrEnum | ||
|
||
from canvas_sdk.effects import Effect | ||
from canvas_sdk.effects.show_button import ShowButtonEffect | ||
from canvas_sdk.events import EventType | ||
from canvas_sdk.handlers.base import BaseHandler | ||
|
||
|
||
class ActionButton(BaseHandler): | ||
"""Base class for action buttons.""" | ||
|
||
RESPONDS_TO = [ | ||
EventType.Name(EventType.SHOW_NOTE_HEADER_BUTTON), | ||
EventType.Name(EventType.SHOW_NOTE_FOOTER_BUTTON), | ||
EventType.Name(EventType.ACTION_BUTTON_CLICKED), | ||
] | ||
|
||
class ButtonLocation(StrEnum): | ||
NOTE_HEADER = "note_header" | ||
NOTE_FOOTER = "note_footer" | ||
|
||
BUTTON_TITLE: str = "" | ||
BUTTON_KEY: str = "" | ||
BUTTON_LOCATION: ButtonLocation | None = None | ||
|
||
@abstractmethod | ||
def handle(self) -> list[Effect]: | ||
"""Method to handle button click.""" | ||
raise NotImplementedError("Implement to handle button click") | ||
|
||
def visible(self) -> bool: | ||
"""Method to determine button visibility.""" | ||
return True | ||
|
||
def compute(self) -> list[Effect]: | ||
"""Method to compute the effects.""" | ||
if self.BUTTON_LOCATION is None: | ||
return [] | ||
|
||
if self.event.type in ( | ||
EventType.SHOW_NOTE_HEADER_BUTTON, | ||
EventType.SHOW_NOTE_FOOTER_BUTTON, | ||
): | ||
if self.context["location"].lower() == self.BUTTON_LOCATION.value and self.visible(): | ||
return [ShowButtonEffect(key=self.BUTTON_KEY, title=self.BUTTON_TITLE).apply()] | ||
else: | ||
return [] | ||
elif ( | ||
self.event.type == EventType.ACTION_BUTTON_CLICKED | ||
and self.context["key"] == self.BUTTON_KEY | ||
): | ||
return self.handle() | ||
|
||
return [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters