mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-23 21:58:09 +08:00
Make component more functionnal
This commit is contained in:
@@ -1,132 +1,130 @@
|
||||
#include <functional> // for function
|
||||
#include <memory> // for allocator_traits<>...
|
||||
#include <string> // for operator+, wstring
|
||||
#include <vector> // for vector
|
||||
#include <memory> // for allocator, __shared_ptr_access, shared_ptr, make_shared
|
||||
#include <string> // for wstring, operator+, basic_string, char_traits
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "ftxui/component/button.hpp" // for Button
|
||||
#include "ftxui/component/component.hpp" // for Component
|
||||
#include "ftxui/component/container.hpp" // for Container
|
||||
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
|
||||
#include "ftxui/dom/elements.hpp" // for Element, operator|
|
||||
#include "ftxui/screen/box.hpp" // for ftxui
|
||||
#include "ftxui/component/captured_mouse.hpp" // for ftxui
|
||||
#include "ftxui/component/component.hpp" // for Button, Make
|
||||
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
||||
#include "ftxui/component/container.hpp" // for Container
|
||||
#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
|
||||
|
||||
using namespace ftxui;
|
||||
|
||||
// The main screen, at depth 0. It display the main content.
|
||||
class Content : public Component {
|
||||
class Content : public ComponentBase {
|
||||
private:
|
||||
std::wstring label_rate_ftxui_ = L"Rate FTXUI";
|
||||
std::wstring label_quit_ = L"Quit";
|
||||
bool modal_open_ = false;
|
||||
|
||||
Component button_rate_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::function<void()> on_rate_ftxui = [] {};
|
||||
std::function<void()> on_quit = [] {};
|
||||
std::wstring rating_ = L"3/5 stars";
|
||||
Content() {
|
||||
Add(&container_);
|
||||
container_.Add(&button_rate_ftxui);
|
||||
container_.Add(&button_quit_);
|
||||
button_rate_ftxui.on_click = [&] { on_rate_ftxui(); };
|
||||
button_quit_.on_click = [&] { on_quit(); };
|
||||
}
|
||||
std::wstring rating = L"3/5 stars";
|
||||
std::function<void()> on_rate_ftxui;
|
||||
std::function<void()> on_quit;
|
||||
|
||||
Content() { Add(container_); }
|
||||
|
||||
Element Render() final {
|
||||
auto button_elements = hbox({
|
||||
button_rate_ftxui.Render(),
|
||||
filler(),
|
||||
button_quit_.Render(),
|
||||
});
|
||||
|
||||
auto document = //
|
||||
vbox({
|
||||
text(L"Modal dialog example"),
|
||||
separator(),
|
||||
text(L"☆☆☆ FTXUI:" + rating_ + L" ☆☆☆") | bold,
|
||||
text(L"☆☆☆ FTXUI:" + rating + L" ☆☆☆") | bold,
|
||||
filler(),
|
||||
button_elements,
|
||||
hbox({
|
||||
button_rate_ftxui_->Render(),
|
||||
filler(),
|
||||
button_quit_->Render(),
|
||||
}),
|
||||
}) |
|
||||
border;
|
||||
|
||||
return document | size(HEIGHT, GREATER_THAN, 18) | center;
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
Container container_ = Container::Horizontal();
|
||||
Button button_rate_ftxui = Button(L"Rate FTXUI");
|
||||
Button button_quit_ = Button(L"Quit");
|
||||
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 Component {
|
||||
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:
|
||||
std::function<void(std::wstring)> on_click;
|
||||
|
||||
Modal() {
|
||||
Add(&container_);
|
||||
buttons_.resize(5);
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
std::wstring stars = std::to_wstring(i + 1) + L"/5 stars";
|
||||
buttons_[i] = Button(stars);
|
||||
buttons_[i].on_click = [&, stars] { on_click(stars); };
|
||||
container_.Add(&buttons_[i]);
|
||||
}
|
||||
}
|
||||
Modal() { Add(container_); }
|
||||
|
||||
Element Render() final {
|
||||
return vbox({
|
||||
text(L"Do you like FTXUI?"),
|
||||
separator(),
|
||||
hbox({
|
||||
buttons_[0].Render(),
|
||||
buttons_[1].Render(),
|
||||
buttons_[2].Render(),
|
||||
buttons_[3].Render(),
|
||||
buttons_[4].Render(),
|
||||
}),
|
||||
hbox(container_->Render()),
|
||||
}) |
|
||||
border;
|
||||
}
|
||||
|
||||
private:
|
||||
Container container_ = Container::Horizontal();
|
||||
std::vector<Button> buttons_;
|
||||
};
|
||||
|
||||
class MyComponent : public Component {
|
||||
class MyComponent : public ComponentBase {
|
||||
private:
|
||||
std::shared_ptr<Content> content_ = std::make_shared<Content>();
|
||||
std::shared_ptr<Modal> modal_ = std::make_shared<Modal>();
|
||||
|
||||
int depth = 0;
|
||||
Component container_ = Container::Tab(&depth,
|
||||
{
|
||||
content_,
|
||||
modal_,
|
||||
});
|
||||
|
||||
std::function<void()> on_quit_;
|
||||
|
||||
public:
|
||||
std::function<void()> on_quit = [] {};
|
||||
MyComponent(std::function<void()> on_quit) : on_quit_(on_quit) {
|
||||
Add(container_);
|
||||
|
||||
MyComponent() {
|
||||
Add(&container_);
|
||||
container_.Add(&content_);
|
||||
container_.Add(&modal_);
|
||||
|
||||
content_.on_quit = [&] { on_quit(); };
|
||||
content_.on_rate_ftxui = [&] { modal_.TakeFocus(); };
|
||||
modal_.on_click = [&](std::wstring rating) {
|
||||
content_.rating_ = rating;
|
||||
content_.TakeFocus();
|
||||
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 (modal_.Focused()) {
|
||||
Element document = content_->Render();
|
||||
|
||||
if (depth == 1) {
|
||||
document = dbox({
|
||||
document,
|
||||
modal_.Render() | clear_under | center,
|
||||
modal_->Render() | clear_under | center,
|
||||
});
|
||||
}
|
||||
return document;
|
||||
}
|
||||
|
||||
private:
|
||||
Container container_ = Container::Tab(nullptr);
|
||||
Content content_;
|
||||
Modal modal_;
|
||||
};
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
auto screen = ScreenInteractive::TerminalOutput();
|
||||
MyComponent my_component;
|
||||
my_component.on_quit = screen.ExitLoopClosure();
|
||||
screen.Loop(&my_component);
|
||||
screen.Loop(Make<MyComponent>(screen.ExitLoopClosure()));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user