From add61daa70e2082e319c76c0b0f83ab9beece400 Mon Sep 17 00:00:00 2001 From: philmoz Date: Tue, 20 Aug 2024 10:21:13 +1000 Subject: [PATCH] feat(color): adjust font size of text in Choice box to fit (#5458) --- radio/src/gui/colorlcd/libui/choice.cpp | 18 +++++++++++++++--- radio/src/gui/colorlcd/libui/choice.h | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/radio/src/gui/colorlcd/libui/choice.cpp b/radio/src/gui/colorlcd/libui/choice.cpp index 6b24edc9b9b..f704e62dcc9 100644 --- a/radio/src/gui/colorlcd/libui/choice.cpp +++ b/radio/src/gui/colorlcd/libui/choice.cpp @@ -66,10 +66,13 @@ ChoiceBase::ChoiceBase(Window* parent, const rect_t& rect, std::function _setValue, ChoiceType type) : FormField(parent, rect, choice_create), - vmin(vmin), vmax(vmax), menuTitle(title), + vmin(vmin), vmax(vmax), menuTitle(title), type(type), _getValue(std::move(_getValue)), _setValue(std::move(_setValue)) { + padLeft(PAD_TINY); + padRight(PAD_SMALL); + // Add image lv_obj_t* img = lv_img_create(lvobj); lv_img_set_src( @@ -78,7 +81,8 @@ ChoiceBase::ChoiceBase(Window* parent, const rect_t& rect, // Add label label = lv_label_create(lvobj); - lv_obj_set_pos(label, ICON_W, PAD_TINY); + lv_obj_set_pos(label, type == CHOICE_TYPE_DROPOWN ? ICON_W - 2 : ICON_W, PAD_TINY); + etx_font(label, FONT_XS_INDEX, LV_STATE_USER_1); lv_obj_add_event_cb(lvobj, changedCB, LV_EVENT_VALUE_CHANGED, this); } @@ -106,8 +110,16 @@ std::string Choice::getLabelText() void ChoiceBase::update() { - if (!deleted()) + if (!deleted()) { + if (width() > 0) { + int w = width() - (type == CHOICE_TYPE_DROPOWN ? ICON_W - 2 : ICON_W) - PAD_TINY * 3; + if (getTextWidth(getLabelText().c_str(), 0, FONT(STD)) > w) + lv_obj_add_state(label, LV_STATE_USER_1); + else + lv_obj_clear_state(label, LV_STATE_USER_1); + } lv_label_set_text(label, getLabelText().c_str()); + } } Choice::Choice(Window* parent, const rect_t& rect, int vmin, int vmax, diff --git a/radio/src/gui/colorlcd/libui/choice.h b/radio/src/gui/colorlcd/libui/choice.h index 852f87b4f16..ad6b5ee029b 100644 --- a/radio/src/gui/colorlcd/libui/choice.h +++ b/radio/src/gui/colorlcd/libui/choice.h @@ -59,7 +59,7 @@ class ChoiceBase : public FormField int vmin = 0; int vmax = 0; const char *menuTitle = nullptr; - + ChoiceType type; std::function _getValue; std::function _setValue; std::function textHandler;