mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2024-11-26 04:31:34 +08:00
Prevent spamming
This commit is contained in:
parent
2de925f2c6
commit
53f179c776
@ -74,7 +74,6 @@ int main() {
|
|||||||
// 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: 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) {
|
||||||
|
@ -40,6 +40,7 @@ typedef struct {
|
|||||||
uint16_t endx = 0;
|
uint16_t endx = 0;
|
||||||
uint16_t starty = 0;
|
uint16_t starty = 0;
|
||||||
uint16_t endy = 0;
|
uint16_t endy = 0;
|
||||||
|
bool changed = false;
|
||||||
} Region;
|
} Region;
|
||||||
|
|
||||||
// Pipe elements into decorator togethers.
|
// Pipe elements into decorator togethers.
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
namespace ftxui {
|
namespace ftxui {
|
||||||
|
|
||||||
Region newSelection;
|
Region selectedRegion;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
class Selectable : public NodeDecorator {
|
class Selectable : public NodeDecorator {
|
||||||
@ -24,15 +24,18 @@ class Selectable : public NodeDecorator {
|
|||||||
void Render(Screen& screen) override {
|
void Render(Screen& screen) override {
|
||||||
Node::Render(screen);
|
Node::Render(screen);
|
||||||
std::string selectedText = "";
|
std::string selectedText = "";
|
||||||
for (int y = std::min(newSelection.starty, newSelection.endy); y <= std::max(newSelection.starty, newSelection.endy); ++y) {
|
for (int y = std::min(selectedRegion.starty, selectedRegion.endy); y <= std::max(selectedRegion.starty, selectedRegion.endy); ++y) {
|
||||||
for (int x = std::min(newSelection.startx, newSelection.endx); x <= std::max(newSelection.startx, newSelection.endx)-1; ++x) {
|
for (int x = std::min(selectedRegion.startx, selectedRegion.endx); x <= std::max(selectedRegion.startx, selectedRegion.endx)-1; ++x) {
|
||||||
screen.PixelAt(x, y).inverted ^= true;
|
screen.PixelAt(x, y).inverted ^= true;
|
||||||
selectedText += screen.PixelAt(x, y).character;
|
selectedText += screen.PixelAt(x, y).character;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(selectedRegion.changed == true) {
|
||||||
|
selectedRegion.changed = false;;
|
||||||
onSelectionChange_(selectedText);
|
onSelectionChange_(selectedText);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::function<void(const std::string)> onSelectionChange_;
|
std::function<void(const std::string)> onSelectionChange_;
|
||||||
@ -58,16 +61,19 @@ bool selectableCatchEvent(Event event) {
|
|||||||
if (mouse.button == Mouse::Left) {
|
if (mouse.button == Mouse::Left) {
|
||||||
|
|
||||||
if (mouse.motion == Mouse::Pressed) {
|
if (mouse.motion == Mouse::Pressed) {
|
||||||
newSelection.startx = mouse.x;
|
selectedRegion.startx = mouse.x;
|
||||||
newSelection.starty = mouse.y;
|
selectedRegion.starty = mouse.y;
|
||||||
newSelection.endx = mouse.x;
|
selectedRegion.endx = mouse.x;
|
||||||
newSelection.endy = mouse.y;
|
selectedRegion.endy = mouse.y;
|
||||||
|
selectedRegion.changed = true;
|
||||||
} else if (mouse.motion == Mouse::Released) {
|
} else if (mouse.motion == Mouse::Released) {
|
||||||
newSelection.endx = mouse.x;
|
selectedRegion.endx = mouse.x;
|
||||||
newSelection.endy = mouse.y;
|
selectedRegion.endy = mouse.y;
|
||||||
|
selectedRegion.changed = true;
|
||||||
} else if (mouse.motion == Mouse::Moved) {
|
} else if (mouse.motion == Mouse::Moved) {
|
||||||
newSelection.endx = mouse.x;
|
selectedRegion.endx = mouse.x;
|
||||||
newSelection.endy = mouse.y;
|
selectedRegion.endy = mouse.y;
|
||||||
|
selectedRegion.changed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
Reference in New Issue
Block a user