~aleteoryx/muditaos

ref: bc737e93f697de2dda221f8f97f0ce0ff05edd0b muditaos/module-apps/locks/widgets/LockBoxConstantSize.cpp -rw-r--r-- 2.4 KiB
bc737e93 — Przemyslaw Brudny [EGD-5885] Added SimLockHandler 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "Lock.hpp"
#include "LockBoxConstantSize.hpp"

#include <locks/data/LockStyle.hpp>
#include <locks/windows/LockInputWindow.hpp>

namespace gui
{
    void LockBoxConstantSize::buildLockBox(unsigned int pinSize)
    {
        buildPinLabels(pinSize);
    }

    void LockBoxConstantSize::clear()
    {
        for (unsigned i = 0; i < inputLabels.size(); i++) {
            popChar(i);
        }
    }

    void LockBoxConstantSize::popChar(unsigned int charNum)
    {
        if (charNum < inputLabels.size()) {
            inputLabels[charNum]->setVisibleState(false);
        }
    }

    void LockBoxConstantSize::putChar(unsigned int charNum)
    {
        if (charNum < inputLabels.size()) {
            inputLabels[charNum]->setVisibleState(true);
        }
    }

    void LockBoxConstantSize::buildPinLabels(unsigned int pinSize)
    {
        using namespace style::window::lock_input;

        inputLabels.clear();

        if (pinSize == 0) {
            return;
        }

        unsigned int singleLabelWidth        = input_box::h;
        unsigned int maxNoMarginsLabelsWidth = input_box::w - pinSize * 2 * input_box::label_margin;

        if (pinSize * singleLabelWidth > maxNoMarginsLabelsWidth) {
            singleLabelWidth = maxNoMarginsLabelsWidth / pinSize;
        }

        auto itemBuilder = [this, singleLabelWidth]() {
            auto label = new InputLabel(nullptr, singleLabelWidth, input_box::h);
            label->setEdges(RectangleEdge::Bottom);
            label->setMargins(Margins(input_box::label_margin, 0, input_box::label_margin, 0));
            inputLabels.push_back(label);
            return label;
        };

        lockWindow->buildPinLabels(itemBuilder, pinSize);
    }

    LockBoxConstantSize::InputLabel::InputLabel(Item *parent, uint32_t w, uint32_t h) : HBox(parent, 0, 0, w, h)
    {}

    void LockBoxConstantSize::InputLabel::setVisibleState(bool isImageVisible)
    {
        if (isImageVisible && image == nullptr) {
            image = new gui::Image("dot_12px_hard_alpha_W_G");
            image->setVisible(true);
            image->activeItem = false;
            addWidget(image);
        }
        else if (!isImageVisible && image != nullptr) {
            erase(image);
            image = nullptr;
        }
    }
} // namespace gui