Skip to content

Commit f8753dc

Browse files
Tinyu-Zhaolbuque
authored andcommitted
libs/m5ui: Fix screen size retrieval in emulator.
Signed-off-by: tinyu <[email protected]>
1 parent a45490b commit f8753dc

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

m5stack/libs/m5ui/base.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,6 @@ def set_bg_color(self, color: int, opa: int, part: int) -> None:
105105
time.sleep(0.01)
106106
self.set_style_bg_opa(opa, part)
107107

108-
@staticmethod
109-
def set_line_color(self, color: int, opa: int, part: int = lv.PART.MAIN) -> None:
110-
"""Set the line color and opacity for a given part of the object.
111-
112-
:param int color: The color to set, can be an integer (hex) or a lv.color object.
113-
:param int opa: The opacity level (0-255).
114-
:param int part: The part of the object to apply the style to (e.g., lv.PART.MAIN).
115-
:return: None
116-
"""
117-
if isinstance(color, int):
118-
color = lv.color_hex(color)
119-
120-
self.set_style_line_color(color, part)
121-
time.sleep(0.01)
122-
self.set_style_line_opa(opa, part)
123-
124108
@staticmethod
125109
def set_bg_grad_color(self, color, opa, grad_color, grad_opd, grad_dir, part: int) -> None:
126110
"""Set the background gradient color of the bar.
@@ -168,3 +152,13 @@ def set_border_color(self, color: int, opa: int, part: int):
168152
self.set_style_border_color(color, part)
169153
time.sleep(0.01)
170154
self.set_style_border_opa(opa, part)
155+
156+
@staticmethod
157+
def get_display_size(self):
158+
"""Get the display size.
159+
160+
:return: A tuple containing the width and height of the display.
161+
:rtype: tuple
162+
"""
163+
scr = lv.screen_active()
164+
return scr.get_width(), scr.get_height()

m5stack/libs/m5ui/switch.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from m5ui.base import M5Base
66
import lvgl as lv
7-
import M5
87

98

109
class M5Switch(lv.switch):
@@ -49,12 +48,12 @@ def __init__(
4948
parent = lv.screen_active()
5049
super().__init__(parent)
5150
self.set_pos(x, y)
52-
self.set_bg_color(bg_c, 255, lv.PART.MAIN | lv.STATE.DEFAULT)
53-
self.set_bg_color(bg_c_checked, 255, lv.PART.INDICATOR | lv.STATE.CHECKED)
54-
self.set_bg_color(circle_c, 255, lv.PART.KNOB | lv.STATE.DEFAULT)
5551
self.set_size(w, h)
5652
self.width = w
5753
self.height = h
54+
self.set_bg_color(bg_c, 255, lv.PART.MAIN | lv.STATE.DEFAULT)
55+
self.set_bg_color(bg_c_checked, 255, lv.PART.INDICATOR | lv.STATE.CHECKED)
56+
self.set_bg_color(circle_c, 255, lv.PART.KNOB | lv.STATE.DEFAULT)
5857

5958
def set_direction(self, direction):
6059
"""Set the direction of the switch.
@@ -89,13 +88,13 @@ def set_size(self, w, h):
8988
if w < 0 or h < 0:
9089
raise ValueError("Width and height must be non-negative")
9190

92-
_width = M5.Display.width()
93-
_height = M5.Display.height()
91+
_width, _height = self.get_display_size()
9492

9593
if w < _width and h < _height:
9694
super().set_size(w, h)
9795
else:
9896
print("Warning: width or height exceed screen size, auto set to screen size")
97+
self.set_pos(0, 0)
9998
super().set_size(_width, _height)
10099

101100
self.width = w

0 commit comments

Comments
 (0)