diff --git a/CHANGELOG.md b/CHANGELOG.md index bfbcfb15..fbe57ae2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,9 @@ current (development) - Feature: Add support for `Input`'s insert mode. Add `InputOption::insert` option. Added by @mingsheng13. - Feature: Add `DropdownOption` to configure the dropdown. See #826. +- Feature: Add support for Selection. Thanks @clement-roblot. See #926. + - See `ScreenInteractive::GetSelection()`. + - See `ScreenInteractive::SelectionChange(...)` listener. - Bugfix/Breaking change: `Mouse transition`: - Detect when the mouse move, as opposed to being pressed. The Mouse::Moved motion was added. @@ -49,6 +52,12 @@ current (development) - Feature: Add `extend_beyond_screen` option to `Dimension::Fit(..)`, allowing the element to be larger than the screen. Proposed by @LordWhiro. See #572 and #949. +- Feature: Add support for Selection. Thanks @clement-roblot. See #926. + - See `selectionColor` decorator. + - See `selectionBackgroundColor` decorator. + - See `selectionForegroundColor` decorator. + - See `selectionStyle(style)` decorator. + - See `selectionStyleReset` decorator. ### Screen - Feature: Add `Box::IsEmpty()`. diff --git a/examples/component/selection.cpp b/examples/component/selection.cpp index 89eaf7c9..93e96ea1 100644 --- a/examples/component/selection.cpp +++ b/examples/component/selection.cpp @@ -14,12 +14,10 @@ using namespace ftxui; Element LoremIpsum() { return vbox({ - text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " - "eiusmod tempor incididunt ut labore et dolore magna aliqua."), - text("Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris " - "nisi ut aliquip ex ea commodo consequat."), - text("Duis aute irure dolor in reprehenderit in voluptate velit esse " - "cillum dolore eu fugiat nulla pariatur."), + text("FTXUI: A powerful library for building user interfaces."), + text("Enjoy a rich set of components and a declarative style."), + text("Create beautiful and responsive UIs with minimal effort."), + text("Join the community and experience the power of FTXUI."), }); } @@ -31,9 +29,9 @@ int main() { int selection_change_counter = 0; std::string selection_content = ""; - screen.SelectionOnChange([&] { + screen.SelectionChange([&] { selection_change_counter++; - selection_content = screen.SelectionAsString(); + selection_content = screen.GetSelection(); }); // The components: @@ -42,7 +40,7 @@ int main() { text("Select changed: " + std::to_string(selection_change_counter) + " times"), text("Currently selected: "), - paragraph(selection_content) | frame | border | xflex | + paragraph(selection_content) | vscroll_indicator | frame | border | size(HEIGHT, EQUAL, 10), window(text("Horizontal split"), hbox({ LoremIpsum(), @@ -58,7 +56,7 @@ int main() { separator(), LoremIpsum(), })), - window(text("Grid split"), + window(text("Grid split with different style"), vbox({ hbox({ LoremIpsum(), diff --git a/include/ftxui/component/screen_interactive.hpp b/include/ftxui/component/screen_interactive.hpp index a2b7b650..a2909fca 100644 --- a/include/ftxui/component/screen_interactive.hpp +++ b/include/ftxui/component/screen_interactive.hpp @@ -70,8 +70,8 @@ class ScreenInteractive : public Screen { void ForceHandleCtrlZ(bool force); // Selection API. - std::string SelectionAsString(); - void SelectionOnChange(std::function callback); + std::string GetSelection(); + void SelectionChange(std::function callback); private: void ExitNow(); diff --git a/src/ftxui/component/screen_interactive.cpp b/src/ftxui/component/screen_interactive.cpp index 642b67b0..0c179442 100644 --- a/src/ftxui/component/screen_interactive.cpp +++ b/src/ftxui/component/screen_interactive.cpp @@ -577,14 +577,14 @@ void ScreenInteractive::ForceHandleCtrlZ(bool force) { } /// @brief Returns the content of the current selection -std::string ScreenInteractive::SelectionAsString() { +std::string ScreenInteractive::GetSelection() { if (!selection_) { return ""; } return selection_->GetParts(); } -void ScreenInteractive::SelectionOnChange(std::function callback) { +void ScreenInteractive::SelectionChange(std::function callback) { selection_on_change_ = std::move(callback); } diff --git a/src/ftxui/dom/selection_test.cpp b/src/ftxui/dom/selection_test.cpp index ac607fc5..7c48e28e 100644 --- a/src/ftxui/dom/selection_test.cpp +++ b/src/ftxui/dom/selection_test.cpp @@ -58,20 +58,20 @@ Event MouseMove(int x, int y) { TEST(SelectionTest, DefaultSelection) { auto component = Renderer([&] { return text("Lorem ipsum dolor"); }); auto screen = ScreenInteractive::FixedSize(20, 1); - EXPECT_EQ(screen.SelectionAsString(), ""); + EXPECT_EQ(screen.GetSelection(), ""); Loop loop(&screen, component); screen.PostEvent(MousePressed(3, 1)); screen.PostEvent(MouseReleased(10, 1)); loop.RunOnce(); - EXPECT_EQ(screen.SelectionAsString(), "rem ipsu"); + EXPECT_EQ(screen.GetSelection(), "rem ipsu"); } -TEST(SelectionTest, SelectionOnChange) { +TEST(SelectionTest, SelectionChange) { int selectionChangeCounter = 0; auto component = Renderer([&] { return text("Lorem ipsum dolor"); }); auto screen = ScreenInteractive::FixedSize(20, 1); - screen.SelectionOnChange([&] { selectionChangeCounter++; }); + screen.SelectionChange([&] { selectionChangeCounter++; }); Loop loop(&screen, component); loop.RunOnce(); @@ -97,7 +97,7 @@ TEST(SelectionTest, SelectionOnChange) { loop.RunOnce(); EXPECT_EQ(selectionChangeCounter, 3); - EXPECT_EQ(screen.SelectionAsString(), "rem ipsu"); + EXPECT_EQ(screen.GetSelection(), "rem ipsu"); } // Check that submitting multiple mouse events quickly doesn't trigger multiple @@ -106,7 +106,7 @@ TEST(SelectionTest, SelectionOnChangeSquashedEvents) { int selectionChangeCounter = 0; auto component = Renderer([&] { return text("Lorem ipsum dolor"); }); auto screen = ScreenInteractive::FixedSize(20, 1); - screen.SelectionOnChange([&] { selectionChangeCounter++; }); + screen.SelectionChange([&] { selectionChangeCounter++; }); Loop loop(&screen, component); loop.RunOnce(); @@ -123,7 +123,7 @@ TEST(SelectionTest, SelectionOnChangeSquashedEvents) { loop.RunOnce(); EXPECT_EQ(selectionChangeCounter, 2); - EXPECT_EQ(screen.SelectionAsString(), "rem ipsu"); + EXPECT_EQ(screen.GetSelection(), "rem ipsu"); } TEST(SelectionTest, StyleSelection) {