FTXUI  5.0.0
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 <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/box.hpp" // for Box
10#include "ftxui/screen/color.hpp" // for Color
11#include "ftxui/screen/screen.hpp" // for Pixel, Screen
12
13namespace ftxui {
14
15namespace {
16
17class SelectionStyleReset : public NodeDecorator {
18 public:
19 SelectionStyleReset(Element child) : NodeDecorator(std::move(child)) {}
20
21 void Render(Screen& screen) final {
22 auto old_style = screen.GetSelectionStyle();
23 screen.SetSelectionStyle([](Pixel&) {});
25 screen.SetSelectionStyle(old_style);
26 }
27};
28
29class SelectionStyle : public NodeDecorator {
30 public:
31 SelectionStyle(Element child, std::function<void(Pixel&)> style)
32 : NodeDecorator(std::move(child)), style_(style) {}
33
34 void Render(Screen& screen) final {
35 auto old_style = screen.GetSelectionStyle();
36 auto new_style = [&, old_style](Pixel& pixel) {
38 style_(pixel);
39 };
40 screen.SetSelectionStyle(new_style);
42 screen.SetSelectionStyle(old_style);
43 }
44
45 std::function<void(Pixel&)> style_;
46};
47
48} // namespace
49
50/// @brief Reset the selection style of an element.
51/// @param child The input element.
52/// @return The output element with the selection style reset.
54 return std::make_shared<SelectionStyleReset>(std::move(child));
55}
56
57/// @brief Set the background color of an element when selected.
58/// Note that the style is applied on top of the existing style.
60 return selectionStyle([foreground](Pixel& pixel) { //
61 pixel.background_color = foreground;
62 });
63}
64
65/// @brief Set the foreground color of an element when selected.
66/// Note that the style is applied on top of the existing style.
68 return selectionStyle([foreground](Pixel& pixel) { //
69 pixel.foreground_color = foreground;
70 });
71}
72
73/// @brief Set the color of an element when selected.
74/// @param foreground The color to be applied.
75/// Note that the style is applied on top of the existing style.
77 return selectionForegroundColor(foreground);
78}
79
80/// @brief Set the style of an element when selected.
81/// @param style The style to be applied.
82/// Note that the style is applied on top of the existing style.
83Decorator selectionStyle(std::function<void(Pixel&)> style) {
84 return [style](Element child) -> Element {
85 return std::make_shared<SelectionStyle>(std::move(child), style);
86 };
87}
88
89} // namespace ftxui
A class representing terminal colors.
Definition color.hpp:20
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition node.cpp:47
std::function< Element(Element)> Decorator
Definition elements.hpp:24
std::shared_ptr< Node > Element
Definition elements.hpp:22
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
Decorator selectionStyle(std::function< void(Pixel &)> style)
Set the style of an element when selected.
Decorator selectionForegroundColor(Color foreground)
Set the foreground color of an element when selected. Note that the style is applied on top of the ex...
Decorator selectionBackgroundColor(Color foreground)
Set the background color of an element when selected. Note that the style is applied on top of the ex...
Decorator selectionColor(Color foreground)
Set the color of an element when selected.
Element selectionStyleReset(Element)
Reset the selection style of an element.
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:72
A Unicode character and its associated style.
Definition pixel.hpp:15