Skip to content

Commit 5ac4590

Browse files
committed
libs/m5ui/spinbox.py: Fix bugs related to size and state.
Signed-off-by: lbuque <[email protected]>
1 parent e647471 commit 5ac4590

File tree

4 files changed

+140
-93
lines changed

4 files changed

+140
-93
lines changed

docs/en/m5ui/spinbox.rst

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -93,41 +93,6 @@ M5Spinbox
9393
9494
spinbox_0.toggle_flag(lv.obj.FLAG.HIDDEN)
9595
96-
.. py:method:: set_state(state, value)
97-
98-
Set the state of the spinbox. If ``value`` is True, the state is set; if False, the state is unset.
99-
100-
:param int state: The state to set.
101-
:param bool value: If True, the state is set; if False, the state is unset.
102-
:return: None
103-
104-
UiFlow2 Code Block:
105-
106-
|set_state.png|
107-
108-
MicroPython Code Block:
109-
110-
.. code-block:: python
111-
112-
spinbox_0.set_state(lv.STATE.DISABLED, True)
113-
114-
.. py:method:: toggle_state(state)
115-
116-
Toggle the state of the spinbox. If the state is set, it is unset; if not set, it is set.
117-
118-
:param int state: The state to toggle.
119-
:return: None
120-
121-
UiFlow2 Code Block:
122-
123-
|toggle_state.png|
124-
125-
MicroPython Code Block:
126-
127-
.. code-block:: python
128-
129-
spinbox_0.toggle_state(lv.STATE.CHECKED)
130-
13196
.. py:method:: set_pos(x, y)
13297
13398
Set the position of the spinbox.
@@ -180,41 +145,6 @@ M5Spinbox
180145
181146
spinbox_0.set_y(250)
182147
183-
.. py:method:: set_size(width, height)
184-
185-
Set the size of the spinbox.
186-
187-
:param int width: The width of the spinbox.
188-
:param int height: The height of the spinbox.
189-
:return: None
190-
191-
UiFlow2 Code Block:
192-
193-
|set_size.png|
194-
195-
MicroPython Code Block:
196-
197-
.. code-block:: python
198-
199-
spinbox_0.set_size(150, 40)
200-
201-
.. py:method:: set_width(width)
202-
203-
Set the width of the spinbox.
204-
205-
:param int width: The width of the spinbox.
206-
:return: None
207-
208-
UiFlow2 Code Block:
209-
210-
|set_width.png|
211-
212-
MicroPython Code Block:
213-
214-
.. code-block:: python
215-
216-
spinbox_0.set_width(180)
217-
218148
.. py:method:: get_width()
219149
220150
Get the width of the spinbox.
@@ -232,23 +162,6 @@ M5Spinbox
232162
233163
width = spinbox_0.get_width()
234164
235-
.. py:method:: set_height(height)
236-
237-
Set the height of the spinbox.
238-
239-
:param int height: The height of the spinbox.
240-
:return: None
241-
242-
UiFlow2 Code Block:
243-
244-
|set_height.png|
245-
246-
MicroPython Code Block:
247-
248-
.. code-block:: python
249-
250-
spinbox_0.set_height(50)
251-
252165
.. py:method:: get_height()
253166
254167
Get the height of the spinbox.

m5stack/libs/m5ui/port.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import lvgl as lv
66
import sys
77
import lv_utils
8-
import m5utils
98
import micropython
109

1110
_event_loop_instance = None
@@ -27,6 +26,8 @@ def __init__(self, freq=33, max_scheduled=2):
2726

2827
self._initialized = True
2928
self.delay = 1000 // freq
29+
import m5utils
30+
3031
self.timer = m5utils.Timer(
3132
0, mode=m5utils.Timer.PERIODIC, period=self.delay, callback=self.timer_cb
3233
)

m5stack/libs/m5ui/spinbox.py

