Skip to content

Commit fdf13e9

Browse files
committed
Add more annotations, improve documentation
Added type annotations for button.py, as well as correcting/updating documentation.
1 parent 89e804b commit fdf13e9

File tree

2 files changed

+73
-58
lines changed

2 files changed

+73
-58
lines changed

adafruit_button/button.py

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,49 @@
2626
from adafruit_display_shapes.roundrect import RoundRect
2727
from adafruit_button.button_base import ButtonBase, _check_color
2828

29+
try:
30+
from typing import Optional, Union, Tuple, Any, List
31+
from fontio import FontProtocol
32+
except ImportError:
33+
pass
34+
2935
__version__ = "0.0.0+auto.0"
3036
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Button.git"
3137

3238

3339
class Button(ButtonBase):
3440
# pylint: disable=too-many-instance-attributes, too-many-locals
35-
"""Helper class for creating UI buttons for ``displayio``.
36-
37-
:param x: The x position of the button.
38-
:param y: The y position of the button.
39-
:param width: The width of the button in pixels.
40-
:param height: The height of the button in pixels.
41-
:param name: The name of the button.
41+
"""Helper class for creating UI buttons for ``displayio``. Provides the following
42+
buttons:
43+
RECT: A rectangular button. SHAWDOWRECT adds a drop shadow.
44+
ROUNDRECT: A rectangular button with rounded corners. SHADOWROUNDRECT adds
45+
a drop shadow.
46+
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 pixels.
50+
:param int height: The height of the button in pixels.
51+
:param str name: The name of the button.
4252
:param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT.
4353
Defaults to RECT.
44-
:param fill_color: The color to fill the button. Defaults to 0xFFFFFF.
45-
:param outline_color: The color of the outline of the button.
46-
:param label: The text that appears inside the button. Defaults to not displaying the label.
47-
:param label_font: The button label font.
48-
:param label_color: The color of the button label text. Defaults to 0x0.
49-
:param selected_fill: Inverts the fill color.
50-
:param selected_outline: Inverts the outline color.
51-
:param selected_label: Inverts the label color.
54+
:param int|Tuple(int, int, int) fill_color: The color to fill the button. Defaults to 0xFFFFFF.
55+
:param int|Tuple(int, int, int) outline_color: The color of the outline of the button.
56+
:param str label: The text that appears inside the button. Defaults to not displaying the label.
57+
:param FontProtocol label_font: The button label font. Defaults to terminalio.FONT
58+
:param int|Tuple(int, int, int) label_color: The color of the button label text. Defaults to 0x0.
59+
:param int|Tuple(int, int, int) selected_fill: The fill color when the button is selected.
60+
Defaults to the inverse of the fill_color.
61+
:param int|Tuple(int, int, int) selected_outline: The outline color when the button is selected.
62+
Defaults to the inverse of outline_color.
63+
:param selected_label: The label color when the button is selected.
64+
Defaults to inverting the label_color.
5265
"""
5366

54-
def _empty_self_group(self):
67+
def _empty_self_group(self) -> None:
5568
while len(self) > 0:
5669
self.pop()
5770

58-
def _create_body(self):
71+
def _create_body(self) -> None:
5972
if (self.outline_color is not None) or (self.fill_color is not None):
6073
if self.style == Button.RECT:
6174
self.body = Rect(
@@ -117,21 +130,21 @@ def _create_body(self):
117130
def __init__(
118131
self,
119132
*,
120-
x,
121-
y,
122-
width,
123-
height,
124-
name=None,
125-
style=RECT,
126-
fill_color=0xFFFFFF,
127-
outline_color=0x0,
128-
label=None,
129-
label_font=None,
130-
label_color=0x0,
131-
selected_fill=None,
132-
selected_outline=None,
133-
selected_label=None,
134-
label_scale=None
133+
x: int,
134+
y: int,
135+
width: int,
136+
height: int,
137+
name: Optional[str] = None,
138+
style = RECT,
139+
fill_color: Optional[Union[int, tuple[int, int, int]]] = 0xFFFFFF,
140+
outline_color: Optional[Union[int, tuple[int, int, int]]] = 0x0,
141+
label: Optional[str] = None,
142+
label_font: Optional[FontProtocol] = None,
143+
label_color: Optional[int] = 0x0,
144+
selected_fill: Optional[Union[int, tuple[int, int , int]]] = None,
145+
selected_outline: Optional[Union[int, tuple[int, int, int]]] = None,
146+
selected_label: Optional[Union[int, tuple[int, int, int]]] = None,
147+
label_scale: Optional[int] = 1
135148
):
136149
super().__init__(
137150
x=x,
@@ -167,7 +180,7 @@ def __init__(
167180

168181
self.label = label
169182

170-
def _subclass_selected_behavior(self, value):
183+
def _subclass_selected_behavior(self, value: Optional[Any]) -> None:
171184
if self._selected:
172185
new_fill = self.selected_fill
173186
new_out = self.selected_outline
@@ -180,7 +193,7 @@ def _subclass_selected_behavior(self, value):
180193
self.body.outline = new_out
181194

182195
@property
183-
def group(self):
196+
def group(self) -> "Button":
184197
"""Return self for compatibility with old API."""
185198
print(
186199
"Warning: The group property is being deprecated. "
@@ -189,7 +202,7 @@ def group(self):
189202
)
190203
return self
191204

192-
def contains(self, point):
205+
def contains(self, point: List[int]) -> bool:
193206
"""Used to determine if a point is contained within a button. For example,
194207
``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for
195208
determining that a button has been touched.
@@ -199,56 +212,56 @@ def contains(self, point):
199212
)
200213

