FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
selection_style.cpp
浏览该文件的文档.
1// 版权所有 2024 Arthur Sonzogni. 保留所有权利。
2// 本源代码的使用受 MIT 许可证的约束,该许可证可在 LICENSE 文件中找到。
3#include <functional> // for function
4#include <memory> // for make_shared
5#include <utility> // for move
6
7#include "ftxui/dom/elements.hpp" // for Element, Decorator, bgcolor, color
8#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
9#include "ftxui/screen/color.hpp" // for Color
10#include "ftxui/screen/pixel.hpp" // for Pixel
11#include "ftxui/screen/screen.hpp" // for Screen
12
13namespace ftxui {
14
15namespace {
16
17class SelectionStyleReset : public NodeDecorator {
18 public:
19 explicit SelectionStyleReset(Element child)
20 : NodeDecorator(std::move(child)) {}
21
22 void Render(Screen& screen) final {
23 auto old_style = screen.GetSelectionStyle();
24 screen.SetSelectionStyle([](Pixel&) {});
26 screen.SetSelectionStyle(old_style);
27 }
28};
29
30class SelectionStyle : public NodeDecorator {
31 public:
32 SelectionStyle(Element child, const std::function<void(Pixel&)>& style)
33 : NodeDecorator(std::move(child)), style_(style) {}
34
35 void Render(Screen& screen) final {
36 auto old_style = screen.GetSelectionStyle();
37 auto new_style = [&, old_style](Pixel& pixel) {
38 old_style(pixel);
39 style_(pixel);
40 };
41 screen.SetSelectionStyle(new_style);
43 screen.SetSelectionStyle(old_style);
44 }
45
46 std::function<void(Pixel&)> style_;
47};
48
49} // namespace
50
51/// @brief 重置元素的选中样式。
52/// @param child 输入元素。
53/// @return 重置选中样式的输出元素。
55 return std::make_shared<SelectionStyleReset>(std::move(child));
56}
57
58/// @brief 设置元素选中时的背景颜色。
59/// 请注意,此样式是在现有样式之上应用的。
61 return selectionStyle([foreground](Pixel& pixel) { //
62 pixel.background_color = foreground;
63 });
64}
65
66/// @brief 设置元素选中时的前景色。
67/// 请注意,此样式是在现有样式之上应用的。
69 return selectionStyle([foreground](Pixel& pixel) { //
70 pixel.foreground_color = foreground;
71 });
72}
73
74/// @brief 设置元素选中时的颜色。
75/// @param foreground 要应用的颜色。
76/// 请注意,此样式是在现有样式之上应用的。
78 return selectionForegroundColor(foreground);
79}
80
81/// @brief 设置元素选中时的样式。
82/// @param style 要应用的样式。
83/// 请注意,此样式是在现有样式之上应用的。
84// NOLINTNEXTLINE
85Decorator selectionStyle(std::function<void(Pixel&)> style) {
86 return [style](Element child) -> Element {
87 return std::make_shared<SelectionStyle>(std::move(child), style);
88 };
89}
90
91} // namespace ftxui
friend void Render(Screen &screen, Node *node, Selection &selection)
void Render(Screen &screen, const Element &element)
在 ftxui::Screen 上显示元素。
Color foreground_color
Color background_color
Color 是一个表示终端用户界面中颜色的类。
一个 Unicode 字符及其相关样式。
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::function< Element(Element)> Decorator
std::shared_ptr< Node > Element
Decorator selectionStyle(std::function< void(Pixel &)> style)
设置元素选中时的样式。
Decorator selectionForegroundColor(Color foreground)
设置元素选中时的前景色。 请注意,此样式是在现有样式之上应用的。
Decorator selectionBackgroundColor(Color foreground)
设置元素选中时的背景颜色。 请注意,此样式是在现有样式之上应用的。
Decorator selectionColor(Color foreground)
设置元素选中时的颜色。
Element selectionStyleReset(Element)
重置元素的选中样式。
std::function< void(Pixel &)> style_