~aleteoryx/muditaos

muditaos/module-gui/gui/widgets/SideListView.cpp -rw-r--r-- 3.8 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
// 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 "Image.hpp"
#include "InputEvent.hpp"
#include "SideListView.hpp"
#include "Style.hpp"

namespace gui
{
    SideListView::SideListView(Item *parent,
                               unsigned int x,
                               unsigned int y,
                               unsigned int w,
                               unsigned int h,
                               std::shared_ptr<ListItemProvider> prov,
                               PageBarType pageBarType)
        : Rect{parent, x, y, w, h}, ListViewEngine(prov)
    {
        setEdges(RectangleEdge::All);

        bodyOverlay = new VBox{this, 0, 0, w, h};
        bodyOverlay->setEdges(RectangleEdge::None);

        if (pageBarType != PageBarType::None) {
            createPageBar();
            applyScrollCallbacks();
        }

        body = new HBox{bodyOverlay, 0, 0, 0, 0};
        body->setMaximumSize(w, h);
        body->setAlignment(Alignment::Horizontal::Center);
        body->setEdges(RectangleEdge::None);
        bodyOverlay->resizeItems();

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

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

        type = gui::ItemType::LIST;
    }

    auto SideListView::createPageBar() -> void
    {
        pageBar = new HBarGraph(bodyOverlay, 0U, 0U, 0);
        pageBar->setMinimumHeight(style::sidelistview::progress_bar::h);
        pageBar->setMargins(Margins(style::sidelistview::progress_bar::margin_left,
                                    style::sidelistview::progress_bar::margin_top,
                                    style::sidelistview::progress_bar::margin_right,
                                    style::sidelistview::progress_bar::margin_bottom));
        pageBar->setAlignment(Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
        pageBar->activeItem = false;
    }

    auto SideListView::applyScrollCallbacks() -> void
    {
        updateScrollCallback = [this](ListViewScrollUpdateData data) {
            if (pageBar != nullptr) {
                auto elementsOnPage = body->widgetArea.w / data.elementMinimalSpaceRequired;
                auto pagesCount     = data.elementsCount % elementsOnPage == 0 ? data.elementsCount / elementsOnPage
                                                                               : data.elementsCount / elementsOnPage + 1;
                auto currentPage    = data.startIndex / elementsOnPage + 1;

                pageBar->setMaximum(pagesCount);
                pageBar->createGraph();
                pageBar->setValue(currentPage);
            }
        };
    }

    auto SideListView::setFocus() -> void
    {
        if (!focus) {
            return;
        }
        setFocusItem(body);

        ListViewEngine::setFocus();
    }

    auto SideListView::onInput(const InputEvent &inputEvent) -> bool
    {
        if (body->onInput(inputEvent)) {
            return true;
        }
        if (body->borderCallback && body->borderCallback(inputEvent)) {
            return true;
        }
        return false;
    }
} /* namespace gui */