Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mde-pach committed Feb 21, 2024
1 parent 6cf00c1 commit 19bb76a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
31 changes: 28 additions & 3 deletions blitz/ui/components/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
from typing import Any, Self, TypeVar
from typing import Any, Generic, Self, TypeVar, Protocol
from blitz.ui.blitz_ui import BlitzUI, get_blitz_ui

T = TypeVar("T", bound="BaseComponent")

class NiceGUIComponent(Protocol):
def __enter__(self) -> Any:
...

def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
...


V = TypeVar("V", bound=NiceGUIComponent)


# Get the blitz_ui through a metaclass
Expand All @@ -12,8 +21,9 @@ def __new__(cls, name: str, bases: tuple[type, ...], namespace: dict[str, Any])
return super().__new__(cls, name, bases, namespace)


class BaseComponent(metaclass=BaseComponentMeta):
class BaseComponent(Generic[V], metaclass=BaseComponentMeta):
def __init__(self, *args: Any, props: str = "", classes: str = "", **kwargs: Any) -> None:
self._ng: V
self.props: str
self.classes: str
self.blitz_ui: BlitzUI
Expand All @@ -35,6 +45,14 @@ def __init__(self, *args: Any, props: str = "", classes: str = "", **kwargs: Any
def render(self) -> None:
raise NotImplementedError

@property
def ng(self) -> V:
return self._ng

@ng.setter
def ng(self, value: V) -> None:
self._ng = value

@classmethod
def variant(cls, name: str, *, props: str = "", classes: str = "", **kwargs: Any) -> type[Self]:
"""
Expand All @@ -58,3 +76,10 @@ def variant(cls, name: str, *, props: str = "", classes: str = "", **kwargs: Any
"classes": classes,
},
)

def __enter__(self) -> V:
return self.ng

def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
if hasattr(self.ng, "__exit__"):
self.ng.__exit__(exc_type, exc_value, traceback)
18 changes: 11 additions & 7 deletions blitz/ui/components/buttons/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from blitz.ui.components.base import BaseComponent


class BaseButton(BaseComponent):
class BaseButton(BaseComponent[ui.button]):
def __init__(
self,
text: str = "",
Expand All @@ -21,9 +21,13 @@ def __init__(
super().__init__(props=props, classes=classes)

def render(self) -> None:
ui.button(
self.text,
on_click=self.on_click,
color=self.color,
icon=self.icon,
).props(self.props).classes(self.classes)
self.ng = (
ui.button(
self.text,
on_click=self.on_click,
color=self.color,
icon=self.icon,
)
.props(self.props)
.classes(self.classes)
)
2 changes: 1 addition & 1 deletion blitz/ui/components/buttons/flat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .base import BaseButton


FlatButton = BaseButton.variant("Flat", props="flat")
FlatButton = BaseButton.variant("Flat", props="flat")

0 comments on commit 19bb76a

Please sign in to comment.