Skip to content

Commit 1e8ce3e

Browse files
committed
Merge branch 'refs/heads/main' into use_ruff
# Conflicts: # adafruit_button/button.py # adafruit_button/button_base.py
2 parents daf6bc2 + 09e3958 commit 1e8ce3e

File tree

4 files changed

+123
-93
lines changed

4 files changed

+123
-93
lines changed

adafruit_button/button.py

Lines changed: 56 additions & 41 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
--------------------
@@ -28,8 +29,7 @@
2829
from adafruit_button.button_base import ButtonBase, _check_color
2930

3031
try:
31-
from typing import Optional, Union
32-
32+
from typing import Optional, Union, Tuple
3333
from displayio import Group
3434
from fontio import FontProtocol
3535
except ImportError:
@@ -40,30 +40,45 @@
4040

4141

4242
class 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

adafruit_button/button_base.py

Lines changed: 35 additions & 28 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
--------------------
@@ -23,16 +24,16 @@
2324

2425
from adafruit_display_text.bitmap_label import Label
2526
from displayio import Group
27+
import terminalio
2628

2729
try:
28-
from typing import Optional, Union
29-
30+
from typing import Optional, Union, Tuple
3031
from fontio import FontProtocol
3132
except ImportError:
3233
pass
3334

3435

35-
def _check_color(color):
36+
def _check_color(color: Optional[Union[int, tuple[int, int, int]]]) -> int:
3637
# if a tuple is supplied, convert it to a RGB number
3738
if isinstance(color, tuple):
3839
r, g, b = color
@@ -43,15 +44,20 @@ def _check_color(color):
4344
class ButtonBase(Group):
4445
"""Superclass for creating UI buttons for ``displayio``.
4546
46-
:param x: The x position of the button.
47-
:param y: The y position of the button.
48-
:param width: The width of the button in tiles.
49-
:param height: The height of the button in tiles.
50-
:param name: A name, or miscellaneous string that is stored on the button.
51-
:param label: The text that appears inside the button. Defaults to not displaying the label.
52-
:param label_font: The button label font.
53-
:param label_color: The color of the button label text. Defaults to 0x0.
54-
: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.
5561
"""
5662

5763
def __init__( # noqa: PLR0913 Too many arguments
@@ -64,10 +70,10 @@ def __init__( # noqa: PLR0913 Too many arguments
6470
name: Optional[str] = None,
6571
label: Optional[str] = None,
6672
label_font: Optional[FontProtocol] = None,
67-
label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0,
68-
selected_label: Optional[Union[int, tuple[int, int, int]]] = None,
69-
label_scale: Optional[int] = None,
70-
):
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:
7177
super().__init__(x=x, y=y)
7278
self.x = x
7379
self.y = y
@@ -77,13 +83,13 @@ def __init__( # noqa: PLR0913 Too many arguments
7783
self._selected = False
7884
self.name = name
7985
self._label = label
80-
self._label_color = label_color
86+
self._label_color = _check_color(label_color)
8187
self._label_font = label_font
8288
self._selected_label = _check_color(selected_label)
83-
self._label_scale = label_scale or 1
89+
self._label_scale = label_scale
8490

8591
@property
86-
def label(self) -> str:
92+
def label(self) -> Optional[str]:
8793
"""The text label of the button"""
8894
return getattr(self._label, "text", None)
8995

@@ -97,7 +103,7 @@ def label(self, newtext: str) -> None:
97103
return # nothing to do!
98104

99105
if not self._label_font:
100-
raise RuntimeError("Please provide label font")
106+
self._label_font = terminalio.FONT
101107
self._label = Label(self._label_font, text=newtext, scale=self._label_scale)
102108
dims = list(self._label.bounding_box)
103109
dims[2] *= self._label.scale
@@ -116,15 +122,15 @@ def label(self, newtext: str) -> None:
116122
self.append(self._label)
117123

118124
if (self.selected_label is None) and (self._label_color is not None):
119-
self.selected_label = (~self._label_color) & 0xFFFFFF
125+
self.selected_label = (~_check_color(self._label_color)) & 0xFFFFFF
120126

121-
def _subclass_selected_behavior(self, value):
122-
# Subclasses should overide this!
127+
def _subclass_selected_behavior(self, value: bool):
128+
# Subclasses should override this!
123129
pass
124130

125131
@property
126132
def selected(self) -> bool:
127-
"""Selected inverts the colors."""
133+
"""Returns whether the button is selected."""
128134
return self._selected
129135

130136
@selected.setter
@@ -144,11 +150,12 @@ def selected(self, value: bool) -> None:
144150

145151
@property
146152
def selected_label(self) -> int:
147-
"""The font color of the button when selected"""
153+
"""The font color of the button when selected.
154+
If no color is specified it defaults to the inverse of the label_color"""
148155
return self._selected_label
149156

150157
@selected_label.setter
151-
def selected_label(self, new_color: int) -> None:
158+
def selected_label(self, new_color: Union[int, Tuple[int, int, int]]) -> None:
152159
self._selected_label = _check_color(new_color)
153160

154161
@property
@@ -157,6 +164,6 @@ def label_color(self) -> int:
157164
return self._label_color
158165

159166
@label_color.setter
160-
def label_color(self, new_color: int):
167+
def label_color(self, new_color: Union[int, Tuple[int, int, int]]) -> None:
161168
self._label_color = _check_color(new_color)
162169
self._label.color = self._label_color

0 commit comments

Comments
 (0)