mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2024-11-22 10:40:00 +08:00
Add option for input.
This commit is contained in:
parent
2b7daf061f
commit
33b3d1c7ab
@ -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<std::wstring> 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
|
||||
|
@ -30,7 +30,9 @@ Component Button(ConstStringRef label,
|
||||
Component Checkbox(ConstStringRef label,
|
||||
bool* checked,
|
||||
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,
|
||||
int* selected_,
|
||||
ConstRef<MenuOption> = {});
|
||||
|
@ -31,6 +31,11 @@ struct CheckboxOption {
|
||||
std::function<void()> on_change = []() {};
|
||||
};
|
||||
|
||||
struct InputOption {
|
||||
std::function<void()> on_change = [] {};
|
||||
std::function<void()> on_enter = [] {};
|
||||
};
|
||||
|
||||
}; // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_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<InputOption> option = {});
|
||||
~InputBase() override = default;
|
||||
|
||||
// State.
|
||||
int cursor_position = 0;
|
||||
|
||||
// State update callback.
|
||||
std::function<void()> on_change = [] {};
|
||||
std::function<void()> 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<InputOption> option_;
|
||||
};
|
||||
|
||||
} // namespace ftxui
|
||||
|
@ -31,8 +31,10 @@ namespace ftxui {
|
||||
/// ```bash
|
||||
/// placeholder
|
||||
/// ```
|
||||
Component Input(StringRef content, ConstStringRef placeholder) {
|
||||
return Make<InputBase>(content, placeholder);
|
||||
Component Input(StringRef content,
|
||||
ConstStringRef placeholder,
|
||||
ConstRef<InputOption> option) {
|
||||
return Make<InputBase>(content, placeholder, std::move(option));
|
||||
}
|
||||
|
||||
// static
|
||||
@ -40,8 +42,10 @@ InputBase* InputBase::From(Component component) {
|
||||
return static_cast<InputBase*>(component.get());
|
||||
}
|
||||
|
||||
InputBase::InputBase(StringRef content, ConstStringRef placeholder)
|
||||
: content_(content), placeholder_(placeholder) {}
|
||||
InputBase::InputBase(StringRef content,
|
||||
ConstStringRef placeholder,
|
||||
ConstRef<InputOption> 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<int>(content_->size(), new_cursor_position));
|
||||
if (cursor_position != new_cursor_position) {
|
||||
cursor_position = new_cursor_position;
|
||||
on_change();
|
||||
option_->on_change();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
Loading…
Reference in New Issue
Block a user