mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2024-11-23 03:10:01 +08:00
Fix on_enter bug in ftxui::Input (#667)
Fixed:https://github.com/ArthurSonzogni/FTXUI/issues/666
This commit is contained in:
parent
7b7177b59c
commit
ff5817b8a6
@ -341,27 +341,19 @@ class InputBase : public ComponentBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool HandleReturn() {
|
bool HandleReturn() {
|
||||||
int& cursor_position = option_->cursor_position();
|
if (option_->multiline()) {
|
||||||
content_->insert(cursor_position, "\n");
|
HandleCharacter("\n");
|
||||||
cursor_position++;
|
}
|
||||||
option_->on_change();
|
option_->on_enter();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HandleCharacter(const std::string& character) {
|
bool HandleCharacter(const std::string& character) {
|
||||||
if (character == "\n" && !option_->multiline()) {
|
|
||||||
option_->on_enter();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int& cursor_position = option_->cursor_position();
|
int& cursor_position = option_->cursor_position();
|
||||||
content_->insert(cursor_position, character);
|
content_->insert(cursor_position, character);
|
||||||
cursor_position += character.size();
|
cursor_position += character.size();
|
||||||
option_->on_change();
|
option_->on_change();
|
||||||
|
|
||||||
if (character == "\n") {
|
|
||||||
option_->on_enter();
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -369,6 +361,9 @@ class InputBase : public ComponentBase {
|
|||||||
int& cursor_position = option_->cursor_position();
|
int& cursor_position = option_->cursor_position();
|
||||||
cursor_position = util::clamp(cursor_position, 0, (int)content_->size());
|
cursor_position = util::clamp(cursor_position, 0, (int)content_->size());
|
||||||
|
|
||||||
|
if (event == Event::Return) {
|
||||||
|
return HandleReturn();
|
||||||
|
}
|
||||||
if (event.is_character()) {
|
if (event.is_character()) {
|
||||||
return HandleCharacter(event.character());
|
return HandleCharacter(event.character());
|
||||||
}
|
}
|
||||||
@ -405,9 +400,6 @@ class InputBase : public ComponentBase {
|
|||||||
if (event == Event::ArrowRightCtrl) {
|
if (event == Event::ArrowRightCtrl) {
|
||||||
return HandleRightCtrl();
|
return HandleRightCtrl();
|
||||||
}
|
}
|
||||||
if (event == Event::Return) {
|
|
||||||
return HandleReturn();
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -722,6 +722,18 @@ TEST(InputTest, MouseClickComplex) {
|
|||||||
EXPECT_EQ(option.cursor_position(), 17);
|
EXPECT_EQ(option.cursor_position(), 17);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(InputTest, OnEnter) {
|
||||||
|
std::string content;
|
||||||
|
auto option = InputOption();
|
||||||
|
bool on_enter_called = false;
|
||||||
|
option.on_enter = [&] { on_enter_called = true; };
|
||||||
|
Component input = Input(&content, &option);
|
||||||
|
|
||||||
|
EXPECT_FALSE(on_enter_called);
|
||||||
|
EXPECT_TRUE(input->OnEvent(Event::Return));
|
||||||
|
EXPECT_TRUE(on_enter_called);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace ftxui
|
} // namespace ftxui
|
||||||
|
|
||||||
// Copyright 2023 Arthur Sonzogni. All rights reserved.
|
// Copyright 2023 Arthur Sonzogni. All rights reserved.
|
||||||
|
Loading…
Reference in New Issue
Block a user