FTXUI 6.1.9 C++ functional terminal UI.

Loading...
Searching...
No Matches
selection_style.cpp
Go to the documentation of this file.
1// Copyright 2024 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 <functional> // for function
5#include <memory> // for make_shared
6#include <utility> // for move
7
8#include "ftxui/dom/elements.hpp" // for Element, Decorator, bgcolor, color
9#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
10#include "ftxui/screen/color.hpp" // for Color
11#include "ftxui/screen/pixel.hpp" // for Pixel
12#include "ftxui/screen/screen.hpp" // for Screen
13
14 namespace ftxui {
15
16 namespace {
17
18 class SelectionStyleReset : public NodeDecorator {
19 public:
20 explicit SelectionStyleReset(Element child)
21 : NodeDecorator(std::move(child)) {}
22
23 void Render(Screen& screen) final {
24 auto old_style = screen.GetSelectionStyle();
25 screen.SetSelectionStyle([](Pixel&) {});
26 NodeDecorator::Render(screen);
27 screen.SetSelectionStyle(old_style);
28 }
29};
30
31 class SelectionStyle : public NodeDecorator {
32 public:
33 SelectionStyle(Element child, const std::function<void(Pixel&)>& style)
34 : NodeDecorator(std::move(child)), style_(style) {}
35
36 void Render(Screen& screen) final {
37 auto old_style = screen.GetSelectionStyle();
38 auto new_style = [&, old_style](Pixel& pixel) {
39 old_style(pixel);
40 style_(pixel);
41 };
42 screen.SetSelectionStyle(new_style);
43 NodeDecorator::Render(screen);
44 screen.SetSelectionStyle(old_style);
45 }
46
47 std::function<void(Pixel&)> style_;
48};
49
50} // namespace
51
52/// @brief Reset the selection style of an element.
53/// @param child The input element.
54/// @return The output element with the selection style reset.
55 Element selectionStyleReset(Element child) {
56 return std::make_shared<SelectionStyleReset>(std::move(child));
57}
58
59/// @brief Set the background color of an element when selected.
60/// Note that the style is applied on top of the existing style.
61 Decorator selectionBackgroundColor(Color foreground) {
62 return selectionStyle([foreground](Pixel& pixel) { //
63 pixel.background_color = foreground;
64 });
65}
66
67/// @brief Set the foreground color of an element when selected.
68/// Note that the style is applied on top of the existing style.
69 Decorator selectionForegroundColor(Color foreground) {
70 return selectionStyle([foreground](Pixel& pixel) { //
71 pixel.foreground_color = foreground;
72 });
73}
74
75/// @brief Set the color of an element when selected.
76/// @param foreground The color to be applied.
77/// Note that the style is applied on top of the existing style.
78 Decorator selectionColor(Color foreground) {
79 return selectionForegroundColor(foreground);
80}
81
82/// @brief Set the style of an element when selected.
83/// @param style The style to be applied.
84/// Note that the style is applied on top of the existing style.
85// NOLINTNEXTLINE
86 Decorator selectionStyle(std::function<void(Pixel&)> style) {
87 return [style](Element child) -> Element {
88 return std::make_shared<SelectionStyle>(std::move(child), style);
89 };
90}
91
92} // namespace ftxui
color.hpp
elements.hpp
ftxui::Node::Render
friend void Render(Screen &screen, Node *node, Selection &selection)
Definition node.cpp:96
ftxui::Render
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:84
ftxui::Pixel::foreground_color
Color foreground_color
Definition pixel.hpp:49
ftxui::Pixel::background_color
Color background_color
Definition pixel.hpp:48
ftxui::Color
Color is a class that represents a color in the terminal user interface.
Definition color.hpp:22
ftxui::Pixel
A Unicode character and its associated style.
Definition pixel.hpp:15
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Decorator
std::function< Element(Element)> Decorator
Definition elements.hpp:24
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::selectionStyle
Decorator selectionStyle(std::function< void(Pixel &)> style)
Set the style of an element when selected.
Definition selection_style.cpp:86
ftxui::selectionForegroundColor
Decorator selectionForegroundColor(Color foreground)
Set the foreground color of an element when selected. Note that the style is applied on top of the ex...
Definition selection_style.cpp:69
ftxui::selectionBackgroundColor
Decorator selectionBackgroundColor(Color foreground)
Set the background color of an element when selected. Note that the style is applied on top of the ex...
Definition selection_style.cpp:61
ftxui::selectionColor
Decorator selectionColor(Color foreground)
Set the color of an element when selected.
Definition selection_style.cpp:78
ftxui::selectionStyleReset
Element selectionStyleReset(Element)
Reset the selection style of an element.
Definition selection_style.cpp:55
node_decorator.hpp
pixel.hpp
screen.hpp
style_
std::function< void(Pixel &)> style_
Definition selection_style.cpp:47