Skip to content

Commit 09e3958

Browse files
authored
Merge pull request #48 from ch4nsuk3/add-annotations
Add annotations
2 parents c368494 + f6026b2 commit 09e3958

File tree

4 files changed

+123
-91
lines changed

4 files changed

+123
-91
lines changed

adafruit_button/button.py

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2024 Channing Ramos
23
#
34
# SPDX-License-Identifier: MIT
45

@@ -9,7 +10,7 @@
910
UI Buttons for displayio
1011
1112
12-
* Author(s): Limor Fried
13+
* Author(s): Limor Fried, Channing Ramos
1314
1415
Implementation Notes
1516
--------------------
@@ -27,7 +28,7 @@
2728
from adafruit_button.button_base import ButtonBase, _check_color
2829

2930
try:
30-
from typing import Optional, Union
31+
from typing import Optional, Union, Tuple
3132
from fontio import FontProtocol
3233
from displayio import Group
3334
except ImportError:
@@ -39,30 +40,45 @@
3940

4041
class Button(ButtonBase):
4142
# pylint: disable=too-many-instance-attributes, too-many-locals
42-
"""Helper class for creating UI buttons for ``displayio``.
43-
44-
:param x: The x position of the button.
45-
:param y: The y position of the button.
46-
:param width: The width of the button in pixels.
47-
:param height: The height of the button in pixels.
48-
:param name: The name of the button.
43+
"""Helper class for creating UI buttons for ``displayio``. Provides the following
44+
buttons:
45+
RECT: A rectangular button. SHAWDOWRECT adds a drop shadow.
46+
ROUNDRECT: A rectangular button with rounded corners. SHADOWROUNDRECT adds
47+
a drop shadow.
48+
49+
:param int x: The x position of the button.
50+
:param int y: The y position of the button.
51+
:param int width: The width of the button in pixels.
52+
:param int height: The height of the button in pixels.
53+
:param Optional[str] name: The name of the button.
4954
:param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT.
5055
Defaults to RECT.
51-
:param fill_color: The color to fill the button. Defaults to 0xFFFFFF.
52-
:param outline_color: The color of the outline of the button.
53-
:param label: The text that appears inside the button. Defaults to not displaying the label.
54-
:param label_font: The button label font.
55-
:param label_color: The color of the button label text. Defaults to 0x0.
56-
:param selected_fill: Inverts the fill color.
57-
:param selected_outline: Inverts the outline color.
58-
:param selected_label: Inverts the label color.
56+
:param Optional[Union[int, Tuple[int, int, int]]] fill_color: The color to fill the button.
57+
Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0xFFFFFF.
58+
:param Optional[Union[int, Tuple[int, int, int]]] outline_color: The color of the outline of
59+
the button. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0.
60+
:param Optional[str] label: The text that appears inside the button.
61+
:param Optional[FontProtocol] label_font: The button label font. Defaults to
62+
''terminalio.FONT''
63+
:param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label
64+
text. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0.
65+
:param Optional[Union[int, Tuple[int, int, int]]] selected_fill: The fill color when the
66+
button is selected. Accepts an int or a tuple of 3 integers representing RGB values.
67+
Defaults to the inverse of the fill_color.
68+
:param Optional[Union[int, Tuple[int, int, int]]] selected_outline: The outline color when the
69+
button is selected. Accepts an int or a tuple of 3 integers representing RGB values.
70+
Defaults to the inverse of outline_color.
71+
:param Optional[Union[int, Tuple[int, int, int]]] selected_label: The label color when the
72+
button is selected. Accepts an int or a tuple of 3 integers representing RGB values.
73+
Defaults to inverting the label_color.
74+
:param Optional[int] label_scale: The scale factor used for the label. Defaults to 1.
5975
"""
6076

61-
def _empty_self_group(self):
77+
def _empty_self_group(self) -> None:
6278
while len(self) > 0:
6379
self.pop()
6480

65-
def _create_body(self):
81+
def _create_body(self) -> None:
6682
if (self.outline_color is not None) or (self.fill_color is not None):
6783
if self.style == Button.RECT:
6884
self.body = Rect(
@@ -129,17 +145,17 @@ def __init__(
129145
width: int,
130146
height: int,
131147
name: Optional[str] = None,
132-
style: int = RECT,
133-
fill_color: Optional[Union[int, tuple[int, int, int]]] = 0xFFFFFF,
134-
outline_color: Optional[Union[int, tuple[int, int, int]]] = 0x0,
148+
style=RECT,
149+
fill_color: Optional[Union[int, Tuple[int, int, int]]] = 0xFFFFFF,
150+
outline_color: Optional[Union[int, Tuple[int, int, int]]] = 0x0,
135151
label: Optional[str] = None,
136152
label_font: Optional[FontProtocol] = None,
137-
label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0,
138-
selected_fill: Optional[Union[int, tuple[int, int, int]]] = None,
139-
selected_outline: Optional[Union[int, tuple[int, int, int]]] = None,
140-
selected_label: Optional[Union[int, tuple[int, int, int]]] = None,
141-
label_scale: Optional[int] = None
142-
) -> None:
153+
label_color: Optional[Union[int, Tuple[int, int, int]]] = 0x0,
154+
selected_fill: Optional[Union[int, Tuple[int, int, int]]] = None,
155+
selected_outline: Optional[Union[int, Tuple[int, int, int]]] = None,
156+
selected_label: Optional[Union[int, Tuple[int, int, int]]] = None,
157+
label_scale: Optional[int] = 1
158+
):
143159
super().__init__(
144160
x=x,
145161
y=y,
@@ -164,9 +180,9 @@ def __init__(
164180
self._selected_outline = _check_color(selected_outline)
165181

166182
if self.selected_fill is None and fill_color is not None:
167-
self.selected_fill = (~self._fill_color) & 0xFFFFFF
183+
self.selected_fill = (~_check_color(self._fill_color)) & 0xFFFFFF
168184
if self.selected_outline is None and outline_color is not None:
169-
self.selected_outline = (~self._outline_color) & 0xFFFFFF
185+
self.selected_outline = (~_check_color(self._outline_color)) & 0xFFFFFF
170186

171187
self._create_body()
172188
if self.body:
@@ -206,45 +222,45 @@ def contains(self, point: tuple[int, int]) -> bool:
206222
)
207223

208224
@property
209-
def fill_color(self) -> int:
225+
def fill_color(self) -> Optional[int]:
210226
"""The fill color of the button body"""
211227
return self._fill_color
212228

213229
@fill_color.setter
214-
def fill_color(self, new_color: int) -> None:
230+
def fill_color(self, new_color: Union[int, Tuple[int, int, int]]) -> None:
215231
self._fill_color = _check_color(new_color)
216232
if not self.selected:
217233
self.body.fill = self._fill_color
218234

219235
@property
220-
def outline_color(self) -> int:
236+
def outline_color(self) -> Optional[int]:
221237
"""The outline color of the button body"""
222238
return self._outline_color
223239

224240
@outline_color.setter
225-
def outline_color(self, new_color: int) -> None:
241+
def outline_color(self, new_color: Union[int, Tuple[int, int, int]]) -> None:
226242
self._outline_color = _check_color(new_color)
227243
if not self.selected:
228244
self.body.outline = self._outline_color
229245

230246
@property
231-
def selected_fill(self) -> int:
247+
def selected_fill(self) -> Optional[int]:
232248
"""The fill color of the button body when selected"""
233249
return self._selected_fill
234250

235251
@selected_fill.setter
236-
def selected_fill(self, new_color: int) -> None:
252+
def selected_fill(self, new_color: Union[int, Tuple[int, int, int]]) -> None:
237253
self._selected_fill = _check_color(new_color)
238254
if self.selected:
239255
self.body.fill = self._selected_fill
240256

241257
@property
242-
def selected_outline(self) -> int:
258+
def selected_outline(self) -> Optional[int]:
243259
"""The outline color of the button body when selected"""
244260
return self._selected_outline
245261

246262
@selected_outline.setter
247-
def selected_outline(self, new_color: int) -> None:
263+
def selected_outline(self, new_color: Union[int, Tuple[int, int, int]]) -> None:
248264
self._selected_outline = _check_color(new_color)
249265
if self.selected:
250266
self.body.outline = self._selected_outline
@@ -279,8 +295,8 @@ def height(self, new_height: int) -> None:
279295

280296
def resize(self, new_width: int, new_height: int) -> None:
281297
"""Resize the button to the new width and height given
282-
:param new_width int the desired width
283-
:param new_height int the desired height
298+
:param int new_width: The desired width in pixels.
299+
:param int new_height: he desired height in pixels.
284300
"""
285301
self._width = new_width
286302
self._height = new_height

adafruit_button/button_base.py

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2024 Channing Ramos
23
#
34
# SPDX-License-Identifier: MIT
45

@@ -9,7 +10,7 @@
910
UI Buttons for displayio
1011
1112
12-
* Author(s): Limor Fried
13+
* Author(s): Limor Fried, Channing Ramos
1314
1415
Implementation Notes
1516
--------------------
@@ -22,15 +23,16 @@
2223
"""
2324
from adafruit_display_text.bitmap_label import Label
2425
from displayio import Group
26+
import terminalio
2527

