11# SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries
2+ # SPDX-FileCopyrightText: 2024 Channing Ramos
23#
34# SPDX-License-Identifier: MIT
45
910UI Buttons for displayio
1011
1112
12- * Author(s): Limor Fried
13+ * Author(s): Limor Fried, Channing Ramos
1314
1415Implementation Notes
1516--------------------
2829from adafruit_button .button_base import ButtonBase , _check_color
2930
3031try :
31- from typing import Optional , Union
32-
32+ from typing import Optional , Union , Tuple
3333 from displayio import Group
3434 from fontio import FontProtocol
3535except ImportError :
4040
4141
4242class Button (ButtonBase ):
43- """Helper class for creating UI buttons for ``displayio``.
44-
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 pixels.
48- :param height: The height of the button in pixels.
49- :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.
5054 :param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT.
5155 Defaults to RECT.
52- :param fill_color: The color to fill the button. Defaults to 0xFFFFFF.
53- :param outline_color: The color of the outline of the button.
54- :param label: The text that appears inside the button. Defaults to not displaying the label.
55- :param label_font: The button label font.
56- :param label_color: The color of the button label text. Defaults to 0x0.
57- :param selected_fill: Inverts the fill color.
58- :param selected_outline: Inverts the outline color.
59- :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.
6075 """
6176
62- def _empty_self_group (self ):
77+ def _empty_self_group (self ) -> None :
6378 while len (self ) > 0 :
6479 self .pop ()
6580
66- def _create_body (self ):
81+ def _create_body (self ) -> None :
6782 if (self .outline_color is not None ) or (self .fill_color is not None ):
6883 if self .style == Button .RECT :
6984 self .body = Rect (
@@ -128,17 +143,17 @@ def __init__( # noqa: PLR0913 Too many arguments
128143 width : int ,
129144 height : int ,
130145 name : Optional [str ] = None ,
131- style : int = RECT ,
132- fill_color : Optional [Union [int , tuple [int , int , int ]]] = 0xFFFFFF ,
133- outline_color : Optional [Union [int , tuple [int , int , int ]]] = 0x0 ,
146+ style = RECT ,
147+ fill_color : Optional [Union [int , Tuple [int , int , int ]]] = 0xFFFFFF ,
148+ outline_color : Optional [Union [int , Tuple [int , int , int ]]] = 0x0 ,
134149 label : Optional [str ] = None ,
135150 label_font : Optional [FontProtocol ] = None ,
136- label_color : Optional [Union [int , tuple [int , int , int ]]] = 0x0 ,
137- selected_fill : Optional [Union [int , tuple [int , int , int ]]] = None ,
138- selected_outline : Optional [Union [int , tuple [int , int , int ]]] = None ,
139- selected_label : Optional [Union [int , tuple [int , int , int ]]] = None ,
140- label_scale : Optional [int ] = None ,
141- ) -> None :
151+ label_color : Optional [Union [int , Tuple [int , int , int ]]] = 0x0 ,
152+ selected_fill : Optional [Union [int , Tuple [int , int , int ]]] = None ,
153+ selected_outline : Optional [Union [int , Tuple [int , int , int ]]] = None ,
154+ selected_label : Optional [Union [int , Tuple [int , int , int ]]] = None ,
155+ label_scale : Optional [int ] = 1 ,
156+ ):
142157 super ().__init__ (
143158 x = x ,
144159 y = y ,
@@ -163,9 +178,9 @@ def __init__( # noqa: PLR0913 Too many arguments
163178 self ._selected_outline = _check_color (selected_outline )
164179
165180 if self .selected_fill is None and fill_color is not None :
166- self .selected_fill = (~ self ._fill_color ) & 0xFFFFFF
181+ self .selected_fill = (~ _check_color ( self ._fill_color ) ) & 0xFFFFFF
167182 if self .selected_outline is None and outline_color is not None :
168- self .selected_outline = (~ self ._outline_color ) & 0xFFFFFF
183+ self .selected_outline = (~ _check_color ( self ._outline_color ) ) & 0xFFFFFF
169184
170185 self ._create_body ()
171186 if self .body :
@@ -205,45 +220,45 @@ def contains(self, point: tuple[int, int]) -> bool:
205220 )
206221
207222 @property
208- def fill_color (self ) -> int :
223+ def fill_color (self ) -> Optional [ int ] :
209224 """The fill color of the button body"""
210225 return self ._fill_color
211226
212227 @fill_color .setter
213- def fill_color (self , new_color : int ) -> None :
228+ def fill_color (self , new_color : Union [ int , Tuple [ int , int , int ]] ) -> None :
214229 self ._fill_color = _check_color (new_color )
215230 if not self .selected :
216231 self .body .fill = self ._fill_color
217232
218233 @property
219- def outline_color (self ) -> int :
234+ def outline_color (self ) -> Optional [ int ] :
220235 """The outline color of the button body"""
221236 return self ._outline_color
222237
223238 @outline_color .setter
224- def outline_color (self , new_color : int ) -> None :
239+ def outline_color (self , new_color : Union [ int , Tuple [ int , int , int ]] ) -> None :
225240 self ._outline_color = _check_color (new_color )
226241 if not self .selected :
227242 self .body .outline = self ._outline_color
228243
229244 @property
230- def selected_fill (self ) -> int :
245+ def selected_fill (self ) -> Optional [ int ] :
231246 """The fill color of the button body when selected"""
232247 return self ._selected_fill
233248
234249 @selected_fill .setter
235- def selected_fill (self , new_color : int ) -> None :
250+ def selected_fill (self , new_color : Union [ int , Tuple [ int , int , int ]] ) -> None :
236251 self ._selected_fill = _check_color (new_color )
237252 if self .selected :
238253 self .body .fill = self ._selected_fill
239254
240255 @property
241- def selected_outline (self ) -> int :
256+ def selected_outline (self ) -> Optional [ int ] :
242257 """The outline color of the button body when selected"""
243258 return self ._selected_outline
244259
245260 @selected_outline .setter
246- def selected_outline (self , new_color : int ) -> None :
261+ def selected_outline (self , new_color : Union [ int , Tuple [ int , int , int ]] ) -> None :
247262 self ._selected_outline = _check_color (new_color )
248263 if self .selected :
249264 self .body .outline = self ._selected_outline
@@ -278,8 +293,8 @@ def height(self, new_height: int) -> None:
278293
279294 def resize (self , new_width : int , new_height : int ) -> None :
280295 """Resize the button to the new width and height given
281- :param new_width int the desired width
282- :param new_height int the desired height
296+ :param int new_width: The desired width in pixels.
297+ :param int new_height: he desired height in pixels.
283298 """
284299 self ._width = new_width
285300 self ._height = new_height
0 commit comments