FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
examples/component/focus.cpp
浏览该文件的文档.
1// Copyright 2020 Arthur Sonzogni. 保留所有权利。
2// 此源代码的使用受 MIT 许可协议的约束,该协议可在
3// LICENSE 文件中找到。
4#include <memory> // 用于分配器、shared_ptr、__shared_ptr_access
5#include <string> // 用于 operator+、char_traits、to_string、string
6#include <vector> // 用于 vector
7
8#include "ftxui/component/captured_mouse.hpp" // 用于 ftxui
9#include "ftxui/component/component.hpp" // 用于 Slider、Renderer、Vertical
10#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
11#include "ftxui/component/screen_interactive.hpp" // 用于 ScreenInteractive
12#include "ftxui/dom/elements.hpp" // 用于 Elements、Element、operator|、separator、text、focusPositionRelative、size、border、flex、frame、bgcolor、gridbox、vbox、EQUAL、center、HEIGHT、WIDTH
13#include "ftxui/screen/color.hpp" // 用于 Color
14
15using namespace ftxui;
16
17Element 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
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
37int 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}
Element make_box(int x, int y)
static ScreenInteractive Fullscreen()
Component Renderer(Component child, std::function< Element()>)
返回一个新组件,类似于 |child|,但使用 |render| 作为 Component::Render() 事件。
virtual void Render(Screen &screen)
在 ftxui::Screen 上显示元素。
Decorator bgcolor(Color)
使用背景色进行装饰。
Decorator focusPositionRelative(float x, float y)
在 frame 内部使用,这会强制视图滚动到给定位置。位置以请求大小的比例表示。
Decorator size(WidthOrHeight, Constraint, int value)
对元素大小应用约束。
Element flex(Element)
使子元素按比例扩展以填充容器中剩余的空间。
Element text(std::wstring text)
显示一段Unicode文本。
Element separator()
在两个其他元素之间绘制垂直或水平分隔线。
Element vbox(Elements)
垂直一个接一个显示元素的容器。
static Color HSV(uint8_t hue, uint8_t saturation, uint8_t value)
从其 HSV 表示构建一个颜色。 https://en.wikipedia.org/wiki/HSL_and_HSV
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::shared_ptr< Node > Element
Component Slider(SliderOption< T > options)
任意方向的滑块。
Element gridbox(std::vector< Elements > lines)
一个显示元素网格的容器。