~aleteoryx/muditaos

ref: b7bc7cc89fc09f93240473b8aef047f349d2e8c0 muditaos/module-apps/application-meditation/widgets/MeditationTimer.cpp -rw-r--r-- 4.0 KiB
b7bc7cc8 — Marcin Smoczyński changelog: update for v0.42.1 (#820) 5 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "MeditationTimer.hpp"

#include <cassert>

#include <application-meditation/data/Style.hpp>
#include <time/time_conversion.hpp>

#include "GuiTimer.hpp"

namespace gui
{
    namespace
    {
        constexpr gui::ms TimerInterval{1000};
    } // namespace

    MeditationTimer::MeditationTimer(std::uint32_t x,
                                     std::uint32_t y,
                                     std::uint32_t width,
                                     std::uint32_t height,
                                     app::ApplicationMeditation *app,
                                     Item *_parent)
        : HBox(_parent, x, y, width, height), application{app}
    {
        setEdges(RectangleEdgeFlags::GUI_RECT_EDGE_NO_EDGES);
        build();
    }

    void MeditationTimer::build()
    {
        namespace timerStyle = style::meditation::timer;

        const Point boxCenter(getX() + (getWidth() / 2), getY() + (getHeight() / 2));
        Circle::ShapeParams params;
        params.setCenterPoint(boxCenter)
            .setRadius(timerStyle::Radius)
            .setBorderColor(timerStyle::BorderColor)
            .setPenWidth(timerStyle::PenWidth)
            .setFocusPenWidth(timerStyle::PenWidth);
        progressBar = new CircularProgressBar(this, params);

        timer = new Text(progressBar, 0, 0, getWidth(), getHeight());
        timer->setEdges(RectangleEdgeFlags::GUI_RECT_EDGE_NO_EDGES);
        timer->setFont(style::window::font::supersizemelight);
        timer->setAlignment(Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
        timer->setEditMode(EditMode::BROWSE);

        onReset();
    }

    void MeditationTimer::onReset()
    {
        updateTimer();
        progressBar->setMaximum(duration.count());
        progressBar->setValue(0);
    }

    void MeditationTimer::update()
    {
        updateTimer();
        progressBar->setPercentageValue(calculatePercentageValue());
        application->refreshWindow(RefreshModes::GUI_REFRESH_FAST);
    }

    void MeditationTimer::updateTimer()
    {
        using utils::time::Duration;

        const auto secondsRemaining = duration - elapsed;
        const Duration remainingDuration{std::time_t{secondsRemaining.count()}};
        timer->setText(remainingDuration.str(Duration::DisplayedFormat::Fixed0M0S));
    }

    auto MeditationTimer::onDimensionChanged(const BoundingBox &oldDim, const BoundingBox &newDim) -> bool
    {
        setPosition(newDim.x, newDim.y);
        setSize(newDim.w, newDim.h);
        return true;
    }

    void MeditationTimer::reset(std::chrono::seconds _duration) noexcept
    {
        assert(_duration != std::chrono::seconds::zero()); // Pre-condition check.

        duration = _duration;
        elapsed  = std::chrono::seconds::zero();
        onReset();
    }

    void MeditationTimer::start()
    {
        startTimer();
        isRunning = true;
    }

    void MeditationTimer::startTimer()
    {
        assert(application != nullptr);
        auto timerTask =
            std::make_unique<app::GuiTimer>("MeditationTimer", application, TimerInterval, Timer::Type::Continous);
        timerCallback = [this](Item &it, Timer &timerTask) { return onTimerTimeout(it, timerTask); };
        timerTask->start();
        application->connect(std::move(timerTask), this);
    }

    auto MeditationTimer::onTimerTimeout(Item &self, Timer &timerTask) -> bool
    {
        if (isFinished()) {
            timerTask.stop();
            detachTimer(timerTask);
            return true;
        }

        ++elapsed;
        update();
        return true;
    }

    auto MeditationTimer::isFinished() const noexcept -> bool
    {
        return duration <= elapsed || !isRunning;
    }

    auto MeditationTimer::calculatePercentageValue() const noexcept -> unsigned int
    {
        const auto percentage = static_cast<float>(elapsed.count()) / duration.count();
        return std::lround(percentage * 100);
    }

    void MeditationTimer::stop()
    {
        isRunning = false;
    }
} // namespace gui