201214
@property
202-
def fill_color(self):
215+
def fill_color(self) -> Optional[int]:
203216
"""The fill color of the button body"""
204217
return self._fill_color
205218

206219
@fill_color.setter
207-
def fill_color(self, new_color):
220+
def fill_color(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None:
208221
self._fill_color = _check_color(new_color)
209222
if not self.selected:
210223
self.body.fill = self._fill_color
211224

212225
@property
213-
def outline_color(self):
226+
def outline_color(self) -> Optional[int]:
214227
"""The outline color of the button body"""
215228
return self._outline_color
216229

217230
@outline_color.setter
218-
def outline_color(self, new_color):
231+
def outline_color(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None:
219232
self._outline_color = _check_color(new_color)
220233
if not self.selected:
221234
self.body.outline = self._outline_color
222235

223236
@property
224-
def selected_fill(self):
237+
def selected_fill(self) -> Optional[int]:
225238
"""The fill color of the button body when selected"""
226239
return self._selected_fill
227240

228241
@selected_fill.setter
229-
def selected_fill(self, new_color):
242+
def selected_fill(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None:
230243
self._selected_fill = _check_color(new_color)
231244
if self.selected:
232245
self.body.fill = self._selected_fill
233246

234247
@property
235-
def selected_outline(self):
248+
def selected_outline(self) -> Optional[int]:
236249
"""The outline color of the button body when selected"""
237250
return self._selected_outline
238251

239252
@selected_outline.setter
240-
def selected_outline(self, new_color):
253+
def selected_outline(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None:
241254
self._selected_outline = _check_color(new_color)
242255
if self.selected:
243256
self.body.outline = self._selected_outline
244257

245258
@property
246-
def width(self):
259+
def width(self) -> int:
247260
"""The width of the button"""
248261
return self._width
249262

250263
@width.setter
251-
def width(self, new_width):
264+
def width(self, new_width: int) -> None:
252265
self._width = new_width
253266
self._empty_self_group()
254267
self._create_body()
@@ -257,20 +270,20 @@ def width(self, new_width):
257270
self.label = self.label
258271

259272
@property
260-
def height(self):
273+
def height(self) -> int:
261274
"""The height of the button"""
262275
return self._height
263276

264277
@height.setter
265-
def height(self, new_height):
278+
def height(self, new_height: int) -> None:
266279
self._height = new_height
267280
self._empty_self_group()
268281
self._create_body()
269282
if self.body:
270283
self.append(self.body)
271284
self.label = self.label
272285

273-
def resize(self, new_width, new_height):
286+
def resize(self, new_width: int, new_height: int) -> None:
274287
"""Resize the button to the new width and height given
275288
:param new_width int the desired width
276289
:param new_height int the desired height

adafruit_button/button_base.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from displayio import Group
2525
import terminalio
2626
try:
27-
from typing import Optional, Union, List, Tuple, Any
27+
from typing import Optional, Union, Tuple, Any
2828
from fontio import FontProtocol
2929
except ImportError:
3030
pass
@@ -48,9 +48,10 @@ class ButtonBase(Group):
4848
:param int height: The height of the button in tiles.
4949
:param str name: A name, or miscellaneous string that is stored on the button.
5050
:param str label: The text that appears inside the button. Defaults to not displaying the label.
51-
:param FontProtocol label_font: The button label font.
52-
:param int label_color: The color of the button label text. Defaults to 0x0.
53-
:param selected_label: The color of button label text when the button is selected.
51+
:param FontProtocol label_font: The button label font. Defaults to terminalio.FONT
52+
:param int|Tuple(int, int, int) label_color: The color of the button label text. Defaults to 0x0.
53+
:param int|Tuple(int, int, int) selected_label: The color of button label text when the button is selected.
54+
:param int label_scale: The scale factor used for the label. Defaults to 1.
5455
"""
5556

5657
def __init__(
@@ -127,7 +128,7 @@ def _subclass_selected_behavior(self, value: Optional[Any]) -> None:
127128

128129
@property
129130
def selected(self) -> bool:
130-
"""Selected inverts the colors."""
131+
"""Returns whether the button is selected."""
131132
return self._selected
132133

133134
@selected.setter
@@ -146,20 +147,21 @@ def selected(self, value: bool) -> None:
146147
self._subclass_selected_behavior(value)
147148

148149
@property
149-
def selected_label(self) -> int:
150-
"""The font color of the button when selected"""
150+
def selected_label(self) -> Optional[Union[int, tuple[int, int, int]]]:
151+
"""The font color of the button when selected.
152+
If no color is specified it defaults to the inverse of the label_color"""
151153
return self._selected_label
152154

153155
@selected_label.setter
154-
def selected_label(self, new_color: int) -> None:
156+
def selected_label(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None:
155157
self._selected_label = _check_color(new_color)
156158

157159
@property
158-
def label_color(self) -> int:
160+
def label_color(self) -> Optional[Union[int, tuple[int, int, int]]]:
159161
"""The font color of the button"""
160162
return self._label_color
161163

162164
@label_color.setter
163-
def label_color(self, new_color: int) -> None:
165+
def label_color(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None:
164166
self._label_color = _check_color(new_color)
165167
self._label.color = self._label_color

0 commit comments

Comments
 (0)