2628
try:
27-
from typing import Optional, Union
29+
from typing import Optional, Union, Tuple
2830
from fontio import FontProtocol
2931
except ImportError:
3032
pass
3133

3234

33-
def _check_color(color):
35+
def _check_color(color: Optional[Union[int, tuple[int, int, int]]]) -> int:
3436
# if a tuple is supplied, convert it to a RGB number
3537
if isinstance(color, tuple):
3638
r, g, b = color
@@ -42,15 +44,20 @@ class ButtonBase(Group):
4244
# pylint: disable=too-many-instance-attributes
4345
"""Superclass for creating UI buttons for ``displayio``.
4446
45-
:param x: The x position of the button.
46-
:param y: The y position of the button.
47-
:param width: The width of the button in tiles.
48-
:param height: The height of the button in tiles.
49-
:param name: A name, or miscellaneous string that is stored on the button.
50-
:param label: The text that appears inside the button. Defaults to not displaying the label.
51-
:param label_font: The button label font.
52-
:param label_color: The color of the button label text. Defaults to 0x0.
53-
:param selected_label: Text that appears when selected
47+
:param int x: The x position of the button.
48+
:param int y: The y position of the button.
49+
:param int width: The width of the button in tiles.
50+
:param int height: The height of the button in tiles.
51+
:param Optional[str] name: A name, or miscellaneous string that is stored on the button.
52+
:param Optional[str] label: The text that appears inside the button.
53+
:param Optional[FontProtocol] label_font: The button label font.
54+
Defaults to ''terminalio.FONT''
55+
:param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the label text.
56+
Defaults to 0x0. Accepts an int or a tuple of 3 integers representing RGB values.
57+
:param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of the label text
58+
when the button is selected. Accepts an int or a tuple of 3 integers representing RGB values.
59+
Defaults to an inverse of label_color.
60+
:param Optional[int] label_scale: The scale factor used for the label. Defaults to 1.
5461
"""
5562

