mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-12-16 01:48:56 +08:00
Feature: Selection
Add support for selection content in the dom.
This commit is contained in:
89
src/ftxui/dom/selection_style.cpp
Normal file
89
src/ftxui/dom/selection_style.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
// 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 <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/box.hpp" // for Box
|
||||
#include "ftxui/screen/color.hpp" // for Color
|
||||
#include "ftxui/screen/screen.hpp" // for Pixel, Screen
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
namespace {
|
||||
|
||||
class SelectionStyleReset : public NodeDecorator {
|
||||
public:
|
||||
SelectionStyleReset(Element child) : NodeDecorator(std::move(child)) {}
|
||||
|
||||
void Render(Screen& screen) final {
|
||||
auto old_style = screen.GetSelectionStyle();
|
||||
screen.SetSelectionStyle([](Pixel& pixel) {});
|
||||
NodeDecorator::Render(screen);
|
||||
screen.SetSelectionStyle(old_style);
|
||||
}
|
||||
};
|
||||
|
||||
class SelectionStyle : public NodeDecorator {
|
||||
public:
|
||||
SelectionStyle(Element child, 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.
|
||||
Decorator selectionStyle(std::function<void(Pixel&)> style) {
|
||||
return [style](Element child) -> Element {
|
||||
return std::make_shared<SelectionStyle>(std::move(child), style);
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace ftxui
|
||||
Reference in New Issue
Block a user