26
26
from adafruit_display_shapes .roundrect import RoundRect
27
27
from adafruit_button .button_base import ButtonBase , _check_color
28
28
29
+ try :
30
+ from typing import Optional , Union , Tuple , Any , List
31
+ from fontio import FontProtocol
32
+ except ImportError :
33
+ pass
34
+
29
35
__version__ = "0.0.0+auto.0"
30
36
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Button.git"
31
37
32
38
33
39
class Button (ButtonBase ):
34
40
# 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.
42
52
:param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT.
43
53
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.
52
65
"""
53
66
54
- def _empty_self_group (self ):
67
+ def _empty_self_group (self ) -> None :
55
68
while len (self ) > 0 :
56
69
self .pop ()
57
70
58
- def _create_body (self ):
71
+ def _create_body (self ) -> None :
59
72
if (self .outline_color is not None ) or (self .fill_color is not None ):
60
73
if self .style == Button .RECT :
61
74
self .body = Rect (
@@ -117,21 +130,21 @@ def _create_body(self):
117
130
def __init__ (
118
131
self ,
119
132
* ,
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
135
148
):
136
149
super ().__init__ (
137
150
x = x ,
@@ -167,7 +180,7 @@ def __init__(
167
180
168
181
self .label = label
169
182
170
- def _subclass_selected_behavior (self , value ) :
183
+ def _subclass_selected_behavior (self , value : Optional [ Any ]) -> None :
171
184
if self ._selected :
172
185
new_fill = self .selected_fill
173
186
new_out = self .selected_outline
@@ -180,7 +193,7 @@ def _subclass_selected_behavior(self, value):
180
193
self .body .outline = new_out
181
194
182
195
@property
183
- def group (self ):
196
+ def group (self ) -> "Button" :
184
197
"""Return self for compatibility with old API."""
185
198
print (
186
199
"Warning: The group property is being deprecated. "
@@ -189,7 +202,7 @@ def group(self):
189
202
)
190
203
return self
191
204
192
- def contains (self , point ) :
205
+ def contains (self , point : List [ int ]) -> bool :
193
206
"""Used to determine if a point is contained within a button. For example,
194
207
``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for
195
208
determining that a button has been touched.
@@ -199,56 +212,56 @@ def contains(self, point):
199
212
)
200
213
201
214
@property
202
- def fill_color (self ):
215
+ def fill_color (self ) -> Optional [ int ] :
203
216
"""The fill color of the button body"""
204
217
return self ._fill_color
205
218
206
219
@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 :
208
221
self ._fill_color = _check_color (new_color )
209
222
if not self .selected :
210
223
self .body .fill = self ._fill_color
211
224
212
225
@property
213
- def outline_color (self ):
226
+ def outline_color (self ) -> Optional [ int ] :
214
227
"""The outline color of the button body"""
215
228
return self ._outline_color
216
229
217
230
@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 :
219
232
self ._outline_color = _check_color (new_color )
220
233
if not self .selected :
221
234
self .body .outline = self ._outline_color
222
235
223
236
@property
224
- def selected_fill (self ):
237
+ def selected_fill (self ) -> Optional [ int ] :
225
238
"""The fill color of the button body when selected"""
226
239
return self ._selected_fill
227
240
228
241
@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 :
230
243
self ._selected_fill = _check_color (new_color )
231
244
if self .selected :
232
245
self .body .fill = self ._selected_fill
233
246
234
247
@property
235
- def selected_outline (self ):
248
+ def selected_outline (self ) -> Optional [ int ] :
236
249
"""The outline color of the button body when selected"""
237
250
return self ._selected_outline
238
251
239
252
@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 :
241
254
self ._selected_outline = _check_color (new_color )
242
255
if self .selected :
243
256
self .body .outline = self ._selected_outline
244
257
245
258
@property
246
- def width (self ):
259
+ def width (self ) -> int :
247
260
"""The width of the button"""
248
261
return self ._width
249
262
250
263
@width .setter
251
- def width (self , new_width ) :
264
+ def width (self , new_width : int ) -> None :
252
265
self ._width = new_width
253
266
self ._empty_self_group ()
254
267
self ._create_body ()
@@ -257,20 +270,20 @@ def width(self, new_width):
257
270
self .label = self .label
258
271
259
272
@property
260
- def height (self ):
273
+ def height (self ) -> int :
261
274
"""The height of the button"""
262
275
return self ._height
263
276
264
277
@height .setter
265
- def height (self , new_height ) :
278
+ def height (self , new_height : int ) -> None :
266
279
self ._height = new_height
267
280
self ._empty_self_group ()
268
281
self ._create_body ()
269
282
if self .body :
270
283
self .append (self .body )
271
284
self .label = self .label
272
285
273
- def resize (self , new_width , new_height ) :
286
+ def resize (self , new_width : int , new_height : int ) -> None :
274
287
"""Resize the button to the new width and height given
275
288
:param new_width int the desired width
276
289
:param new_height int the desired height
0 commit comments