katja: Update to ftxui 6
This commit is contained in:
parent
24ecc7becd
commit
de94f3d355
@ -1,6 +1,6 @@
|
||||
FetchContent_Declare(ftxui
|
||||
GIT_REPOSITORY https://github.com/ArthurSonzogni/ftxui
|
||||
GIT_TAG v5.0.0
|
||||
GIT_TAG v6.0.2
|
||||
GIT_PROGRESS TRUE
|
||||
GIT_SHALLOW TRUE
|
||||
EXCLUDE_FROM_ALL
|
||||
|
@ -1,41 +1,58 @@
|
||||
#include "component.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace katja
|
||||
{
|
||||
PageContainer::PageContainer(std::vector<std::pair<std::string, Page>> pages)
|
||||
ScreenContainer::ScreenContainer(std::vector<std::pair<std::string, Page>> pages, std::function<void()> on_enter)
|
||||
: on_enter(on_enter)
|
||||
{
|
||||
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),
|
||||
std::transform(std::cbegin(pages), std::cend(pages), std::back_inserter(this->menu_pages),
|
||||
[](const std::pair<std::string, Page>& pair) { return pair.second; });
|
||||
std::copy(std::cbegin(this->menu_pages), std::cend(this->menu_pages), std::back_inserter(menu_pages));
|
||||
|
||||
ftxui::MenuOption menu_option = ftxui::MenuOption::Horizontal();
|
||||
this->menu = ftxui::Menu(&this->menu_entries, &this->menu_selected, menu_option);
|
||||
this->menu = ftxui::Toggle(&this->menu_entries, &this->menu_selected);
|
||||
|
||||
this->content = ftxui::Container::Tab(menu_pages, &this->menu_selected);
|
||||
this->content = ftxui::Container::Tab(std::move(menu_pages), &this->menu_selected);
|
||||
}
|
||||
|
||||
ftxui::Element PageContainer::Render()
|
||||
ftxui::Element ScreenContainer::OnRender()
|
||||
{
|
||||
return ftxui::vbox({
|
||||
this->menu->Render(),
|
||||
ftxui::separator(),
|
||||
this->content->Render()
|
||||
});
|
||||
}
|
||||
|
||||
bool PageContainer::OnEvent(ftxui::Event event)
|
||||
bool ScreenContainer::OnEvent(ftxui::Event event)
|
||||
{
|
||||
if (event.character() == "q" && this->on_enter)
|
||||
{
|
||||
on_enter();
|
||||
return true;
|
||||
}
|
||||
return menu->OnEvent(event);
|
||||
int previously = this->menu_selected;
|
||||
bool result = menu->OnEvent(event);
|
||||
|
||||
if (previously != this->menu_selected)
|
||||
{
|
||||
this->menu_pages.at(this->menu_selected)->Load();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
ftxui::Element WelcomePage::Render()
|
||||
ftxui::Component Screen(std::vector<std::pair<std::string, Page>> pages, std::function<void()> on_enter)
|
||||
{
|
||||
return std::make_shared<ScreenContainer>(std::move(pages), on_enter);
|
||||
}
|
||||
|
||||
ftxui::Element WelcomePage::OnRender()
|
||||
{
|
||||
return ftxui::text("Select an action in the menu.");
|
||||
}
|
||||
@ -53,7 +70,7 @@ namespace katja
|
||||
{
|
||||
}
|
||||
|
||||
ftxui::Element UpdatesPage::Render()
|
||||
ftxui::Element UpdatesPage::OnRender()
|
||||
{
|
||||
std::vector<std::shared_ptr<ftxui::Node>> lines;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <ftxui/component/event.hpp>
|
||||
#include <ftxui/component/component_base.hpp>
|
||||
#include <ftxui/component/component.hpp>
|
||||
|
||||
@ -15,28 +15,31 @@ namespace katja
|
||||
};
|
||||
|
||||
using Page = std::shared_ptr<PageBase>;
|
||||
using Pages = std::vector<Page>;
|
||||
|
||||
class PageContainer final : public ftxui::ComponentBase
|
||||
class ScreenContainer final : public ftxui::ComponentBase
|
||||
{
|
||||
int menu_selected{ 0 };
|
||||
ftxui::Component menu;
|
||||
ftxui::Component content;
|
||||
std::vector<std::string> menu_entries;
|
||||
|
||||
public:
|
||||
Pages menu_pages;
|
||||
std::function<void()> on_enter;
|
||||
|
||||
PageContainer(std::vector<std::pair<std::string, Page>> pages);
|
||||
public:
|
||||
ScreenContainer(std::vector<std::pair<std::string, Page>> pages, std::function<void()> on_enter);
|
||||
|
||||
ftxui::Element Render() override;
|
||||
ftxui::Element OnRender() override;
|
||||
bool OnEvent(ftxui::Event event) override;
|
||||
};
|
||||
|
||||
ftxui::Component Screen(std::vector<std::pair<std::string, Page>> pages, std::function<void()> on_enter);
|
||||
|
||||
class WelcomePage final : public PageBase
|
||||
{
|
||||
public:
|
||||
void Load() override;
|
||||
ftxui::Element Render() override;
|
||||
ftxui::Element OnRender() override;
|
||||
};
|
||||
|
||||
class UpdatesPage final : public PageBase
|
||||
@ -47,7 +50,7 @@ namespace katja
|
||||
explicit UpdatesPage(std::vector<package_identifier>&& updatable);
|
||||
|
||||
void Load() override;
|
||||
ftxui::Element Render() override;
|
||||
ftxui::Element OnRender() override;
|
||||
};
|
||||
|
||||
class SearchPage final : public PageBase
|
||||
|
@ -21,12 +21,11 @@ int main(int argc, const char **argv)
|
||||
|
||||
auto screen = ftxui::ScreenInteractive::Fullscreen();
|
||||
|
||||
auto container = std::make_shared<katja::PageContainer>(std::vector<std::pair<std::string, katja::Page>>{
|
||||
auto container = Screen(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>() }
|
||||
});
|
||||
container->on_enter = screen.ExitLoopClosure();
|
||||
}, screen.ExitLoopClosure());
|
||||
|
||||
screen.Loop(container);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user