~aleteoryx/muditaos

muditaos/module-gui/gui/widgets/header/Header.cpp -rw-r--r-- 4.4 KiB
a405cad6Aleteoryx trim readme 7 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// 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 "Header.hpp"
#include "Style.hpp"

#include <Text.hpp>

namespace gui::header
{
    Header::Header(Item *parent, Position x, Position y, Length w, Length h) : HThreeBox(parent, x, y, w, h)
    {
        setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));
        setEdges(RectangleEdge::None);

        firstBox = new HBox(this, 0, 0, 0, 0);
        firstBox->setMinimumSize(style::header::navigation_indicator::box_width, h);
        firstBox->setAlignment(Alignment(Alignment::Vertical::Top));
        firstBox->setEdges(RectangleEdge::None);
        firstBox->setVisible(false);

        centerBox = new HBox(this, 0, 0, 0, 0);
        centerBox->setEdges(RectangleEdge::None);
        centerBox->setAlignment(Alignment(gui::Alignment::Horizontal::Center));
        centerBox->setMaximumSize(w, h);

        lastBox = new HBox(this, 0, 0, 0, 0);
        lastBox->setMinimumSize(style::header::navigation_indicator::box_width, h);
        lastBox->setAlignment(Alignment(Alignment::Vertical::Top));
        lastBox->setEdges(RectangleEdge::None);
        lastBox->setVisible(false);
    }

    Item *Header::createTitle(const UTF8 &text)
    {
        title = new gui::Label(nullptr, 0, 0, 0, 0);
        title->setMaximumSize(getWidth() - 2 * style::header::title::margins, getHeight());
        title->setFont(style::header::font::title);
        title->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Top));
        title->setPadding(gui::Padding(0, style::header::title::top_padding, 0, 0));
        title->setEdges(RectangleEdge::None);
        title->setTextEllipsisType(TextEllipsis::Right);
        title->setVisible(true);
        title->setText(text);
        return title;
    }

    void Header::showOuterBoxes()
    {
        firstBox->setVisible(true);
        lastBox->setVisible(true);
        resizeItems();
    }

    void Header::hideOuterBoxes()
    {
        if (lastBox->empty() && firstBox->empty()) {
            lastBox->setVisible(false);
            firstBox->setVisible(false);
        }
    }

    void Header::addToLeftBox(Item *item)
    {
        firstBox->erase();
        showOuterBoxes();
        firstBox->addWidget(item);

        firstBox->resizeItems();
    }

    void Header::addToCenterBox(Item *item)
    {
        centerBox->erase();
        centerBox->addWidget(item);

        centerBox->resizeItems();
    }

    void Header::addToRightBox(Item *item)
    {
        lastBox->erase();
        showOuterBoxes();
        lastBox->addWidget(item);

        lastBox->resizeItems();
    }

    void Header::setTitle(const UTF8 &text)
    {
        addToCenterBox(createTitle(text));

        setEdges(RectangleEdge::Bottom);
        resizeItems();
    }

    UTF8 Header::getTitle()
    {
        return title ? title->getText() : "";
    }

    void Header::setTitleVisibility(bool visibility)
    {
        if (title) {
            visibility ? setEdges(RectangleEdge::Bottom) : setEdges(RectangleEdge::None);
            title->setVisible(visibility);
        }
    }

    void Header::navigationIndicatorAdd(Item *item, BoxSelection boxSelection)
    {
        switch (boxSelection) {
        case BoxSelection::Left:
            addToLeftBox(item);
            break;
        case BoxSelection::Center:
            addToCenterBox(item);
            break;
        case BoxSelection::Right:
            addToRightBox(item);
            break;
        }

        resizeItems();
    }

    void Header::navigationIndicatorRemove(BoxSelection boxSelection)
    {
        switch (boxSelection) {
        case BoxSelection::Left:
            firstBox->erase();
            break;
        case BoxSelection::Center:
            centerBox->erase();
            break;
        case BoxSelection::Right:
            lastBox->erase();
            break;
        }

        hideOuterBoxes();
        resizeItems();
    }

    bool Header::navigationIndicatorVisible(BoxSelection boxSelection)
    {
        switch (boxSelection) {
        case BoxSelection::Left:
            return firstBox->visible && !firstBox->empty();
        case BoxSelection::Center:
            return centerBox->visible && !centerBox->empty();
        case BoxSelection::Right:
            return lastBox->visible && !lastBox->empty();
        }
        return false;
    }
} // namespace gui::header