fix(keyboard): reset state on re-entry, defensive bounds checks, deduplicate navigation

This commit is contained in:
pablohc
2026-04-15 12:00:19 +02:00
committed by jpirnay
parent 155650d23c
commit 500c2be4bc
2 changed files with 26 additions and 26 deletions
+25 -26
View File
@@ -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;
@@ -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;
};