katja: Use the tab container for pages

This commit is contained in:
Eugen Wissner 2025-04-05 14:23:44 +02:00
parent ff3c508ceb
commit 24ecc7becd
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
3 changed files with 80 additions and 45 deletions

View File

@ -2,16 +2,57 @@
namespace katja
{
PageContainer::PageContainer(std::vector<std::pair<std::string, Page>> pages)
{
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(menu_pages),
[](const std::pair<std::string, Page>& pair) { return pair.second; });
ftxui::MenuOption menu_option = ftxui::MenuOption::Horizontal();
this->menu = ftxui::Menu(&this->menu_entries, &this->menu_selected, menu_option);
this->content = ftxui::Container::Tab(menu_pages, &this->menu_selected);
}
ftxui::Element PageContainer::Render()
{
return ftxui::vbox({
this->menu->Render(),
this->content->Render()
});
}
bool PageContainer::OnEvent(ftxui::Event event)
{
if (event.character() == "q" && this->on_enter)
{
on_enter();
return true;
}
return menu->OnEvent(event);
}
ftxui::Element WelcomePage::Render()
{
return ftxui::text("Select an action in the menu.");
}
void WelcomePage::Load()
{
}
UpdatesPage::UpdatesPage(std::vector<package_identifier>&& updatable)
: updatable(std::move(updatable))
{
}
void UpdatesPage::Load()
{
}
ftxui::Element UpdatesPage::Render()
{
std::vector<std::shared_ptr<ftxui::Node>> lines;
@ -25,4 +66,8 @@ namespace katja
return ftxui::window(summary, ftxui::vbox(lines));
}
void SearchPage::Load()
{
}
}

View File

@ -1,27 +1,58 @@
#pragma once
#include <algorithm>
#include <ftxui/component/component_base.hpp>
#include <ftxui/component/component.hpp>
#include "katja/repository.hpp"
namespace katja
{
class WelcomePage final : public ftxui::ComponentBase
class PageBase : public ftxui::ComponentBase
{
public:
virtual void Load() = 0;
};
using Page = std::shared_ptr<PageBase>;
class PageContainer final : public ftxui::ComponentBase
{
int menu_selected{ 0 };
ftxui::Component menu;
ftxui::Component content;
std::vector<std::string> menu_entries;
public:
std::function<void()> on_enter;
PageContainer(std::vector<std::pair<std::string, Page>> pages);
ftxui::Element Render() override;
bool OnEvent(ftxui::Event event) override;
};
class WelcomePage final : public PageBase
{
public:
void Load() override;
ftxui::Element Render() override;
};
class UpdatesPage final : public ftxui::ComponentBase
class UpdatesPage final : public PageBase
{
std::vector<package_identifier> updatable;
public:
explicit UpdatesPage(std::vector<package_identifier>&& updatable);
void Load() override;
ftxui::Element Render() override;
};
class SearchPage final : public ftxui::ComponentBase
class SearchPage final : public PageBase
{
public:
void Load() override;
};
}

View File

@ -1,6 +1,5 @@
#include <filesystem>
#include <ftxui/component/component.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/dom/elements.hpp>
#include <toml.hpp>
@ -9,46 +8,6 @@
#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");
@ -62,7 +21,7 @@ int main(int argc, const char **argv)
auto screen = ftxui::ScreenInteractive::Fullscreen();
auto container = std::make_shared<MyContainer>(std::vector<std::pair<std::string, ftxui::Component>>{
auto container = std::make_shared<katja::PageContainer>(std::vector<std::pair<std::string, katja::Page>>{
{ "Home", std::make_shared<katja::WelcomePage>() },
{ "Updates", std::make_shared<katja::UpdatesPage>(std::move(updates)) },
{ "Search", std::make_shared<katja::SearchPage>() }