~aleteoryx/muditaos

ref: 6b329ce52257773e3a027fc90a15b352129a35b8 muditaos/module-apps/apps-common/widgets/ButtonOnOff.cpp -rw-r--r-- 1.6 KiB
6b329ce5 — Marcin Zieliński [MOS-242] Fix windows flow after PIN mistakes 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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "ButtonOnOff.hpp"

#include <Style.hpp>
#include <i18n/i18n.hpp>

namespace gui
{
    ButtonOnOff::ButtonOnOff(Item *parent, const ButtonState buttonState) : Label{parent}
    {
        setMinimumSize(style::buttonOnOff::w, style::buttonOnOff::h);
        setEdges(RectangleEdge::None);

        setAlignment(Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
        setFont(style::window::font::small);

        fill = new Rect(this, 0, 0, 0, 0);
        fill->setEdges(RectangleEdge::All);
        fill->setCorners(RectangleRoundedCorner::All);
        fill->setRadius(4);
        fill->setPenWidth(2);

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

        switchState(buttonState);
    }

    void ButtonOnOff::switchState(const ButtonState newButtonState)
    {
        currentState = newButtonState;
        if (currentState == ButtonState::On) {
            fill->setFillColor(ColorFullBlack);
            setColor(ColorFullWhite);
            setText(utils::translate("app_settings_toggle_on"));
        }
        else {
            fill->setFillColor(ColorFullWhite);
            setColor(ColorFullBlack);
            setText(utils::translate("app_settings_toggle_off"));
        }
    }
    void ButtonOnOff::toggleState()
    {
        switchState(static_cast<ButtonState>(!static_cast<bool>(currentState)));
    }

} /* namespace gui */