|
| 1 | +# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +from m5ui.base import M5Base |
| 6 | +import lvgl as lv |
| 7 | +import m5ui |
| 8 | + |
| 9 | + |
| 10 | +class M5Msgbox(lv.msgbox): |
| 11 | + def __init__( |
| 12 | + self, |
| 13 | + title="", |
| 14 | + x=0, |
| 15 | + y=0, |
| 16 | + w=0, |
| 17 | + h=0, |
| 18 | + parent=None, |
| 19 | + ): |
| 20 | + if parent is None: |
| 21 | + parent = lv.screen_active() |
| 22 | + super().__init__(parent) |
| 23 | + self.set_align(lv.ALIGN.DEFAULT) |
| 24 | + self.add_title(title) |
| 25 | + self.set_pos(x, y) |
| 26 | + self.set_size(w, h) |
| 27 | + |
| 28 | + def add_text( |
| 29 | + self, |
| 30 | + text, |
| 31 | + text_c=0x212121, |
| 32 | + text_opa=255, |
| 33 | + bg_c=0xFFFFFF, |
| 34 | + bg_opa=255, |
| 35 | + font=lv.font_montserrat_14, |
| 36 | + ): |
| 37 | + _label = m5ui.M5Label( |
| 38 | + text=text, |
| 39 | + text_c=text_c, |
| 40 | + bg_c=bg_c, |
| 41 | + bg_opa=bg_opa, |
| 42 | + font=font, |
| 43 | + parent=self.get_content(), |
| 44 | + ) |
| 45 | + _label.set_width(lv.pct(100)) |
| 46 | + _label.set_style_text_opa(text_opa, lv.PART.MAIN | lv.STATE.DEFAULT) |
| 47 | + return _label |
| 48 | + |
| 49 | + def add_button( |
| 50 | + self, |
| 51 | + icon=None, |
| 52 | + text="", |
| 53 | + bg_c=0x2196F3, |
| 54 | + bg_opa=255, |
| 55 | + text_c=0xFFFFFF, |
| 56 | + text_opa=255, |
| 57 | + font=lv.font_montserrat_14, |
| 58 | + option="footer", |
| 59 | + ): |
| 60 | + _parent = None |
| 61 | + _w = 0 |
| 62 | + _h = 0 |
| 63 | + if option == "header": |
| 64 | + _parent = self.get_header() |
| 65 | + if _parent is None: |
| 66 | + self.add_title("") |
| 67 | + _parent = self.get_header() |
| 68 | + _w = lv.DPI_DEF // 3 |
| 69 | + _h = lv.pct(100) |
| 70 | + elif option == "footer": |
| 71 | + _parent = self.get_footer() |
| 72 | + if _parent is None: |
| 73 | + _tmp_btn = self.add_footer_button("") |
| 74 | + _parent = self.get_footer() |
| 75 | + _tmp_btn.delete() |
| 76 | + _w = lv.SIZE_CONTENT |
| 77 | + _h = lv.pct(100) |
| 78 | + _button = m5ui.M5Button( |
| 79 | + text=text, w=_w, h=_h, bg_c=bg_c, text_c=text_c, font=font, parent=_parent |
| 80 | + ) |
| 81 | + _button.set_style_bg_opa(bg_opa, lv.PART.MAIN | lv.STATE.DEFAULT) |
| 82 | + _button.set_style_text_opa(text_opa, lv.PART.MAIN | lv.STATE.DEFAULT) |
| 83 | + |
| 84 | + if icon is not None: |
| 85 | + _icon = lv.image(_button) |
| 86 | + _icon.set_src(icon) |
| 87 | + _icon.set_align(lv.ALIGN.CENTER) |
| 88 | + return _button |
| 89 | + |
| 90 | + def __getattr__(self, name): |
| 91 | + if hasattr(M5Base, name): |
| 92 | + method = getattr(M5Base, name) |
| 93 | + bound_method = lambda *args, **kwargs: method(self, *args, **kwargs) |
| 94 | + setattr(self, name, bound_method) |
| 95 | + return bound_method |
| 96 | + raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'") |
0 commit comments