~aleteoryx/muditaos

ref: 0a322b878cb26c450064e5e3d9ec825415de241c muditaos/module-gui/gui/widgets/CheckBox.cpp -rw-r--r-- 3.0 KiB
0a322b87 — Przemyslaw Brudny [EGD-6770] ListView onPageElement rebuild page jump added 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CheckBox.hpp"
#include "InputEvent.hpp"
#include "Style.hpp"
#include "Utils.hpp"

namespace gui
{

    CheckBox::CheckBox(Item *parent,
                       const uint32_t &x,
                       const uint32_t &y,
                       const uint32_t &w,
                       const uint32_t &h,
                       std::function<void(const UTF8 &text)> bottomBarTemporaryMode,
                       std::function<void()> bottomBarRestoreFromTemporaryMode,
                       bool textOnLeft)
        : HBox(parent, x, y, w, h), bottomBarTemporaryMode(bottomBarTemporaryMode),
          bottomBarRestoreFromTemporaryMode(bottomBarRestoreFromTemporaryMode), textOnLeft(textOnLeft)

    {
        setEdges(RectangleEdge::Bottom);
        setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));

        image = new Image("small_tick_W_M");
        image->setVisible(false);
        addWidget(image);

        applyCallbacks();
    }

    void CheckBox::applyCallbacks()
    {
        focusChangedCallback = [&](Item &item) {
            if (focus) {
                setFocusItem(image);
                if (image->visible) {
                    bottomBarTemporaryMode(utils::translate("common_uncheck"));
                }
                else {
                    bottomBarTemporaryMode(utils::translate("common_check"));
                }
            }
            else {
                setFocusItem(nullptr);
                bottomBarRestoreFromTemporaryMode();
            }
            return true;
        };

        inputCallback = [&](gui::Item &item, const gui::InputEvent &event) {
            if (!event.isShortRelease()) {
                return false;
            }
            if (textOnLeft) {
                if (event.is(gui::KeyCode::KEY_LF)) {
                    image->setVisible(!image->visible);
                    if (image->visible) {
                        bottomBarTemporaryMode(utils::translate("common_uncheck"));
                    }
                    else {
                        bottomBarTemporaryMode(utils::translate("common_check"));
                    }
                    return true;
                }
            }
            else {
                if (event.is(gui::KeyCode::KEY_ENTER)) {
                    image->setVisible(!image->visible);
                    if (image->visible) {
                        bottomBarTemporaryMode(utils::translate("common_uncheck"));
                    }
                    else {
                        bottomBarTemporaryMode(utils::translate("common_check"));
                    }
                    return true;
                }
            }
            return false;
        };
    }

    void CheckBox::setImageVisible(bool state)
    {
        image->setVisible(state);
    }

    bool CheckBox::isChecked()
    {
        return image->visible;
    }

} /* namespace gui */