diff options
| author | Eugen Wissner <belka@caraus.de> | 2025-04-01 23:36:50 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2025-04-01 23:36:50 +0200 |
| commit | ff3c508ceb185d631aea7fed93329525d77a1704 (patch) | |
| tree | e67a52c78776e5c0f28c73d7797bcc2302f40dac /cli/main.cpp | |
| parent | 86ea47342b4a87c25c3daac1fe87d784990d65d6 (diff) | |
| download | kazbek-ff3c508ceb185d631aea7fed93329525d77a1704.tar.gz | |
Extract pages into components
Diffstat (limited to 'cli/main.cpp')
| -rw-r--r-- | cli/main.cpp | 61 |
1 files changed, 47 insertions, 14 deletions
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<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; } |
