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
13using 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.
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
33int 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}
ButtonOption Style()
static ButtonOption Animated()
Create a ButtonOption, using animated colors.
static ScreenInteractive FitComponent()
Component Horizontal(Components children)
A list of components, drawn one by one horizontally and navigated horizontally using left/right arrow...
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked.
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Option for the AnimatedButton component.
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition node.cpp:59
Element flex(Element)
Make a child element to expand proportionally to the space left in a container.
Definition flex.cpp:123
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
arguments for transform from |ButtonOption|, |CheckboxOption|, |RadioboxOption|, |MenuEntryOption|,...