From d62dc6e305cdafbc55c1eeaa0225089a69f9b349 Mon Sep 17 00:00:00 2001 From: Clement Roblot Date: Mon, 2 Dec 2024 18:48:48 +0700 Subject: [PATCH] Added a decorator to prevent selectability --- examples/component/selectable_input.cpp | 2 +- include/ftxui/dom/elements.hpp | 1 + src/ftxui/dom/selection.cpp | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/examples/component/selectable_input.cpp b/examples/component/selectable_input.cpp index 1055c65d..36e22996 100644 --- a/examples/component/selectable_input.cpp +++ b/examples/component/selectable_input.cpp @@ -47,7 +47,7 @@ int main() { window(text("Vertical split"), vbox({ LoremIpsum(), separator(), - LoremIpsum(), + LoremIpsum() | unselectable, separator(), LoremIpsum(), })), diff --git a/include/ftxui/dom/elements.hpp b/include/ftxui/dom/elements.hpp index a2dd0442..bd086def 100644 --- a/include/ftxui/dom/elements.hpp +++ b/include/ftxui/dom/elements.hpp @@ -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. diff --git a/src/ftxui/dom/selection.cpp b/src/ftxui/dom/selection.cpp index c45df40c..2e651d54 100644 --- a/src/ftxui/dom/selection.cpp +++ b/src/ftxui/dom/selection.cpp @@ -5,8 +5,22 @@ #include "ftxui/dom/selection.hpp" // for Selection #include // 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(std::move(child)); +} + } // namespace ftxui