From 500c2be4bc3869a87ba3c317e105b457745a9c34 Mon Sep 17 00:00:00 2001 From: pablohc Date: Wed, 15 Apr 2026 02:03:57 +0200 Subject: [PATCH] fix(keyboard): reset state on re-entry, defensive bounds checks, deduplicate navigation --- src/activities/util/KeyboardEntryActivity.cpp | 51 +++++++++---------- src/activities/util/KeyboardEntryActivity.h | 1 + 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/activities/util/KeyboardEntryActivity.cpp b/src/activities/util/KeyboardEntryActivity.cpp index d5520e0c..f75f53df 100644 --- a/src/activities/util/KeyboardEntryActivity.cpp +++ b/src/activities/util/KeyboardEntryActivity.cpp @@ -11,6 +11,13 @@ const char* const KeyboardEntryActivity::shiftString[2] = {"shift", "SHIFT"}; void KeyboardEntryActivity::onEnter() { Activity::onEnter(); cursorPos = text.length(); + symMode = false; + urlMode = false; + cursorMode = false; + passwordVisible = false; + shiftState = 0; + selectedRow = 0; + selectedCol = 0; requestUpdate(); } @@ -58,6 +65,7 @@ char KeyboardEntryActivity::getAlternativeChar() const { bool KeyboardEntryActivity::insertChar(char c) { if (c == '\0') return true; if (maxLength != 0 && text.length() >= maxLength) return true; + if (cursorPos > text.length()) cursorPos = text.length(); text.insert(cursorPos, 1, c); cursorPos++; @@ -65,7 +73,10 @@ bool KeyboardEntryActivity::insertChar(char c) { } void KeyboardEntryActivity::insertString(const std::string& str) { + if (str.empty()) return; if (maxLength != 0 && text.length() + str.length() > maxLength) return; + if (cursorPos > text.length()) cursorPos = text.length(); + text.insert(cursorPos, str); cursorPos += str.length(); } @@ -133,6 +144,16 @@ bool KeyboardEntryActivity::handleKeyPress() { return insertChar(getSelectedChar()); } +void KeyboardEntryActivity::mapColContentBottom(int& col, bool goingUp) const { + if (urlMode) { + col = goingUp ? col - 1 : col + 1; + if (col < 0) col = 0; + if (col >= 3) col = 2; + } else { + col = goingUp ? col * 2 : col / 2; + } +} + void KeyboardEntryActivity::loop() { const int totalRows = getTotalRowCount(); @@ -154,20 +175,9 @@ void KeyboardEntryActivity::loop() { const int contentCols = getContentColCount(); selectedRow = ButtonNavigator::previousIndex(selectedRow, totalRows); if (wasBottom && !isBottomRow(selectedRow)) { - if (urlMode) { - selectedCol = selectedCol - 1; - if (selectedCol < 0) selectedCol = 0; - if (selectedCol >= 3) selectedCol = 2; - } else { - selectedCol = selectedCol * 2; - } + mapColContentBottom(selectedCol, true); } else if (!wasBottom && isBottomRow(selectedRow)) { - if (urlMode) { - selectedCol = selectedCol + 1; - if (selectedCol > BOTTOM_KEY_COUNT - 1) selectedCol = BOTTOM_KEY_COUNT - 1; - } else { - selectedCol = selectedCol / 2; - } + mapColContentBottom(selectedCol, false); } int maxCol = isBottomRow(selectedRow) ? BOTTOM_KEY_COUNT - 1 : contentCols - 1; if (selectedCol > maxCol) selectedCol = maxCol; @@ -198,20 +208,9 @@ void KeyboardEntryActivity::loop() { const int contentCols = getContentColCount(); selectedRow = ButtonNavigator::nextIndex(selectedRow, totalRows); if (wasBottom && !isBottomRow(selectedRow)) { - if (urlMode) { - selectedCol = selectedCol - 1; - if (selectedCol < 0) selectedCol = 0; - if (selectedCol >= 3) selectedCol = 2; - } else { - selectedCol = selectedCol * 2; - } + mapColContentBottom(selectedCol, true); } else if (!wasBottom && isBottomRow(selectedRow)) { - if (urlMode) { - selectedCol = selectedCol + 1; - if (selectedCol > BOTTOM_KEY_COUNT - 1) selectedCol = BOTTOM_KEY_COUNT - 1; - } else { - selectedCol = selectedCol / 2; - } + mapColContentBottom(selectedCol, false); } int maxCol = isBottomRow(selectedRow) ? BOTTOM_KEY_COUNT - 1 : contentCols - 1; if (selectedCol > maxCol) selectedCol = maxCol; diff --git a/src/activities/util/KeyboardEntryActivity.h b/src/activities/util/KeyboardEntryActivity.h index 95f9d324..cdaf3675 100644 --- a/src/activities/util/KeyboardEntryActivity.h +++ b/src/activities/util/KeyboardEntryActivity.h @@ -170,4 +170,5 @@ class KeyboardEntryActivity : public Activity { bool handleKeyPress(); bool insertChar(char c); void insertString(const std::string& str); + void mapColContentBottom(int& col, bool goingUp) const; };