From 9ee1160578e22e8dfa3da89dfd6405439d374da7 Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Thu, 21 Jul 2022 22:53:36 +0300 Subject: [PATCH] Reset timer by long pressing on the button (#1214) * Reset timer by long pressing on the button * Consider press_lost as released Otherwise the bar would keep increasing if the finger slid off the button --- src/displayapp/DisplayApp.cpp | 2 +- src/displayapp/screens/Timer.cpp | 100 ++++++++++++++++++++++++------- src/displayapp/screens/Timer.h | 21 +++++-- src/libs/lv_conf.h | 2 +- 4 files changed, 99 insertions(+), 26 deletions(-) diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 731d9c01..93d7277d 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -183,7 +183,7 @@ void DisplayApp::Refresh() { case Messages::TimerDone: if (currentApp == Apps::Timer) { auto* timer = static_cast(currentScreen.get()); - timer->SetDone(); + timer->Reset(); } else { LoadApp(Apps::Timer, DisplayApp::FullRefreshDirections::Down); } diff --git a/src/displayapp/screens/Timer.cpp b/src/displayapp/screens/Timer.cpp index a1df662a..a25be1c4 100644 --- a/src/displayapp/screens/Timer.cpp +++ b/src/displayapp/screens/Timer.cpp @@ -7,7 +7,13 @@ using namespace Pinetime::Applications::Screens; static void btnEventHandler(lv_obj_t* obj, lv_event_t event) { auto* screen = static_cast(obj->user_data); - screen->OnButtonEvent(obj, event); + if (event == LV_EVENT_PRESSED) { + screen->ButtonPressed(); + } else if (event == LV_EVENT_RELEASED || event == LV_EVENT_PRESS_LOST) { + screen->MaskReset(); + } else if (event == LV_EVENT_SHORT_CLICKED) { + screen->ToggleRunning(); + } } Timer::Timer(DisplayApp* app, Controllers::TimerController& timerController) : Screen(app), timerController {timerController} { @@ -23,14 +29,37 @@ Timer::Timer(DisplayApp* app, Controllers::TimerController& timerController) : S lv_obj_align(minuteCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 0); lv_obj_align(secondCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_RIGHT, 0, 0); - btnPlayPause = lv_btn_create(lv_scr_act(), nullptr); + highlightObjectMask = lv_objmask_create(lv_scr_act(), nullptr); + lv_obj_set_size(highlightObjectMask, 240, 50); + lv_obj_align(highlightObjectMask, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, 0); + + lv_draw_mask_line_param_t tmpMaskLine; + + lv_draw_mask_line_points_init(&tmpMaskLine, 0, 0, 0, 240, LV_DRAW_MASK_LINE_SIDE_LEFT); + highlightMask = lv_objmask_add_mask(highlightObjectMask, &tmpMaskLine); + + lv_obj_t* btnHighlight = lv_obj_create(highlightObjectMask, nullptr); + lv_obj_set_style_local_radius(btnHighlight, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE); + lv_obj_set_style_local_bg_color(btnHighlight, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_ORANGE); + lv_obj_set_size(btnHighlight, LV_HOR_RES, 50); + lv_obj_align(btnHighlight, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, 0); + + btnObjectMask = lv_objmask_create(lv_scr_act(), nullptr); + lv_obj_set_size(btnObjectMask, 240, 50); + lv_obj_align(btnObjectMask, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, 0); + + lv_draw_mask_line_points_init(&tmpMaskLine, 0, 0, 0, 240, LV_DRAW_MASK_LINE_SIDE_RIGHT); + btnMask = lv_objmask_add_mask(btnObjectMask, &tmpMaskLine); + + btnPlayPause = lv_btn_create(btnObjectMask, nullptr); btnPlayPause->user_data = this; lv_obj_set_style_local_radius(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE); lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x38, 0x38, 0x38)); lv_obj_set_event_cb(btnPlayPause, btnEventHandler); lv_obj_set_size(btnPlayPause, LV_HOR_RES, 50); - lv_obj_align(btnPlayPause, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, 0); - txtPlayPause = lv_label_create(btnPlayPause, nullptr); + + txtPlayPause = lv_label_create(lv_scr_act(), nullptr); + lv_obj_align(txtPlayPause, btnPlayPause, LV_ALIGN_CENTER, 0, 0); if (timerController.IsRunning()) { SetTimerRunning(); @@ -46,11 +75,46 @@ Timer::~Timer() { lv_obj_clean(lv_scr_act()); } +void Timer::ButtonPressed() { + pressTime = xTaskGetTickCount(); + buttonPressing = true; +} + +void Timer::MaskReset() { + buttonPressing = false; + // A click event is processed before a release event, + // so the release event would override the "Pause" text without this check + if (!timerController.IsRunning()) { + lv_label_set_text_static(txtPlayPause, "Start"); + } + maskPosition = 0; + UpdateMask(); +} + +void Timer::UpdateMask() { + lv_draw_mask_line_param_t maskLine; + + lv_draw_mask_line_points_init(&maskLine, maskPosition, 0, maskPosition, 240, LV_DRAW_MASK_LINE_SIDE_LEFT); + lv_objmask_update_mask(highlightObjectMask, highlightMask, &maskLine); + + lv_draw_mask_line_points_init(&maskLine, maskPosition, 0, maskPosition, 240, LV_DRAW_MASK_LINE_SIDE_RIGHT); + lv_objmask_update_mask(btnObjectMask, btnMask, &maskLine); +} + void Timer::Refresh() { if (timerController.IsRunning()) { uint32_t seconds = timerController.GetTimeRemaining() / 1000; minuteCounter.SetValue(seconds / 60); secondCounter.SetValue(seconds % 60); + } else if (buttonPressing && xTaskGetTickCount() > pressTime + pdMS_TO_TICKS(150)) { + lv_label_set_text_static(txtPlayPause, "Reset"); + maskPosition += 15; + if (maskPosition > 240) { + MaskReset(); + Reset(); + } else { + UpdateMask(); + } } } @@ -66,25 +130,21 @@ void Timer::SetTimerStopped() { lv_label_set_text_static(txtPlayPause, "Start"); } -void Timer::OnButtonEvent(lv_obj_t* obj, lv_event_t event) { - if (event == LV_EVENT_CLICKED) { - if (obj == btnPlayPause) { - if (timerController.IsRunning()) { - uint32_t seconds = timerController.GetTimeRemaining() / 1000; - minuteCounter.SetValue(seconds / 60); - secondCounter.SetValue(seconds % 60); - timerController.StopTimer(); - SetTimerStopped(); - } else if (secondCounter.GetValue() + minuteCounter.GetValue() > 0) { - timerController.StartTimer((secondCounter.GetValue() + minuteCounter.GetValue() * 60) * 1000); - Refresh(); - SetTimerRunning(); - } - } +void Timer::ToggleRunning() { + if (timerController.IsRunning()) { + uint32_t seconds = timerController.GetTimeRemaining() / 1000; + minuteCounter.SetValue(seconds / 60); + secondCounter.SetValue(seconds % 60); + timerController.StopTimer(); + SetTimerStopped(); + } else if (secondCounter.GetValue() + minuteCounter.GetValue() > 0) { + timerController.StartTimer((secondCounter.GetValue() + minuteCounter.GetValue() * 60) * 1000); + Refresh(); + SetTimerRunning(); } } -void Timer::SetDone() { +void Timer::Reset() { minuteCounter.SetValue(0); secondCounter.SetValue(0); SetTimerStopped(); diff --git a/src/displayapp/screens/Timer.h b/src/displayapp/screens/Timer.h index 29b5e813..a6b60a17 100644 --- a/src/displayapp/screens/Timer.h +++ b/src/displayapp/screens/Timer.h @@ -5,29 +5,42 @@ #include "systemtask/SystemTask.h" #include "displayapp/LittleVgl.h" #include "displayapp/widgets/Counter.h" +#include #include "components/timer/TimerController.h" namespace Pinetime::Applications::Screens { class Timer : public Screen { public: - enum class Modes { Normal, Done }; - Timer(DisplayApp* app, Controllers::TimerController& timerController); ~Timer() override; void Refresh() override; - void SetDone(); - void OnButtonEvent(lv_obj_t* obj, lv_event_t event); + void Reset(); + void ToggleRunning(); + void ButtonPressed(); + void MaskReset(); private: void SetTimerRunning(); void SetTimerStopped(); + void UpdateMask(); Controllers::TimerController& timerController; + lv_obj_t* msecTime; lv_obj_t* btnPlayPause; lv_obj_t* txtPlayPause; + + lv_obj_t* btnObjectMask; + lv_obj_t* highlightObjectMask; + lv_objmask_mask_t* btnMask; + lv_objmask_mask_t* highlightMask; + lv_task_t* taskRefresh; Widgets::Counter minuteCounter = Widgets::Counter(0, 59, jetbrains_mono_76); Widgets::Counter secondCounter = Widgets::Counter(0, 59, jetbrains_mono_76); + + bool buttonPressing = false; + int maskPosition = 0; + TickType_t pressTime; }; } diff --git a/src/libs/lv_conf.h b/src/libs/lv_conf.h index 73109c5a..b3ff8f57 100644 --- a/src/libs/lv_conf.h +++ b/src/libs/lv_conf.h @@ -678,7 +678,7 @@ typedef void* lv_obj_user_data_t; #endif /*Mask (dependencies: -)*/ -#define LV_USE_OBJMASK 0 +#define LV_USE_OBJMASK 1 /*Message box (dependencies: lv_rect, lv_btnm, lv_label)*/ #define LV_USE_MSGBOX 0