FTXUI 6.1.9 C++ functional terminal UI.

Loading...
Searching...
No Matches
examples/component/focus.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 allocator, shared_ptr, __shared_ptr_access
5#include <string> // for operator+, char_traits, to_string, string
6#include <vector> // for vector
7
8#include "ftxui/component/captured_mouse.hpp" // for ftxui
9#include "ftxui/component/component.hpp" // for Slider, Renderer, Vertical
10#include "ftxui/component/component_base.hpp" // for ComponentBase
11#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
12#include "ftxui/dom/elements.hpp" // for Elements, Element, operator|, separator, text, focusPositionRelative, size, border, flex, frame, bgcolor, gridbox, vbox, EQUAL, center, HEIGHT, WIDTH
13#include "ftxui/screen/color.hpp" // for Color
14
15 using namespace ftxui;
16
17 Element make_box(int x, int y) {
18 std::string title = "(" + std::to_string(x) + ", " + std::to_string(y) + ")";
19 return text(title) | center | size(WIDTH, EQUAL, 18) |
20 size(HEIGHT, EQUAL, 9) | border |
21 bgcolor(Color::HSV(x * 255 / 15, 255, y * 255 / 15));
22};
23
24 Element make_grid() {
25 std::vector<Elements> rows;
26 for (int i = 0; i < 15; i++) {
27 std::vector<Element> cols;
28 for (int j = 0; j < 15; j++) {
29 cols.push_back(make_box(i, j));
30 }
31 rows.push_back(cols);
32 }
33
34 return gridbox(rows);
35};
36
37 int main() {
38 float focus_x = 0.5f;
39 float focus_y = 0.5f;
40
41 auto slider_x = Slider("x", &focus_x, 0.f, 1.f, 0.01f);
42 auto slider_y = Slider("y", &focus_y, 0.f, 1.f, 0.01f);
43
44 auto renderer = Renderer(
45 Container::Vertical({
46 slider_x,
47 slider_y,
48 }),
49 [&] {
50 auto title = "focusPositionRelative(" + //
51 std::to_string(focus_x) + ", " + //
52 std::to_string(focus_y) + ")"; //
53 return vbox({
54 text(title),
55 separator(),
56 slider_x->Render(),
57 slider_y->Render(),
58 separator(),
59 make_grid() | focusPositionRelative(focus_x, focus_y) |
60 frame | flex,
61 }) |
62 border;
63 });
64
65 auto screen = ScreenInteractive::Fullscreen();
66 screen.Loop(renderer);
67
68 return 0;
69}
captured_mouse.hpp
color.hpp
component.hpp
component_base.hpp
elements.hpp
make_grid
Element make_grid()
Definition examples/component/focus.cpp:24
make_box
Element make_box(int x, int y)
Definition examples/component/focus.cpp:17
main
int main()
Definition examples/component/focus.cpp:37
ftxui::ScreenInteractive::Fullscreen
static ScreenInteractive Fullscreen()
Definition screen_interactive.cpp:376
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::Node::Render
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition node.cpp:59
ftxui::bgcolor
Decorator bgcolor(Color)
Decorate using a background color.
Definition dom/color.cpp:124
ftxui::focusPositionRelative
Decorator focusPositionRelative(float x, float y)
Used inside a frame, this force the view to be scrolled toward a a given position....
Definition src/ftxui/dom/focus.cpp:31
ftxui::size
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition src/ftxui/dom/size.cpp:89
ftxui::flex
Element flex(Element)
Make a child element to expand proportionally to the space left in a container.
Definition flex.cpp:123
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::Color::HSV
static Color HSV(uint8_t hue, uint8_t saturation, uint8_t value)
Build a Color from its HSV representation. https://en.wikipedia.org/wiki/HSL_and_HSV.
Definition screen/color.cpp:198
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::HEIGHT
@ HEIGHT
Definition elements.hpp:161
ftxui::WIDTH
@ WIDTH
Definition elements.hpp:161
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::Slider
Component Slider(SliderOption< T > options)
A slider in any direction.
Definition src/ftxui/component/slider.cpp:346
ftxui::gridbox
Element gridbox(std::vector< Elements > lines)
A container displaying a grid of elements.
Definition src/ftxui/dom/gridbox.cpp:173
ftxui::EQUAL
@ EQUAL
Definition elements.hpp:162
screen_interactive.hpp