~aleteoryx/muditaos

ref: 46bdedf9cd614f843a48cfc677d554154c2a1f68 muditaos/module-apps/apps-common/widgets/spinners/ItemSpinner.hpp -rw-r--r-- 5.1 KiB
46bdedf9 — Mateusz Piesta [BH-1350] Meditation timer update 3 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <Item.hpp>
#include <InputEvent.hpp>

namespace gui
{
    template <typename Container> class ItemSpinner : public Item
    {
      public:
        using range      = typename Container::range;
        using value_type = typename Container::value_type;

        using OnValueChanged = std::function<void(const value_type &&)>;

        explicit ItemSpinner(Item *parent,
                             range range,
                             Boundaries boundaries   = Boundaries::Continuous,
                             Orientation orientation = Orientation::Vertical);

        [[nodiscard]] value_type getCurrentValue() const noexcept;
        void setCurrentValue(value_type val);
        void setRange(range range);

        void setFocusEdges(RectangleEdge edges);
        bool onInput(const InputEvent &inputEvent) override;
        [[nodiscard]] bool isAtMin() const;
        [[nodiscard]] bool isAtMax() const;

        OnValueChanged onValueChanged;

      private:
        inline typename Container::Boundaries convert_boundaries(gui::Boundaries boundaries)
        {
            switch (boundaries) {
            case gui::Boundaries::Fixed:
                return Container::Boundaries::Fixed;
            case gui::Boundaries::Continuous:
                return Container::Boundaries::Continuous;
            }
            return Container::Boundaries::Fixed;
        }
        void stepNext();
        void stepPrevious();
        bool isPreviousEvent(const InputEvent &inputEvent);
        bool isNextEvent(const InputEvent &inputEvent);
        void update();
        void invoke();

        Container container;
        RectangleEdge focusEdges = RectangleEdge::Bottom;
        Orientation orientation  = Orientation::Vertical;
        gui::Item *currentLayout = nullptr;
    };

    template <typename Container>
    ItemSpinner<Container>::ItemSpinner(Item *parent,
                                        ItemSpinner::range range,
                                        Boundaries boundaries,
                                        Orientation orientation)
        : Item(), container{range, convert_boundaries(boundaries)}, orientation(orientation)
    {
        this->parent = parent;
        if (parent != nullptr) {
            parent->addWidget(this);
        }
    }

    template <typename Container> void ItemSpinner<Container>::setFocusEdges(RectangleEdge edges)
    {
        focusEdges = edges;
    }

    template <typename Container> void ItemSpinner<Container>::setRange(range range)
    {
        container.updateRange(range);
        update();
    }

    template <typename Container> void ItemSpinner<Container>::setCurrentValue(const value_type val)
    {
        container.set(val);
        update();
    }

    template <typename Container>
    typename ItemSpinner<Container>::value_type ItemSpinner<Container>::getCurrentValue() const noexcept
    {
        return container.get();
    }

    template <typename Container> bool ItemSpinner<Container>::isPreviousEvent(const InputEvent &inputEvent)
    {
        return (orientation == Orientation::Vertical && inputEvent.is(KeyCode::KEY_DOWN)) ||
               (orientation == Orientation::Horizontal && inputEvent.is(KeyCode::KEY_LEFT));
    }

    template <typename Container> bool ItemSpinner<Container>::isNextEvent(const InputEvent &inputEvent)
    {
        return (orientation == Orientation::Vertical && inputEvent.is(KeyCode::KEY_UP)) ||
               (orientation == Orientation::Horizontal && inputEvent.is(KeyCode::KEY_RIGHT));
    }

    template <typename Container> bool ItemSpinner<Container>::onInput(const InputEvent &inputEvent)
    {
        if (inputEvent.isShortRelease()) {
            if (isPreviousEvent(inputEvent)) {
                stepPrevious();
                return true;
            }
            else if (isNextEvent(inputEvent)) {
                stepNext();
                return true;
            }
        }
        return false;
    }

    template <typename Container> void ItemSpinner<Container>::stepNext()
    {
        if (container.next()) {
            update();
            invoke();
        }
    }

    template <typename Container> void ItemSpinner<Container>::stepPrevious()
    {
        if (container.previous()) {
            update();
            invoke();
        }
    }

    template <typename Container> void ItemSpinner<Container>::update()
    {
        if (currentLayout != nullptr) {
            this->removeWidget(currentLayout);
        }
        currentLayout = container.get();
        this->addWidget(currentLayout);
        informContentChanged();
    }
    template <typename Container> void ItemSpinner<Container>::invoke()
    {
        if (onValueChanged) {
            onValueChanged(getCurrentValue());
        }
    }
    template <typename Container> bool ItemSpinner<Container>::isAtMin() const
    {
        return container.is_min();
    }
    template <typename Container> bool ItemSpinner<Container>::isAtMax() const
    {
        return container.is_max();
    }
} // namespace gui