~aleteoryx/muditaos

muditaos/module-gui/gui/widgets/ListViewWithArrows.cpp -rw-r--r-- 8.4 KiB
a405cad6Aleteoryx trim readme 6 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// 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 "ListViewWithArrows.hpp"
#include "TextFixedSize.hpp"
#include "ImageBox.hpp"
#include <InputEvent.hpp>

namespace gui
{
    ListViewWithArrows::ListViewWithArrows(Item *parent,
                                           unsigned int x,
                                           unsigned int y,
                                           unsigned int w,
                                           unsigned int h,
                                           std::shared_ptr<ListItemProvider> prov)
        : Rect{parent, x, y, w, h}, ListViewEngine(std::move(prov))
    {
        this->setEdges(RectangleEdge::None);
        createLayout();

        body->borderCallback = [this](const InputEvent &inputEvent) -> bool {
            if (!inputEvent.isShortRelease()) {
                return false;
            }
            if (inputEvent.is(KeyCode::KEY_UP) && pageLoaded) {
                return this->requestPreviousPage();
            }
            else if (inputEvent.is(KeyCode::KEY_DOWN) && pageLoaded) {
                return this->requestNextPage();
            }
            else {
                return false;
            }
        };

        inputCallback = [&](Item &item, const InputEvent &event) { return body->onInput(event); };

        focusChangedCallback = [this]([[maybe_unused]] Item &item) -> bool {
            if (focus) {
                setFocus();
            }
            else {
                setFocusItem(nullptr);
            }
            return true;
        };

        body->dimensionChangedCallback = [&](gui::Item &, const BoundingBox &newDim) -> bool {
            if (listOverlay->lastBox->visible && direction == listview::Direction::Top) {
                if (startIndex > 1 && listOverlay->firstBox->visible) {
                    startIndex += 1;
                }

                if (startIndex == 1 && !listOverlay->firstBox->visible) {
                    startIndex = 0;
                }
            }

            currentPageSize = body->getVisibleChildrenCount();
            return true;
        };

        applyScrollCallbacks();

        type = gui::ItemType::LIST;
    }

    void ListViewWithArrows::createLayout()
    {
        widgetBody = new VBox(this, 0, 0, widgetArea.w, widgetArea.h);
        widgetBody->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));
        widgetBody->setEdges(RectangleEdge::None);

        listOverlay = new VThreeBox<VBox, VBox, VBox>(widgetBody, 0, 0, 0, 0);
        listOverlay->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));
        listOverlay->setEdges(RectangleEdge::None);

        listOverlay->firstBox = new VBox(listOverlay);
        listOverlay->firstBox->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));
        listOverlay->firstBox->setEdges(RectangleEdge::None);
        listOverlay->firstBox->setVisible(false);

        arrowTop = new ImageBox(listOverlay->firstBox, new Image("bell_arrow_top_W_M"));
        arrowTop->setMinimumSizeToFitImage();

        listOverlay->centerBox = new VBox(listOverlay);
        listOverlay->centerBox->setAlignment(Alignment(gui::Alignment::Horizontal::Center));
        listOverlay->centerBox->setEdges(RectangleEdge::None);

        body = new VBox(listOverlay->centerBox);
        body->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Top));
        body->setEdges(RectangleEdge::None);

        listOverlay->lastBox = new VBox(listOverlay);
        listOverlay->lastBox->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));
        listOverlay->lastBox->setEdges(RectangleEdge::None);
        listOverlay->lastBox->setVisible(false);

        arrowDown = new ImageBox(listOverlay->lastBox, new Image("bell_arrow_down_W_M"));
        arrowDown->setMinimumSizeToFitImage();
    }

    void ListViewWithArrows::applySizeRestrictions(unsigned int w,
                                                   unsigned int h,
                                                   unsigned int outerLayoutsH,
                                                   int outerLayoutsMargin)
    {
        listOverlay->setMinimumSize(w, h);
        body->setMaximumSize(w, h);

        listOverlay->firstBox->setMinimumSize(w, outerLayoutsH);
        listOverlay->centerBox->setMaximumSize(w, h);
        listOverlay->centerBox->setMargins(Margins(0, outerLayoutsMargin, 0, outerLayoutsMargin));
        listOverlay->lastBox->setMinimumSize(w, outerLayoutsH);

        widgetBody->resizeItems();
    }

    void ListViewWithArrows::setListTitle(const std::string &title)
    {
        titleBody = new TextFixedSize(listOverlay->firstBox);
        titleBody->drawUnderline(false);
        titleBody->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Top));
        titleBody->setFont(style::window::font::large);
        titleBody->setMinimumSize(listOverlay->firstBox->widgetMinimumArea.w,
                                  listOverlay->firstBox->widgetMinimumArea.h);
        titleBody->setEdges(RectangleEdge::None);
        titleBody->setEditMode(EditMode::Browse);
        titleBody->setText(title);

        body->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Top));
        listOverlay->firstBox->setVisible(true);

        listOverlay->resizeItems();
        // Second resize is needed as we need to first apply max size for center box and next extra margins.
        listOverlay->resizeItems();

        arrowTop->setVisible(false);
        titleBody->setVisible(true);
        listOverlay->firstBox->resizeItems();
    }

    void ListViewWithArrows::setListTitleFont(const UTF8 &fontName)
    {
        titleBody->setFont(fontName);
    }

    void ListViewWithArrows::applyScrollCallbacks()
    {
        updateScrollCallback = [this](ListViewScrollUpdateData data) {
            auto elementsOnPage = listOverlay->centerBox->widgetArea.h / data.elementMinimalSpaceRequired;

            if (boundaries == Boundaries::Continuous) {
                if (elementsOnPage == data.elementsCount) {
                    listOverlay->firstBox->setVisible(true);
                    listOverlay->lastBox->setVisible(false);
                }
                else if (elementsOnPage < data.elementsCount) {
                    listOverlay->firstBox->setVisible(true);
                    listOverlay->lastBox->setVisible(true);
                }
                else {
                    listOverlay->firstBox->setVisible(false);
                    listOverlay->lastBox->setVisible(false);
                }
            }
            else if (boundaries == Boundaries::Fixed) {
                if (elementsOnPage + data.startIndex < data.elementsCount) {
                    listOverlay->lastBox->setVisible(true);
                }
                else {
                    listOverlay->lastBox->setVisible(false);
                }

                if (data.startIndex == 0 && titleBody) {
                    titleBody->setVisible(true);
                    arrowTop->setVisible(false);
                    listOverlay->firstBox->setVisible(true);
                }
                else if (data.startIndex > 1) {
                    if (titleBody) {
                        titleBody->setVisible(false);
                    }
                    arrowTop->setVisible(true);
                    listOverlay->firstBox->setVisible(true);
                }
                else {
                    listOverlay->firstBox->setVisible(false);
                }
            }

            listOverlay->resizeItems();
            // Second resize is needed as we need to first apply max size for center box and next extra margins.
            listOverlay->resizeItems();
        };
    }

    void ListViewWithArrows::setFocus()
    {
        if (!focus) {
            return;
        }
        setFocusItem(body);

        ListViewEngine::setFocus();
    }

    void ListViewWithArrows::setAlignment(const Alignment &value)
    {
        if (body->getAlignment() != value) {
            body->setAlignment(value);
        }
    }

    bool ListViewWithArrows::onDimensionChanged(const BoundingBox &oldDim, const BoundingBox &newDim)
    {
        Rect::onDimensionChanged(oldDim, newDim);
        body->setSize(body->getWidth(), newDim.h);

        return true;
    }
} /* namespace gui */