FTXUI/examples/component/gallery.cpp

135 lines
3.9 KiB
C++
Raw Normal View History

2021-05-02 02:40:35 +08:00
#include <functional> // for function
#include <memory> // for allocator, unique_ptr
#include <string> // for wstring
#include <vector> // for vector
#include "ftxui/component/button.hpp" // for Button
#include "ftxui/component/checkbox.hpp" // for CheckBox
#include "ftxui/component/component.hpp" // for Component, Compone...
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/input.hpp" // for Input
#include "ftxui/component/menu.hpp" // for Menu
#include "ftxui/component/radiobox.hpp" // for RadioBox
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/slider.hpp" // for Slider
#include "ftxui/component/toggle.hpp" // for Toggle
#include "ftxui/dom/elements.hpp" // for separator, operator|
#include "ftxui/screen/box.hpp" // for ftxui
2019-01-13 05:25:49 +08:00
using namespace ftxui;
class MyComponent : public Component {
Container container = Container::Vertical();
Menu menu;
Toggle toggle;
2019-01-19 05:58:32 +08:00
Container checkbox_container = Container::Vertical();
CheckBox checkbox1;
CheckBox checkbox2;
RadioBox radiobox;
Input input;
2020-08-26 22:26:09 +08:00
Button button;
2019-01-13 05:25:49 +08:00
int slider_value_1_ = 12;
int slider_value_2_ = 56;
int slider_value_3_ = 128;
ComponentPtr slider_1_ = Slider(L"R:", &slider_value_1_, 0, 256, 1);
ComponentPtr slider_2_ = Slider(L"G:", &slider_value_2_, 0, 256, 1);
ComponentPtr slider_3_ = Slider(L"B:", &slider_value_3_, 0, 256, 1);
2019-01-13 05:25:49 +08:00
public:
MyComponent() {
Add(&container);
menu.entries = {
2019-01-19 07:20:29 +08:00
L"Menu 1",
L"Menu 2",
L"Menu 3",
L"Menu 4",
2019-01-13 05:25:49 +08:00
};
container.Add(&menu);
toggle.entries = {
2019-01-19 07:20:29 +08:00
L"Toggle_1",
L"Toggle_2",
2019-01-13 05:25:49 +08:00
};
container.Add(&toggle);
2019-01-19 05:58:32 +08:00
container.Add(&checkbox_container);
checkbox1.label = L"checkbox1";
checkbox_container.Add(&checkbox1);
checkbox2.label = L"checkbox2";
checkbox_container.Add(&checkbox2);
radiobox.entries = {
2019-01-19 07:20:29 +08:00
L"Radiobox 1",
L"Radiobox 2",
L"Radiobox 3",
L"Radiobox 4",
2019-01-19 05:58:32 +08:00
};
container.Add(&radiobox);
2019-01-13 05:25:49 +08:00
input.placeholder = L"Input placeholder";
container.Add(&input);
2020-08-26 22:26:09 +08:00
container.Add(slider_1_.get());
container.Add(slider_2_.get());
container.Add(slider_3_.get());
2020-08-26 22:26:09 +08:00
button.label = L"Quit";
button.on_click = [&] { on_quit(); };
container.Add(&button);
2019-01-13 05:25:49 +08:00
}
Element Render(std::wstring name, Element element) {
return hbox({
2021-05-02 02:40:35 +08:00
text(name) | size(WIDTH, EQUAL, 8),
separator(),
element | xflex,
}) |
xflex;
}
Element Render(std::wstring name, Component& component) {
return Render(name, component.Render());
2019-01-19 05:58:32 +08:00
}
2019-01-13 05:25:49 +08:00
Element Render() override {
2020-08-26 22:26:09 +08:00
return //
vbox({
Render(L"menu", menu),
separator(),
Render(L"toggle", toggle),
separator(),
Render(L"checkbox", checkbox_container),
separator(),
Render(L"radiobox", radiobox),
separator(),
Render(L"input", input) | size(WIDTH, LESS_THAN, 50),
separator(),
Render(L"slider", //
vbox({
slider_1_->Render(),
slider_2_->Render(),
slider_3_->Render(),
})),
2020-08-26 22:26:09 +08:00
separator(),
Render(L"button", button),
}) |
xflex | size(WIDTH, GREATER_THAN, 40) | border;
2019-01-13 05:25:49 +08:00
}
2020-08-26 22:26:09 +08:00
std::function<void()> on_quit = [] {};
2019-01-13 05:25:49 +08:00
};
int main(int argc, const char* argv[]) {
2019-01-19 07:20:29 +08:00
auto screen = ScreenInteractive::FitComponent();
2019-01-13 05:25:49 +08:00
MyComponent component;
2020-08-26 22:26:09 +08:00
component.on_quit = screen.ExitLoopClosure();
2019-01-13 05:25:49 +08:00
screen.Loop(&component);
return 0;
}
2020-09-06 19:46:56 +08:00
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.