Add recursive selection style.

This commit is contained in:
ArthurSonzogni
2024-12-22 17:19:50 +01:00
parent d755ab31f1
commit 9f9effa683
12 changed files with 70 additions and 218 deletions

View File

@@ -71,7 +71,6 @@ class ScreenInteractive : public Screen {
// Selection API.
std::string GetSelectedContent(Component component);
void setSelectionOptions(SelectionOption option);
private:
void ExitNow();
@@ -137,14 +136,12 @@ class ScreenInteractive : public Screen {
int cursor_reset_shape_ = 1;
// Selection API:
bool selection_enabled_ = false;
CapturedMouse selection_pending_;
int selection_start_x_ = 0;
int selection_start_y_ = 0;
int selection_end_x_ = 0;
int selection_end_y_ = 0;
bool selection_changed = false;
SelectionOption selection_options_ = SelectionOption::Simple();
friend class Loop;

View File

@@ -113,7 +113,11 @@ 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);
Element selectionStyleReset(Element);
Decorator selectionColor(Color foreground);
Decorator selectionBackgroundColor(Color foreground);
Decorator selectionForegroundColor(Color foreground);
Decorator selectionStyle(std::function<void(Pixel&)> style);
// --- Layout is
// Horizontal, Vertical or stacked set of elements.

View File

@@ -7,34 +7,16 @@
#include <functional>
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/pixel.hpp" // for Pixel
namespace ftxui {
/// @brief Option for the selection of content.
/// @ingroup component
struct SelectionOption {
/// @brief Selection is simply inverted:
static SelectionOption Simple();
// Style:
std::function<void(Pixel& pixel)> transform = [](Pixel& pixel) {
pixel.inverted = true;
};
// Observers:
/// Called when the selection changed.
std::function<void()> on_change = [] {};
};
/// @brief Represent a selection in the terminal.
class Selection {
public:
Selection(int start_x, int start_y, int end_x, int end_y, SelectionOption option = SelectionOption::Simple());
Selection(int start_x, int start_y, int end_x, int end_y);
const Box& GetBox() const;
const SelectionOption& GetOption() const;
Selection SaturateHorizontal(Box box);
Selection SaturateVertical(Box box);
@@ -46,7 +28,6 @@ class Selection {
const int end_x_;
const int end_y_;
const Box box_;
const SelectionOption option;
};
} // namespace ftxui

View File

@@ -4,12 +4,14 @@
#ifndef FTXUI_SCREEN_SCREEN_HPP
#define FTXUI_SCREEN_SCREEN_HPP
#include <cstdint> // for uint8_t
#include <string> // for string, basic_string, allocator
#include <vector> // for vector
#include <cstdint> // for uint8_t
#include <functional> // for function
#include <string> // for string, basic_string, allocator
#include <vector> // for vector
#include "ftxui/screen/image.hpp" // for Pixel, Image
#include "ftxui/screen/terminal.hpp" // for Dimensions
#include "ftxui/util/autoreset.hpp" // for AutoReset
namespace ftxui {
@@ -67,9 +69,18 @@ class Screen : public Image {
uint8_t RegisterHyperlink(const std::string& link);
const std::string& Hyperlink(uint8_t id) const;
using SelectionStyle = std::function<void(Pixel&)>;
const SelectionStyle& GetSelectionStyle() const;
void SetSelectionStyle(SelectionStyle decorator);
protected:
Cursor cursor_;
std::vector<std::string> hyperlinks_ = {""};
// The current selection style. This is overridden by various dom elements.
SelectionStyle selection_style_ = [](Pixel& pixel) {
pixel.inverted ^= true;
};
};
} // namespace ftxui