From 33b3d1c7abbf96c895890021b6dacb2234f70dcf Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Thu, 8 Jul 2021 00:01:42 +0200 Subject: [PATCH] Add option for input. --- examples/component/homescreen.cpp | 15 +++++++------ include/ftxui/component/component.hpp | 4 +++- include/ftxui/component/component_options.hpp | 5 +++++ include/ftxui/component/input.hpp | 9 ++++---- src/ftxui/component/input.cpp | 22 +++++++++++-------- 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/examples/component/homescreen.cpp b/examples/component/homescreen.cpp index e25a1c2..8a604b6 100644 --- a/examples/component/homescreen.cpp +++ b/examples/component/homescreen.cpp @@ -156,13 +156,19 @@ int main(int argc, const char* argv[]) { false, false, }; - std::wstring input_add_content; - Component input_add = Input(&input_add_content, "input files"); std::vector input_entries; int input_selected = 0; Component input = Menu(&input_entries, &input_selected); + auto input_option = InputOption(); + std::wstring input_add_content; + input_option.on_enter = [&] { + input_entries.push_back(input_add_content); + input_add_content = L""; + }; + Component input_add = Input(&input_add_content, "input files", input_option); + std::wstring executable_content_ = L""; Component executable_ = Input(&executable_content_, "executable"); @@ -185,11 +191,6 @@ int main(int argc, const char* argv[]) { }), }); - InputBase::From(input_add)->on_enter = [&] { - input_entries.push_back(input_add_content); - input_add_content = L""; - }; - auto render_command = [&] { Elements line; // Compiler diff --git a/include/ftxui/component/component.hpp b/include/ftxui/component/component.hpp index c80e670..8a59d11 100644 --- a/include/ftxui/component/component.hpp +++ b/include/ftxui/component/component.hpp @@ -30,7 +30,9 @@ Component Button(ConstStringRef label, Component Checkbox(ConstStringRef label, bool* checked, ConstRef option = {}); -Component Input(StringRef content, ConstStringRef placeholder); +Component Input(StringRef content, + ConstStringRef placeholder, + ConstRef option = {}); Component Menu(const std::vector* entries, int* selected_, ConstRef = {}); diff --git a/include/ftxui/component/component_options.hpp b/include/ftxui/component/component_options.hpp index beb4f65..897fc0b 100644 --- a/include/ftxui/component/component_options.hpp +++ b/include/ftxui/component/component_options.hpp @@ -31,6 +31,11 @@ struct CheckboxOption { std::function on_change = []() {}; }; +struct InputOption { + std::function on_change = [] {}; + std::function on_enter = [] {}; +}; + }; // namespace ftxui #endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */ diff --git a/include/ftxui/component/input.hpp b/include/ftxui/component/input.hpp index 605db99..efa9e0c 100644 --- a/include/ftxui/component/input.hpp +++ b/include/ftxui/component/input.hpp @@ -21,16 +21,14 @@ class InputBase : public ComponentBase { static InputBase* From(Component component); // Constructor. - InputBase(StringRef content, ConstStringRef placeholder); + InputBase(StringRef content, + ConstStringRef placeholder, + ConstRef option = {}); ~InputBase() override = default; // State. int cursor_position = 0; - // State update callback. - std::function on_change = [] {}; - std::function on_enter = [] {}; - // Component implementation. Element Render() override; bool OnEvent(Event) override; @@ -42,6 +40,7 @@ class InputBase : public ComponentBase { bool OnMouseEvent(Event); Box input_box_; Box cursor_box_; + ConstRef option_; }; } // namespace ftxui diff --git a/src/ftxui/component/input.cpp b/src/ftxui/component/input.cpp index 3ecaa3a..e419f36 100644 --- a/src/ftxui/component/input.cpp +++ b/src/ftxui/component/input.cpp @@ -31,8 +31,10 @@ namespace ftxui { /// ```bash /// placeholder /// ``` -Component Input(StringRef content, ConstStringRef placeholder) { - return Make(content, placeholder); +Component Input(StringRef content, + ConstStringRef placeholder, + ConstRef option) { + return Make(content, placeholder, std::move(option)); } // static @@ -40,8 +42,10 @@ InputBase* InputBase::From(Component component) { return static_cast(component.get()); } -InputBase::InputBase(StringRef content, ConstStringRef placeholder) - : content_(content), placeholder_(placeholder) {} +InputBase::InputBase(StringRef content, + ConstStringRef placeholder, + ConstRef option) + : content_(content), placeholder_(placeholder), option_(option) {} // Component implementation. Element InputBase::Render() { @@ -97,7 +101,7 @@ bool InputBase::OnEvent(Event event) { return false; content_->erase(cursor_position - 1, 1); cursor_position--; - on_change(); + option_->on_change(); return true; } @@ -106,13 +110,13 @@ bool InputBase::OnEvent(Event event) { if (cursor_position == int(content_->size())) return false; content_->erase(cursor_position, 1); - on_change(); + option_->on_change(); return true; } // Enter. if (event == Event::Return) { - on_enter(); + option_->on_enter(); return true; } @@ -144,7 +148,7 @@ bool InputBase::OnEvent(Event event) { if (event.is_character()) { content_->insert(cursor_position, 1, event.character()); cursor_position++; - on_change(); + option_->on_change(); return true; } return false; @@ -166,7 +170,7 @@ bool InputBase::OnMouseEvent(Event event) { std::max(0, std::min(content_->size(), new_cursor_position)); if (cursor_position != new_cursor_position) { cursor_position = new_cursor_position; - on_change(); + option_->on_change(); } } return true;