Loading...
Searching...
No Matches
selection_style.cpp
Go to the documentation of this file.
// Copyright 2024 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <functional> // for function
#include <memory> // for make_shared
#include <utility> // for move
#include "ftxui/dom/elements.hpp" // for Element, Decorator, bgcolor, color
#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
#include "ftxui/screen/color.hpp" // for Color
#include "ftxui/screen/pixel.hpp" // for Pixel
#include "ftxui/screen/screen.hpp" // for Screen
namespace ftxui {
namespace {
class SelectionStyleReset : public NodeDecorator {
public:
explicit SelectionStyleReset(Element child)
: NodeDecorator(std::move(child)) {}
void Render(Screen& screen) final {
auto old_style = screen.GetSelectionStyle();
screen.SetSelectionStyle([](Pixel&) {});
NodeDecorator::Render(screen);
screen.SetSelectionStyle(old_style);
}
};
class SelectionStyle : public NodeDecorator {
public:
SelectionStyle(Element child, const std::function<void(Pixel&)>& style)
: NodeDecorator(std::move(child)), style_(style) {}
void Render(Screen& screen) final {
auto old_style = screen.GetSelectionStyle();
auto new_style = [&, old_style](Pixel& pixel) {
old_style(pixel);
style_(pixel);
};
screen.SetSelectionStyle(new_style);
NodeDecorator::Render(screen);
screen.SetSelectionStyle(old_style);
}
std::function<void(Pixel&)> style_;
};
} // namespace
/// @brief Reset the selection style of an element.
/// @param child The input element.
/// @return The output element with the selection style reset.
Element selectionStyleReset(Element child) {
return std::make_shared<SelectionStyleReset>(std::move(child));
}
/// @brief Set the background color of an element when selected.
/// Note that the style is applied on top of the existing style.
Decorator selectionBackgroundColor(Color foreground) {
return selectionStyle([foreground](Pixel& pixel) { //
pixel.background_color = foreground;
});
}
/// @brief Set the foreground color of an element when selected.
/// Note that the style is applied on top of the existing style.
Decorator selectionForegroundColor(Color foreground) {
return selectionStyle([foreground](Pixel& pixel) { //
pixel.foreground_color = foreground;
});
}
/// @brief Set the color of an element when selected.
/// @param foreground The color to be applied.
/// Note that the style is applied on top of the existing style.
Decorator selectionColor(Color foreground) {
return selectionForegroundColor(foreground);
}
/// @brief Set the style of an element when selected.
/// @param style The style to be applied.
/// Note that the style is applied on top of the existing style.
// NOLINTNEXTLINE
Decorator selectionStyle(std::function<void(Pixel&)> style) {
return [style](Element child) -> Element {
return std::make_shared<SelectionStyle>(std::move(child), style);
};
}
} // namespace ftxui
ftxui::Color
A class representing terminal colors.
Definition color.hpp:20
ftxui::NodeDecorator
Definition node_decorator.hpp:16
ftxui::Node::Render
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition node.cpp:63
color.hpp
elements.hpp
ftxui
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
ftxui::Render
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:88
node_decorator.hpp
pixel.hpp
screen.hpp
ftxui::Pixel
A Unicode character and its associated style.
Definition pixel.hpp:15
ftxui::Pixel::foreground_color
Color foreground_color
Definition pixel.hpp:49
ftxui::Pixel::background_color
Color background_color
Definition pixel.hpp:48