Extract pages into components

This commit is contained in:
Eugen Wissner 2025-04-01 23:36:50 +02:00
parent 86ea47342b
commit ff3c508ceb
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
3 changed files with 88 additions and 33 deletions

View File

@ -0,0 +1,28 @@
#include "component.hpp"
namespace katja
{
ftxui::Element WelcomePage::Render()
{
return ftxui::text("Select an action in the menu.");
}
UpdatesPage::UpdatesPage(std::vector<package_identifier>&& updatable)
: updatable(std::move(updatable))
{
}
ftxui::Element UpdatesPage::Render()
{
std::vector<std::shared_ptr<ftxui::Node>> 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));
}
}

View File

@ -6,28 +6,22 @@
namespace katja
{
class UpdatesPage : public ftxui::ComponentBase
{
std::vector<package_identifier> updatable;
public:
explicit UpdatesPage(std::vector<package_identifier>&& updatable)
: updatable(std::move(updatable))
class WelcomePage final : public ftxui::ComponentBase
{
}
ftxui::Element Render() override;
};
ftxui::Element Render() override
class UpdatesPage final : public ftxui::ComponentBase
{
std::vector<std::shared_ptr<ftxui::Node>> lines;
std::vector<package_identifier> updatable;
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()) + ")");
public:
explicit UpdatesPage(std::vector<package_identifier>&& updatable);
return ftxui::window(summary, ftxui::vbox(lines));
}
};
ftxui::Element Render() override;
};
class SearchPage final : public ftxui::ComponentBase
{
};
}

View File

@ -9,6 +9,46 @@
#include "katja/database.hpp"
#include "component.hpp"
class MyContainer final : public ftxui::ComponentBase
{
public:
std::vector<std::string> menu_entries;
std::vector<ftxui::Component> menu_pages;
int menu_selected{ 0 };
ftxui::Component menu;
std::function<void()> on_enter;
MyContainer(std::vector<std::pair<std::string, ftxui::Component>> pages)
{
std::transform(std::cbegin(pages), std::cend(pages), std::back_inserter(this->menu_entries),
[](const std::pair<std::string, ftxui::Component>& pair) { return pair.first; });
std::transform(std::cbegin(pages), std::cend(pages), std::back_inserter(this->menu_pages),
[](const std::pair<std::string, ftxui::Component>& pair) { return pair.second; });
ftxui::MenuOption menu_option = ftxui::MenuOption::Horizontal();
menu = ftxui::Menu(&this->menu_entries, &this->menu_selected, menu_option);
}
ftxui::Element Render() override
{
return ftxui::vbox({
this->menu->Render(),
this->menu_pages.at(this->menu_selected)->Render()
});
}
bool OnEvent(ftxui::Event event) override
{
if (event.character() == "q")
{
on_enter();
return true;
}
return menu->OnEvent(event);
}
};
int main(int argc, const char **argv)
{
auto configuration = toml::parse("config/katja.toml");
@ -21,22 +61,15 @@ int main(int argc, const char **argv)
auto updates = repository.get_updates(installed_database);
auto screen = ftxui::ScreenInteractive::Fullscreen();
std::vector<std::string> menu_entries = {
"Updates",
"Search",
"Quit"
};
int menu_selected = 0;
ftxui::MenuOption menu_option = ftxui::MenuOption::Horizontal();
menu_option.on_enter = screen.ExitLoopClosure();
ftxui::Component menu = ftxui::Menu(&menu_entries, &menu_selected, menu_option);
auto custom_component = std::make_shared<katja::UpdatesPage>(std::move(updates));
ftxui::Component renderer = ftxui::Container::Vertical({
menu,
custom_component
auto container = std::make_shared<MyContainer>(std::vector<std::pair<std::string, ftxui::Component>>{
{ "Home", std::make_shared<katja::WelcomePage>() },
{ "Updates", std::make_shared<katja::UpdatesPage>(std::move(updates)) },
{ "Search", std::make_shared<katja::SearchPage>() }
});
screen.Loop(renderer);
container->on_enter = screen.ExitLoopClosure();
screen.Loop(container);
}
return EXIT_SUCCESS;
}