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

type annotations #50

Merged
merged 1 commit into from
Dec 4, 2024
Merged
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
71 changes: 39 additions & 32 deletions adafruit_button/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
from adafruit_display_shapes.roundrect import RoundRect
from adafruit_button.button_base import ButtonBase, _check_color

try:
from typing import Optional, Union
from fontio import FontProtocol
from displayio import Group
except ImportError:
pass

__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Button.git"

Expand Down Expand Up @@ -117,22 +124,22 @@ def _create_body(self):
def __init__(
self,
*,
x,
y,
width,
height,
name=None,
style=RECT,
fill_color=0xFFFFFF,
outline_color=0x0,
label=None,
label_font=None,
label_color=0x0,
selected_fill=None,
selected_outline=None,
selected_label=None,
label_scale=None
):
x: int,
y: int,
width: int,
height: int,
name: Optional[str] = None,
style: int = RECT,
fill_color: Optional[Union[int, tuple[int, int, int]]] = 0xFFFFFF,
outline_color: Optional[Union[int, tuple[int, int, int]]] = 0x0,
label: Optional[str] = None,
label_font: Optional[FontProtocol] = None,
label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0,
selected_fill: Optional[Union[int, tuple[int, int, int]]] = None,
selected_outline: Optional[Union[int, tuple[int, int, int]]] = None,
selected_label: Optional[Union[int, tuple[int, int, int]]] = None,
label_scale: Optional[int] = None
) -> None:
super().__init__(
x=x,
y=y,
Expand Down Expand Up @@ -167,7 +174,7 @@ def __init__(

self.label = label

def _subclass_selected_behavior(self, value):
def _subclass_selected_behavior(self, value: bool) -> None:
if self._selected:
new_fill = self.selected_fill
new_out = self.selected_outline
Expand All @@ -180,7 +187,7 @@ def _subclass_selected_behavior(self, value):
self.body.outline = new_out

@property
def group(self):
def group(self) -> Group:
"""Return self for compatibility with old API."""
print(
"Warning: The group property is being deprecated. "
Expand All @@ -189,7 +196,7 @@ def group(self):
)
return self

def contains(self, point):
def contains(self, point: tuple[int, int]) -> bool:
"""Used to determine if a point is contained within a button. For example,
``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for
determining that a button has been touched.
Expand All @@ -199,56 +206,56 @@ def contains(self, point):
)

@property
def fill_color(self):
def fill_color(self) -> int:
"""The fill color of the button body"""
return self._fill_color

@fill_color.setter
def fill_color(self, new_color):
def fill_color(self, new_color: int) -> None:
self._fill_color = _check_color(new_color)
if not self.selected:
self.body.fill = self._fill_color

@property
def outline_color(self):
def outline_color(self) -> int:
"""The outline color of the button body"""
return self._outline_color

@outline_color.setter
def outline_color(self, new_color):
def outline_color(self, new_color: int) -> None:
self._outline_color = _check_color(new_color)
if not self.selected:
self.body.outline = self._outline_color

@property
def selected_fill(self):
def selected_fill(self) -> int:
"""The fill color of the button body when selected"""
return self._selected_fill

@selected_fill.setter
def selected_fill(self, new_color):
def selected_fill(self, new_color: int) -> None:
self._selected_fill = _check_color(new_color)
if self.selected:
self.body.fill = self._selected_fill

@property
def selected_outline(self):
def selected_outline(self) -> int:
"""The outline color of the button body when selected"""
return self._selected_outline

@selected_outline.setter
def selected_outline(self, new_color):
def selected_outline(self, new_color: int) -> None:
self._selected_outline = _check_color(new_color)
if self.selected:
self.body.outline = self._selected_outline

@property
def width(self):
def width(self) -> int:
"""The width of the button"""
return self._width

@width.setter
def width(self, new_width):
def width(self, new_width: int) -> None:
self._width = new_width
self._empty_self_group()
self._create_body()
Expand All @@ -257,20 +264,20 @@ def width(self, new_width):
self.label = self.label

@property
def height(self):
def height(self) -> int:
"""The height of the button"""
return self._height

@height.setter
def height(self, new_height):
def height(self, new_height: int) -> None:
self._height = new_height
self._empty_self_group()
self._create_body()
if self.body:
self.append(self.body)
self.label = self.label

def resize(self, new_width, new_height):
def resize(self, new_width: int, new_height: int) -> None:
"""Resize the button to the new width and height given
:param new_width int the desired width
:param new_height int the desired height
Expand Down
42 changes: 24 additions & 18 deletions adafruit_button/button_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
from adafruit_display_text.bitmap_label import Label
from displayio import Group

try:
from typing import Optional, Union
from fontio import FontProtocol
except ImportError:
pass


def _check_color(color):
# if a tuple is supplied, convert it to a RGB number
Expand Down Expand Up @@ -50,16 +56,16 @@ class ButtonBase(Group):
def __init__(
self,
*,
x,
y,
width,
height,
name=None,
label=None,
label_font=None,
label_color=0x0,
selected_label=None,
label_scale=None
x: int,
y: int,
width: int,
height: int,
name: Optional[str] = None,
label: Optional[str] = None,
label_font: Optional[FontProtocol] = None,
label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0,
selected_label: Optional[Union[int, tuple[int, int, int]]] = None,
label_scale: Optional[int] = None
):
super().__init__(x=x, y=y)
self.x = x
Expand All @@ -76,12 +82,12 @@ def __init__(
self._label_scale = label_scale or 1

@property
def label(self):
def label(self) -> str:
"""The text label of the button"""
return getattr(self._label, "text", None)

@label.setter
def label(self, newtext):
def label(self, newtext: str) -> None:
if self._label and self and (self[-1] == self._label):
self.pop()

Expand Down Expand Up @@ -120,12 +126,12 @@ def _subclass_selected_behavior(self, value):
pass

@property
def selected(self):
def selected(self) -> bool:
"""Selected inverts the colors."""
return self._selected

@selected.setter
def selected(self, value):
def selected(self, value: bool) -> None:
if value == self._selected:
return # bail now, nothing more to do
self._selected = value
Expand All @@ -140,20 +146,20 @@ def selected(self, value):
self._subclass_selected_behavior(value)

@property
def selected_label(self):
def selected_label(self) -> int:
"""The font color of the button when selected"""
return self._selected_label

@selected_label.setter
def selected_label(self, new_color):
def selected_label(self, new_color: int) -> None:
self._selected_label = _check_color(new_color)

@property
def label_color(self):
def label_color(self) -> int:
"""The font color of the button"""
return self._label_color

@label_color.setter
def label_color(self, new_color):
def label_color(self, new_color: int):
self._label_color = _check_color(new_color)
self._label.color = self._label_color
40 changes: 23 additions & 17 deletions adafruit_button/sprite_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
from adafruit_imageload import load
from adafruit_button.button_base import ButtonBase

try:
from typing import Optional, Union, Tuple
from fontio import FontProtocol
except ImportError:
pass


class SpriteButton(ButtonBase):
"""Helper class for creating 3x3 Bitmap Spritesheet UI buttons for ``displayio``.
Expand All @@ -45,19 +51,19 @@ class SpriteButton(ButtonBase):
def __init__(
self,
*,
x,
y,
width,
height,
name=None,
label=None,
label_font=None,
label_color=0x0,
selected_label=None,
bmp_path=None,
selected_bmp_path=None,
transparent_index=None,
label_scale=None
x: int,
y: int,
width: int,
height: int,
name: Optional[str] = None,
label: Optional[str] = None,
label_font: Optional[FontProtocol] = None,
label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0,
selected_label: Optional[Union[int, tuple[int, int, int]]] = None,
bmp_path: Optional[str] = None,
selected_bmp_path: Optional[str] = None,
transparent_index: Optional[int] = None,
label_scale: Optional[int] = None
):
if bmp_path is None:
raise ValueError("Please supply bmp_path. It cannot be None.")
Expand Down Expand Up @@ -104,16 +110,16 @@ def __init__(
self.label = label

@property
def width(self):
def width(self) -> int:
"""The width of the button"""
return self._width

@property
def height(self):
def height(self) -> int:
"""The height of the button"""
return self._height

def contains(self, point):
def contains(self, point: Tuple[int, int]):
"""Used to determine if a point is contained within a button. For example,
``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for
determining that a button has been touched.
Expand All @@ -122,7 +128,7 @@ def contains(self, point):
self.y <= point[1] <= self.y + self.height
)

def _subclass_selected_behavior(self, value):
def _subclass_selected_behavior(self, value: bool) -> None:
if self._selected:
if self._selected_bmp is not None:
self._btn_tilegrid.bitmap = self._selected_bmp
Expand Down
Loading