FTXUI 7.0.3
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/cell.hpp" // for Cell
11#include "ftxui/screen/color.hpp" // for Color
12#include "ftxui/screen/screen.hpp" // for Screen
13
14namespace ftxui {
15
16namespace {
17
18class 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([](Cell&) {});
26 NodeDecorator::Render(screen);
27 screen.SetSelectionStyle(old_style);
28 }
29};
30
31class SelectionStyle : public NodeDecorator {
32 public:
33 SelectionStyle(Element child, const std::function<void(Cell&)>& 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](Cell& 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(Cell&)> style_;
48};
49
50} // namespace
51
52/// @brief Réinitialise le style de sélection d'un élément.
53/// @param child L'élément d'entrée.
54/// @return L'élément de sortie avec le style de sélection réinitialisé.
56 return std::make_shared<SelectionStyleReset>(std::move(child));
57}
58
59/// @brief Définit la couleur d'arrière-plan d'un élément lorsqu'il est sélectionné.
60/// Notez que le style est appliqué en plus du style existant.
62 return selectionStyle([foreground](Cell& pixel) { //
63 pixel.background_color = foreground;
64 });
65}
66
67/// @brief Définit la couleur de premier plan d'un élément lorsqu'il est sélectionné.
68/// Notez que le style est appliqué en plus du style existant.
70 return selectionStyle([foreground](Cell& pixel) { //
71 pixel.foreground_color = foreground;
72 });
73}
74
75/// @brief Définit la couleur d'un élément lorsqu'il est sélectionné.
76/// @param foreground La couleur à appliquer.
77/// Notez que le style est appliqué en plus du style existant.
78Decorator selectionColor(Color foreground) {
79 return selectionForegroundColor(foreground);
80}
81
82/// @brief Définit le style d'un élément lorsqu'il est sélectionné.
83/// @param style Le style à appliquer.
84/// Notez que le style est appliqué en plus du style existant.
85// NOLINTNEXTLINE
86Decorator selectionStyle(std::function<void(Cell&)> style) {
87 return [style](Element child) -> Element {
88 return std::make_shared<SelectionStyle>(std::move(child), style);
89 };
90}
91
92} // namespace ftxui
L'espace de noms FTXUI ftxui::
Definition animation.hpp:11
std::shared_ptr< Node > Element
Definition elements.hpp:25
Decorator selectionForegroundColor(Color foreground)
Définit la couleur de premier plan d'un élément lorsqu'il est sélectionné. Notez que le style est app...
Decorator selectionBackgroundColor(Color foreground)
Définit la couleur d'arrière-plan d'un élément lorsqu'il est sélectionné. Notez que le style est appl...
std::function< Element(Element)> Decorator
Definition elements.hpp:27
Decorator selectionColor(Color foreground)
Définit la couleur d'un élément lorsqu'il est sélectionné.
Element selectionStyleReset(Element child)
Réinitialise le style de sélection d'un élément.
void Render(Screen &screen, Node *node, Selection &selection)
Definition node.cpp:105
Decorator selectionStyle(std::function< void(Cell &)> style)
Définit le style d'un élément lorsqu'il est sélectionné.
std::function< void(Cell &)> style_