~aleteoryx/muditaos

ref: 3cbfe43a95610ad53a00a32bbfc3bf842013966f muditaos/module-apps/apps-common/widgets/ButtonTriState.cpp -rw-r--r-- 1.5 KiB
3cbfe43a — Marcin Zieliński [MOS-791] Introduce tri-state switch widget 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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "ButtonTriState.hpp"

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

namespace gui
{
    ButtonTriState::ButtonTriState(Item *parent, State state) : Label{parent}
    {
        setMinimumSize(style::buttonTriState::w, style::buttonTriState::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(state);
    }

    void ButtonTriState::switchState(State requestedState)
    {
        currentState = requestedState;
        if (currentState == State::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"));
        }
    }
} /* namespace gui */