Compare commits

..

2 Commits

Author SHA1 Message Date
Clément Roblot
697d11c380
Merge 3754136dd6 into dfb9558eaf 2024-08-31 13:40:08 +00:00
ArthurSonzogni
3754136dd6
Reformat + fix pending selection. 2024-08-31 15:39:42 +02:00
4 changed files with 79 additions and 51 deletions

View File

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

View File

@ -27,7 +27,6 @@ using Component = std::shared_ptr<ComponentBase>;
class ScreenInteractivePrivate; class ScreenInteractivePrivate;
typedef struct { typedef struct {
uint16_t startx = 0; uint16_t startx = 0;
uint16_t endx = 0; uint16_t endx = 0;
uint16_t starty = 0; uint16_t starty = 0;
@ -76,7 +75,7 @@ class ScreenInteractive : public Screen {
void ForceHandleCtrlC(bool force); void ForceHandleCtrlC(bool force);
void ForceHandleCtrlZ(bool force); void ForceHandleCtrlZ(bool force);
std::string getSelection(void); std::string GetSelection();
private: private:
void ExitNow(); void ExitNow();
@ -92,8 +91,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 selectableCatchEvent(Event event); bool HandleSelection(Event event);
void refreshSelection(void); void RefreshSelection();
void Draw(Component component); void Draw(Component component);
void ResetCursorPosition(); void ResetCursorPosition();
@ -138,8 +137,10 @@ class ScreenInteractive : public Screen {
bool force_handle_ctrl_c_ = true; bool force_handle_ctrl_c_ = true;
bool force_handle_ctrl_z_ = true; bool force_handle_ctrl_z_ = true;
Region selectedRegion; bool selection_enabled = false;
std::string selectedText; CapturedMouse selection_pending;
Region selection_region;
std::string selection_text;
// 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;

View File

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

View File

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