// 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 // for function #include // for make_shared #include // 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& 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 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(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 style) { return [style](Element child) -> Element { return std::make_shared(std::move(child), style); }; } } // namespace ftxui