Skip to content

Commit

Permalink
BatteryIcon: fix set color when using color on low battery
Browse files Browse the repository at this point in the history
  • Loading branch information
ljahn committed Dec 23, 2024
1 parent d69cfcf commit cd14b2f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/displayapp/screens/BatteryIcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ BatteryIcon::BatteryIcon(bool colorOnLowBattery) : colorOnLowBattery {colorOnLow
void BatteryIcon::Create(lv_obj_t* parent) {
batteryImg = lv_img_create(parent, nullptr);
lv_img_set_src(batteryImg, &batteryicon);
lv_obj_set_style_local_image_recolor(batteryImg, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_set_style_local_image_recolor(batteryImg, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, baseColor);

batteryJuice = lv_obj_create(batteryImg, nullptr);
lv_obj_set_width(batteryJuice, 8);
Expand All @@ -30,21 +30,31 @@ void BatteryIcon::SetBatteryPercentage(uint8_t percentage) {
static constexpr int lowBatteryThreshold = 15;
static constexpr int criticalBatteryThreshold = 5;
if (percentage > lowBatteryThreshold) {
SetColor(LV_COLOR_WHITE);
_SetColor(baseColor);
} else if (percentage > criticalBatteryThreshold) {
SetColor(LV_COLOR_ORANGE);
_SetColor(LV_COLOR_ORANGE);
} else {
SetColor(Colors::deepOrange);
_SetColor(Colors::deepOrange);
}
}
}

void BatteryIcon::SetColor(lv_color_t color) {
baseColor = color;
_SetColor(color);
}

void BatteryIcon::_SetColor(lv_color_t color) {
lv_obj_set_style_local_image_recolor(batteryImg, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, color);
lv_obj_set_style_local_image_recolor_opa(batteryImg, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);
lv_obj_set_style_local_bg_color(batteryJuice, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, color);
}

void BatteryIcon::SetVisible(bool visible) {
lv_obj_set_hidden(batteryImg, !visible);
lv_obj_set_hidden(batteryJuice, !visible);
}

const char* BatteryIcon::GetPlugIcon(bool isCharging) {
if (isCharging)
return Symbols::plug;
Expand Down
4 changes: 4 additions & 0 deletions src/displayapp/screens/BatteryIcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Pinetime {
public:
explicit BatteryIcon(bool colorOnLowBattery);
void Create(lv_obj_t* parent);
void SetVisible(bool visible);

void SetColor(lv_color_t);
void SetBatteryPercentage(uint8_t percentage);
Expand All @@ -21,6 +22,9 @@ namespace Pinetime {
lv_obj_t* batteryImg;
lv_obj_t* batteryJuice;
bool colorOnLowBattery = false;

lv_color_t baseColor = LV_COLOR_WHITE;
void _SetColor(lv_color_t color);
};
}
}
Expand Down

0 comments on commit cd14b2f

Please sign in to comment.