Skip to content

Commit

Permalink
feat(color): adjust font size of text in Choice box to fit (#5458)
Browse files Browse the repository at this point in the history
  • Loading branch information
philmoz committed Aug 20, 2024
1 parent 5ca2419 commit add61da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
18 changes: 15 additions & 3 deletions radio/src/gui/colorlcd/libui/choice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,13 @@ ChoiceBase::ChoiceBase(Window* parent, const rect_t& rect,
std::function<void(int)> _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(
Expand All @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/libui/choice.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ChoiceBase : public FormField
int vmin = 0;
int vmax = 0;
const char *menuTitle = nullptr;

ChoiceType type;
std::function<int()> _getValue;
std::function<void(int)> _setValue;
std::function<std::string(int)> textHandler;
Expand Down

0 comments on commit add61da

Please sign in to comment.