FTXUI/examples/component/button.cpp

46 lines
1.5 KiB
C++
Raw Normal View History

2021-07-10 19:20:43 +08:00
#include <memory> // for shared_ptr, __shared_ptr_access
#include <string> // for operator+, to_wstring
2020-08-26 22:26:09 +08:00
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
#include "ftxui/component/component_base.hpp" // for ComponentBase
2021-07-10 19:20:43 +08:00
#include "ftxui/component/component_options.hpp" // for ButtonOption
2021-05-02 02:40:35 +08:00
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
2021-07-10 19:20:43 +08:00
#include "ftxui/dom/elements.hpp" // for separator, gauge, text, Element, operator|, vbox, border
2020-08-26 22:26:09 +08:00
using namespace ftxui;
2021-05-13 17:44:47 +08:00
int main(int argc, const char* argv[]) {
int value = 50;
2020-08-26 22:26:09 +08:00
2021-05-13 17:44:47 +08:00
// The tree of components. This defines how to navigate using the keyboard.
2021-07-08 04:23:07 +08:00
auto button_option = ButtonOption();
button_option.border = false;
2021-05-13 17:44:47 +08:00
auto buttons = Container::Horizontal({
2021-05-23 18:53:20 +08:00
Button(
2021-07-08 04:23:07 +08:00
"[Decrease]", [&] { value--; }, &button_option),
2021-05-23 18:53:20 +08:00
Button(
2021-07-08 04:23:07 +08:00
"[Increase]", [&] { value++; }, &button_option),
2021-05-13 17:44:47 +08:00
});
2020-08-26 22:26:09 +08:00
2021-05-13 17:44:47 +08:00
// Modify the way to render them on screen:
auto component = Renderer(buttons, [&] {
2021-05-10 02:32:27 +08:00
return vbox({
2021-05-13 17:44:47 +08:00
text(L"value = " + std::to_wstring(value)),
2021-05-10 02:32:27 +08:00
separator(),
2021-05-13 17:44:47 +08:00
gauge(value * 0.01f),
2021-05-10 02:32:27 +08:00
separator(),
2021-05-13 17:44:47 +08:00
buttons->Render(),
2021-05-10 02:32:27 +08:00
}) |
border;
2021-05-13 17:44:47 +08:00
});
2020-08-26 22:26:09 +08:00
2021-05-10 02:32:27 +08:00
auto screen = ScreenInteractive::FitComponent();
2021-05-13 17:44:47 +08:00
screen.Loop(component);
2020-08-26 22:26:09 +08:00
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.