~aleteoryx/muditaos

ref: a405cad694b867fcd2498984830bd97d4b9bde2f muditaos/module-apps/apps-common/widgets/ButtonTriState.cpp -rw-r--r-- 2.7 KiB
a405cad6Aleteoryx trim readme 8 days 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/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)
    {
        setPenWidth(style::buttonTriState::penWidth);

        const auto setRectangleStyle = [this](State state) {
            setFont(style::window::font::small);
            setAlignment(Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
            setEdges(RectangleEdge::All);
            setCorners(RectangleRoundedCorner::All);
            setRadius(style::buttonTriState::cornerRadiusRectangleView);
            setMinimumSize(style::buttonTriState::w, style::buttonTriState::h);
            setFillColor(state == State::On ? ColorFullBlack : ColorFullWhite);
            setColor(state == State::On ? ColorFullWhite : ColorFullBlack);
        };

        const auto setTextOnlyStyle = [this](const std::string &text) {
            setFont(style::window::font::verysmall);
            setAlignment(Alignment(gui::Alignment::Horizontal::Right, gui::Alignment::Vertical::Center));
            setEdges(RectangleEdge::None);
            setCorners(RectangleRoundedCorner::None);

            /* This needs to be changed even though the corners aren't visible
             * here - it seems setMinimumWidthToFitText() completely ignores radius
             * setting, what causes the text to not fit properly when radius != 0. */
            setRadius(style::buttonTriState::cornerRadiusTextView);

            setMinimumHeight(style::buttonTriState::h);
            setMinimumWidthToFitText(text);
            setColor(ColorFullBlack);
        };

        currentState = requestedState;

        switch (currentState) {
        case State::On:
            setRectangleStyle(State::On);
            setText(utils::translate("app_settings_toggle_on"));
            break;
        case State::Transiting:
            setTextOnlyStyle(transitingText);
            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(State::Off);
            setText(utils::translate("app_settings_toggle_off"));
            break;
        }
    }
} /* namespace gui */