diff --git a/cli/component.cpp b/cli/component.cpp index e69de29..9e0593e 100644 --- a/cli/component.cpp +++ b/cli/component.cpp @@ -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&& updatable) + : updatable(std::move(updatable)) + { + } + + ftxui::Element UpdatesPage::Render() + { + std::vector> 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)); + } +} diff --git a/cli/component.hpp b/cli/component.hpp index a6c1f0d..7fe9c6d 100644 --- a/cli/component.hpp +++ b/cli/component.hpp @@ -6,28 +6,22 @@ namespace katja { -class UpdatesPage : public ftxui::ComponentBase -{ - std::vector updatable; - -public: - explicit UpdatesPage(std::vector&& 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> lines; + std::vector 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&& updatable); - return ftxui::window(summary, ftxui::vbox(lines)); - } -}; + ftxui::Element Render() override; + }; + + class SearchPage final : public ftxui::ComponentBase + { + }; } diff --git a/cli/main.cpp b/cli/main.cpp index 21906d4..d5f8cf3 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -9,6 +9,46 @@ #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"); @@ -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 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(std::move(updates)); - ftxui::Component renderer = ftxui::Container::Vertical({ - menu, - custom_component + auto container = std::make_shared(std::vector>{ + { "Home", std::make_shared() }, + { "Updates", std::make_shared(std::move(updates)) }, + { "Search", std::make_shared() } }); - screen.Loop(renderer); + container->on_enter = screen.ExitLoopClosure(); + + screen.Loop(container); } return EXIT_SUCCESS; }