mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2024-11-23 03:10:01 +08:00
Better integratiion
This commit is contained in:
parent
10a4a04529
commit
2de925f2c6
@ -82,7 +82,7 @@ add_library(dom
|
|||||||
src/ftxui/dom/gridbox.cpp
|
src/ftxui/dom/gridbox.cpp
|
||||||
src/ftxui/dom/hbox.cpp
|
src/ftxui/dom/hbox.cpp
|
||||||
src/ftxui/dom/inverted.cpp
|
src/ftxui/dom/inverted.cpp
|
||||||
src/ftxui/dom/selected.cpp
|
src/ftxui/dom/selectable.cpp
|
||||||
src/ftxui/dom/linear_gradient.cpp
|
src/ftxui/dom/linear_gradient.cpp
|
||||||
src/ftxui/dom/node.cpp
|
src/ftxui/dom/node.cpp
|
||||||
src/ftxui/dom/node_decorator.cpp
|
src/ftxui/dom/node_decorator.cpp
|
||||||
|
@ -68,52 +68,18 @@ int main() {
|
|||||||
text("select_end " + std::to_string(selection.endx) + ";" + std::to_string(selection.endy)),
|
text("select_end " + std::to_string(selection.endx) + ";" + std::to_string(selection.endy)),
|
||||||
text("textToCopy " + textToCopy)
|
text("textToCopy " + textToCopy)
|
||||||
}) |
|
}) |
|
||||||
border | selected(selection, [&textToCopy](std::string selected){textToCopy = selected;});
|
border | selectable([&textToCopy](std::string txtSelected){textToCopy = txtSelected;});
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: Is there a way for me to embedd the catchEvent in the selected decorator? At a minimum move the function in the selected.cpp file and add doc to call it
|
|
||||||
// TODO: Implement the double click on word to select the word
|
// TODO: Implement the double click on word to select the word
|
||||||
// TODO: Implement the double click and drag to select word by word (optional)
|
// TODO: Implement the double click and drag to select word by word (optional)
|
||||||
|
// TODO: Implement the tripple click to select an entire line
|
||||||
|
// TODO: Call onSelectionChange_ only when the selection indeed did change, not at every render?
|
||||||
// TODO: Add a "selectable" flag in the pixel class and take it into account when selecting things
|
// TODO: Add a "selectable" flag in the pixel class and take it into account when selecting things
|
||||||
|
|
||||||
renderer |= CatchEvent([&](Event event) {
|
renderer |= CatchEvent([&](Event event) {
|
||||||
if (event.is_mouse()) {
|
|
||||||
auto& mouse = event.mouse();
|
|
||||||
if (mouse.button == Mouse::Left) {
|
|
||||||
|
|
||||||
if (mouse.motion == Mouse::Pressed)
|
return selectableCatchEvent(event);
|
||||||
{
|
|
||||||
selection.startx = mouse.x;
|
|
||||||
selection.starty = mouse.y;
|
|
||||||
selection.endx = mouse.x;
|
|
||||||
selection.endy = mouse.y;
|
|
||||||
}
|
|
||||||
else if (mouse.motion == Mouse::Released)
|
|
||||||
{
|
|
||||||
selection.endx = mouse.x;
|
|
||||||
selection.endy = mouse.y;
|
|
||||||
}
|
|
||||||
else if (mouse.motion == Mouse::Moved)
|
|
||||||
{
|
|
||||||
selection.endx = mouse.x;
|
|
||||||
selection.endy = mouse.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
screen.PostEvent(Event::Custom);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if (event == Event::SpecialKey("Ctrl+Shift+C")) {
|
|
||||||
// textToCopy = "Kikoo!";
|
|
||||||
// //clip::set_text(text_to_copy); // Set the clipboard content
|
|
||||||
|
|
||||||
// screen.PostEvent(Event::Custom);
|
|
||||||
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
|
|
||||||
return false;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
screen.Loop(renderer);
|
screen.Loop(renderer);
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
#include "ftxui/screen/color.hpp"
|
#include "ftxui/screen/color.hpp"
|
||||||
#include "ftxui/screen/terminal.hpp"
|
#include "ftxui/screen/terminal.hpp"
|
||||||
#include "ftxui/util/ref.hpp"
|
#include "ftxui/util/ref.hpp"
|
||||||
|
#include "ftxui/component/event.hpp"
|
||||||
|
|
||||||
namespace ftxui {
|
namespace ftxui {
|
||||||
class Node;
|
class Node;
|
||||||
@ -104,8 +105,9 @@ Element canvas(std::function<void(Canvas&)>);
|
|||||||
Element bold(Element);
|
Element bold(Element);
|
||||||
Element dim(Element);
|
Element dim(Element);
|
||||||
Element inverted(Element);
|
Element inverted(Element);
|
||||||
Element selected(Region &selection, std::function<void(const std::string)> onSelectionChange, Element);
|
Element selectable(std::function<void(const std::string)> onSelectionChange, Element);
|
||||||
Decorator selected(Region &selection, std::function<void(const std::string)> onSelectionChange);
|
Decorator selectable(std::function<void(const std::string)> onSelectionChange);
|
||||||
|
bool selectableCatchEvent(Event event);
|
||||||
Element underlined(Element);
|
Element underlined(Element);
|
||||||
Element underlinedDouble(Element);
|
Element underlinedDouble(Element);
|
||||||
Element blink(Element);
|
Element blink(Element);
|
||||||
|
80
src/ftxui/dom/selectable.cpp
Normal file
80
src/ftxui/dom/selectable.cpp
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
// 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 <memory> // for make_shared
|
||||||
|
#include <utility> // for move
|
||||||
|
|
||||||
|
#include "ftxui/dom/elements.hpp" // for Element, inverted
|
||||||
|
#include "ftxui/dom/node.hpp" // for Node
|
||||||
|
#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
|
||||||
|
#include "ftxui/screen/box.hpp" // for Box
|
||||||
|
#include "ftxui/screen/screen.hpp" // for Pixel, Screen
|
||||||
|
|
||||||
|
namespace ftxui {
|
||||||
|
|
||||||
|
Region newSelection;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
class Selectable : public NodeDecorator {
|
||||||
|
public:
|
||||||
|
using NodeDecorator::NodeDecorator;
|
||||||
|
Selectable(Element child, std::function<void(const std::string)> onSelectionChange)
|
||||||
|
: NodeDecorator(std::move(child)), onSelectionChange_(onSelectionChange) {}
|
||||||
|
|
||||||
|
void Render(Screen& screen) override {
|
||||||
|
Node::Render(screen);
|
||||||
|
std::string selectedText = "";
|
||||||
|
for (int y = std::min(newSelection.starty, newSelection.endy); y <= std::max(newSelection.starty, newSelection.endy); ++y) {
|
||||||
|
for (int x = std::min(newSelection.startx, newSelection.endx); x <= std::max(newSelection.startx, newSelection.endx)-1; ++x) {
|
||||||
|
screen.PixelAt(x, y).inverted ^= true;
|
||||||
|
selectedText += screen.PixelAt(x, y).character;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onSelectionChange_(selectedText);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::function<void(const std::string)> onSelectionChange_;
|
||||||
|
};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
/// @brief Add a filter that will invert the foreground and the background
|
||||||
|
/// colors.
|
||||||
|
/// @ingroup dom
|
||||||
|
|
||||||
|
Element selectable(std::function<void(const std::string)> onSelectionChange, Element child) {
|
||||||
|
return std::make_shared<Selectable>(std::move(child), onSelectionChange);
|
||||||
|
}
|
||||||
|
|
||||||
|
Decorator selectable(std::function<void(const std::string)> onSelectionChange) {
|
||||||
|
return [onSelectionChange](Element child) { return selectable(onSelectionChange, std::move(child)); };
|
||||||
|
}
|
||||||
|
|
||||||
|
bool selectableCatchEvent(Event event) {
|
||||||
|
|
||||||
|
if (event.is_mouse()) {
|
||||||
|
auto& mouse = event.mouse();
|
||||||
|
if (mouse.button == Mouse::Left) {
|
||||||
|
|
||||||
|
if (mouse.motion == Mouse::Pressed) {
|
||||||
|
newSelection.startx = mouse.x;
|
||||||
|
newSelection.starty = mouse.y;
|
||||||
|
newSelection.endx = mouse.x;
|
||||||
|
newSelection.endy = mouse.y;
|
||||||
|
} else if (mouse.motion == Mouse::Released) {
|
||||||
|
newSelection.endx = mouse.x;
|
||||||
|
newSelection.endy = mouse.y;
|
||||||
|
} else if (mouse.motion == Mouse::Moved) {
|
||||||
|
newSelection.endx = mouse.x;
|
||||||
|
newSelection.endy = mouse.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace ftxui
|
@ -1,53 +0,0 @@
|
|||||||
// 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 <memory> // for make_shared
|
|
||||||
#include <utility> // for move
|
|
||||||
|
|
||||||
#include "ftxui/dom/elements.hpp" // for Element, inverted
|
|
||||||
#include "ftxui/dom/node.hpp" // for Node
|
|
||||||
#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
|
|
||||||
#include "ftxui/screen/box.hpp" // for Box
|
|
||||||
#include "ftxui/screen/screen.hpp" // for Pixel, Screen
|
|
||||||
|
|
||||||
namespace ftxui {
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
class Selected : public NodeDecorator {
|
|
||||||
public:
|
|
||||||
using NodeDecorator::NodeDecorator;
|
|
||||||
Selected(Element child, Region &selection, std::function<void(const std::string)> onSelectionChange)
|
|
||||||
: NodeDecorator(std::move(child)), selection_(selection), onSelectionChange_(onSelectionChange) {}
|
|
||||||
|
|
||||||
void Render(Screen& screen) override {
|
|
||||||
Node::Render(screen);
|
|
||||||
std::string selectedText = "";
|
|
||||||
for (int y = std::min(selection_.starty, selection_.endy); y <= std::max(selection_.starty, selection_.endy); ++y) {
|
|
||||||
for (int x = std::min(selection_.startx, selection_.endx); x <= std::max(selection_.startx, selection_.endx)-1; ++x) {
|
|
||||||
screen.PixelAt(x, y).inverted ^= true;
|
|
||||||
selectedText += screen.PixelAt(x, y).character;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
onSelectionChange_(selectedText);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
Region &selection_;
|
|
||||||
std::function<void(const std::string)> onSelectionChange_;
|
|
||||||
};
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
/// @brief Add a filter that will invert the foreground and the background
|
|
||||||
/// colors.
|
|
||||||
/// @ingroup dom
|
|
||||||
|
|
||||||
Element selected(Region &selection, std::function<void(const std::string)> onSelectionChange, Element child) {
|
|
||||||
return std::make_shared<Selected>(std::move(child), selection, onSelectionChange);
|
|
||||||
}
|
|
||||||
|
|
||||||
Decorator selected(Region &selection, std::function<void(const std::string)> onSelectionChange) {
|
|
||||||
return [&selection, onSelectionChange](Element child) { return selected(selection, onSelectionChange, std::move(child)); };
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace ftxui
|
|
Loading…
Reference in New Issue
Block a user