mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2024-12-05 09:21:57 +08:00
Merge 52e92a99be
into dfa461b46b
This commit is contained in:
commit
7cad9d86a7
@ -88,6 +88,7 @@ add_library(dom
|
||||
src/ftxui/dom/paragraph.cpp
|
||||
src/ftxui/dom/reflect.cpp
|
||||
src/ftxui/dom/scroll_indicator.cpp
|
||||
src/ftxui/dom/selectable.cpp
|
||||
src/ftxui/dom/separator.cpp
|
||||
src/ftxui/dom/size.cpp
|
||||
src/ftxui/dom/spinner.cpp
|
||||
|
@ -38,6 +38,7 @@ example(radiobox)
|
||||
example(radiobox_in_frame)
|
||||
example(renderer)
|
||||
example(resizable_split)
|
||||
example(selectable_input)
|
||||
example(scrollbar)
|
||||
example(slider)
|
||||
example(slider_direction)
|
||||
|
73
examples/component/selectable_input.cpp
Normal file
73
examples/component/selectable_input.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
// Copyright 2020 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 <string> // for char_traits, operator+, string, basic_string
|
||||
|
||||
#include "ftxui/component/component.hpp" // for Input, Renderer, Vertical
|
||||
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
||||
#include "ftxui/component/component_options.hpp" // for InputOption
|
||||
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
|
||||
#include "ftxui/dom/elements.hpp" // for text, hbox, separator, Element, operator|, vbox, border
|
||||
#include "ftxui/util/ref.hpp" // for Ref
|
||||
|
||||
using namespace ftxui;
|
||||
|
||||
Element LoremIpsum() {
|
||||
return vbox({
|
||||
text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
|
||||
"eiusmod tempor incididunt ut labore et dolore magna aliqua."),
|
||||
text("Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris "
|
||||
"nisi ut aliquip ex ea commodo consequat."),
|
||||
text("Duis aute irure dolor in reprehenderit in voluptate velit esse "
|
||||
"cillum dolore eu fugiat nulla pariatur."),
|
||||
text("Excepteur sint occaecat cupidatat non proident, sunt in culpa qui "
|
||||
"officia deserunt mollit anim id est laborum."),
|
||||
});
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto screen = ScreenInteractive::TerminalOutput();
|
||||
|
||||
auto quit = Button("Quit", screen.ExitLoopClosure());
|
||||
|
||||
// The components:
|
||||
auto renderer = Renderer(quit, [&] {
|
||||
return vbox({
|
||||
window(text("Horizontal split"), hbox({
|
||||
LoremIpsum(),
|
||||
separator(),
|
||||
LoremIpsum(),
|
||||
separator(),
|
||||
LoremIpsum(),
|
||||
})),
|
||||
window(text("Vertical split"), vbox({
|
||||
LoremIpsum(),
|
||||
separator(),
|
||||
LoremIpsum(),
|
||||
separator(),
|
||||
LoremIpsum(),
|
||||
})),
|
||||
window(text("Vertical split"),
|
||||
vbox({
|
||||
window(text("horizontal split"), hbox({
|
||||
LoremIpsum(),
|
||||
separator(),
|
||||
LoremIpsum(),
|
||||
separator(),
|
||||
LoremIpsum(),
|
||||
})),
|
||||
separator(),
|
||||
window(text("horizontal split"), hbox({
|
||||
LoremIpsum(),
|
||||
separator(),
|
||||
LoremIpsum(),
|
||||
separator(),
|
||||
LoremIpsum(),
|
||||
})),
|
||||
})),
|
||||
quit->Render(),
|
||||
});
|
||||
});
|
||||
|
||||
screen.Loop(renderer);
|
||||
}
|
@ -68,6 +68,10 @@ class ScreenInteractive : public Screen {
|
||||
void ForceHandleCtrlC(bool force);
|
||||
void ForceHandleCtrlZ(bool force);
|
||||
|
||||
// Selection API.
|
||||
//void OnSelectionChange(std::function<void(std::
|
||||
//void ClearSelection();
|
||||
|
||||
private:
|
||||
void ExitNow();
|
||||
|
||||
@ -82,6 +86,8 @@ class ScreenInteractive : public Screen {
|
||||
void RunOnceBlocking(Component component);
|
||||
|
||||
void HandleTask(Component component, Task& task);
|
||||
bool HandleSelection(Event event);
|
||||
void RefreshSelection();
|
||||
void Draw(Component component);
|
||||
void ResetCursorPosition();
|
||||
|
||||
@ -129,6 +135,11 @@ class ScreenInteractive : public Screen {
|
||||
// The style of the cursor to restore on exit.
|
||||
int cursor_reset_shape_ = 1;
|
||||
|
||||
// Selection API:
|
||||
bool selection_enabled_ = false;
|
||||
CapturedMouse selection_pending_;
|
||||
Box selection_box_;
|
||||
|
||||
friend class Loop;
|
||||
|
||||
public:
|
||||
|
@ -113,6 +113,8 @@ Decorator focusPositionRelative(float x, float y);
|
||||
Element automerge(Element child);
|
||||
Decorator hyperlink(std::string link);
|
||||
Element hyperlink(std::string link, Element child);
|
||||
Element selectable(Element child);
|
||||
Decorator selectable(void);
|
||||
|
||||
// --- Layout is
|
||||
// Horizontal, Vertical or stacked set of elements.
|
||||
|
@ -4,6 +4,7 @@
|
||||
#ifndef FTXUI_DOM_NODE_HPP
|
||||
#define FTXUI_DOM_NODE_HPP
|
||||
|
||||
#include <list> // for list
|
||||
#include <memory> // for shared_ptr
|
||||
#include <vector> // for vector
|
||||
|
||||
@ -40,7 +41,11 @@ class Node {
|
||||
// Propagated from Parents to Children.
|
||||
virtual void SetBox(Box box);
|
||||
|
||||
// Step 3: Draw this element.
|
||||
// Step 3: (optional) Selection
|
||||
// Propagated from Parents to Children.
|
||||
virtual void Selection(Box selection, std::vector<Box>* selected);
|
||||
|
||||
// Step 4: Draw this element.
|
||||
virtual void Render(Screen& screen);
|
||||
|
||||
// Layout may not resolve within a single iteration for some elements. This
|
||||
@ -52,6 +57,7 @@ class Node {
|
||||
};
|
||||
virtual void Check(Status* status);
|
||||
|
||||
|
||||
protected:
|
||||
Elements children_;
|
||||
Requirement requirement_;
|
||||
@ -60,6 +66,7 @@ class Node {
|
||||
|
||||
void Render(Screen& screen, const Element& element);
|
||||
void Render(Screen& screen, Node* node);
|
||||
void Render(Screen& screen, Node* node, Box selection);
|
||||
|
||||
} // namespace ftxui
|
||||
|
||||
|
@ -21,6 +21,7 @@ struct Pixel {
|
||||
underlined(false),
|
||||
underlined_double(false),
|
||||
strikethrough(false),
|
||||
selectable(false),
|
||||
automerge(false) {}
|
||||
|
||||
// A bit field representing the style:
|
||||
@ -30,6 +31,7 @@ struct Pixel {
|
||||
bool inverted : 1;
|
||||
bool underlined : 1;
|
||||
bool underlined_double : 1;
|
||||
bool selectable : 1;
|
||||
bool strikethrough : 1;
|
||||
bool automerge : 1;
|
||||
|
||||
|
@ -10,6 +10,7 @@
|
||||
|
||||
#include "ftxui/screen/image.hpp" // for Pixel, Image
|
||||
#include "ftxui/screen/terminal.hpp" // for Dimensions
|
||||
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
|
@ -781,7 +781,9 @@ void ScreenInteractive::HandleTask(Component component, Task& task) {
|
||||
|
||||
arg.screen_ = this;
|
||||
|
||||
const bool handled = component->OnEvent(arg);
|
||||
bool handled = component->OnEvent(arg);
|
||||
|
||||
handled = handled || HandleSelection(arg);
|
||||
|
||||
if (arg == Event::CtrlC && (!handled || force_handle_ctrl_c_)) {
|
||||
RecordSignal(SIGABRT);
|
||||
@ -824,6 +826,44 @@ void ScreenInteractive::HandleTask(Component component, Task& task) {
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
// private
|
||||
bool ScreenInteractive::HandleSelection(Event event) {
|
||||
if (!event.is_mouse()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto& mouse = event.mouse();
|
||||
if (mouse.button != Mouse::Left) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(mouse.motion == Mouse::Pressed) {
|
||||
selection_pending_ = CaptureMouse();
|
||||
if (!selection_pending_) {
|
||||
return false;
|
||||
}
|
||||
selection_enabled_ = true;
|
||||
selection_box_.x_min = mouse.x;
|
||||
selection_box_.y_min = mouse.y;
|
||||
selection_box_.x_max = mouse.x;
|
||||
selection_box_.y_max = mouse.y;
|
||||
return true;
|
||||
}
|
||||
else if((mouse.motion == Mouse::Moved) && (selection_pending_)) {
|
||||
selection_box_.x_max = mouse.x;
|
||||
selection_box_.y_max = mouse.y;
|
||||
return true;
|
||||
}
|
||||
else if((mouse.motion == Mouse::Released) && (selection_pending_)) {
|
||||
selection_box_.x_max = mouse.x;
|
||||
selection_box_.y_max = mouse.y;
|
||||
selection_pending_ = nullptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// private
|
||||
// NOLINTNEXTLINE
|
||||
void ScreenInteractive::Draw(Component component) {
|
||||
@ -899,7 +939,7 @@ void ScreenInteractive::Draw(Component component) {
|
||||
#endif
|
||||
previous_frame_resized_ = resized;
|
||||
|
||||
Render(*this, document);
|
||||
Render(*this, document.get(), selection_box_);
|
||||
|
||||
// Set cursor position for user using tools to insert CJK characters.
|
||||
{
|
||||
|
@ -64,6 +64,30 @@ class HBox : public Node {
|
||||
x = box.x_max + 1;
|
||||
}
|
||||
}
|
||||
|
||||
void Selection(Box selection, std::vector<Box>* selected) override {
|
||||
// If this Node box_ doesn't intersect with the selection, then no
|
||||
// selection.
|
||||
if (Box::Intersection(selection, box_).IsEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const bool xmin_saturated =
|
||||
selection.y_min < box_.y_min || selection.x_min < box_.x_min;
|
||||
const bool xmax_saturated =
|
||||
selection.y_max > box_.y_max || selection.x_max > box_.x_max;
|
||||
|
||||
if (xmin_saturated) {
|
||||
selection.x_min = std::min(box_.x_min, selection.x_min);
|
||||
}
|
||||
if (xmax_saturated) {
|
||||
selection.x_max = std::max(box_.x_max, selection.x_max);
|
||||
}
|
||||
|
||||
for (auto& child : children_) {
|
||||
child->Selection(selection, selected);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
@ -1,3 +1,4 @@
|
||||
#include <iostream>
|
||||
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
||||
// Use of this source code is governed by the MIT license that can be found in
|
||||
// the LICENSE file.
|
||||
@ -27,6 +28,20 @@ void Node::SetBox(Box box) {
|
||||
box_ = box;
|
||||
}
|
||||
|
||||
/// @brief Compute the selection of an element.
|
||||
/// @ingroup dom
|
||||
void Node::Selection(Box selection, std::vector<Box>* selected) {
|
||||
// If this Node box_ doesn't intersect with the selection, then no selection.
|
||||
if (Box::Intersection(selection, box_).IsEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// By default we defer the selection to the children.
|
||||
for (auto& child : children_) {
|
||||
child->Selection(selection, selected);
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Display an element on a ftxui::Screen.
|
||||
/// @ingroup dom
|
||||
void Node::Render(Screen& screen) {
|
||||
@ -45,12 +60,16 @@ void Node::Check(Status* status) {
|
||||
/// @brief Display an element on a ftxui::Screen.
|
||||
/// @ingroup dom
|
||||
void Render(Screen& screen, const Element& element) {
|
||||
Render(screen, element.get());
|
||||
Render(screen, element.get(), Box{0, 0, -1, -1});
|
||||
}
|
||||
|
||||
/// @brief Display an element on a ftxui::Screen.
|
||||
/// @ingroup dom
|
||||
void Render(Screen& screen, Node* node) {
|
||||
Render(screen, node, Box{0, 0, -1, -1});
|
||||
}
|
||||
|
||||
void Render(Screen& screen, Node* node, Box selection) {
|
||||
Box box;
|
||||
box.x_min = 0;
|
||||
box.y_min = 0;
|
||||
@ -73,11 +92,29 @@ void Render(Screen& screen, Node* node) {
|
||||
node->Check(&status);
|
||||
}
|
||||
|
||||
// Step 3: Draw the element.
|
||||
// Step 3: Selection
|
||||
std::vector<Box> selected;
|
||||
|
||||
Box selectionCleaned = selection;
|
||||
if(selection.x_min > selection.x_max)
|
||||
{
|
||||
selectionCleaned.x_min = selection.x_max;
|
||||
selectionCleaned.x_max = selection.x_min;
|
||||
}
|
||||
|
||||
if(selection.y_min > selection.y_max)
|
||||
{
|
||||
selectionCleaned.y_min = selection.y_max;
|
||||
selectionCleaned.y_max = selection.y_min;
|
||||
}
|
||||
|
||||
node->Selection(selectionCleaned, &selected);
|
||||
|
||||
// Step 4: Draw the element.
|
||||
screen.stencil = box;
|
||||
node->Render(screen);
|
||||
|
||||
// Step 4: Apply shaders
|
||||
// Step 5: Apply shaders
|
||||
screen.ApplyShader();
|
||||
}
|
||||
|
||||
|
39
src/ftxui/dom/selectable.cpp
Normal file
39
src/ftxui/dom/selectable.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include "ftxui/dom/elements.hpp" // for Element, Decorator
|
||||
#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
|
||||
#include "ftxui/component/event.hpp" // for Event
|
||||
|
||||
|
||||
namespace ftxui {
|
||||
namespace {
|
||||
|
||||
class Selectable : public NodeDecorator {
|
||||
public:
|
||||
explicit Selectable(Element child)
|
||||
: NodeDecorator(std::move(child)) {}
|
||||
|
||||
private:
|
||||
void Render(Screen& screen) override {
|
||||
|
||||
for (int y = box_.y_min; y <= box_.y_max; ++y) {
|
||||
for (int x = box_.x_min; x <= box_.x_max; ++x) {
|
||||
screen.PixelAt(x, y).selectable = true;
|
||||
}
|
||||
}
|
||||
|
||||
NodeDecorator::Render(screen);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
Element selectable(Element child) {
|
||||
return std::make_shared<Selectable>(std::move(child));
|
||||
}
|
||||
|
||||
Decorator selectable(void) {
|
||||
return
|
||||
[](Element child) { return selectable(std::move(child)); };
|
||||
}
|
||||
|
||||
} // namespace ftxui
|
@ -28,26 +28,60 @@ class Text : public Node {
|
||||
requirement_.min_y = 1;
|
||||
}
|
||||
|
||||
void Selection(Box selection, std::vector<Box>* selected) override {
|
||||
if (Box::Intersection(selection, box_).IsEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const bool xmin_saturated =
|
||||
selection.y_min < box_.y_min || selection.x_min < box_.x_min;
|
||||
const bool xmax_saturated =
|
||||
selection.y_max > box_.y_max || selection.x_max > box_.x_max;
|
||||
|
||||
selection_start_ = xmin_saturated ? box_.x_min : selection.x_min;
|
||||
selection_end_ = xmax_saturated ? box_.x_max : selection.x_max;
|
||||
|
||||
has_selection = true;
|
||||
|
||||
Box out;
|
||||
out.x_min = selection_start_;
|
||||
out.x_max = selection_end_;
|
||||
out.y_min = box_.y_min;
|
||||
out.y_max = box_.y_max;
|
||||
selected->push_back(out);
|
||||
}
|
||||
|
||||
void Render(Screen& screen) override {
|
||||
int x = box_.x_min;
|
||||
const int y = box_.y_min;
|
||||
if (y > box_.y_max) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& cell : Utf8ToGlyphs(text_)) {
|
||||
if (x > box_.x_max) {
|
||||
return;
|
||||
break;
|
||||
}
|
||||
if (cell == "\n") {
|
||||
continue;
|
||||
}
|
||||
screen.PixelAt(x, y).character = cell;
|
||||
|
||||
if (has_selection) {
|
||||
if((x >= selection_start_) && (x <= selection_end_)) {
|
||||
screen.PixelAt(x, y).inverted = true;
|
||||
}
|
||||
}
|
||||
|
||||
++x;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::string text_;
|
||||
bool has_selection = false;
|
||||
int selection_start_ = 0;
|
||||
int selection_end_ = 10;
|
||||
};
|
||||
|
||||
class VText : public Node {
|
||||
|
@ -64,6 +64,30 @@ class VBox : public Node {
|
||||
y = box.y_max + 1;
|
||||
}
|
||||
}
|
||||
|
||||
void Selection(Box selection, std::vector<Box>* selected) override {
|
||||
// If this Node box_ doesn't intersect with the selection, then no
|
||||
// selection.
|
||||
if (Box::Intersection(selection, box_).IsEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const bool ymin_saturated =
|
||||
selection.x_min < box_.x_min || selection.y_min < box_.y_min;
|
||||
const bool ymax_saturated =
|
||||
selection.x_max > box_.x_max || selection.y_max > box_.y_max;
|
||||
|
||||
if (ymin_saturated) {
|
||||
selection.y_min = std::min(box_.y_min, selection.y_min);
|
||||
}
|
||||
if (ymax_saturated) {
|
||||
selection.y_max = std::max(box_.y_max, selection.y_max);
|
||||
}
|
||||
|
||||
for (auto& child : children_) {
|
||||
child->Selection(selection, selected);
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
|
@ -389,7 +389,8 @@ Screen Screen::Create(Dimensions dimension) {
|
||||
return {dimension.dimx, dimension.dimy};
|
||||
}
|
||||
|
||||
Screen::Screen(int dimx, int dimy) : Image{dimx, dimy} {
|
||||
Screen::Screen(int dimx, int dimy) :
|
||||
Image{dimx, dimy} {
|
||||
#if defined(_WIN32)
|
||||
// The placement of this call is a bit weird, however we can assume that
|
||||
// anybody who instantiates a Screen object eventually wants to output
|
||||
|
Loading…
Reference in New Issue
Block a user