summaryrefslogtreecommitdiff
path: root/cli/page.cpp
blob: fe0bd5a8c159cd2ce9edbdce145dfb4cddc73656 (plain)
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
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */
module;

#include <algorithm>

#include "component.hpp"

export module page;

export namespace katja
{
    class PageBase : public ftxui::ComponentBase
    {
    public:
        virtual void Load() = 0;
    };

    using Page = std::shared_ptr<PageBase>;
    using Pages = std::vector<Page>;

    class ScreenContainer final : public ftxui::ComponentBase
    {
        int menu_selected{ 0 };
        ftxui::Component menu;
        ftxui::Component content;
        std::vector<std::string> menu_entries;
        Pages menu_pages;
        std::function<void()> on_enter;

    public:
        ScreenContainer(std::vector<std::pair<std::string, Page>> pages, std::function<void()> on_enter)
            : on_enter(on_enter)
        {
            ftxui::Components menu_pages;

            std::transform(std::cbegin(pages), std::cend(pages), std::back_inserter(menu_entries),
                    [](const std::pair<std::string, Page>& pair) { return pair.first; });
            std::transform(std::cbegin(pages), std::cend(pages), std::back_inserter(this->menu_pages),
                    [](const std::pair<std::string, Page>& pair) { return pair.second; });
            std::copy(std::cbegin(this->menu_pages), std::cend(this->menu_pages), std::back_inserter(menu_pages));

            ftxui::MenuOption menu_option = ftxui::MenuOption::Horizontal();
            this->menu = ftxui::Toggle(&this->menu_entries, &this->menu_selected);

            this->content = ftxui::Container::Tab(std::move(menu_pages), &this->menu_selected);
        }

        ftxui::Element OnRender() override
        {
            return ftxui::vbox({
                this->menu->Render(),
                ftxui::separator(),
                this->content->Render()
            });
        }

        bool OnEvent(ftxui::Event event) override
        {
            if (event == ftxui::Event::CtrlQ && this->on_enter)
            {
                on_enter();
                return true;
            }
            int previously = this->menu_selected;
            bool result{ false };

            if (event == ftxui::Event::CtrlP)
            {
                result = menu->OnEvent(ftxui::Event::ArrowLeft);
            }
            else if (event == ftxui::Event::CtrlN)
            {
                result = menu->OnEvent(ftxui::Event::ArrowRight);
            }
            if (previously != this->menu_selected)
            {
                this->menu_pages.at(this->menu_selected)->Load();
            }
            if (!result)
            {
                result = this->menu_pages.at(this->menu_selected)->OnEvent(event);
            }
            return result;
        }
    };

    ftxui::Component Screen(std::vector<std::pair<std::string, Page>> pages, std::function<void()> on_enter)
    {
        return std::make_shared<ScreenContainer>(std::move(pages), on_enter);
    }

    class WelcomePage final : public PageBase
    {
    public:
        void Load() override
        {
        }

        ftxui::Element OnRender() override
        {
            return ftxui::paragraph("Select an action in the menu.");
        }
    };

    class UpdatesPage final : public PageBase
    {
        ftxui::Component updatable = PackageList("Updates");
        std::shared_ptr<struct repository> repository;
        package_database database;

    public:
        UpdatesPage(std::shared_ptr<struct repository> repository, package_database database)
            : repository(repository), database(database)
        {
        }

        void Load() override
        {
            this->updatable = PackageList("Updates", repository->get_updates(this->database));
        }

        ftxui::Element OnRender() override
        {
            return this->updatable->Render();
        }
    };

    class SearchPage final : public PageBase
    {
        std::string needle;
        ftxui::Component search_input;
        ftxui::Component type_input;
        std::shared_ptr<struct repository> repository;
        std::string architecture;
        ftxui::Component search_results = PackageList("Results");
        int search_type{ 0 };

    public:
        SearchPage(std::shared_ptr<struct repository> repository, const std::string& architecture)
            : repository(repository), architecture(architecture)
        {
            ftxui::InputOption search_input_option = { .multiline = false };
            this->search_input = ftxui::Input(&this->needle, "Search", search_input_option);
            this->type_input = ftxui::Radiobox(std::vector<std::string>{ "Names", "Description" }, &this->search_type);
        }

        void Load() override
        {
            this->needle.erase();
        }

        ftxui::Element OnRender() override
        {
            std::vector<ftxui::Element> lines;

            ftxui::FlexboxConfig config;
            config.justify_content = ftxui::FlexboxConfig::JustifyContent::SpaceAround;
            config.align_items = ftxui::FlexboxConfig::AlignItems::FlexStart;
            config.direction = ftxui::FlexboxConfig::Direction::Row;
            config.wrap = ftxui::FlexboxConfig::Wrap::NoWrap;
            config.SetGap(3, 0);

            return ftxui::vbox({
                ftxui::flexbox({
                    this->search_input->Render(),
                    ftxui::window(ftxui::text("Search in"), type_input->Render())
                }, config),
                this->search_results->Render() | ftxui::flex
            });
        }

        bool OnEvent(ftxui::Event event) override
        {
            if (event == ftxui::Event::Return)
            {
                if (!this->needle.empty())
                {
                    std::vector<package_identifier> result =
                        this->repository->search_names(this->architecture, this->needle);
                    this->search_results = PackageList("Search", std::move(result));
                }
                return true;
            }
            else if (event == ftxui::Event::ArrowUp || event == ftxui::Event::ArrowDown)
            {
                this->search_results->OnEvent(event);
                return true;
            }
            else
            {
                return this->search_input->OnEvent(event);
            }
            return false;
        }
    };
}