FTXUI 6.1.9 C++ functional terminal UI.

Loading...
Searching...
No Matches
examples/component/button.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <memory> // for shared_ptr, __shared_ptr_access
5#include <string> // for operator+, to_string
6
7#include "ftxui/component/captured_mouse.hpp" // for ftxui
8#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
9#include "ftxui/component/component_base.hpp" // for ComponentBase
10#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
11#include "ftxui/dom/elements.hpp" // for separator, gauge, text, Element, operator|, vbox, border
12
13 using namespace ftxui;
14
15// This is a helper function to create a button with a custom style.
16// The style is defined by a lambda function that takes an EntryState and
17// returns an Element.
18// We are using `center` to center the text inside the button, then `border` to
19// add a border around the button, and finally `flex` to make the button fill
20// the available space.
21 ButtonOption Style() {
22 auto option = ButtonOption::Animated();
23 option.transform = [](const EntryState& s) {
24 auto element = text(s.label);
25 if (s.focused) {
26 element |= bold;
27 }
28 return element | center | borderEmpty | flex;
29 };
30 return option;
31}
32
33 int main() {
34 int value = 50;
35
36 // clang-format off
37 auto btn_dec_01 = Button("-1", [&] { value += 1; }, Style());
38 auto btn_inc_01 = Button("+1", [&] { value -= 1; }, Style());
39 auto btn_dec_10 = Button("-10", [&] { value -= 10; }, Style());
40 auto btn_inc_10 = Button("+10", [&] { value += 10; }, Style());
41 // clang-format on
42
43 // The tree of components. This defines how to navigate using the keyboard.
44 // The selected `row` is shared to get a grid layout.
45 int row = 0;
46 auto buttons = Container::Vertical({
47 Container::Horizontal({btn_dec_01, btn_inc_01}, &row) | flex,
48 Container::Horizontal({btn_dec_10, btn_inc_10}, &row) | flex,
49 });
50
51 // Modify the way to render them on screen:
52 auto component = Renderer(buttons, [&] {
53 return vbox({
54 text("value = " + std::to_string(value)),
55 separator(),
56 buttons->Render() | flex,
57 }) |
58 flex | border;
59 });
60
61 auto screen = ScreenInteractive::FitComponent();
62 screen.Loop(component);
63 return 0;
64}
captured_mouse.hpp
component.hpp
component_base.hpp
elements.hpp
Style
ButtonOption Style()
Definition examples/component/button.cpp:21
main
int main()
Definition examples/component/button.cpp:33
ftxui::ButtonOption::Animated
static ButtonOption Animated()
Create a ButtonOption, using animated colors.
Definition component_options.cpp:196
ftxui::ScreenInteractive::FitComponent
static ScreenInteractive FitComponent()
Definition screen_interactive.cpp:423
ftxui::Container::Horizontal
Component Horizontal(Components children)
A list of components, drawn one by one horizontally and navigated horizontally using left/right arrow...
Definition container.cpp:360
ftxui::Button
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked.
Definition src/ftxui/component/button.cpp:175
ftxui::Renderer
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Definition src/ftxui/component/renderer.cpp:61
ftxui::Container::Vertical
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Definition container.cpp:317
ftxui::ButtonOption
Option for the AnimatedButton component.
Definition component_options.hpp:113
ftxui::Node::Render
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition node.cpp:59
ftxui::flex
Element flex(Element)
Make a child element to expand proportionally to the space left in a container.
Definition flex.cpp:123
ftxui::bold
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
ftxui::text
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
ftxui::separator
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Definition src/ftxui/dom/separator.cpp:134
ftxui::vbox
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
screen_interactive.hpp
ftxui::EntryState
arguments for transform from |ButtonOption|, |CheckboxOption|, |RadioboxOption|, |MenuEntryOption|,...
Definition component_options.hpp:22