5663
def __init__(
@@ -63,10 +70,10 @@ def __init__(
6370
name: Optional[str] = None,
6471
label: Optional[str] = None,
6572
label_font: Optional[FontProtocol] = None,
66-
label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0,
67-
selected_label: Optional[Union[int, tuple[int, int, int]]] = None,
68-
label_scale: Optional[int] = None
69-
):
73+
label_color: Optional[Union[int, Tuple[int, int, int]]] = 0x0,
74+
selected_label: Optional[Union[int, Tuple[int, int, int]]] = None,
75+
label_scale: Optional[int] = 1,
76+
) -> None:
7077
super().__init__(x=x, y=y)
7178
self.x = x
7279
self.y = y
@@ -76,13 +83,13 @@ def __init__(
7683
self._selected = False
7784
self.name = name
7885
self._label = label
79-
self._label_color = label_color
86+
self._label_color = _check_color(label_color)
8087
self._label_font = label_font
8188
self._selected_label = _check_color(selected_label)
82-
self._label_scale = label_scale or 1
89+
self._label_scale = label_scale
8390

8491
@property
85-
def label(self) -> str:
92+
def label(self) -> Optional[str]:
8693
"""The text label of the button"""
8794
return getattr(self._label, "text", None)
8895

@@ -96,7 +103,7 @@ def label(self, newtext: str) -> None:
96103
return # nothing to do!
97104

98105
if not self._label_font:
99-
raise RuntimeError("Please provide label font")
106+
self._label_font = terminalio.FONT
100107
self._label = Label(self._label_font, text=newtext, scale=self._label_scale)
101108
dims = list(self._label.bounding_box)
102109
dims[2] *= self._label.scale
@@ -119,15 +126,15 @@ def label(self, newtext: str) -> None:
119126
self.append(self._label)
120127

121128
if (self.selected_label is None) and (self._label_color is not None):
122-
self.selected_label = (~self._label_color) & 0xFFFFFF
129+
self.selected_label = (~_check_color(self._label_color)) & 0xFFFFFF
123130

124-
def _subclass_selected_behavior(self, value):
125-
# Subclasses should overide this!
131+
def _subclass_selected_behavior(self, value: bool):
132+
# Subclasses should override this!
126133
pass
127134

128135
@property
129136
def selected(self) -> bool:
130-
"""Selected inverts the colors."""
137+
"""Returns whether the button is selected."""
131138
return self._selected
132139

133140
@selected.setter
@@ -147,11 +154,12 @@ def selected(self, value: bool) -> None:
147154

148155
@property
149156
def selected_label(self) -> int:
150-
"""The font color of the button when selected"""
157+
"""The font color of the button when selected.
158+
If no color is specified it defaults to the inverse of the label_color"""
151159
return self._selected_label
152160

153161
@selected_label.setter
154-
def selected_label(self, new_color: int) -> None:
162+
def selected_label(self, new_color: Union[int, Tuple[int, int, int]]) -> None:
155163
self._selected_label = _check_color(new_color)
156164

157165
@property
@@ -160,6 +168,6 @@ def label_color(self) -> int:
160168
return self._label_color
161169

162170
@label_color.setter
163-
def label_color(self, new_color: int):
171+
def label_color(self, new_color: Union[int, Tuple[int, int, int]]) -> None:
164172
self._label_color = _check_color(new_color)
165173
self._label.color = self._label_color

0 commit comments

Comments
 (0)