Compare commits

...

22 Commits

Author SHA1 Message Date
Clément Roblot
5035d923fc
Merge 52e92a99be into 58ff448e76 2024-12-01 17:03:23 +08:00
Vemy
58ff448e76
Fix: Properly changing window title text color #940 (#961)
Some checks failed
Build / Tests (llvm, llvm-cov gcov, Linux Clang, ubuntu-latest) (push) Failing after 32s
Build / Tests (gcc, gcov, Linux GCC, ubuntu-latest) (push) Failing after 35s
Build / documentation (push) Failing after 33s
CodeQL / Analyze (cpp) (push) Failing after 49s
Build / Tests (cl, Windows MSVC, windows-latest) (push) Has been cancelled
Build / Create release (push) Has been cancelled
Build / Build packages (build/ftxui*Darwin*, macos-latest) (push) Has been cancelled
Build / Build packages (build/ftxui*Linux*, ubuntu-latest) (push) Has been cancelled
Build / Build packages (build/ftxui*Win64*, windows-latest) (push) Has been cancelled
2024-12-01 09:38:09 +01:00
Clement Roblot
52e92a99be More robust saturation mechanism 2024-11-27 00:29:08 +07:00
Clement Roblot
907e146385 Correct the title highlighting of hbox grabing to top border 2024-11-26 21:47:01 +07:00
Clement Roblot
9a94abc31d We can reverse select 2024-11-26 21:39:29 +07:00
Clement Roblot
26507ad5b9 Corrected typo 2024-11-26 17:25:16 +07:00
Clement Roblot
46f7fbb8dc Easier to read HandleSelection 2024-11-26 17:13:40 +07:00
ArthurSonzogni
d38f3d229a
Start the tree-aware selection. 2024-11-13 22:05:04 +01:00
Clement Roblot
143d152e3d Select only if the selection starts in my text widget 2024-11-06 18:53:08 +07:00
Clement Roblot
810ae40b14 Reverse selection are now possible 2024-10-31 21:49:31 +07:00
Clement Roblot
bb73b8feb0 More intuitive selection of text 2024-10-31 21:34:53 +07:00
Clement Roblot
37baddbb38 Wrap around both directions 2024-10-31 21:15:59 +07:00
Clement Roblot
2b5701214a Dirty wrap around implementation 2024-10-31 21:09:59 +07:00
Clement Roblot
68ba5cf744 Implementation of the selection in the text node 2024-10-31 21:05:39 +07:00
Clement Roblot
54b7a93178 Make use of Box instead of my custom Region struct 2024-10-31 20:57:45 +07:00
ArthurSonzogni
3754136dd6
Reformat + fix pending selection. 2024-08-31 15:39:42 +02:00
Clement Roblot
f8dd258d65 More cleanup 2024-08-27 21:20:19 +07:00
Clement Roblot
118055d942 Cleanup 2024-08-27 21:16:04 +07:00
Clement Roblot
364993464a It somewhat works 2024-08-27 21:13:00 +07:00
Clement Roblot
e4a63318ad We can act on the screen 2024-08-27 17:40:48 +07:00
Clement Roblot
dea2d6408b We can catch mouse events 2024-08-27 17:35:21 +07:00
Clement Roblot
4e75cf9e0e We have a basic decorator 2024-08-27 15:33:25 +07:00
16 changed files with 306 additions and 9 deletions

View File

@ -88,6 +88,7 @@ add_library(dom
src/ftxui/dom/paragraph.cpp src/ftxui/dom/paragraph.cpp
src/ftxui/dom/reflect.cpp src/ftxui/dom/reflect.cpp
src/ftxui/dom/scroll_indicator.cpp src/ftxui/dom/scroll_indicator.cpp
src/ftxui/dom/selectable.cpp
src/ftxui/dom/separator.cpp src/ftxui/dom/separator.cpp
src/ftxui/dom/size.cpp src/ftxui/dom/size.cpp
src/ftxui/dom/spinner.cpp src/ftxui/dom/spinner.cpp

View File

@ -38,6 +38,7 @@ example(radiobox)
example(radiobox_in_frame) example(radiobox_in_frame)
example(renderer) example(renderer)
example(resizable_split) example(resizable_split)
example(selectable_input)
example(scrollbar) example(scrollbar)
example(slider) example(slider)
example(slider_direction) example(slider_direction)

View 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);
}