Lines changed: 126 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(
4545
self.remove_style_all()
4646
self.set_flex_flow(lv.FLEX_FLOW.ROW)
4747
self.set_flex_align(lv.FLEX_ALIGN.SPACE_AROUND, lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.CENTER)
48-
self.set_size(w, h)
48+
super().set_size(w, h)
4949
self.set_style_pad_gap(5, 0)
5050
self.set_pos(x, y)
5151
self.set_style_text_font(font, lv.PART.MAIN | lv.STATE.DEFAULT)
@@ -80,6 +80,124 @@ def __init__(
8080
self._btn_inc.add_event_cb(self._increment_event_cb, lv.EVENT.SHORT_CLICKED, None)
8181
self._btn_inc.add_event_cb(self._increment_event_cb, lv.EVENT.LONG_PRESSED_REPEAT, None)
8282

83+
def set_state(self, state: int, value: bool) -> None:
84+
"""Set the state of the spinbox. If ``value`` is True, the state is set; if False, the state is unset.
85+
86+
:param int state: The state to set.
87+
:param bool value: If True, the state is set; if False, the state is unset.
88+
:return: None
89+
90+
UiFlow2 Code Block:
91+
92+
|set_state.png|
93+
94+
MicroPython Code Block:
95+
96+
.. code-block:: python
97+
98+
spinbox_0.set_state(lv.STATE.DISABLED, True)
99+
"""
100+
if value:
101+
self._spinbox.add_state(state)
102+
self._btn_dec.add_state(state)
103+
self._btn_inc.add_state(state)
104+
else:
105+
self._spinbox.remove_state(state)
106+
self._btn_dec.remove_state(state)
107+
self._btn_inc.remove_state(state)
108+
109+
def toggle_state(self, state: int) -> None:
110+
"""Toggle the state of the spinbox. If the state is set, it is unset; if not set, it is set.
111+
112+
:param int state: The state to toggle.
113+
:return: None
114+
115+
UiFlow2 Code Block:
116+
117+
|toggle_state.png|
118+
119+
MicroPython Code Block:
120+
121+
.. code-block:: python
122+
123+
spinbox_0.toggle_state(lv.STATE.CHECKED)
124+
"""
125+
if self._spinbox.has_state(state):
126+
self._spinbox.remove_state(state)
127+
self._btn_dec.remove_state(state)
128+
self._btn_inc.remove_state(state)
129+
else:
130+
self._spinbox.add_state(state)
131+
self._btn_dec.add_state(state)
132+
self._btn_inc.add_state(state)
133+
return
134+
135+
def set_size(self, width: int, height: int) -> None:
136+
"""Set the size of the spinbox.
137+
138+
:param int width: The width of the spinbox.
139+
:param int height: The height of the spinbox.
140+
:return: None
141+
142+
UiFlow2 Code Block:
143+
144+
|set_size.png|
145+
146+
MicroPython Code Block:
147+
148+
.. code-block:: python
149+
150+
spinbox_0.set_size(150, 40)
151+
"""
152+
super().set_size(width, height)
153+
self._btn_dec.set_size(height, height)
154+
self._btn_inc.set_size(height, height)
155+
self._spinbox.set_height(height)
156+
self._spinbox.set_flex_grow(1)
157+
self.update_layout()
158+
159+
def set_width(self, width: int) -> None:
160+
"""Set the width of the spinbox.
161+
162+
:param int width: The width of the spinbox.
163+
:return: None
164+
165+
UiFlow2 Code Block:
166+
167+
|set_width.png|
168+
169+
MicroPython Code Block:
170+
171+
.. code-block:: python
172+
173+
spinbox_0.set_width(180)
174+
"""
175+
super().set_width(width)
176+
self._spinbox.set_flex_grow(1)
177+
self.update_layout()
178+
179+
def set_height(self, height: int) -> None:
180+
"""Set the height of the spinbox.
181+
182+
:param int height: The height of the spinbox.
183+
:return: None
184+
185+
UiFlow2 Code Block:
186+
187+
|set_height.png|
188+
189+
MicroPython Code Block:
190+
191+
.. code-block:: python
192+
193+
spinbox_0.set_height(50)
194+
"""
195+
super().set_height(height)
196+
self._btn_dec.set_size(height, height)
197+
self._btn_inc.set_size(height, height)
198+
self._spinbox.set_height(height)
199+
self.update_layout()
200+
83201
def _check_value(self, value, min_value, max_value, digit_count, prec):
84202
if value < min_value or value > max_value:
85203
warnings.warn(f"Value must be between {min_value} and {max_value}.")
@@ -216,12 +334,12 @@ def set_style_radius(self, radius: int, part: int) -> None:
216334

217335
def _increment_event_cb(self, event_struct):
218336
event = event_struct.code
219-
if event in (lv.EVENT.CLICKED, lv.EVENT.SHORT_CLICKED, lv.EVENT.LONG_PRESSED_REPEAT):
337+
if event in (lv.EVENT.SHORT_CLICKED, lv.EVENT.LONG_PRESSED_REPEAT):
220338
self._spinbox.increment()
221339

222340
def _decrement_event_cb(self, event_struct):
223341
event = event_struct.code
224-
if event in (lv.EVENT.CLICKED, lv.EVENT.SHORT_CLICKED, lv.EVENT.LONG_PRESSED_REPEAT):
342+
if event in (lv.EVENT.SHORT_CLICKED, lv.EVENT.LONG_PRESSED_REPEAT):
225343
self._spinbox.decrement()
226344

227345
def set_digit_format(self, digit_count: int, sep_pos: int) -> None:
@@ -325,6 +443,9 @@ def set_step(self, step: float | int) -> None:
325443
spinbox0.set_step(1)
326444
spinbox0.set_step(0.1)
327445
"""
446+
if self._digit_count - self._sep_pos == 0:
447+
warnings.warn(f"No decimal places, step {step} converted to {int(step * 10)}.")
448+
step = int(step * 10)
328449
self._spinbox.set_step(self.value2raw(step, self._digit_count, self._sep_pos))
329450

330451
@staticmethod
@@ -335,7 +456,7 @@ def value2raw(value: float | int, digit_count: int, sep_pos: int) -> int:
335456
:return: The converted integer value.
336457
:rtype: int
337458
"""
338-
if sep_pos < 0 or sep_pos >= digit_count:
459+
if sep_pos < 0 or sep_pos > digit_count:
339460
raise ValueError("sep_pos must be between 0 and digit_count.")
340461

341462
dec_pos = digit_count - sep_pos
@@ -351,7 +472,7 @@ def raw2value(raw: int, digit_count: int, sep_pos: int) -> float | int:
351472
:return: The converted float value.
352473
:rtype: float
353474
"""
354-
if sep_pos < 0 or sep_pos >= digit_count:
475+
if sep_pos < 0 or sep_pos > digit_count:
355476
raise ValueError("sep_pos must be between 0 and digit_count.")
356477

357478
if sep_pos == 0:

tests/m5ui/test_spinbox.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ def test_step(self):
6969
self.spinbox1.decrement()
7070
self.assertEqual(self.spinbox1.get_value(), 50)
7171

72+
def test_set_size(self):
73+
self.spinbox1.set_size(150, 30)
74+
self.assertEqual((self.spinbox1.get_width(), self.spinbox1.get_height()), (150, 30))
75+
76+
def test_set_width(self):
77+
self.spinbox1.set_width(180)
78+
self.assertEqual(self.spinbox1.get_width(), 180)
79+
80+
def test_set_height(self):
81+
self.spinbox1.set_height(35)
82+
self.assertEqual(self.spinbox1.get_height(), 35)
83+
7284

7385
if __name__ == "__main__":
7486
unittest.main()

0 commit comments

Comments
 (0)