Update every component examples.

Use the functional style instead of classes.
This commit is contained in:
ArthurSonzogni 2021-05-14 00:45:03 +02:00
parent c9aa1805eb
commit 58287c147a
No known key found for this signature in database
GPG Key ID: 41D98248C074CD6C
9 changed files with 233 additions and 358 deletions

View File

@ -8,38 +8,34 @@
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive #include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, hbox, separator, border, vbox, Element #include "ftxui/dom/elements.hpp" // for text, hbox, separator, border, vbox, Element
using namespace ftxui; int main(int argc, const char* argv[]) {
using namespace ftxui;
class MyComponent : public ComponentBase {
private:
std::wstring first_name_; std::wstring first_name_;
std::wstring last_name_; std::wstring last_name_;
std::wstring first_name_placeholder_ = L"first_name"; std::wstring first_name_placeholder_ = L"first_name";
std::wstring last_name_placeholder_ = L"last_name"; std::wstring last_name_placeholder_ = L"last_name";
Component input_first_name_ = Input(&first_name_, &first_name_placeholder_); Component input_first_name_ = Input(&first_name_, &first_name_placeholder_);
Component input_last_name_ = Input(&last_name_, &last_name_placeholder_); Component input_last_name_ = Input(&last_name_, &last_name_placeholder_);
Element Render() override { auto component = Container::Vertical({
return border(vbox({ input_first_name_,
text(L"Hello " + first_name_ + L" " + last_name_), input_last_name_,
separator(), });
hbox({text(L" First name : "), input_first_name_->Render()}),
hbox({text(L" Last name : "), input_last_name_->Render()}),
}));
}
public: auto renderer = Renderer(component, [&] {
MyComponent() { return vbox({
Add(Container::Vertical({ text(L"Hello " + first_name_ + L" " + last_name_),
input_first_name_, separator(),
input_last_name_, hbox({text(L" First name : "), input_first_name_->Render()}),
})); hbox({text(L" Last name : "), input_last_name_->Render()}),
} }) |
}; border;
});
int main(int argc, const char* argv[]) {
auto screen = ScreenInteractive::TerminalOutput(); auto screen = ScreenInteractive::TerminalOutput();
screen.Loop(Make<MyComponent>()); screen.Loop(renderer);
} }
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -12,10 +12,9 @@
#include "ftxui/dom/elements.hpp" // for text, separator, bold, hcenter, vbox, hbox, gauge, Element, operator|, border #include "ftxui/dom/elements.hpp" // for text, separator, bold, hcenter, vbox, hbox, gauge, Element, operator|, border
#include "ftxui/screen/string.hpp" // for to_wstring #include "ftxui/screen/string.hpp" // for to_wstring
using namespace ftxui; int main(int argc, const char* argv[]) {
using namespace ftxui;
class MyComponent : public ComponentBase {
private:
std::vector<std::wstring> left_menu_entries = { std::vector<std::wstring> left_menu_entries = {
L"0%", L"10%", L"20%", L"30%", L"40%", L"0%", L"10%", L"20%", L"30%", L"40%",
L"50%", L"60%", L"70%", L"80%", L"90%", L"50%", L"60%", L"70%", L"80%", L"90%",
@ -28,12 +27,13 @@ class MyComponent : public ComponentBase {
int right_menu_selected = 0; int right_menu_selected = 0;
Component left_menu_ = Menu(&left_menu_entries, &left_menu_selected); Component left_menu_ = Menu(&left_menu_entries, &left_menu_selected);
Component right_menu_ = Menu(&right_menu_entries, &right_menu_selected); Component right_menu_ = Menu(&right_menu_entries, &right_menu_selected);
Component container = Container::Horizontal({ Component container = Container::Horizontal({
left_menu_, left_menu_,
right_menu_, right_menu_,
}); });
Element Render() override { auto renderer = Renderer(container, [&] {
int sum = left_menu_selected * 10 + right_menu_selected; int sum = left_menu_selected * 10 + right_menu_selected;
return vbox({ return vbox({
// -------- Top panel -------------- // -------- Top panel --------------
@ -67,22 +67,12 @@ class MyComponent : public ComponentBase {
}), }),
}) | }) |
border; border;
} });
public:
MyComponent() {
Add(container);
MenuBase::From(left_menu_)->on_enter = [this]() { on_enter(); };
MenuBase::From(right_menu_)->on_enter = [this]() { on_enter(); };
}
std::function<void()> on_enter = []() {};
};
int main(int argc, const char* argv[]) {
auto screen = ScreenInteractive::TerminalOutput(); auto screen = ScreenInteractive::TerminalOutput();
auto component = Make<MyComponent>(); MenuBase::From(left_menu_)->on_enter = screen.ExitLoopClosure();
component->on_enter = screen.ExitLoopClosure(); MenuBase::From(right_menu_)->on_enter = screen.ExitLoopClosure();
screen.Loop(component); screen.Loop(renderer);
} }
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -13,62 +13,24 @@
#include "ftxui/dom/elements.hpp" // for operator|, Element, separator, bgcolor, color, flex, Decorator, bold, hbox, border, dim #include "ftxui/dom/elements.hpp" // for operator|, Element, separator, bgcolor, color, flex, Decorator, bold, hbox, border, dim
#include "ftxui/screen/color.hpp" // for Color, Color::Blue, Color::BlueLight, Color::Red, Color::Yellow #include "ftxui/screen/color.hpp" // for Color, Color::Blue, Color::BlueLight, Color::Red, Color::Yellow
using namespace ftxui; int main(int argc, const char* argv[]) {
using namespace ftxui;
std::vector<std::wstring> entries = { std::vector<std::wstring> entries = {
L"Monkey", L"Dog", L"Cat", L"Bird", L"Elephant", L"Monkey", L"Dog", L"Cat", L"Bird", L"Elephant",
}; };
class MyComponent : public ComponentBase {
public:
MyComponent(std::function<void(void)> exit) {
on_enter_ = exit;
Add(container);
for (Component menu :
{menu_1_, menu_2_, menu_3_, menu_4_, menu_5_, menu_6_})
MenuBase::From(menu)->on_enter = [this] { on_enter_(); };
MenuBase::From(menu_2_)->focused_style = bold | color(Color::Blue);
MenuBase::From(menu_2_)->selected_style = color(Color::Blue);
MenuBase::From(menu_2_)->selected_focused_style = bold | color(Color::Blue);
MenuBase::From(menu_3_)->selected_style = color(Color::Blue);
MenuBase::From(menu_3_)->focused_style = bgcolor(Color::Blue);
MenuBase::From(menu_3_)->selected_focused_style = bgcolor(Color::Blue);
MenuBase::From(menu_4_)->selected_style = bgcolor(Color::Blue);
MenuBase::From(menu_4_)->focused_style = bgcolor(Color::BlueLight);
MenuBase::From(menu_4_)->selected_focused_style = bgcolor(Color::BlueLight);
MenuBase::From(menu_5_)->normal_style = bgcolor(Color::Blue);
MenuBase::From(menu_5_)->selected_style = bgcolor(Color::Yellow);
MenuBase::From(menu_5_)->focused_style = bgcolor(Color::Red);
MenuBase::From(menu_5_)->selected_focused_style = bgcolor(Color::Red);
MenuBase::From(menu_6_)->normal_style = dim | color(Color::Blue);
MenuBase::From(menu_6_)->selected_style = color(Color::Blue);
MenuBase::From(menu_6_)->focused_style = bold | color(Color::Blue);
MenuBase::From(menu_6_)->selected_focused_style = bold | color(Color::Blue);
}
std::function<void()> on_enter_;
private:
int menu_1_selected_ = 0; int menu_1_selected_ = 0;
int menu_2_selected_ = 0; int menu_2_selected_ = 0;
int menu_3_selected_ = 0; int menu_3_selected_ = 0;
int menu_4_selected_ = 0; int menu_4_selected_ = 0;
int menu_5_selected_ = 0; int menu_5_selected_ = 0;
int menu_6_selected_ = 0; int menu_6_selected_ = 0;
Component menu_1_ = Menu(&entries, &menu_1_selected_); auto menu_1_ = Menu(&entries, &menu_1_selected_);
Component menu_2_ = Menu(&entries, &menu_2_selected_); auto menu_2_ = Menu(&entries, &menu_2_selected_);
Component menu_3_ = Menu(&entries, &menu_3_selected_); auto menu_3_ = Menu(&entries, &menu_3_selected_);
Component menu_4_ = Menu(&entries, &menu_4_selected_); auto menu_4_ = Menu(&entries, &menu_4_selected_);
Component menu_5_ = Menu(&entries, &menu_5_selected_); auto menu_5_ = Menu(&entries, &menu_5_selected_);
Component menu_6_ = Menu(&entries, &menu_6_selected_); auto menu_6_ = Menu(&entries, &menu_6_selected_);
Component container = Container::Horizontal({ auto container = Container::Horizontal({
menu_1_, menu_1_,
menu_2_, menu_2_,
menu_3_, menu_3_,
@ -78,7 +40,7 @@ class MyComponent : public ComponentBase {
}); });
// clang-format off // clang-format off
Element Render() override { auto renderer = Renderer(container, [&] {
return return
hbox({ hbox({
menu_1_->Render() | flex, separator(), menu_1_->Render() | flex, separator(),
@ -88,13 +50,35 @@ class MyComponent : public ComponentBase {
menu_5_->Render() | flex, separator(), menu_5_->Render() | flex, separator(),
menu_6_->Render() | flex, menu_6_->Render() | flex,
}) | border; }) | border;
} });
// clang-format on // clang-format on
};
int main(int argc, const char* argv[]) {
auto screen = ScreenInteractive::TerminalOutput(); auto screen = ScreenInteractive::TerminalOutput();
screen.Loop(Make<MyComponent>(screen.ExitLoopClosure())); for (Component menu : {menu_1_, menu_2_, menu_3_, menu_4_, menu_5_, menu_6_})
MenuBase::From(menu)->on_enter = screen.ExitLoopClosure();
MenuBase::From(menu_2_)->focused_style = bold | color(Color::Blue);
MenuBase::From(menu_2_)->selected_style = color(Color::Blue);
MenuBase::From(menu_2_)->selected_focused_style = bold | color(Color::Blue);
MenuBase::From(menu_3_)->selected_style = color(Color::Blue);
MenuBase::From(menu_3_)->focused_style = bgcolor(Color::Blue);
MenuBase::From(menu_3_)->selected_focused_style = bgcolor(Color::Blue);
MenuBase::From(menu_4_)->selected_style = bgcolor(Color::Blue);
MenuBase::From(menu_4_)->focused_style = bgcolor(Color::BlueLight);
MenuBase::From(menu_4_)->selected_focused_style = bgcolor(Color::BlueLight);
MenuBase::From(menu_5_)->normal_style = bgcolor(Color::Blue);
MenuBase::From(menu_5_)->selected_style = bgcolor(Color::Yellow);
MenuBase::From(menu_5_)->focused_style = bgcolor(Color::Red);
MenuBase::From(menu_5_)->selected_focused_style = bgcolor(Color::Red);
MenuBase::From(menu_6_)->normal_style = dim | color(Color::Blue);
MenuBase::From(menu_6_)->selected_style = color(Color::Blue);
MenuBase::From(menu_6_)->focused_style = bold | color(Color::Blue);
MenuBase::From(menu_6_)->selected_focused_style = bold | color(Color::Blue);
screen.Loop(renderer);
} }
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -10,121 +10,84 @@
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive #include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, operator|, filler, text, hbox, separator, center, vbox, bold, border, clear_under, dbox, size, GREATER_THAN, HEIGHT #include "ftxui/dom/elements.hpp" // for Element, operator|, filler, text, hbox, separator, center, vbox, bold, border, clear_under, dbox, size, GREATER_THAN, HEIGHT
using namespace ftxui; int main(int argc, const char* argv[]) {
using namespace ftxui;
auto screen = ScreenInteractive::TerminalOutput();
// The main screen, at depth 0. It display the main content. // There are two layers. One at depth = 0 and the modal window at depth = 1;
class Content : public ComponentBase { int depth = 0;
private:
std::wstring label_rate_ftxui_ = L"Rate FTXUI";
std::wstring label_quit_ = L"Quit";
bool modal_open_ = false;
Component button_rate_ftxui_ = // The current rating of FTXUI.
Button(&label_rate_ftxui_, [this] { on_rate_ftxui(); });
Component button_quit_ = Button(&label_quit_, [this] { on_quit(); });
Component container_ = Container::Horizontal({
button_rate_ftxui_,
button_quit_,
});
public:
std::wstring rating = L"3/5 stars"; std::wstring rating = L"3/5 stars";
std::function<void()> on_rate_ftxui;
std::function<void()> on_quit;
Content() { Add(container_); } // At depth=0, two buttons. One for rating FTXUI and one for quitting.
std::wstring label_rate_ftxui = L"Rate FTXUI";
std::wstring label_quit = L"Quit";
auto button_rate_ftxui = Button(&label_rate_ftxui, [&] { depth = 1; });
auto button_quit = Button(&label_quit, screen.ExitLoopClosure());
Element Render() final { auto depth_0_container = Container::Horizontal({
auto document = // button_rate_ftxui,
vbox({ button_quit,
text(L"Modal dialog example"), });
separator(), auto depth_0_renderer = Renderer(depth_0_container, [&] {
text(L"☆☆☆ FTXUI:" + rating + L" ☆☆☆") | bold, return vbox({
filler(), text(L"Modal dialog example"),
hbox({ separator(),
button_rate_ftxui_->Render(), text(L"☆☆☆ FTXUI:" + rating + L" ☆☆☆") | bold,
filler(), filler(),
button_quit_->Render(), hbox({
}), button_rate_ftxui->Render(),
}) | filler(),
border; button_quit->Render(),
}),
return document | size(HEIGHT, GREATER_THAN, 18) | center; }) |
} border | size(HEIGHT, GREATER_THAN, 18) | center;
};
std::vector<std::wstring> rating_labels = {
L"1/5 stars", L"2/5 stars", L"3/5 stars", L"4/5 stars", L"5/5 stars",
};
// The "modal" screen, at depth 1. It display the modal dialog.
class Modal : public ComponentBase {
private:
Component container_ = Container::Horizontal({
Button(&rating_labels[0], [this] { on_click(rating_labels[0]); }),
Button(&rating_labels[1], [this] { on_click(rating_labels[1]); }),
Button(&rating_labels[2], [this] { on_click(rating_labels[2]); }),
Button(&rating_labels[3], [this] { on_click(rating_labels[3]); }),
Button(&rating_labels[4], [this] { on_click(rating_labels[4]); }),
}); });
public: // At depth=1, The "modal" window.
std::function<void(std::wstring)> on_click; std::vector<std::wstring> rating_labels = {
L"1/5 stars", L"2/5 stars", L"3/5 stars", L"4/5 stars", L"5/5 stars",
};
auto on_rating = [&](std::wstring new_rating) {
rating = new_rating;
depth = 0;
};
auto depth_1_container = Container::Horizontal({
Button(&rating_labels[0], [&] { on_rating(rating_labels[0]); }),
Button(&rating_labels[1], [&] { on_rating(rating_labels[1]); }),
Button(&rating_labels[2], [&] { on_rating(rating_labels[2]); }),
Button(&rating_labels[3], [&] { on_rating(rating_labels[3]); }),
Button(&rating_labels[4], [&] { on_rating(rating_labels[4]); }),
});
Modal() { Add(container_); } auto depth_1_renderer = Renderer(depth_1_container, [&] {
Element Render() final {
return vbox({ return vbox({
text(L"Do you like FTXUI?"), text(L"Do you like FTXUI?"),
separator(), separator(),
hbox(container_->Render()), hbox(depth_1_container->Render()),
}) | }) |
border; border;
} });
};
class MyComponent : public ComponentBase { auto main_container = Container::Tab(&depth, {
private: depth_0_renderer,
std::shared_ptr<Content> content_ = std::make_shared<Content>(); depth_1_renderer,
std::shared_ptr<Modal> modal_ = std::make_shared<Modal>(); });
int depth = 0; auto main_renderer = Renderer(main_container, [&] {
Component container_ = Container::Tab(&depth, Element document = depth_0_renderer->Render();
{
content_,
modal_,
});
std::function<void()> on_quit_;
public:
MyComponent(std::function<void()> on_quit) : on_quit_(on_quit) {
Add(container_);
content_->on_quit = [&] { on_quit(); };
content_->on_rate_ftxui = [this] { depth = 1; };
modal_->on_click = [&](std::wstring rating) {
content_->rating = rating;
depth = 0;
};
}
Element Render() final {
Element document = content_->Render();
if (depth == 1) { if (depth == 1) {
document = dbox({ document = dbox({
document, document,
modal_->Render() | clear_under | center, depth_1_renderer->Render() | clear_under | center,
}); });
} }
return document; return document;
} });
};
int main(int argc, const char* argv[]) { screen.Loop(main_renderer);
auto screen = ScreenInteractive::TerminalOutput();
screen.Loop(Make<MyComponent>(screen.ExitLoopClosure()));
return 0; return 0;
} }

View File

@ -10,27 +10,19 @@
using namespace ftxui; using namespace ftxui;
class MyComponent : public ComponentBase {
private:
std::vector<std::wstring> entries_;
int selected_ = 0;
public:
MyComponent() {
for (int i = 0; i < 30; ++i)
entries_.push_back(L"RadioBox " + to_wstring(i));
Add(Radiobox(&entries_, &selected_));
}
Element Render() override {
return ComponentBase::Render() | frame | size(HEIGHT, LESS_THAN, 10) |
border;
}
};
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
std::vector<std::wstring> entries;
int selected = 0;
for (int i = 0; i < 30; ++i)
entries.push_back(L"RadioBox " + to_wstring(i));
auto radiobox = Radiobox(&entries, &selected);
auto renderer = Renderer(radiobox, [&] {
return radiobox->Render() | frame | size(HEIGHT, LESS_THAN, 10) | border;
});
auto screen = ScreenInteractive::FitComponent(); auto screen = ScreenInteractive::FitComponent();
screen.Loop(Make<MyComponent>()); screen.Loop(renderer);
return 0; return 0;
} }

