diff --git a/cli/component.cpp b/cli/component.cpp index 9e0593e..4def946 100644 --- a/cli/component.cpp +++ b/cli/component.cpp @@ -2,16 +2,57 @@ namespace katja { + PageContainer::PageContainer(std::vector> pages) + { + ftxui::Components menu_pages; + + std::transform(std::cbegin(pages), std::cend(pages), std::back_inserter(menu_entries), + [](const std::pair& pair) { return pair.first; }); + std::transform(std::cbegin(pages), std::cend(pages), std::back_inserter(menu_pages), + [](const std::pair& 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&& updatable) : updatable(std::move(updatable)) { } + void UpdatesPage::Load() + { + } + ftxui::Element UpdatesPage::Render() { std::vector> lines; @@ -25,4 +66,8 @@ namespace katja return ftxui::window(summary, ftxui::vbox(lines)); } + + void SearchPage::Load() + { + } } diff --git a/cli/component.hpp b/cli/component.hpp index 7fe9c6d..fe02d0b 100644 --- a/cli/component.hpp +++ b/cli/component.hpp @@ -1,27 +1,58 @@ #pragma once +#include #include +#include #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; + + class PageContainer final : public ftxui::ComponentBase + { + int menu_selected{ 0 }; + ftxui::Component menu; + ftxui::Component content; + std::vector menu_entries; + + public: + std::function on_enter; + + PageContainer(std::vector> 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 updatable; public: explicit UpdatesPage(std::vector&& updatable); + void Load() override; ftxui::Element Render() override; }; - class SearchPage final : public ftxui::ComponentBase + class SearchPage final : public PageBase { + public: + void Load() override; }; } diff --git a/cli/main.cpp b/cli/main.cpp index d5f8cf3..c2d91ca 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -1,6 +1,5 @@ #include -#include #include #include #include @@ -9,46 +8,6 @@ #include "katja/database.hpp" #include "component.hpp" -class MyContainer final : public ftxui::ComponentBase -{ -public: - std::vector menu_entries; - std::vector menu_pages; - int menu_selected{ 0 }; - ftxui::Component menu; - std::function on_enter; - - MyContainer(std::vector> pages) - { - std::transform(std::cbegin(pages), std::cend(pages), std::back_inserter(this->menu_entries), - [](const std::pair& pair) { return pair.first; }); - std::transform(std::cbegin(pages), std::cend(pages), std::back_inserter(this->menu_pages), - [](const std::pair& 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(std::vector>{ + auto container = std::make_shared(std::vector>{ { "Home", std::make_shared() }, { "Updates", std::make_shared(std::move(updates)) }, { "Search", std::make_shared() }