~aleteoryx/muditaos

ref: cd54ba058903b2c518f246cfdbc46ee33f41c1c3 muditaos/module-apps/application-meditation/widgets/TimerProperty.hpp -rw-r--r-- 2.1 KiB
cd54ba05 — Maciej-Mudita [MOS-202] Add meditation parameters to non-volatile memory 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <gui/widgets/Circle.hpp>
#include <gui/widgets/text/Label.hpp>
#include <gui/widgets/Rect.hpp>
#include <gui/widgets/BoxLayout.hpp>

#include <chrono>

namespace gui
{
    class TimerProperty : public Rect
    {
        class State
        {
            static constexpr int counterMaxDigits             = 2;
            static constexpr int minimalValue                 = 1;
            static constexpr int maximalValue                 = 99;

            bool resetValueOnNumeric = true;
            int timeInMinutes;

          public:
            [[nodiscard]] std::chrono::minutes getTime() const noexcept
            {
                return std::chrono::minutes{timeInMinutes};
            }
            bool setTime(int value);
            void checkBounds() noexcept;
            void putNumericValue(int digit) noexcept;
            void delNumericValue() noexcept;
            void increment() noexcept;
            void decrement() noexcept;
            void onFocus() noexcept
            {
                resetValueOnNumeric = true;
                checkBounds();
            }
        } state;

      public:
        using OnChangeCallback = std::function<void(std::int32_t newValue)>;

        TimerProperty(
            Item *parent, const std::uint32_t x, const std::uint32_t y, const std::uint32_t w, const std::uint32_t h);

        bool onFocus(bool isFocused) final;
        bool onInput(const InputEvent &inputEvent) final;
        [[nodiscard]] std::chrono::minutes getTime() noexcept;
        bool setTime(std::int32_t newValue);
        void setOnChangeCallback(OnChangeCallback callback);

      private:
        Circle *circle       = nullptr;
        VBox *centerBody     = nullptr;
        Label *timeLabel     = nullptr;
        Rect *divRect        = nullptr;
        Label *timeUnitLabel = nullptr;
        OnChangeCallback onChangeCallback;

        void build();
        void setMeditationTime();
    };

} // namespace gui