FTXUI/examples/component/gallery.cpp

134 lines
4.3 KiB
C++
Raw Normal View History

2023-08-19 19:56:36 +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.
2021-05-02 02:40:35 +08:00
#include <functional> // for function
#include <memory> // for shared_ptr, allocator, __shared_ptr_access
#include <string> // for string, basic_string
2021-05-02 02:40:35 +08:00
#include <vector> // for vector
2021-05-10 02:32:27 +08:00
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Slider, Checkbox, Vertical, Renderer, Button, Input, Menu, Radiobox, Toggle
2021-05-10 02:32:27 +08:00
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for separator, operator|, Element, size, xflex, text, WIDTH, hbox, vbox, EQUAL, border, GREATER_THAN
2019-01-13 05:25:49 +08:00
using namespace ftxui;
2021-05-13 17:44:47 +08:00
// Display a component nicely with a title on the left.
Component Wrap(std::string name, Component component) {
2021-05-13 17:44:47 +08:00
return Renderer(component, [name, component] {
return hbox({
text(name) | size(WIDTH, EQUAL, 8),
separator(),
component->Render() | xflex,
}) |
xflex;
});
}
int main() {
2021-05-13 17:44:47 +08:00
auto screen = ScreenInteractive::FitComponent();
// -- Menu
// ----------------------------------------------------------------------
const std::vector<std::string> menu_entries = {
"Menu 1",
"Menu 2",
"Menu 3",
"Menu 4",
2021-05-10 02:32:27 +08:00
};
2021-05-13 17:44:47 +08:00
int menu_selected = 0;
auto menu = Menu(&menu_entries, &menu_selected);
menu = Wrap("Menu", menu);
2021-05-10 02:32:27 +08:00
2021-05-13 17:44:47 +08:00
// -- Toggle------------------------------------------------------------------
int toggle_selected = 0;
std::vector<std::string> toggle_entries = {
"Toggle_1",
"Toggle_2",
2021-05-10 02:32:27 +08:00
};
2021-05-13 17:44:47 +08:00
auto toggle = Toggle(&toggle_entries, &toggle_selected);
toggle = Wrap("Toggle", toggle);
2021-05-13 17:44:47 +08:00
// -- Checkbox ---------------------------------------------------------------
bool checkbox_1_selected = false;
bool checkbox_2_selected = false;
2021-09-16 06:47:31 +08:00
bool checkbox_3_selected = false;
bool checkbox_4_selected = false;
2021-05-13 17:44:47 +08:00
auto checkboxes = Container::Vertical({
Checkbox("checkbox1", &checkbox_1_selected),
Checkbox("checkbox2", &checkbox_2_selected),
2021-09-16 06:47:31 +08:00
Checkbox("checkbox3", &checkbox_3_selected),
Checkbox("checkbox4", &checkbox_4_selected),
2021-05-10 02:32:27 +08:00
});
checkboxes = Wrap("Checkbox", checkboxes);
2021-05-10 02:32:27 +08:00
2021-05-13 17:44:47 +08:00
// -- Radiobox ---------------------------------------------------------------
int radiobox_selected = 0;
std::vector<std::string> radiobox_entries = {
"Radiobox 1",
"Radiobox 2",
"Radiobox 3",
"Radiobox 4",
2021-05-10 02:32:27 +08:00
};
2021-05-13 17:44:47 +08:00
auto radiobox = Radiobox(&radiobox_entries, &radiobox_selected);
radiobox = Wrap("Radiobox", radiobox);
2021-05-10 02:32:27 +08:00
2021-05-13 17:44:47 +08:00
// -- Input ------------------------------------------------------------------
std::string input_label;
auto input = Input(&input_label, "placeholder");
input = Wrap("Input", input);
2021-05-10 02:32:27 +08:00
2021-05-13 17:44:47 +08:00
// -- Button -----------------------------------------------------------------
std::string button_label = "Quit";
2021-05-10 02:32:27 +08:00
std::function<void()> on_button_clicked_;
2021-05-13 17:44:47 +08:00
auto button = Button(&button_label, screen.ExitLoopClosure());
button = Wrap("Button", button);
2021-05-13 17:44:47 +08:00
// -- Slider -----------------------------------------------------------------
int slider_value_1 = 12;
int slider_value_2 = 56;
int slider_value_3 = 128;
auto sliders = Container::Vertical({
Slider("R:", &slider_value_1, 0, 256, 1),
Slider("G:", &slider_value_2, 0, 256, 1),
Slider("B:", &slider_value_3, 0, 256, 1),
2021-05-13 17:44:47 +08:00
});
sliders = Wrap("Slider", sliders);
2021-05-13 17:44:47 +08:00
// -- Layout -----------------------------------------------------------------
auto layout = Container::Vertical({
menu,
toggle,
checkboxes,
radiobox,
input,
sliders,
button,
2021-05-10 02:32:27 +08:00
});
2021-05-13 17:44:47 +08:00
auto component = Renderer(layout, [&] {
return vbox({
menu->Render(),
separator(),
toggle->Render(),
separator(),
checkboxes->Render(),
separator(),
radiobox->Render(),
2021-05-02 02:40:35 +08:00
separator(),
2021-05-13 17:44:47 +08:00
input->Render(),
separator(),
sliders->Render(),
separator(),
button->Render(),
2021-05-02 02:40:35 +08:00
}) |
2021-05-13 17:44:47 +08:00
xflex | size(WIDTH, GREATER_THAN, 40) | border;
});
2019-01-13 05:25:49 +08:00
2021-05-10 02:32:27 +08:00
screen.Loop(component);
2019-01-13 05:25:49 +08:00
return 0;
}