Compare commits

..

1 Commits

Author SHA1 Message Date
Clément Roblot
747de7b3f5
Merge f8dd258d65 into dfb9558eaf 2024-08-27 14:34:29 +00:00
4 changed files with 51 additions and 79 deletions

View File

@ -1,8 +1,10 @@
// 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 allocator, __shared_ptr_access
#include <string> // for char_traits, operator+, string, basic_string
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#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
@ -10,6 +12,7 @@
#include "ftxui/dom/elements.hpp" // for text, hbox, separator, Element, operator|, vbox, border
#include "ftxui/util/ref.hpp" // for Ref
int main() {
using namespace ftxui;
@ -35,10 +38,10 @@ int main() {
// The phone number input component:
// We are using `CatchEvent` to filter out non-digit characters.
Component input_phone_number = Input(&phoneNumber, "phone number");
input_phone_number |= CatchEvent([&](const Event& event) {
input_phone_number |= CatchEvent([&](Event event) {
return event.is_character() && !std::isdigit(event.character()[0]);
});
input_phone_number |= CatchEvent([&](const Event& event) {
input_phone_number |= CatchEvent([&](Event event) {
return event.is_character() && phoneNumber.size() > 10;
});
@ -52,18 +55,17 @@ int main() {
// Tweak how the component tree is rendered:
auto renderer = Renderer(component, [&] {
return vbox({
hbox(text(" First name : "), input_first_name->Render()),
hbox(text(" Last name : ") | selectable(),
input_last_name->Render()),
hbox(text(" Last name : ") | selectable(), input_last_name->Render()),
hbox(text(" Password : "), input_password->Render()),
hbox(text(" Phone num : "), input_phone_number->Render()) |
selectable(),
hbox(text(" Phone num : "), input_phone_number->Render()) | selectable(),
separator(),
text("Hello " + first_name + " " + last_name),
text("Your password is " + password),
text("Your phone number is " + phoneNumber),
text("Selected test is " + screen.GetSelection()),
text("Selected test is " + screen.getSelection())
}) |
border;
});

View File

@ -27,6 +27,7 @@ using Component = std::shared_ptr<ComponentBase>;
class ScreenInteractivePrivate;
typedef struct {
uint16_t startx = 0;
uint16_t endx = 0;
uint16_t starty = 0;
@ -75,7 +76,7 @@ class ScreenInteractive : public Screen {
void ForceHandleCtrlC(bool force);
void ForceHandleCtrlZ(bool force);
std::string GetSelection();
std::string getSelection(void);
private:
void ExitNow();
@ -91,8 +92,8 @@ class ScreenInteractive : public Screen {
void RunOnceBlocking(Component component);
void HandleTask(Component component, Task& task);
bool HandleSelection(Event event);
void RefreshSelection();
bool selectableCatchEvent(Event event);
void refreshSelection(void);
void Draw(Component component);
void ResetCursorPosition();
@ -137,10 +138,8 @@ class ScreenInteractive : public Screen {
bool force_handle_ctrl_c_ = true;
bool force_handle_ctrl_z_ = true;
bool selection_enabled = false;
CapturedMouse selection_pending;
Region selection_region;
std::string selection_text;
Region selectedRegion;
std::string selectedText;
// The style of the cursor to restore on exit.
int cursor_reset_shape_ = 1;

View File

@ -52,12 +52,6 @@ class Node {
};
virtual void Check(Status* status);
// Selection.
// Propagated from Parents to Children.
virtual void Select(Box selected_area) {
// TODO: Implement this.
}
protected:
Elements children_;
Requirement requirement_;

View File

@ -352,7 +352,7 @@ ScreenInteractive::ScreenInteractive(int dimx,
: Screen(dimx, dimy),
dimension_(dimension),
use_alternative_screen_(use_alternative_screen),
selection_text("") {
selectedText("") {
task_receiver_ = MakeReceiver<Task>();
}
@ -784,7 +784,13 @@ void ScreenInteractive::HandleTask(Component component, Task& task) {
bool handled = component->OnEvent(arg);
handled = handled || HandleSelection(arg);
if(handled == false)
{
if(selectableCatchEvent(arg))
{
handled = true;
}
}
if (arg == Event::CtrlC && (!handled || force_handle_ctrl_c_)) {
RecordSignal(SIGABRT);
@ -828,77 +834,48 @@ void ScreenInteractive::HandleTask(Component component, Task& task) {
}
// private
bool ScreenInteractive::HandleSelection(Event event) {
if (!event.is_mouse()) {
return false;
}
bool ScreenInteractive::selectableCatchEvent(Event event) {
if (event.is_mouse()) {
auto& mouse = event.mouse();
if (mouse.button != Mouse::Left) {
return false;
}
if (mouse.button == Mouse::Left) {
if (mouse.motion == Mouse::Pressed) {
selection_pending = CaptureMouse();
if (!selection_pending) {
return false;
selectedRegion.startx = mouse.x;
selectedRegion.starty = mouse.y;
selectedRegion.endx = mouse.x;
selectedRegion.endy = mouse.y;
} else if (mouse.motion == Mouse::Released) {
selectedRegion.endx = mouse.x;
selectedRegion.endy = mouse.y;
} else if (mouse.motion == Mouse::Moved) {
selectedRegion.endx = mouse.x;
selectedRegion.endy = mouse.y;
}
selection_enabled = true;
selection_region.startx = mouse.x;
selection_region.starty = mouse.y;
selection_region.endx = mouse.x;
selection_region.endy = mouse.y;
return true;
}
if (!selection_pending) {
return false;
}
if (mouse.motion == Mouse::Moved) {
selection_region.endx = mouse.x;
selection_region.endy = mouse.y;
return true;
}
if (mouse.motion == Mouse::Released) {
selection_region.endx = mouse.x;
selection_region.endy = mouse.y;
selection_pending = nullptr;
if (selection_region.startx == selection_region.endx &&
selection_region.starty == selection_region.endy) {
selection_enabled = false;
return true;
}
return true;
}
return false;
}
void ScreenInteractive::RefreshSelection() {
if (!selection_enabled) {
return;
}
selection_text = "";
void ScreenInteractive::refreshSelection(void) {
for (int y = std::min(selection_region.starty, selection_region.endy);
y <= std::max(selection_region.starty, selection_region.endy); ++y) {
for (int x = std::min(selection_region.startx, selection_region.endx);
x <= std::max(selection_region.startx, selection_region.endx) - 1;
++x) {
if (PixelAt(x, y).selectable == true) {
selectedText = "";
for (int y = std::min(selectedRegion.starty, selectedRegion.endy); y <= std::max(selectedRegion.starty, selectedRegion.endy); ++y) {
for (int x = std::min(selectedRegion.startx, selectedRegion.endx); x <= std::max(selectedRegion.startx, selectedRegion.endx)-1; ++x) {
if(PixelAt(x, y).selectable == true)
{
PixelAt(x, y).inverted ^= true;
selection_text += PixelAt(x, y).character;
selectedText += PixelAt(x, y).character;
}
}
}
}
std::string ScreenInteractive::GetSelection() {
return selection_text;
std::string ScreenInteractive::getSelection(void) {
return selectedText;
}
// private
@ -971,7 +948,7 @@ void ScreenInteractive::Draw(Component component) {
Render(*this, document);
RefreshSelection();
refreshSelection();
// Set cursor position for user using tools to insert CJK characters.
{