~aleteoryx/muditaos

ref: 0e8b4c848e3f87f3bbb1f64ef7460cf56017b87f muditaos/module-apps/apps-common/widgets/DateSetSpinner.cpp -rw-r--r-- 5.0 KiB
0e8b4c84 — Lefucjusz [BH-2108] Fix misaligned charging symbol 3 months 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-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "DateSetSpinner.hpp"

#include <FontManager.hpp>
#include <RawFont.hpp>
#include <gui/widgets/ImageBox.hpp>
#include <gui/widgets/text/Label.hpp>
#include <time/time_locale.hpp>

namespace gui
{
    namespace
    {
        constexpr std::uint8_t step    = 1;
        constexpr std::uint8_t dayMin  = 1;
        constexpr std::uint8_t dayMax  = 31;
        constexpr std::uint8_t monthMin = 1;
        constexpr std::uint8_t monthMax = 12;
        constexpr auto fontName         = style::window::font::supersizeme;

        void setFont(TextFixedSize *elem, const std::string &fontName)
        {
            elem->setFont(fontName);
            elem->setMinimumHeightToFitText();
            elem->setMinimumWidthToFitText();
            elem->setText(elem->getText());
        }
    } // namespace

    DateSetSpinner::DateSetSpinner(
        Item *parent, Type type, TextFixedSize *title, Length x, Length y, Length w, Length h)
        : HBox(parent, x, y, w, h), type{type}, title{title}
    {
        setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));
        setEdges(RectangleEdge::None);

        switch (type) {
        case Type::year:
            attachDateField(
                year,
                U16IntegerSpinnerFixed::range{utils::time::Locale::min_years, utils::time::Locale::max_years, step});
            year->onValueChanged = [this](auto) { updateDate(); };
            setFocusItem(year);
            break;
        case Type::month:
            attachDateField(dayMonth, U8IntegerSpinnerFixed::range{monthMin, monthMax, step});
            dayMonth->onValueChanged = [this](auto) { updateDate(); };
            setFocusItem(dayMonth);
            break;
        case Type::day:
            attachDateField(dayMonth, U8IntegerSpinnerFixed::range{dayMin, dayMax, step});
            dayMonth->onValueChanged = [this](auto) { updateDate(); };
            setFocusItem(dayMonth);
            break;
        }

        resizeItems();
        applySizeRestrictions();
    }

    date::year_month_day DateSetSpinner::getDate() const
    {
        return date;
    }

    void DateSetSpinner::setDate(const date::year_month_day date)
    {
        this->date = date;
        switch (type) {
        case Type::year:
            year->set_value(static_cast<int>(date.year()));
            break;
        case Type::month:
            dayMonth->set_value(static_cast<unsigned>(date.month()));
            break;
        case Type::day:
            const auto lastDayInMonth =
                static_cast<unsigned>((date::year(date.year()) / date::month(date.month()) / date::last).day());
            const auto day = static_cast<unsigned>(date.day());
            dayMonth->set_range({dayMin, static_cast<std::uint8_t>(lastDayInMonth), step});
            dayMonth->set_value(std::min(day, lastDayInMonth));
            break;
        }
        applySizeRestrictions();
    }

    void DateSetSpinner::updateDate()
    {
        switch (type) {
        case Type::year:
            date = date::year(year->get_value()) / date::month(date.month()) / date::day(date.day());
            break;
        case Type::month:
            date = date::year(date.year()) / date::month(dayMonth->get_value()) / date::day(date.day());
            break;
        case Type::day:
            date = date::year(date.year()) / date::month(date.month()) / date::day(dayMonth->get_value());
            break;
        }
    }

    void DateSetSpinner::applySizeRestrictions()
    {
        if (year != nullptr) {
            year->setMinimumWidthToFitText();
        }
        if (dayMonth != nullptr) {
            dayMonth->setMinimumWidthToFitText();
        }

        setMinimumSize(getWidgetMinimumAreaWidth(), getFontHeight(fontName));
        setMaximumWidth(widgetMaximumArea.w);

        HBox::informContentChanged();
    }

    std::uint32_t DateSetSpinner::getWidgetMinimumAreaWidth() const
    {
        return (type == Type::year) ? year->widgetMinimumArea.w : dayMonth->widgetMinimumArea.w;
    }

    std::uint16_t DateSetSpinner::getFontHeight(const std::string &fontName) const
    {
        const auto font = FontManager::getInstance().getFont(fontName);
        return font->info.line_height;
    }

    template <typename Spinner>
    void DateSetSpinner::attachDateField(Spinner *&field, typename Spinner::range &&range)
    {
        field = new Spinner(std::move(range), Boundaries::Continuous);
        setFont(field, fontName);
        field->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));
        field->setEdges(RectangleEdge::None);
        field->setPenFocusWidth(0);
        field->set_value(0);
        addWidget(field);
    }

    bool DateSetSpinner::onInput(const InputEvent &inputEvent)
    {
        if (auto ret = this->focusItem->onInput(inputEvent)) {
            applySizeRestrictions();
            return ret;
        }
        return false;
    }
} /* namespace gui */