~aleteoryx/muditaos

ref: cd0cc6dc1a24355eb8637e8b3af1c07dde272c3e muditaos/module-apps/apps-common/widgets/spinners/SpinnerPolicies.hpp -rw-r--r-- 3.2 KiB
cd0cc6dc — Mateusz Piesta [BH-890] Home screen alarm popups 4 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <gui/Common.hpp>

#include <utf8/UTF8.hpp>

#include <cstdint>
#include <vector>

namespace gui
{
    template <typename ValType> class StringPolicy
    {
      public:
        using Range = std::vector<ValType>;
        using Type  = ValType;

        StringPolicy(Range range, Boundaries boundaries) : range{range}, boundaries{boundaries}
        {}

        ValType get() const
        {
            return range[pos];
        }

        UTF8 str() const
        {
            return range[pos];
        }

        void set(ValType val)
        {
            for (auto i = 0U; i < range.size(); i++) {
                if (range[i] == val) {
                    pos = i;
                    break;
                }
            }
        }

        void next()
        {
            if (pos >= upRange()) {
                if (boundaries == Boundaries::Continuous) {
                    pos = 0;
                }
                else {
                    pos = upRange();
                }
            }
            else {
                pos++;
            }
        }

        void previous()
        {
            if (pos <= 0) {
                if (boundaries == Boundaries::Continuous) {
                    pos = upRange();
                }
                else {
                    pos = 0;
                }
            }
            else {
                pos--;
            }
        }

      private:
        std::uint32_t upRange() const
        {
            return range.size() - 1;
        }

        const Range range;
        std::uint32_t pos{};
        const Boundaries boundaries{};
    };

    template <typename ValType> class NumericPolicy
    {
      public:
        using Range = struct
        {
            ValType min;
            ValType max;
            ValType step;
        };
        using Type = ValType;

        NumericPolicy(Range range, Boundaries boundaries) : range{range}, boundaries{boundaries}
        {}

        ValType get() const
        {
            return currentValue;
        }

        UTF8 str() const
        {
            return std::to_string(currentValue);
        }

        void set(ValType val)
        {
            currentValue = val;
        }

        void next()
        {
            if (currentValue >= range.max) {
                if (boundaries == Boundaries::Continuous) {
                    currentValue = range.min;
                }
                else {
                    currentValue = range.max;
                }
            }
            else {
                currentValue += range.step;
            }
        }

        void previous()
        {
            if (currentValue <= range.min) {
                if (boundaries == Boundaries::Continuous) {
                    currentValue = range.max;
                }
                else {
                    currentValue = range.min;
                }
            }
            else {
                currentValue -= range.step;
            }
        }

      private:
        const Range range;
        ValType currentValue{};
        const Boundaries boundaries{};
    };

} // namespace gui