From ff3c508ceb185d631aea7fed93329525d77a1704 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Tue, 1 Apr 2025 23:36:50 +0200 Subject: Extract pages into components --- cli/main.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 14 deletions(-) (limited to 'cli/main.cpp') 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; } -- cgit v1.2.3