View File

@ -26,56 +26,38 @@ Element ColorString(int red, int green, int blue) {
); );
} }
class MyComponent : public ComponentBase {
private:
int* red_;
int* green_;
int* blue_;
Component slider_red_ = Slider(L"Red :", red_, 0, 255, 1);
Component slider_green_ = Slider(L"Green:", green_, 0, 255, 1);
Component slider_blue_ = Slider(L"Blue :", blue_, 0, 255, 1);
std::function<void(void)> quit_;
public:
MyComponent(int* red, int* green, int* blue, std::function<void(void)> quit)
: red_(red), green_(green), blue_(blue), quit_(quit) {
Add(Container::Vertical({
slider_red_,
slider_green_,
slider_blue_,
}));
}
Element Render() {
return hbox({
ColorTile(*red_, *green_, *blue_),
separator(),
vbox({
slider_red_->Render(),
separator(),
slider_green_->Render(),
separator(),
slider_blue_->Render(),
separator(),
ColorString(*red_, *green_, *blue_),
}) | xflex,
}) |
border | size(WIDTH, LESS_THAN, 80);
}
bool OnEvent(Event event) {
if (event == Event::Return || event == Event::Escape)
quit_();
return ComponentBase::OnEvent(event);
}
};
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
auto screen = ScreenInteractive::TerminalOutput();
int red = 128; int red = 128;
int green = 25; int green = 25;
int blue = 100; int blue = 100;
screen.Loop(Make<MyComponent>(&red, &green, &blue, screen.ExitLoopClosure())); auto slider_red = Slider(L"Red :", &red, 0, 255, 1);
auto slider_green = Slider(L"Green:", &green, 0, 255, 1);
auto slider_blue = Slider(L"Blue :", &blue, 0, 255, 1);
auto container = Container::Vertical({
slider_red,
slider_green,
slider_blue,
});
auto renderer = Renderer(container, [&] {
return hbox({
ColorTile(red, green, blue),
separator(),
vbox({
slider_red->Render(),
separator(),
slider_green->Render(),
separator(),
slider_blue->Render(),
separator(),
ColorString(red, green, blue),
}) | xflex,
}) |
border | size(WIDTH, LESS_THAN, 80);
});
auto screen = ScreenInteractive::TerminalOutput();
screen.Loop(renderer);
} }
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -11,67 +11,59 @@
using namespace ftxui; using namespace ftxui;
class MyComponent : public ComponentBase { int main(int argc, const char* argv[]) {
private: std::vector<std::wstring> tab_values{
std::vector<std::wstring> tab_values_ = {
L"tab_1", L"tab_1",
L"tab_2", L"tab_2",
L"tab_3", L"tab_3",
}; };
int tab_selected_ = 0; int tab_selected = 0;
Component tab_toggle_ = Toggle(&tab_values_, &tab_selected_); auto tab_toggle = Toggle(&tab_values, &tab_selected);
std::vector<std::wstring> tab_1_entries_ = { std::vector<std::wstring> tab_1_entries{
L"Forest", L"Forest",
L"Water", L"Water",
L"I don't know", L"I don't know",
}; };
int tab_1_selected_ = 0; int tab_1_selected = 0;
std::vector<std::wstring> tab_2_entries_ = { std::vector<std::wstring> tab_2_entries{
L"Hello", L"Hello",
L"Hi", L"Hi",
L"Hay", L"Hay",
}; };
int tab_2_selected_ = 0; int tab_2_selected = 0;
std::vector<std::wstring> tab_3_entries_ = { std::vector<std::wstring> tab_3_entries{
L"Table", L"Table",
L"Nothing", L"Nothing",
L"Is", L"Is",
L"Empty", L"Empty",
}; };
int tab_3_selected_ = 0; int tab_3_selected = 0;
auto tab_container = Container::Tab(
Component tab_container_ = &tab_selected, {
Container::Tab(&tab_selected_, Radiobox(&tab_1_entries, &tab_1_selected),
{ Radiobox(&tab_2_entries, &tab_2_selected),
Radiobox(&tab_1_entries_, &tab_1_selected_), Radiobox(&tab_3_entries, &tab_3_selected),
Radiobox(&tab_2_entries_, &tab_2_selected_),
Radiobox(&tab_3_entries_, &tab_3_selected_),
}); });
Component container_ = Container::Vertical({ auto container = Container::Vertical({
tab_toggle_, tab_toggle,
tab_container_, tab_container,
}); });
public: auto renderer = Renderer(container, [&] {
MyComponent() { Add(container_); }
Element Render() {
return vbox({ return vbox({
tab_toggle_->Render(), tab_toggle->Render(),
separator(), separator(),
tab_container_->Render(), tab_container->Render(),
}) | }) |
border; border;
} });
};
int main(int argc, const char* argv[]) {
auto screen = ScreenInteractive::TerminalOutput(); auto screen = ScreenInteractive::TerminalOutput();
screen.Loop(Make<MyComponent>()); screen.Loop(renderer);
} }
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -3,75 +3,67 @@
#include <vector> // for vector #include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Radiobox, Make, Menu #include "ftxui/component/component.hpp" // for Radiobox, Make, Toggle
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container #include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive #include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, separator, hbox, operator|, border #include "ftxui/dom/elements.hpp" // for Element, separator, operator|, vbox, border
using namespace ftxui; using namespace ftxui;
class MyComponent : public ComponentBase { int main(int argc, const char* argv[]) {
private: std::vector<std::wstring> tab_values{
std::vector<std::wstring> tab_values_ = {
L"tab_1", L"tab_1",
L"tab_2", L"tab_2",
L"tab_3", L"tab_3",
}; };
int tab_selected_ = 0; int tab_selected = 0;
Component tab_toggle_ = Menu(&tab_values_, &tab_selected_); auto tab_menu = Menu(&tab_values, &tab_selected);
std::vector<std::wstring> tab_1_entries_ = { std::vector<std::wstring> tab_1_entries{
L"Forest", L"Forest",
L"Water", L"Water",
L"I don't know", L"I don't know",
}; };
int tab_1_selected_ = 0; int tab_1_selected = 0;
std::vector<std::wstring> tab_2_entries_ = { std::vector<std::wstring> tab_2_entries{
L"Hello", L"Hello",
L"Hi", L"Hi",
L"Hay", L"Hay",
}; };
int tab_2_selected_ = 0; int tab_2_selected = 0;
std::vector<std::wstring> tab_3_entries_ = { std::vector<std::wstring> tab_3_entries{
L"Table", L"Table",
L"Nothing", L"Nothing",
L"Is", L"Is",
L"Empty", L"Empty",
}; };
int tab_3_selected_ = 0; int tab_3_selected = 0;
auto tab_container = Container::Tab(
Component tab_container_ = &tab_selected, {
Container::Tab(&tab_selected_, Radiobox(&tab_1_entries, &tab_1_selected),
{ Radiobox(&tab_2_entries, &tab_2_selected),
Radiobox(&tab_1_entries_, &tab_1_selected_), Radiobox(&tab_3_entries, &tab_3_selected),
Radiobox(&tab_2_entries_, &tab_2_selected_),
Radiobox(&tab_3_entries_, &tab_3_selected_),
}); });
Component container_ = Container::Horizontal({ auto container = Container::Horizontal({
tab_toggle_, tab_menu,
tab_container_, tab_container,
}); });
public: auto renderer = Renderer(container, [&] {
MyComponent() { Add(container_); }
Element Render() {
return hbox({ return hbox({
tab_toggle_->Render(), tab_menu->Render(),
separator(), separator(),
tab_container_->Render(), tab_container->Render(),
}) | }) |
border; border;
} });
};
int main(int argc, const char* argv[]) {
auto screen = ScreenInteractive::TerminalOutput(); auto screen = ScreenInteractive::TerminalOutput();
screen.Loop(Make<MyComponent>()); screen.Loop(renderer);
} }
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -13,70 +13,54 @@
using namespace ftxui; using namespace ftxui;
class MyComponent : public ComponentBase { int main(int argc, const char* argv[]) {
private: std::vector<std::wstring> toggle_1_entries = {
std::vector<std::wstring> toggle_1_entries_ = {
L"On", L"On",
L"Off", L"Off",
}; };
std::vector<std::wstring> toggle_2_entries_ = { std::vector<std::wstring> toggle_2_entries = {
L"Enabled", L"Enabled",
L"Disabled", L"Disabled",
}; };
std::vector<std::wstring> toggle_3_entries_ = { std::vector<std::wstring> toggle_3_entries = {
L"10€", L"10€",
L"0€", L"0€",
}; };
std::vector<std::wstring> toggle_4_entries_ = { std::vector<std::wstring> toggle_4_entries = {
L"Nothing", L"Nothing",
L"One element", L"One element",
L"Several elements", L"Several elements",
}; };
int toggle_1_selected_ = 0; int toggle_1_selected = 0;
int toggle_2_selected_ = 0; int toggle_2_selected = 0;
int toggle_3_selected_ = 0; int toggle_3_selected = 0;
int toggle_4_selected_ = 0; int toggle_4_selected = 0;
Component toggle_1_ = Toggle(&toggle_1_entries_, &toggle_1_selected_); Component toggle_1 = Toggle(&toggle_1_entries, &toggle_1_selected);
Component toggle_2_ = Toggle(&toggle_2_entries_, &toggle_2_selected_); Component toggle_2 = Toggle(&toggle_2_entries, &toggle_2_selected);
Component toggle_3_ = Toggle(&toggle_3_entries_, &toggle_3_selected_); Component toggle_3 = Toggle(&toggle_3_entries, &toggle_3_selected);
Component toggle_4_ = Toggle(&toggle_4_entries_, &toggle_4_selected_); Component toggle_4 = Toggle(&toggle_4_entries, &toggle_4_selected);
std::function<void()> exit_; auto container = Container::Vertical({
toggle_1,
toggle_2,
toggle_3,
toggle_4,
});
Element Render() override { auto renderer = Renderer(container, [&] {
return vbox({ return vbox({
text(L"Choose your options:"), text(L"Choose your options:"),
text(L""), text(L""),
hbox(text(L" * Poweroff on startup : "), toggle_1_->Render()), hbox(text(L" * Poweroff on startup : "), toggle_1->Render()),
hbox(text(L" * Out of process : "), toggle_2_->Render()), hbox(text(L" * Out of process : "), toggle_2->Render()),
hbox(text(L" * Price of the information : "), toggle_3_->Render()), hbox(text(L" * Price of the information : "), toggle_3->Render()),
hbox(text(L" * Number of elements : "), toggle_4_->Render()), hbox(text(L" * Number of elements : "), toggle_4->Render()),
}); });
} });
bool OnEvent(Event event) override {
if (event == Event::Return) {
exit_();
return true;
}
return ComponentBase::OnEvent(event);
}
public:
MyComponent(std::function<void()> exit) : exit_(exit) {
Add(Container::Vertical({
toggle_1_,
toggle_2_,
toggle_3_,
toggle_4_,
}));
}
};
int main(int argc, const char* argv[]) {
auto screen = ScreenInteractive::TerminalOutput(); auto screen = ScreenInteractive::TerminalOutput();
screen.Loop(Make<MyComponent>(screen.ExitLoopClosure())); screen.Loop(renderer);
} }
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.