Add option for input.

This commit is contained in:
ArthurSonzogni 2021-07-08 00:01:42 +02:00 committed by Arthur Sonzogni
parent 2b7daf061f
commit 33b3d1c7ab
5 changed files with 33 additions and 22 deletions

View File

@ -156,13 +156,19 @@ int main(int argc, const char* argv[]) {
false, false,
false, false,
}; };
std::wstring input_add_content;
Component input_add = Input(&input_add_content, "input files");
std::vector<std::wstring> input_entries; std::vector<std::wstring> input_entries;
int input_selected = 0; int input_selected = 0;
Component input = Menu(&input_entries, &input_selected); 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""; std::wstring executable_content_ = L"";
Component executable_ = Input(&executable_content_, "executable"); 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 = [&] { auto render_command = [&] {
Elements line; Elements line;
// Compiler // Compiler

View File

@ -30,7 +30,9 @@ Component Button(ConstStringRef label,
Component Checkbox(ConstStringRef label, Component Checkbox(ConstStringRef label,
bool* checked, bool* checked,
ConstRef<CheckboxOption> option = {}); ConstRef<CheckboxOption> option = {});
Component Input(StringRef content, ConstStringRef placeholder); Component Input(StringRef content,
ConstStringRef placeholder,
ConstRef<InputOption> option = {});
Component Menu(const std::vector<std::wstring>* entries, Component Menu(const std::vector<std::wstring>* entries,
int* selected_, int* selected_,
ConstRef<MenuOption> = {}); ConstRef<MenuOption> = {});

View File

@ -31,6 +31,11 @@ struct CheckboxOption {
std::function<void()> on_change = []() {}; std::function<void()> on_change = []() {};
}; };
struct InputOption {
std::function<void()> on_change = [] {};
std::function<void()> on_enter = [] {};
};
}; // namespace ftxui }; // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */ #endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */

View File

@ -21,16 +21,14 @@ class InputBase : public ComponentBase {
static InputBase* From(Component component); static InputBase* From(Component component);
// Constructor. // Constructor.
InputBase(StringRef content, ConstStringRef placeholder); InputBase(StringRef content,
ConstStringRef placeholder,
ConstRef<InputOption> option = {});
~InputBase() override = default; ~InputBase() override = default;
// State. // State.
int cursor_position = 0; int cursor_position = 0;
// State update callback.
std::function<void()> on_change = [] {};
std::function<void()> on_enter = [] {};
// Component implementation. // Component implementation.
Element Render() override; Element Render() override;
bool OnEvent(Event) override; bool OnEvent(Event) override;
@ -42,6 +40,7 @@ class InputBase : public ComponentBase {
bool OnMouseEvent(Event); bool OnMouseEvent(Event);
Box input_box_; Box input_box_;
Box cursor_box_; Box cursor_box_;
ConstRef<InputOption> option_;
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -31,8 +31,10 @@ namespace ftxui {
/// ```bash /// ```bash
/// placeholder /// placeholder
/// ``` /// ```
Component Input(StringRef content, ConstStringRef placeholder) { Component Input(StringRef content,
return Make<InputBase>(content, placeholder); ConstStringRef placeholder,
ConstRef<InputOption> option) {
return Make<InputBase>(content, placeholder, std::move(option));
} }
// static // static
@ -40,8 +42,10 @@ InputBase* InputBase::From(Component component) {
return static_cast<InputBase*>(component.get()); return static_cast<InputBase*>(component.get());
} }
InputBase::InputBase(StringRef content, ConstStringRef placeholder) InputBase::InputBase(StringRef content,
: content_(content), placeholder_(placeholder) {} ConstStringRef placeholder,
ConstRef<InputOption> option)
: content_(content), placeholder_(placeholder), option_(option) {}
// Component implementation. // Component implementation.
Element InputBase::Render() { Element InputBase::Render() {
@ -97,7 +101,7 @@ bool InputBase::OnEvent(Event event) {
return false; return false;
content_->erase(cursor_position - 1, 1); content_->erase(cursor_position - 1, 1);
cursor_position--; cursor_position--;
on_change(); option_->on_change();
return true; return true;
} }
@ -106,13 +110,13 @@ bool InputBase::OnEvent(Event event) {
if (cursor_position == int(content_->size())) if (cursor_position == int(content_->size()))
return false; return false;
content_->erase(cursor_position, 1); content_->erase(cursor_position, 1);
on_change(); option_->on_change();
return true; return true;
} }
// Enter. // Enter.
if (event == Event::Return) { if (event == Event::Return) {
on_enter(); option_->on_enter();
return true; return true;
} }
@ -144,7 +148,7 @@ bool InputBase::OnEvent(Event event) {
if (event.is_character()) { if (event.is_character()) {
content_->insert(cursor_position, 1, event.character()); content_->insert(cursor_position, 1, event.character());
cursor_position++; cursor_position++;
on_change(); option_->on_change();
return true; return true;
} }
return false; return false;
@ -166,7 +170,7 @@ bool InputBase::OnMouseEvent(Event event) {
std::max(0, std::min<int>(content_->size(), new_cursor_position)); std::max(0, std::min<int>(content_->size(), new_cursor_position));
if (cursor_position != new_cursor_position) { if (cursor_position != new_cursor_position) {
cursor_position = new_cursor_position; cursor_position = new_cursor_position;
on_change(); option_->on_change();
} }
} }
return true; return true;