Skip to content

Commit 23c1549

Browse files
committed
mutable size
1 parent 64c79a9 commit 23c1549

File tree

1 file changed

+93
-48
lines changed

1 file changed

+93
-48
lines changed

adafruit_button.py

Lines changed: 93 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,67 @@ class Button(displayio.Group):
5858
:param selected_fill: Inverts the fill color.
5959
:param selected_outline: Inverts the outline color.
6060
:param selected_label: Inverts the label color.
61-
6261
"""
62+
63+
def _empty_self_group(self):
64+
while len(self) > 0:
65+
self.pop()
66+
67+
def _create_body(self):
68+
if (self.outline_color is not None) or (self.fill_color is not None):
69+
if self.style == Button.RECT:
70+
self.body = Rect(
71+
0,
72+
0,
73+
self.width,
74+
self.height,
75+
fill=self._fill_color,
76+
outline=self._outline_color,
77+
)
78+
elif self.style == Button.ROUNDRECT:
79+
self.body = RoundRect(
80+
0,
81+
0,
82+
self.width,
83+
self.height,
84+
r=10,
85+
fill=self._fill_color,
86+
outline=self._outline_color,
87+
)
88+
elif self.style == Button.SHADOWRECT:
89+
self.shadow = Rect(
90+
2, 2, self.width - 2, self.height - 2, fill=self.outline_color
91+
)
92+
self.body = Rect(
93+
0,
94+
0,
95+
self.width - 2,
96+
self.height - 2,
97+
fill=self._fill_color,
98+
outline=self._outline_color,
99+
)
100+
elif self.style == Button.SHADOWROUNDRECT:
101+
self.shadow = RoundRect(
102+
2,
103+
2,
104+
self.width - 2,
105+
self.height - 2,
106+
r=10,
107+
fill=self._outline_color,
108+
)
109+
self.body = RoundRect(
110+
0,
111+
0,
112+
self.width - 2,
113+
self.height - 2,
114+
r=10,
115+
fill=self._fill_color,
116+
outline=self._outline_color,
117+
)
118+
if self.shadow:
119+
self.append(self.shadow)
120+
self.append(self.body)
121+
63122
RECT = const(0)
64123
ROUNDRECT = const(1)
65124
SHADOWRECT = const(2)
@@ -86,13 +145,14 @@ def __init__(
86145
super().__init__(x=x, y=y)
87146
self.x = x
88147
self.y = y
89-
self.width = width
90-
self.height = height
148+
self._width = width
149+
self._height = height
91150
self._font = label_font
92151
self._selected = False
93152
self.name = name
94153
self._label = label
95154
self.body = self.fill = self.shadow = None
155+
self.style = style
96156

97157
self._fill_color = _check_color(fill_color)
98158
self._outline_color = _check_color(outline_color)
@@ -108,51 +168,8 @@ def __init__(
108168
if self.selected_outline is None and outline_color is not None:
109169
self.selected_outline = (~self._outline_color) & 0xFFFFFF
110170

111-
if (outline_color is not None) or (fill_color is not None):
112-
if style == Button.RECT:
113-
self.body = Rect(
114-
0,
115-
0,
116-
width,
117-
height,
118-
fill=self._fill_color,
119-
outline=self._outline_color,
120-
)
121-
elif style == Button.ROUNDRECT:
122-
self.body = RoundRect(
123-
0,
124-
0,
125-
width,
126-
height,
127-
r=10,
128-
fill=self._fill_color,
129-
outline=self._outline_color,
130-
)
131-
elif style == Button.SHADOWRECT:
132-
self.shadow = Rect(2, 2, width - 2, height - 2, fill=outline_color)
133-
self.body = Rect(
134-
0,
135-
0,
136-
width - 2,
137-
height - 2,
138-
fill=self._fill_color,
139-
outline=self._outline_color,
140-
)
141-
elif style == Button.SHADOWROUNDRECT:
142-
self.shadow = RoundRect(
143-
2, 2, width - 2, height - 2, r=10, fill=self._outline_color
144-
)
145-
self.body = RoundRect(
146-
0,
147-
0,
148-
width - 2,
149-
height - 2,
150-
r=10,
151-
fill=self._fill_color,
152-
outline=self._outline_color,
153-
)
154-
if self.shadow:
155-
self.append(self.shadow)
171+
self._create_body()
172+
if self.body:
156173
self.append(self.body)
157174

158175
self.label = label
@@ -291,3 +308,31 @@ def label_color(self):
291308
def label_color(self, new_color):
292309
self._label_color = _check_color(new_color)
293310
self._label.color = self._label_color
311+
312+
@property
313+
def width(self):
314+
"""The width of the button"""
315+
return self._width
316+
317+
@width.setter
318+
def width(self, new_width):
319+
self._width = new_width
320+
self._empty_self_group()
321+
self._create_body()
322+
if self.body:
323+
self.append(self.body)
324+
self.label = self.label
325+
326+
@property
327+
def height(self):
328+
"""The height of the button"""
329+
return self._height
330+
331+
@height.setter
332+
def height(self, new_height):
333+
self._height = new_height
334+
self._empty_self_group()
335+
self._create_body()
336+
if self.body:
337+
self.append(self.body)
338+
self.label = self.label

0 commit comments

Comments
 (0)