diff options
Diffstat (limited to 'src/displayapp/screens/Timer.cpp')
| -rw-r--r-- | src/displayapp/screens/Timer.cpp | 121 |
1 files changed, 30 insertions, 91 deletions
diff --git a/src/displayapp/screens/Timer.cpp b/src/displayapp/screens/Timer.cpp index 332fcef..91e6f49 100644 --- a/src/displayapp/screens/Timer.cpp +++ b/src/displayapp/screens/Timer.cpp @@ -6,19 +6,23 @@ using namespace Pinetime::Applications::Screens; -static void btnEventHandler(lv_obj_t* obj, lv_event_t event) { - Timer* screen = static_cast<Timer*>(obj->user_data); +void Timer::btnEventHandler(lv_obj_t* obj, lv_event_t event) { + auto* screen = static_cast<Timer*>(obj->user_data); screen->OnButtonEvent(obj, event); } -void Timer::createButtons() { +void Timer::CreateButtons() { + if (btnMinutesUp != nullptr) { + return; + } + btnMinutesUp = lv_btn_create(lv_scr_act(), nullptr); btnMinutesUp->user_data = this; lv_obj_set_event_cb(btnMinutesUp, btnEventHandler); lv_obj_set_size(btnMinutesUp, 60, 40); lv_obj_align(btnMinutesUp, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, -85); txtMUp = lv_label_create(btnMinutesUp, nullptr); - lv_label_set_text(txtMUp, "+"); + lv_label_set_text_static(txtMUp, "+"); btnMinutesDown = lv_btn_create(lv_scr_act(), nullptr); btnMinutesDown->user_data = this; @@ -26,7 +30,7 @@ void Timer::createButtons() { lv_obj_set_size(btnMinutesDown, 60, 40); lv_obj_align(btnMinutesDown, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, +35); txtMDown = lv_label_create(btnMinutesDown, nullptr); - lv_label_set_text(txtMDown, "-"); + lv_label_set_text_static(txtMDown, "-"); btnSecondsUp = lv_btn_create(lv_scr_act(), nullptr); btnSecondsUp->user_data = this; @@ -34,7 +38,7 @@ void Timer::createButtons() { lv_obj_set_size(btnSecondsUp, 60, 40); lv_obj_align(btnSecondsUp, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, -20, -85); txtSUp = lv_label_create(btnSecondsUp, nullptr); - lv_label_set_text(txtSUp, "+"); + lv_label_set_text_static(txtSUp, "+"); btnSecondsDown = lv_btn_create(lv_scr_act(), nullptr); btnSecondsDown->user_data = this; @@ -42,10 +46,10 @@ void Timer::createButtons() { lv_obj_set_size(btnSecondsDown, 60, 40); lv_obj_align(btnSecondsDown, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, -20, +35); txtSDown = lv_label_create(btnSecondsDown, nullptr); - lv_label_set_text(txtSDown, "-"); + lv_label_set_text_static(txtSDown, "-"); } -void Timer::stop() { +void Timer::Stop() { int32_t secondsRemaining = timerController.GetSecondsRemaining(); if (timerController.IsOvertime()) { minutesToSet = 0; @@ -57,50 +61,31 @@ void Timer::stop() { } timerController.StopTimer(); timerController.StopAlerting(); - lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY); - lv_label_set_text_fmt(time, "%02lu:%02lu", secondsRemaining / 60, secondsRemaining % 60); - lv_label_set_text(txtPlayPause, Symbols::play); - createButtons(); + RefreshRunning(); } Timer::Timer(DisplayApp* app, Controllers::TimerController& timerController) - : Screen(app), running {true}, 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(backgroundLabel, ""); + 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); - int32_t seconds = timerController.GetSecondsRemaining(); - bool overtime = timerController.IsOvertime(); - - if (overtime) { - seconds = -seconds + 1; // "+ 1" is to not show -00:00 again after +00:00 - lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED); - } else { - lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY); - } - - lv_label_set_text_fmt(time, "%02lu:%02lu", seconds / 60, seconds % 60); - + lv_label_set_text_static(time, "00:00"); lv_obj_align(time, lv_scr_act(), 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, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, 0); txtPlayPause = lv_label_create(btnPlayPause, nullptr); - if (timerController.IsRunning()) { - lv_label_set_text(txtPlayPause, overtime ? Symbols::stop : Symbols::pause); - } else { - lv_label_set_text(txtPlayPause, Symbols::play); - createButtons(); - } + RefreshRunning(); + Refresh(); taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this); } @@ -114,6 +99,7 @@ Timer::~Timer() { } void Timer::Refresh() { + if (timerController.IsRunning()) { int32_t seconds = timerController.GetSecondsRemaining(); if (timerController.IsOvertime()) { @@ -121,7 +107,7 @@ void Timer::Refresh() { // safety measures, lets not overflow counter as it will display badly if (seconds >= 100 * 60) { - stop(); + Stop(); return; } } @@ -129,65 +115,18 @@ void Timer::Refresh() { } } -void Timer::OnButtonEvent(lv_obj_t* obj, lv_event_t event) { - if (event == LV_EVENT_CLICKED) { - if (obj == btnPlayPause) { - if (timerController.IsRunning()) { - stop(); - } else if (secondsToSet + minutesToSet > 0) { - lv_label_set_text(txtPlayPause, Symbols::pause); - timerController.StartTimer((secondsToSet + minutesToSet * 60) * 1000); - - // inlined destroyButtons() - lv_obj_del(btnSecondsDown); - btnSecondsDown = nullptr; - lv_obj_del(btnSecondsUp); - btnSecondsUp = nullptr; - lv_obj_del(btnMinutesDown); - btnMinutesDown = nullptr; - lv_obj_del(btnMinutesUp); - btnMinutesUp = nullptr; - } +void Timer::RefreshRunning() { + if (timerController.IsRunning()) { + if (timerController.IsOvertime()) { + lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED); } else { - if (!timerController.IsRunning()) { - if (obj == btnMinutesUp) { - if (minutesToSet >= 59) { - minutesToSet = 0; - } else { - minutesToSet++; - } - lv_label_set_text_fmt(time, "%02d:%02d", minutesToSet, secondsToSet); - - } else if (obj == btnMinutesDown) { - if (minutesToSet == 0) { - minutesToSet = 59; - } else { - minutesToSet--; - } - lv_label_set_text_fmt(time, "%02d:%02d", minutesToSet, secondsToSet); - - } else if (obj == btnSecondsUp) { - if (secondsToSet >= 59) { - secondsToSet = 0; - } else { - secondsToSet++; - } - lv_label_set_text_fmt(time, "%02d:%02d", minutesToSet, secondsToSet); - - } else if (obj == btnSecondsDown) { - if (secondsToSet == 0) { - secondsToSet = 59; - } else { - secondsToSet--; - } - lv_label_set_text_fmt(time, "%02d:%02d", minutesToSet, secondsToSet); - } - } + lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_LIME); } + lv_label_set_text_static(txtPlayPause, Symbols::pause); + } else { + lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY); + lv_label_set_text_static(txtPlayPause, Symbols::play); + CreateButtons(); } } -void Timer::SetDone() { - lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED); - lv_label_set_text(txtPlayPause, Symbols::stop); -} |
