#include "displayapp/screens/Timer.h" #include "displayapp/screens/Screen.h" #include "displayapp/screens/Symbols.h" #include using namespace Pinetime::Applications::Screens; void Timer::btnEventHandler(lv_obj_t* obj, lv_event_t event) { auto* screen = static_cast(obj->user_data); screen->OnButtonEvent(obj, event); } void Timer::Stop() { int32_t secondsRemaining = timerController.GetSecondsRemaining(); bool ot = timerController.IsOvertime(); if (ot) { minutesToSet = 0; secondsToSet = 0; secondsRemaining = 0; } else { minutesToSet = secondsRemaining / 60; secondsToSet = secondsRemaining % 60; } timerController.StopTimer(); RefreshRunning(); if (ot) { setTimeTextColor(LV_COLOR_RED); } } Timer::Timer(DisplayApp* app, Controllers::TimerController& timerController) : Screen(app), timerController {timerController} { backgroundLabel = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_click(backgroundLabel, true); lv_label_set_long_mode(backgroundLabel, LV_LABEL_LONG_CROP); lv_obj_set_size(backgroundLabel, 240, 240); lv_obj_set_pos(backgroundLabel, 0, 0); lv_label_set_text_static(backgroundLabel, ""); time = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_font(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &neofont3); lv_label_set_text_static(time, "00:00"); lv_obj_align(time, nullptr, LV_ALIGN_CENTER, 0, -20); btnPlayPause = lv_btn_create(lv_scr_act(), nullptr); btnPlayPause->user_data = this; lv_obj_set_event_cb(btnPlayPause, btnEventHandler); lv_obj_set_size(btnPlayPause, 115, 50); lv_obj_align(btnPlayPause, nullptr, LV_ALIGN_IN_BOTTOM_MID, 0, 0); txtPlayPause = lv_label_create(btnPlayPause, nullptr); RefreshRunning(); Refresh(); taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this); } Timer::~Timer() { lv_task_del(taskRefresh); lv_obj_clean(lv_scr_act()); if (timerController.IsRunning() && timerController.IsOvertime()) { timerController.StopTimer(); } } void Timer::Refresh() { if (timerController.IsRunning()) { int32_t seconds = timerController.GetSecondsRemaining(); if (timerController.IsOvertime()) { seconds = -seconds + 1; // "+ 1" is to not show -00:00 again after +00:00 // safety measures, lets not overflow counter as it will display badly if (seconds >= 100 * 60) { Stop(); return; } } lv_label_set_text_fmt(time, "%02lu:%02lu", seconds / 60, seconds % 60); } } void Timer::setTimeTextColor(lv_color_t color) { lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, color); } void Timer::RefreshRunning() { if (timerController.IsRunning()) { if (timerController.IsOvertime()) { setTimeTextColor(LV_COLOR_RED); } else { setTimeTextColor(LV_COLOR_LIME); } lv_label_set_text_static(txtPlayPause, Symbols::pause); } else { setTimeTextColor(LV_COLOR_GRAY); lv_label_set_text_static(txtPlayPause, Symbols::play); CreateButtons(); } }