diff --git a/apps/apps_container.cpp b/apps/apps_container.cpp index 26377c3fc..4ca1aac98 100644 --- a/apps/apps_container.cpp +++ b/apps/apps_container.cpp @@ -79,6 +79,8 @@ void AppsContainer::switchTo(App * app) { m_window.setTitle(app->upperName()); // TODO: Update the battery icon every in a time frame m_window.updateBatteryLevel(); + // TODO: Update the 'isCharging' state when the USB is plugged or unplugged + m_window.updateIsChargingState(); Container::switchTo(app); } diff --git a/apps/apps_window.cpp b/apps/apps_window.cpp index a3b322db6..4b6148973 100644 --- a/apps/apps_window.cpp +++ b/apps/apps_window.cpp @@ -14,8 +14,11 @@ void AppsWindow::setTitle(I18n::Message title) { } void AppsWindow::updateBatteryLevel() { - // TODO: use Ion::Battery::level() - m_titleBarView.setChargeState(Ion::Battery::Charge::EMPTY); + m_titleBarView.setChargeState(Ion::Battery::level()); +} + +void AppsWindow::updateIsChargingState() { + m_titleBarView.setIsCharging(Ion::Battery::isCharging()); } void AppsWindow::refreshPreferences() { diff --git a/apps/apps_window.h b/apps/apps_window.h index 8f4e2cf0e..9b37ca231 100644 --- a/apps/apps_window.h +++ b/apps/apps_window.h @@ -9,6 +9,7 @@ public: AppsWindow(); void setTitle(I18n::Message title); void updateBatteryLevel(); + void updateIsChargingState(); void refreshPreferences(); private: constexpr static KDCoordinate k_titleBarHeight = 18; diff --git a/apps/battery_view.cpp b/apps/battery_view.cpp index 706407a22..5c91df8e5 100644 --- a/apps/battery_view.cpp +++ b/apps/battery_view.cpp @@ -1,8 +1,20 @@ #include "battery_view.h" +const uint8_t flashMask[BatteryView::k_flashHeight][BatteryView::k_flashWidth] = { + {0xDB, 0x00, 0x00, 0xFF}, + {0xB7, 0x00, 0x6D, 0xFF}, + {0x6D, 0x00, 0xDB, 0xFF}, + {0x24, 0x00, 0x00, 0x00}, + {0x00, 0x00, 0x00, 0x24}, + {0xFF, 0xDB, 0x00, 0x6D}, + {0xFF, 0x6D, 0x00, 0xB7}, + {0xFF, 0x00, 0x00, 0xDB}, +}; + BatteryView::BatteryView() : View(), - m_chargeState(Ion::Battery::Charge::SOMEWHERE_INBETWEEN) + m_chargeState(Ion::Battery::Charge::SOMEWHERE_INBETWEEN), + m_isCharging(true) { } @@ -13,19 +25,33 @@ void BatteryView::setChargeState(Ion::Battery::Charge chargeState) { } } +void BatteryView::setIsCharging(bool isCharging) { + if (m_isCharging != isCharging) { + m_isCharging = isCharging; + markRectAsDirty(bounds()); + } +} + +KDColor s_flashWorkingBuffer[BatteryView::k_flashHeight*BatteryView::k_flashWidth]; + 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) { + if (m_isCharging) { + ctx->fillRect(KDRect(k_elementWidth+k_separatorThickness, 0, k_batteryWidth-3*k_elementWidth-2*k_separatorThickness, k_batteryHeight), Palette::YellowLight); + KDRect frame((k_batteryWidth-k_flashWidth)/2, 0, k_flashWidth, k_flashHeight); + ctx->blendRectWithMask(frame, KDColorWhite, (const uint8_t *)flashMask, s_flashWorkingBuffer); + } + if (!m_isCharging && m_chargeState == Ion::Battery::Charge::LOW) { 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::YellowLight); } - if (m_chargeState == Ion::Battery::Charge::SOMEWHERE_INBETWEEN) { + if (!m_isCharging && 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::YellowLight); } - if (m_chargeState == Ion::Battery::Charge::FULL) { + if (!m_isCharging && 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); diff --git a/apps/battery_view.h b/apps/battery_view.h index 99fefd37f..2b500c607 100644 --- a/apps/battery_view.h +++ b/apps/battery_view.h @@ -7,15 +7,19 @@ class BatteryView : public View { public: BatteryView(); void setChargeState(Ion::Battery::Charge chargeState); + void setIsCharging(bool isCharging); void drawRect(KDContext * ctx, KDRect rect) const override; KDSize minimalSizeForOptimalDisplay() const override; + constexpr static int k_flashHeight = 8; + constexpr static int k_flashWidth = 4; private: constexpr static KDCoordinate k_batteryHeight = 8; - constexpr static KDCoordinate k_batteryWidth = 13; + constexpr static KDCoordinate k_batteryWidth = 15; constexpr static KDCoordinate k_elementWidth = 1; constexpr static KDCoordinate k_capHeight = 4; constexpr static KDCoordinate k_separatorThickness = 1; Ion::Battery::Charge m_chargeState; + bool m_isCharging; }; #endif diff --git a/apps/title_bar_view.cpp b/apps/title_bar_view.cpp index e67e89c9a..3c668aa4d 100644 --- a/apps/title_bar_view.cpp +++ b/apps/title_bar_view.cpp @@ -26,6 +26,10 @@ void TitleBarView::setChargeState(Ion::Battery::Charge chargeState) { m_batteryView.setChargeState(chargeState); } +void TitleBarView::setIsCharging(bool isCharging) { + m_batteryView.setIsCharging(isCharging); +} + int TitleBarView::numberOfSubviews() const { return 3; } diff --git a/apps/title_bar_view.h b/apps/title_bar_view.h index 9965fb7d6..98ca955f1 100644 --- a/apps/title_bar_view.h +++ b/apps/title_bar_view.h @@ -11,6 +11,7 @@ public: void drawRect(KDContext * ctx, KDRect rect) const override; void setTitle(I18n::Message title); void setChargeState(Ion::Battery::Charge chargeState); + void setIsCharging(bool isCharging); void refreshPreferences(); private: constexpr static KDCoordinate k_batteryLeftMargin = 5;