FTXUI  5.0.0
C++ functional terminal UI.
examples/component/focus.cpp
// 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.
#include <memory> // for allocator, shared_ptr, __shared_ptr_access
#include <string> // for operator+, char_traits, to_string, string
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Slider, Renderer, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Elements, Element, operator|, separator, text, focusPositionRelative, size, border, flex, frame, bgcolor, gridbox, vbox, EQUAL, center, HEIGHT, WIDTH
#include "ftxui/screen/color.hpp" // for Color
using namespace ftxui;
Element make_box(int x, int y) {
std::string title = "(" + std::to_string(x) + ", " + std::to_string(y) + ")";
return text(title) | center | size(WIDTH, EQUAL, 18) |
bgcolor(Color::HSV(x * 255 / 15, 255, y * 255 / 15));
};
Element make_grid() {
std::vector<Elements> rows;
for (int i = 0; i < 15; i++) {
std::vector<Element> cols;
for (int j = 0; j < 15; j++) {
cols.push_back(make_box(i, j));
}
rows.push_back(cols);
}
return gridbox(rows);
};
int main() {
float focus_x = 0.5f;
float focus_y = 0.5f;
auto slider_x = Slider("x", &focus_x, 0.f, 1.f, 0.01f);
auto slider_y = Slider("y", &focus_y, 0.f, 1.f, 0.01f);
auto renderer = Renderer(
slider_x,
slider_y,
}),
[&] {
auto title = "focusPositionRelative(" + //
std::to_string(focus_x) + ", " + //
std::to_string(focus_y) + ")"; //
return vbox({
text(title),
slider_x->Render(),
slider_y->Render(),
make_grid() | focusPositionRelative(focus_x, focus_y) |
}) |
});
auto screen = ScreenInteractive::Fullscreen();
screen.Loop(renderer);
return 0;
}
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
Decorator bgcolor(Color)
Decorate using a background color.
Definition: color.cpp:124
@ HEIGHT
Definition: elements.hpp:148
@ WIDTH
Definition: elements.hpp:148
Decorator focusPositionRelative(float x, float y)
Used inside a frame, this force the view to be scrolled toward a a given position....
Definition: focus.cpp:31
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition: size.cpp:90
Element flex(Element)
Make a child element to expand proportionnally to the space left in a container.
Definition: flex.cpp:123
std::shared_ptr< Node > Element
Definition: elements.hpp:23
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Definition: renderer.cpp:63
Element center(Element)
Center an element horizontally and vertically.
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
Definition: string.cpp:1565
Element text(std::wstring text)
Display a piece of unicode text.
Definition: text.cpp:120
Component Slider(SliderOption< T > options)
A slider in any direction.
Definition: slider.cpp:338
Element gridbox(std::vector< Elements > lines)
A container displaying a grid of elements.
Definition: gridbox.cpp:183
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Definition: separator.cpp:132
Element frame(Element)
Allow an element to be displayed inside a 'virtual' area. It size can be larger than its container....
Definition: frame.cpp:167
@ EQUAL
Definition: elements.hpp:149
Element border(Element)
Draw a border around the element.
Definition: border.cpp:227
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition: vbox.cpp:83