View File

@ -68,6 +68,10 @@ class ScreenInteractive : public Screen {
void ForceHandleCtrlC(bool force); void ForceHandleCtrlC(bool force);
void ForceHandleCtrlZ(bool force); void ForceHandleCtrlZ(bool force);
// Selection API.
//void OnSelectionChange(std::function<void(std::
//void ClearSelection();
private: private:
void ExitNow(); void ExitNow();
@ -82,6 +86,8 @@ class ScreenInteractive : public Screen {
void RunOnceBlocking(Component component); void RunOnceBlocking(Component component);
void HandleTask(Component component, Task& task); void HandleTask(Component component, Task& task);
bool HandleSelection(Event event);
void RefreshSelection();
void Draw(Component component); void Draw(Component component);
void ResetCursorPosition(); void ResetCursorPosition();
@ -129,6 +135,11 @@ class ScreenInteractive : public Screen {
// The style of the cursor to restore on exit. // The style of the cursor to restore on exit.
int cursor_reset_shape_ = 1; int cursor_reset_shape_ = 1;
// Selection API:
bool selection_enabled_ = false;
CapturedMouse selection_pending_;
Box selection_box_;
friend class Loop; friend class Loop;
public: public:

View File

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

View File

@ -4,6 +4,7 @@
#ifndef FTXUI_DOM_NODE_HPP #ifndef FTXUI_DOM_NODE_HPP
#define FTXUI_DOM_NODE_HPP #define FTXUI_DOM_NODE_HPP
#include <list> // for list
#include <memory> // for shared_ptr #include <memory> // for shared_ptr
#include <vector> // for vector #include <vector> // for vector
@ -40,7 +41,11 @@ class Node {
// Propagated from Parents to Children. // Propagated from Parents to Children.
virtual void SetBox(Box box); 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); virtual void Render(Screen& screen);
// Layout may not resolve within a single iteration for some elements. This // Layout may not resolve within a single iteration for some elements. This
@ -52,6 +57,7 @@ class Node {
}; };
virtual void Check(Status* status); virtual void Check(Status* status);
protected: protected:
Elements children_; Elements children_;
Requirement requirement_; Requirement requirement_;
@ -60,6 +66,7 @@ class Node {
void Render(Screen& screen, const Element& element); void Render(Screen& screen, const Element& element);
void Render(Screen& screen, Node* node); void Render(Screen& screen, Node* node);
void Render(Screen& screen, Node* node, Box selection);
} // namespace ftxui } // namespace ftxui

View File

@ -21,6 +21,7 @@ struct Pixel {
underlined(false), underlined(false),
underlined_double(false), underlined_double(false),
strikethrough(false), strikethrough(false),
selectable(false),
automerge(false) {} automerge(false) {}
// A bit field representing the style: // A bit field representing the style:
@ -30,6 +31,7 @@ struct Pixel {
bool inverted : 1; bool inverted : 1;
bool underlined : 1; bool underlined : 1;
bool underlined_double : 1; bool underlined_double : 1;
bool selectable : 1;
bool strikethrough : 1; bool strikethrough : 1;
bool automerge : 1; bool automerge : 1;

View File

@ -10,6 +10,7 @@
#include "ftxui/screen/image.hpp" // for Pixel, Image #include "ftxui/screen/image.hpp" // for Pixel, Image
#include "ftxui/screen/terminal.hpp" // for Dimensions #include "ftxui/screen/terminal.hpp" // for Dimensions
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
namespace ftxui { namespace ftxui {

View File

@ -781,7 +781,9 @@ void ScreenInteractive::HandleTask(Component component, Task& task) {
arg.screen_ = this; 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_)) { if (arg == Event::CtrlC && (!handled || force_handle_ctrl_c_)) {
RecordSignal(SIGABRT); RecordSignal(SIGABRT);
@ -824,6 +826,44 @@ void ScreenInteractive::HandleTask(Component component, Task& task) {
// clang-format on // 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 // private
// NOLINTNEXTLINE // NOLINTNEXTLINE
void ScreenInteractive::Draw(Component component) { void ScreenInteractive::Draw(Component component) {
@ -899,7 +939,7 @@ void ScreenInteractive::Draw(Component component) {
#endif #endif
previous_frame_resized_ = resized; previous_frame_resized_ = resized;
Render(*this, document); Render(*this, document.get(), selection_box_);
// Set cursor position for user using tools to insert CJK characters. // Set cursor position for user using tools to insert CJK characters.
{ {

View File

@ -65,7 +65,7 @@ class Border : public Node {
if (children_.size() == 2) { if (children_.size() == 2) {
Box title_box; Box title_box;
title_box.x_min = box.x_min + 1; title_box.x_min = box.x_min + 1;
title_box.x_max = box.x_max - 1; title_box.x_max = std::min(box.x_max - 1, box.x_min + children_[1]->requirement().min_x);
title_box.y_min = box.y_min; title_box.y_min = box.y_min;
title_box.y_max = box.y_min; title_box.y_max = box.y_min;
children_[1]->SetBox(title_box); children_[1]->SetBox(title_box);

View File

@ -64,6 +64,30 @@ class HBox : public Node {
x = box.x_max + 1; 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 } // namespace

View File

@ -1,3 +1,4 @@
#include <iostream>
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in // Use of this source code is governed by the MIT license that can be found in
// the LICENSE file. // the LICENSE file.
@ -27,6 +28,20 @@ void Node::SetBox(Box box) {
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. /// @brief Display an element on a ftxui::Screen.
/// @ingroup dom /// @ingroup dom
void Node::Render(Screen& screen) { void Node::Render(Screen& screen) {
@ -45,12 +60,16 @@ void Node::Check(Status* status) {
/// @brief Display an element on a ftxui::Screen. /// @brief Display an element on a ftxui::Screen.
/// @ingroup dom /// @ingroup dom
void Render(Screen& screen, const Element& element) { 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. /// @brief Display an element on a ftxui::Screen.
/// @ingroup dom /// @ingroup dom
void Render(Screen& screen, Node* node) { 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 box;
box.x_min = 0; box.x_min = 0;
box.y_min = 0; box.y_min = 0;
@ -73,11 +92,29 @@ void Render(Screen& screen, Node* node) {
node->Check(&status); 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; screen.stencil = box;
node->Render(screen); node->Render(screen);
// Step 4: Apply shaders // Step 5: Apply shaders
screen.ApplyShader(); screen.ApplyShader();
} }

View 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

View File

@ -28,26 +28,60 @@ class Text : public Node {
requirement_.min_y = 1; 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 { void Render(Screen& screen) override {
int x = box_.x_min; int x = box_.x_min;
const int y = box_.y_min; const int y = box_.y_min;
if (y > box_.y_max) { if (y > box_.y_max) {
return; return;
} }
for (const auto& cell : Utf8ToGlyphs(text_)) { for (const auto& cell : Utf8ToGlyphs(text_)) {
if (x > box_.x_max) { if (x > box_.x_max) {
return; break;
} }
if (cell == "\n") { if (cell == "\n") {
continue; continue;
} }
screen.PixelAt(x, y).character = cell; screen.PixelAt(x, y).character = cell;
if (has_selection) {
if((x >= selection_start_) && (x <= selection_end_)) {
screen.PixelAt(x, y).inverted = true;
}
}
++x; ++x;
} }
} }
private: private:
std::string text_; std::string text_;
bool has_selection = false;
int selection_start_ = 0;
int selection_end_ = 10;
}; };
class VText : public Node { class VText : public Node {

View File

@ -64,6 +64,30 @@ class VBox : public Node {
y = box.y_max + 1; 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 } // namespace

View File

@ -389,7 +389,8 @@ Screen Screen::Create(Dimensions dimension) {
return {dimension.dimx, dimension.dimy}; 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) #if defined(_WIN32)
// The placement of this call is a bit weird, however we can assume that // The placement of this call is a bit weird, however we can assume that
// anybody who instantiates a Screen object eventually wants to output // anybody who instantiates a Screen object eventually wants to output