Implemented a callback

This commit is contained in:
Clement Roblot 2024-08-02 22:45:50 +07:00
parent 465822523d
commit 27424d5c20
3 changed files with 15 additions and 11 deletions

View File

@ -68,11 +68,11 @@ int main() {
text("select_end " + std::to_string(selection.endx) + ";" + std::to_string(selection.endy)),
text("textToCopy " + textToCopy)
}) |
border | selected(selection, textToCopy);
border | selected(selection, textToCopy, [&textToCopy](std::string selected){textToCopy = selected;});
});
// TODO: Make the textToCopy a callback called every times the selected text change
// TODO: Is there a way for me to embedd the catchEvent in the selected decorator?
// 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 and drag to select word by word (optional)
// TODO: Add a "selectable" flag in the pixel class and take it into account when selecting things

View File

@ -104,8 +104,8 @@ Element canvas(std::function<void(Canvas&)>);
Element bold(Element);
Element dim(Element);
Element inverted(Element);
Element selected(Region &selection, std::string &destination, Element);
Decorator selected(Region &selection, std::string &destination);
Element selected(Region &selection, std::string &destination, std::function<void(const std::string)> onSelectionChange, Element);
Decorator selected(Region &selection, std::string &destination, std::function<void(const std::string)> onSelectionChange);
Element underlined(Element);
Element underlinedDouble(Element);
Element blink(Element);

View File

@ -16,23 +16,27 @@ namespace {
class Selected : public NodeDecorator {
public:
using NodeDecorator::NodeDecorator;
Selected(Element child, Region &selection, std::string &destination)
: NodeDecorator(std::move(child)), selection_(selection), destination_(destination) {}
Selected(Element child, Region &selection, std::string &destination, std::function<void(const std::string)> onSelectionChange)
: NodeDecorator(std::move(child)), selection_(selection), destination_(destination), onSelectionChange_(onSelectionChange) {}
void Render(Screen& screen) override {
Node::Render(screen);
destination_ = "";
std::string textToCopy = "";
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;
destination_ += screen.PixelAt(x, y).character;
textToCopy += screen.PixelAt(x, y).character;
}
}
onSelectionChange_(textToCopy);
}
private:
Region &selection_;
std::string &destination_;
std::function<void(const std::string)> onSelectionChange_;
};
} // namespace
@ -40,12 +44,12 @@ private:
/// colors.
/// @ingroup dom
Element selected(Region &selection, std::string &destination, Element child) {
return std::make_shared<Selected>(std::move(child), selection, destination);
Element selected(Region &selection, std::string &destination, std::function<void(const std::string)> onSelectionChange, Element child) {
return std::make_shared<Selected>(std::move(child), selection, destination, onSelectionChange);
}
Decorator selected(Region &selection, std::string &destination) {
return [&selection, &destination](Element child) { return selected(selection, destination, std::move(child)); };
Decorator selected(Region &selection, std::string &destination, std::function<void(const std::string)> onSelectionChange) {
return [&selection, &destination, onSelectionChange](Element child) { return selected(selection, destination, onSelectionChange, std::move(child)); };
}
} // namespace ftxui