153 lines
4.8 KiB
C++
153 lines
4.8 KiB
C++
#include "component.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
namespace katja
|
|
{
|
|
ScreenContainer::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 ScreenContainer::OnRender()
|
|
{
|
|
return ftxui::vbox({
|
|
this->menu->Render(),
|
|
ftxui::separator(),
|
|
this->content->Render()
|
|
});
|
|
}
|
|
|
|
bool ScreenContainer::OnEvent(ftxui::Event event)
|
|
{
|
|
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);
|
|
}
|
|
|
|
ftxui::Element WelcomePage::OnRender()
|
|
{
|
|
return ftxui::paragraph("Select an action in the menu.");
|
|
}
|
|
|
|
void WelcomePage::Load()
|
|
{
|
|
}
|
|
|
|
UpdatesPage::UpdatesPage(std::shared_ptr<struct repository> repository, package_database database)
|
|
: repository(repository), database(database)
|
|
{
|
|
}
|
|
|
|
void UpdatesPage::Load()
|
|
{
|
|
this->updatable = repository->get_updates(this->database);
|
|
}
|
|
|
|
ftxui::Element UpdatesPage::OnRender()
|
|
{
|
|
std::vector<ftxui::Element> lines;
|
|
|
|
for (const auto& package_identifier : this->updatable)
|
|
{
|
|
auto line = ftxui::text(package_identifier.to_string()) | color(ftxui::Color::SkyBlue2);
|
|
lines.push_back(line);
|
|
}
|
|
ftxui::Element summary = ftxui::text(" Updates (" + std::to_string(lines.size()) + ")");
|
|
|
|
return ftxui::window(summary, ftxui::vbox(lines));
|
|
}
|
|
|
|
SearchPage::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 SearchPage::Load()
|
|
{
|
|
this->needle.erase();
|
|
}
|
|
|
|
ftxui::Element SearchPage::OnRender()
|
|
{
|
|
std::vector<ftxui::Element> lines;
|
|
|
|
for (const auto& package_identifier : this->search_results)
|
|
{
|
|
auto line = ftxui::text(package_identifier.to_string()) | color(ftxui::Color::SkyBlue2);
|
|
lines.push_back(line);
|
|
}
|
|
ftxui::FlexboxConfig config;
|
|
config.justify_content = ftxui::FlexboxConfig::JustifyContent::FlexStart;
|
|
config.align_items = ftxui::FlexboxConfig::AlignItems::FlexStart;
|
|
config.direction = ftxui::FlexboxConfig::Direction::Row;
|
|
|
|
return ftxui::vbox({
|
|
ftxui::flexbox({
|
|
this->search_input->Render(),
|
|
ftxui::window(ftxui::text("Search in"), type_input->Render())
|
|
}, config),
|
|
ftxui::vbox(lines)
|
|
});
|
|
}
|
|
|
|
bool SearchPage::OnEvent(ftxui::Event event)
|
|
{
|
|
if (event == ftxui::Event::Return)
|
|
{
|
|
if (!this->needle.empty())
|
|
{
|
|
this->search_results = this->repository->search_names(this->architecture, this->needle);
|
|
}
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return this->search_input->OnEvent(event);
|
|
}
|
|
return false;
|
|
}
|
|
}
|