textedit: removed useless code and added update()

This commit is contained in:
Raphael Coeffic
2022-08-06 08:43:35 +02:00
parent 9c4a0d78a9
commit 9767b4baf1
2 changed files with 115 additions and 112 deletions
+111 -107
View File
@@ -55,18 +55,22 @@ TextEdit::TextEdit(Window *parent, const rect_t &rect, char *value,
lv_textarea_set_password_mode(lvobj, false); lv_textarea_set_password_mode(lvobj, false);
lv_textarea_set_one_line(lvobj, true); lv_textarea_set_one_line(lvobj, true);
// value may not be null-terminated
std::string txt(value, length);
lv_textarea_set_text(lvobj, txt.c_str());
lv_textarea_set_placeholder_text(lvobj, "---"); lv_textarea_set_placeholder_text(lvobj, "---");
lv_textarea_set_max_length(lvobj, length); lv_textarea_set_max_length(lvobj, length);
if (width() == 0) { if (width() == 0) {
lv_obj_set_width(lvobj, LV_DPI_DEF); lv_obj_set_width(lvobj, LV_DPI_DEF);
} }
update();
} }
void TextEdit::update()
{
// value may not be null-terminated
std::string txt(value, length);
lv_textarea_set_text(lvobj, txt.c_str());
}
void TextEdit::setCursorPos(int cursorPos) void TextEdit::setCursorPos(int cursorPos)
{ {
@@ -91,123 +95,123 @@ void TextEdit::onEvent(event_t event)
if (IS_VIRTUAL_KEY_EVENT(event)) { if (IS_VIRTUAL_KEY_EVENT(event)) {
uint8_t c = event & 0xFF; uint8_t c = event & 0xFF;
if (c == (uint8_t)KEYBOARD_ENTER[0]) { if (c == LV_KEY_ENTER) {
changeEnd(); changeEnd();
} }
} }
#if defined(HARDWARE_KEYS) // #if defined(HARDWARE_KEYS)
if (editMode) { // if (editMode) {
int c = value[cursorPos]; // int c = value[cursorPos];
int v = c; // int v = c;
switch (event) { // switch (event) {
case EVT_ROTARY_RIGHT: // case EVT_ROTARY_RIGHT:
for (int i = 0; i < ROTARY_ENCODER_SPEED(); i++) { // for (int i = 0; i < ROTARY_ENCODER_SPEED(); i++) {
v = getNextChar(v); // v = getNextChar(v);
} // }
break; // break;
case EVT_ROTARY_LEFT: // case EVT_ROTARY_LEFT:
for (int i = 0; i < ROTARY_ENCODER_SPEED(); i++) { // for (int i = 0; i < ROTARY_ENCODER_SPEED(); i++) {
v = getPreviousChar(v); // v = getPreviousChar(v);
} // }
break; // break;
case EVT_KEY_BREAK(KEY_LEFT): // case EVT_KEY_BREAK(KEY_LEFT):
if (cursorPos > 0) { // if (cursorPos > 0) {
setCursorPos(cursorPos - 1); // setCursorPos(cursorPos - 1);
} // }
break; // break;
case EVT_KEY_BREAK(KEY_RIGHT): // case EVT_KEY_BREAK(KEY_RIGHT):
if (cursorPos < length - 1 && value[cursorPos + 1] != '\0') { // if (cursorPos < length - 1 && value[cursorPos + 1] != '\0') {
setCursorPos(cursorPos + 1); // setCursorPos(cursorPos + 1);
} // }
break; // break;
case EVT_KEY_BREAK(KEY_ENTER): // case EVT_KEY_BREAK(KEY_ENTER):
if (cursorPos < length - 1) { // if (cursorPos < length - 1) {
if (value[cursorPos] == '\0') { // if (value[cursorPos] == '\0') {
value[cursorPos] = ' '; // value[cursorPos] = ' ';
changed = true; // changed = true;
} // }
cursorPos++; // cursorPos++;
if (value[cursorPos] == '\0') { // if (value[cursorPos] == '\0') {
value[cursorPos] = ' '; // value[cursorPos] = ' ';
changed = true; // changed = true;
} // }
lv_textarea_set_text(lvobj, value); // update();
setCursorPos(cursorPos); // setCursorPos(cursorPos);
} // }
else { // else {
changeEnd(); // changeEnd();
FormField::onEvent(event); // FormField::onEvent(event);
} // }
break; // break;
case EVT_KEY_LONG(KEY_ENTER): // case EVT_KEY_LONG(KEY_ENTER):
{ // {
killEvents(event); // killEvents(event);
auto menu = new Menu(this); // auto menu = new Menu(this);
menu->setTitle(STR_EDIT); // menu->setTitle(STR_EDIT);
#if defined(CLIPBOARD) // #if defined(CLIPBOARD)
menu->addLine(STR_COPY, [=] { // menu->addLine(STR_COPY, [=] {
clipboard.write((uint8_t *)value, length, Clipboard::CONTENT_TEXT); // clipboard.write((uint8_t *)value, length, Clipboard::CONTENT_TEXT);
}); // });
if (clipboard.contentType == Clipboard::CONTENT_TEXT) { // if (clipboard.contentType == Clipboard::CONTENT_TEXT) {
menu->addLine(STR_PASTE, [=] { // menu->addLine(STR_PASTE, [=] {
clipboard.read((uint8_t *)value, length); // clipboard.read((uint8_t *)value, length);
changeEnd(true); // changeEnd(true);
}); // });
} // }
#endif // #endif
menu->addLine(STR_CLEAR, [=] { // menu->addLine(STR_CLEAR, [=] {
memset(value, 0, length); // memset(value, 0, length);
changeEnd(true); // changeEnd(true);
}); // });
break; // break;
} // }
case EVT_KEY_BREAK(KEY_UP): // case EVT_KEY_BREAK(KEY_UP):
v = toggleCase(v); // v = toggleCase(v);
break; // break;
case EVT_KEY_LONG(KEY_LEFT): // case EVT_KEY_LONG(KEY_LEFT):
case EVT_KEY_LONG(KEY_RIGHT): // case EVT_KEY_LONG(KEY_RIGHT):
v = toggleCase(v); // v = toggleCase(v);
if (event == EVT_KEY_LONG(KEY_LEFT)) { // if (event == EVT_KEY_LONG(KEY_LEFT)) {
killEvents(KEY_LEFT); // killEvents(KEY_LEFT);
} // }
break; // break;
case EVT_KEY_BREAK(KEY_PGDN): // case EVT_KEY_BREAK(KEY_PGDN):
if (cursorPos < length) { // if (cursorPos < length) {
memmove(&value[cursorPos], &value[cursorPos + 1], length - cursorPos - 1); // memmove(&value[cursorPos], &value[cursorPos + 1], length - cursorPos - 1);
value[length - 1] = '\0'; // value[length - 1] = '\0';
changed = true; // changed = true;
if (cursorPos > 0 && value[cursorPos] == '\0') { // if (cursorPos > 0 && value[cursorPos] == '\0') {
cursorPos = cursorPos - 1; // cursorPos = cursorPos - 1;
} // }
lv_textarea_set_text(lvobj, value); // update();
setCursorPos(cursorPos); // setCursorPos(cursorPos);
} // }
break; // break;
} // }
if (c != v) { // if (c != v) {
// TRACE("value[%d] = %d", cursorPos, v); // // TRACE("value[%d] = %d", cursorPos, v);
value[cursorPos] = v; // value[cursorPos] = v;
lv_textarea_set_text(lvobj, value); // update();
setCursorPos(cursorPos); // setCursorPos(cursorPos);
changed = true; // changed = true;
} // }
} // }
else { // else {
FormField::onEvent(event); // FormField::onEvent(event);
setCursorPos(0); // setCursorPos(0);
} // }
#endif // #endif
} }
void TextEdit::onClicked() void TextEdit::onClicked()
+4 -5
View File
@@ -27,7 +27,8 @@ class TextEdit : public FormField {
friend class TextKeyboard; friend class TextKeyboard;
public: public:
TextEdit(Window * parent, const rect_t & rect, char * value, uint8_t length, LcdFlags windowFlags = 0, const char * _extra_chars = nullptr); TextEdit(Window* parent, const rect_t& rect, char* value, uint8_t length,
LcdFlags windowFlags = 0, const char* _extra_chars = nullptr);
#if defined(DEBUG_WINDOWS) #if defined(DEBUG_WINDOWS)
std::string getName() const override std::string getName() const override
@@ -46,13 +47,11 @@ class TextEdit : public FormField {
return value; return value;
} }
void update();
void onEvent(event_t event) override; void onEvent(event_t event) override;
void onClicked() override; void onClicked() override;
// #if defined(HARDWARE_TOUCH)
// bool onTouchEnd(coord_t x, coord_t y) override;
// #endif
void onFocusLost() override; void onFocusLost() override;
virtual void paint(BitmapBuffer* dc) override {}; virtual void paint(BitmapBuffer* dc) override {};