~aleteoryx/muditaos

ref: 7fbaf735ed99f5b0d26adb686cfa6ed2bb4267d5 muditaos/module-gui/gui/widgets/SideListView.cpp -rw-r--r-- 5.1 KiB
7fbaf735 — Przemyslaw Brudny [EGD-7813] Option Window titles localizations fix 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
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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/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::createArrowsOverlay(unsigned int x, unsigned y, unsigned int w, unsigned int h) -> void
    {
        arrowsOverlay = new HBox{this, x, y, w, h};
        arrowsOverlay->setEdges(gui::RectangleEdge::None);
        auto arrowLeft = new gui::Image(style::sidelistview::arrow_left_image);
        arrowLeft->setAlignment(Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Center));
        arrowLeft->activeItem = false;
        arrowLeft->setEdges(RectangleEdge::None);
        arrowsOverlay->addWidget(arrowLeft);

        auto arrowRight = new Image(style::sidelistview::arrow_right_image);
        arrowRight->setAlignment(Alignment(Alignment::Horizontal::Right, Alignment::Vertical::Center));
        arrowRight->activeItem = false;
        arrowRight->setEdges(RectangleEdge::None);

        auto rectFiller = new gui::Rect(arrowsOverlay,
                                        0U,
                                        0U,
                                        arrowsOverlay->getWidth() - arrowLeft->getWidth() - arrowRight->getWidth(),
                                        arrowsOverlay->getHeight());

        rectFiller->setMaximumSize(arrowsOverlay->getWidth(), arrowsOverlay->getHeight());
        rectFiller->setEdges(RectangleEdge::None);

        arrowsOverlay->addWidget(arrowRight);
    }

    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 */