Skip to content

Commit

Permalink
Reformat + fix pending selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurSonzogni committed Aug 31, 2024
1 parent f8dd258 commit 3754136
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 54 deletions.
16 changes: 7 additions & 9 deletions examples/component/selectable_input.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
// 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
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#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;

Expand All @@ -38,10 +35,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([&](Event event) {
input_phone_number |= CatchEvent([&](const Event& event) {
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;
});

Expand All @@ -55,17 +52,18 @@ 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;
});
Expand Down
13 changes: 7 additions & 6 deletions include/ftxui/component/screen_interactive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ using Component = std::shared_ptr<ComponentBase>;
class ScreenInteractivePrivate;

typedef struct {

Check failure on line 29 in include/ftxui/component/screen_interactive.hpp

View workflow job for this annotation

GitHub Actions / Tests (Windows MSVC, windows-latest, cl)

unnamed class used in typedef name cannot declare members other than non-static data members, member enumerations, or member classes [D:\a\FTXUI\FTXUI\build\ftxui-tests.vcxproj]

Check failure on line 29 in include/ftxui/component/screen_interactive.hpp

View workflow job for this annotation

GitHub Actions / Tests (Windows MSVC, windows-latest, cl)

unnamed class used in typedef name cannot declare members other than non-static data members, member enumerations, or member classes [D:\a\FTXUI\FTXUI\build\examples\component\ftxui_example_button.vcxproj]

Check failure on line 29 in include/ftxui/component/screen_interactive.hpp

View workflow job for this annotation

GitHub Actions / Tests (Windows MSVC, windows-latest, cl)

unnamed class used in typedef name cannot declare members other than non-static data members, member enumerations, or member classes [D:\a\FTXUI\FTXUI\build\examples\component\ftxui_example_button_animated.vcxproj]

Check failure on line 29 in include/ftxui/component/screen_interactive.hpp

View workflow job for this annotation

GitHub Actions / Tests (Windows MSVC, windows-latest, cl)

unnamed class used in typedef name cannot declare members other than non-static data members, member enumerations, or member classes [D:\a\FTXUI\FTXUI\build\examples\component\ftxui_example_button_in_frame.vcxproj]

Check failure on line 29 in include/ftxui/component/screen_interactive.hpp

View workflow job for this annotation

GitHub Actions / Tests (Windows MSVC, windows-latest, cl)

unnamed class used in typedef name cannot declare members other than non-static data members, member enumerations, or member classes [D:\a\FTXUI\FTXUI\build\examples\component\ftxui_example_button_style.vcxproj]

Check failure on line 29 in include/ftxui/component/screen_interactive.hpp

View workflow job for this annotation

GitHub Actions / Tests (Windows MSVC, windows-latest, cl)

unnamed class used in typedef name cannot declare members other than non-static data members, member enumerations, or member classes [D:\a\FTXUI\FTXUI\build\examples\component\ftxui_example_canvas_animated.vcxproj]

Check failure on line 29 in include/ftxui/component/screen_interactive.hpp

View workflow job for this annotation

GitHub Actions / Tests (Windows MSVC, windows-latest, cl)

unnamed class used in typedef name cannot declare members other than non-static data members, member enumerations, or member classes [D:\a\FTXUI\FTXUI\build\examples\component\ftxui_example_checkbox.vcxproj]

Check failure on line 29 in include/ftxui/component/screen_interactive.hpp

View workflow job for this annotation

GitHub Actions / Tests (Windows MSVC, windows-latest, cl)

unnamed class used in typedef name cannot declare members other than non-static data members, member enumerations, or member classes [D:\a\FTXUI\FTXUI\build\examples\component\ftxui_example_checkbox_in_frame.vcxproj]

Check failure on line 29 in include/ftxui/component/screen_interactive.hpp

View workflow job for this annotation

GitHub Actions / Tests (Windows MSVC, windows-latest, cl)

unnamed class used in typedef name cannot declare members other than non-static data members, member enumerations, or member classes [D:\a\FTXUI\FTXUI\build\examples\component\ftxui_example_collapsible.vcxproj]

Check failure on line 29 in include/ftxui/component/screen_interactive.hpp

View workflow job for this annotation

GitHub Actions / Tests (Windows MSVC, windows-latest, cl)

unnamed class used in typedef name cannot declare members other than non-static data members, member enumerations, or member classes [D:\a\FTXUI\FTXUI\build\examples\component\ftxui_example_composition.vcxproj]

uint16_t startx = 0;
uint16_t endx = 0;
uint16_t starty = 0;
Expand Down Expand Up @@ -76,7 +75,7 @@ class ScreenInteractive : public Screen {
void ForceHandleCtrlC(bool force);
void ForceHandleCtrlZ(bool force);

std::string getSelection(void);
std::string GetSelection();

private:
void ExitNow();
Expand All @@ -92,8 +91,8 @@ class ScreenInteractive : public Screen {
void RunOnceBlocking(Component component);

void HandleTask(Component component, Task& task);
bool selectableCatchEvent(Event event);
void refreshSelection(void);
bool HandleSelection(Event event);
void RefreshSelection();
void Draw(Component component);
void ResetCursorPosition();

Expand Down Expand Up @@ -138,8 +137,10 @@ class ScreenInteractive : public Screen {
bool force_handle_ctrl_c_ = true;
bool force_handle_ctrl_z_ = true;

Region selectedRegion;
std::string selectedText;
bool selection_enabled = false;
CapturedMouse selection_pending;
Region selection_region;
std::string selection_text;

// The style of the cursor to restore on exit.
int cursor_reset_shape_ = 1;
Expand Down
6 changes: 6 additions & 0 deletions include/ftxui/dom/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ 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_;
Expand Down
101 changes: 62 additions & 39 deletions src/ftxui/component/screen_interactive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ ScreenInteractive::ScreenInteractive(int dimx,
: Screen(dimx, dimy),
dimension_(dimension),
use_alternative_screen_(use_alternative_screen),
selectedText("") {
selection_text("") {
task_receiver_ = MakeReceiver<Task>();
}

Expand Down Expand Up @@ -784,13 +784,7 @@ void ScreenInteractive::HandleTask(Component component, Task& task) {

bool handled = component->OnEvent(arg);

if(handled == false)
{
if(selectableCatchEvent(arg))
{
handled = true;
}
}
handled = handled || HandleSelection(arg);

if (arg == Event::CtrlC && (!handled || force_handle_ctrl_c_)) {
RecordSignal(SIGABRT);
Expand Down Expand Up @@ -834,48 +828,77 @@ void ScreenInteractive::HandleTask(Component component, Task& task) {
}

// private
bool ScreenInteractive::selectableCatchEvent(Event event) {

if (event.is_mouse()) {
auto& mouse = event.mouse();
if (mouse.button == Mouse::Left) {

if (mouse.motion == Mouse::Pressed) {
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;
}
bool ScreenInteractive::HandleSelection(Event event) {
if (!event.is_mouse()) {
return false;
}

auto& mouse = event.mouse();
if (mouse.button != Mouse::Left) {
return false;
}

if (mouse.motion == Mouse::Pressed) {
selection_pending = CaptureMouse();
if (!selection_pending) {
return false;
}
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;
}

return false;
}
if (!selection_pending) {
return false;
}

void ScreenInteractive::refreshSelection(void) {
if (mouse.motion == Mouse::Moved) {
selection_region.endx = mouse.x;
selection_region.endy = mouse.y;
return true;
}

selectedText = "";
if (mouse.motion == Mouse::Released) {
selection_region.endx = mouse.x;
selection_region.endy = mouse.y;
selection_pending = nullptr;

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)
{
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 = "";

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) {
PixelAt(x, y).inverted ^= true;
selectedText += PixelAt(x, y).character;
selection_text += PixelAt(x, y).character;
}
}
}
}

std::string ScreenInteractive::getSelection(void) {

return selectedText;
std::string ScreenInteractive::GetSelection() {
return selection_text;
}

// private
Expand Down Expand Up @@ -948,7 +971,7 @@ void ScreenInteractive::Draw(Component component) {

Render(*this, document);

refreshSelection();
RefreshSelection();

// Set cursor position for user using tools to insert CJK characters.
{
Expand Down

0 comments on commit 3754136

Please sign in to comment.