add 'sub-consumer' to textbox

This commit is contained in:
MihailRis
2024-10-27 21:04:32 +03:00
parent 868b69db90
commit 699d8ce2bb
6 changed files with 37 additions and 5 deletions
+20 -4
View File
@@ -170,7 +170,9 @@ void TextBox::paste(const std::wstring& text) {
input.erase(std::remove(input.begin(), input.end(), '\r'), input.end());
refreshLabel();
setCaret(caret + text.length());
validate();
if (validate()) {
onInput();
}
}
/// @brief Remove part of the text and move caret to start of the part
@@ -470,6 +472,12 @@ void TextBox::stepDefaultUp(bool shiftPressed, bool breakSelection) {
}
}
void TextBox::onInput() {
if (subconsumer) {
subconsumer(input);
}
}
void TextBox::performEditingKeyboardEvents(keycode key) {
bool shiftPressed = Events::pressed(keycode::LEFT_SHIFT);
bool breakSelection = getSelectionLength() != 0 && !shiftPressed;
@@ -480,19 +488,23 @@ void TextBox::performEditingKeyboardEvents(keycode key) {
}
input = input.substr(0, caret-1) + input.substr(caret);
setCaret(caret-1);
validate();
if (validate()) {
onInput();
}
}
} else if (key == keycode::DELETE) {
if (!eraseSelected() && caret < input.length()) {
input = input.substr(0, caret) + input.substr(caret + 1);
validate();
if (validate()) {
onInput();
}
}
} else if (key == keycode::ENTER) {
if (multiline) {
paste(L"\n");
} else {
defocus();
if (validate() && consumer) {
if (validate()) {
consumer(label->getText());
}
}
@@ -591,6 +603,10 @@ void TextBox::setTextConsumer(wstringconsumer consumer) {
this->consumer = std::move(consumer);
}
void TextBox::setTextSubConsumer(wstringconsumer consumer) {
this->subconsumer = std::move(consumer);
}
void TextBox::setTextValidator(wstringchecker validator) {
this->validator = std::move(validator);
}