~aleteoryx/muditaos

ref: cd0cc6dc1a24355eb8637e8b3af1c07dde272c3e muditaos/module-apps/application-alarm-clock/widgets/CustomCheckBoxWithLabel.cpp -rw-r--r-- 4.0 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CustomCheckBoxWithLabel.hpp"
#include "AlarmClockStyle.hpp"
#include <InputEvent.hpp>

namespace gui
{
    const std::map<WeekDayIso, std::string> CustomCheckBoxWithLabel::weekDays = {
        {WeekDayIso::Monday, style::strings::common::Monday},
        {WeekDayIso::Tuesday, style::strings::common::Tuesday},
        {WeekDayIso::Wednesday, style::strings::common::Wednesday},
        {WeekDayIso::Thursday, style::strings::common::Thursday},
        {WeekDayIso::Friday, style::strings::common::Friday},
        {WeekDayIso::Saturday, style::strings::common::Saturday},
        {WeekDayIso::Sunday, style::strings::common::Sunday}};

    CustomCheckBoxWithLabel::CustomCheckBoxWithLabel(app::ApplicationCommon *app,
                                                     const std::string &description,
                                                     const WeekDaysRepeatData &data)
        : application(app), checkBoxData(data)
    {
        assert(application != nullptr);

        setMinimumSize(style::window::default_body_width, style::alarmClock::window::item::checkBox::height);
        setMargins(gui::Margins(style::margins::small, style::alarmClock::window::item::checkBox::marginTop, 0, 0));
        setEdges(RectangleEdge::None);

        hBox = new gui::HBox(this, 0, 0, 0, 0);
        hBox->setEdges(gui::RectangleEdge::None);

        checkBox = new gui::CheckBox(
            hBox,
            0,
            0,
            0,
            0,
            [=](const UTF8 &text) { application->getCurrentWindow()->bottomBarTemporaryMode(text, false); },
            [=]() { application->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); });
        checkBox->setMinimumSize(style::alarmClock::window::item::checkBox::inputBox_w,
                                 style::alarmClock::window::item::checkBox::height);

        descriptionLabel = new gui::Label(hBox, 0, 0, 0, 0);
        descriptionLabel->setMinimumSize(style::alarmClock::window::item::checkBox::description_w,
                                         style::alarmClock::window::item::checkBox::height);
        descriptionLabel->setMargins(gui::Margins(style::margins::very_big, 0, 0, 0));
        descriptionLabel->setEdges(gui::RectangleEdge::None);
        descriptionLabel->setAlignment(Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Center));
        descriptionLabel->setFont(style::window::font::medium);
        descriptionLabel->setText(description);

        applyCallbacks();
        setCheckBoxes();
    }

    void CustomCheckBoxWithLabel::applyCallbacks()
    {
        focusChangedCallback = [&](Item &item) {
            if (focus) {
                descriptionLabel->setFont(style::window::font::mediumbold);
                setFocusItem(checkBox);
            }
            else {
                descriptionLabel->setFont(style::window::font::medium);
                setFocusItem(nullptr);
            }
            return true;
        };

        inputCallback = [&](gui::Item &item, const gui::InputEvent &event) {
            if (event.is(gui::KeyCode::KEY_RF) || event.is(gui::KeyCode::KEY_ENTER)) {
                setFocusItem(nullptr);
                return false;
            }
            if (checkBox->onInput(event)) {
                checkBox->resizeItems();
                return true;
            }
            return false;
        };
        onContentChangedCallback = [&]() { return checkBox->isChecked(); };

        dimensionChangedCallback = [&](gui::Item &, const BoundingBox &newDim) -> bool {
            hBox->setArea({0, 0, newDim.w, newDim.h});
            return true;
        };
    }

    void CustomCheckBoxWithLabel::setCheckBoxes()
    {
        for (auto const &[key, dayName] : weekDays) {
            if (descriptionLabel->getText() == utils::translate(dayName)) {
                checkBox->setCheck(checkBoxData.getData(static_cast<uint32_t>(key)));
            }
        }
    }

} // namespace gui