Added a decorator to prevent selectability

This commit is contained in:
Clement Roblot 2024-12-02 18:48:48 +07:00 committed by ArthurSonzogni
parent 307d8b51bf
commit d62dc6e305
No known key found for this signature in database
GPG Key ID: 41D98248C074CD6C
3 changed files with 23 additions and 1 deletions

View File

@ -47,7 +47,7 @@ int main() {
window(text("Vertical split"), vbox({
LoremIpsum(),
separator(),
LoremIpsum(),
LoremIpsum() | unselectable,
separator(),
LoremIpsum(),
})),

View File

@ -113,6 +113,7 @@ Decorator focusPositionRelative(float x, float y);
Element automerge(Element child);
Decorator hyperlink(std::string link);
Element hyperlink(std::string link, Element child);
Element unselectable(Element child);
// --- Layout is
// Horizontal, Vertical or stacked set of elements.

View File

@ -5,8 +5,22 @@
#include "ftxui/dom/selection.hpp" // for Selection
#include <algorithm> // for max, min
#include "ftxui/dom/elements.hpp" // for Element, inverted
#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
namespace ftxui {
namespace {
class Unselectable : public NodeDecorator {
public:
using NodeDecorator::NodeDecorator;
void Select(Selection& selection) override {
// Overwrite the select method to do nothing.
}
};
} // namespace
/// @brief Create a selection.
/// @param start_x The x coordinate of the start of the selection.
/// @param start_y The y coordinate of the start of the selection.
@ -103,4 +117,11 @@ Selection Selection::SaturateVertical(Box box) {
return Selection(start_x, start_y, end_x, end_y);
}
/// @brief Add a filter that will invert the foreground and the background
/// colors.
/// @ingroup dom
Element unselectable(Element child) {
return std::make_shared<Unselectable>(std::move(child));
}
} // namespace ftxui