diff --git a/apps/Makefile b/apps/Makefile index d29a5b724..cc93bc87a 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -10,6 +10,7 @@ app_objs += $(addprefix apps/,\ apps_container.o\ apps_window.o\ banner_view.o\ + battery_view.o\ constant.o\ cursor_view.o\ curve_view.o\ diff --git a/apps/battery_view.cpp b/apps/battery_view.cpp new file mode 100644 index 000000000..0d58879a6 --- /dev/null +++ b/apps/battery_view.cpp @@ -0,0 +1,37 @@ +#include "battery_view.h" + +BatteryView::BatteryView() : + View(), + m_chargeState(Ion::Battery::Charge::SOMEWHERE_INBETWEEN) +{ +} + +void BatteryView::setChargeState(Ion::Battery::Charge chargeState) { + if (chargeState != m_chargeState) { + m_chargeState = chargeState; + markRectAsDirty(bounds()); + } +} + +void BatteryView::drawRect(KDContext * ctx, KDRect rect) const { + /* We draw from left to right. The middle part representing the battery + *'content' depends on the charge */ + ctx->fillRect(KDRect(0, 0, k_elementWidth, k_batteryHeight), KDColorWhite); + if (m_chargeState == Ion::Battery::Charge::EMPTY) { + ctx->fillRect(KDRect(k_elementWidth+k_separatorThickness, 0, 2*k_elementWidth, k_batteryHeight), Palette::LowBattery); + ctx->fillRect(KDRect(3*k_elementWidth+k_separatorThickness, 0, k_batteryWidth-5*k_elementWidth-2*k_separatorThickness, k_batteryHeight), Palette::YellowTwo); + } + if (m_chargeState == Ion::Battery::Charge::SOMEWHERE_INBETWEEN) { + ctx->fillRect(KDRect(k_elementWidth+k_separatorThickness, 0, (k_batteryWidth-3*k_elementWidth-2*k_separatorThickness)/2, k_batteryHeight), KDColorWhite); + ctx->fillRect(KDRect(k_elementWidth+k_separatorThickness+(k_batteryWidth-3*k_elementWidth-2*k_separatorThickness)/2, 0, (k_batteryWidth-3*k_elementWidth-2*k_separatorThickness)/2, k_batteryHeight), Palette::YellowTwo); + } + if (m_chargeState == Ion::Battery::Charge::FULL) { + ctx->fillRect(KDRect(k_elementWidth+k_separatorThickness, 0, k_batteryWidth-3*k_elementWidth-2*k_separatorThickness, k_batteryHeight), KDColorWhite); + } + ctx->fillRect(KDRect(k_batteryWidth-2*k_elementWidth, 0, k_elementWidth, k_batteryHeight), KDColorWhite); + ctx->fillRect(KDRect(k_batteryWidth-k_elementWidth, (k_batteryHeight-k_capHeight)/2, k_elementWidth, k_capHeight), KDColorWhite); +} + +KDSize BatteryView::minimalSizeForOptimalDisplay() { + return KDSize(k_batteryWidth, k_batteryHeight); +} diff --git a/apps/battery_view.h b/apps/battery_view.h new file mode 100644 index 000000000..52e5aa489 --- /dev/null +++ b/apps/battery_view.h @@ -0,0 +1,21 @@ +#ifndef APPS_BATTERY_VIEW_H +#define APPS_BATTERY_VIEW_H + +#include + +class BatteryView : public View { +public: + BatteryView(); + void setChargeState(Ion::Battery::Charge chargeState); + void drawRect(KDContext * ctx, KDRect rect) const override; + KDSize minimalSizeForOptimalDisplay() override; +private: + constexpr static KDCoordinate k_batteryHeight = 8; + constexpr static KDCoordinate k_batteryWidth = 13; + constexpr static KDCoordinate k_elementWidth = 1; + constexpr static KDCoordinate k_capHeight = 4; + constexpr static KDCoordinate k_separatorThickness = 1; + Ion::Battery::Charge m_chargeState; +}; + +#endif