~aleteoryx/muditaos

ref: 1340e6949c0762585b0de844863c91c859fb19b1 muditaos/module-apps/apps-common/widgets/ButtonTriState.cpp -rw-r--r-- 2.2 KiB
1340e694 — Bartosz Revert "[MOS-578] Fix incorrect logic with SMS notifications" 2 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
// 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}, transitingText{utils::translate("app_settings_toggle_transiting")}
    {
        switchState(state);
    }

    void ButtonTriState::switchState(State requestedState)
    {
        setFont(style::window::font::small);
        setRadius(4);
        setPenWidth(2);

        auto setRectangleStyle = [this]() {
            setAlignment(Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
            setMinimumSize(style::buttonTriState::w, style::buttonTriState::h);
            setEdges(RectangleEdge::All);
            setCorners(RectangleRoundedCorner::All);
        };

        auto setTextOnlyStyle = [this]() {
            setAlignment(Alignment(gui::Alignment::Horizontal::Right, gui::Alignment::Vertical::Center));
            setMinimumSize(120,
                           style::buttonTriState::h); // unfortunately, setMinimumWidthToFitText() doesn't work here
            setEdges(RectangleEdge::None);
            setCorners(RectangleRoundedCorner::None);
            setColor(ColorFullBlack);
        };

        currentState = requestedState;

        switch (currentState) {
        case State::On:
            setRectangleStyle();
            setFillColor(ColorFullBlack);
            setColor(ColorFullWhite);
            setText(utils::translate("app_settings_toggle_on"));
            break;
        case State::Transiting:
            setTextOnlyStyle();
            setText(transitingText);
            break;
        default:
            LOG_ERROR("button state '%s' not implemented - defaulting to OFF",
                      magic_enum::enum_name(currentState).data());
            [[fallthrough]];
        case State::Off:
            setRectangleStyle();
            setFillColor(ColorFullWhite);
            setColor(ColorFullBlack);
            setText(utils::translate("app_settings_toggle_off"));
            break;
        }
    }
} /* namespace gui */