fix: 修复节点列表滚动数组越界崩溃\n\n- scrollDown() 添加最大边界检查\n- 渲染函数中添加 startIndex 边界检查\n- 切换模式时重置滚动位置"

This commit is contained in:
2026-03-30 21:02:03 +08:00
parent ee6a843349
commit c37b0d794f
@@ -67,26 +67,33 @@ NodeListMode getCurrentMode()
void switchToNextMode() void switchToNextMode()
{ {
currentMode = static_cast<NodeListMode>((currentMode + 1) % MODE_COUNT); currentMode = static_cast<NodeListMode>((currentMode + 1) % MODE_COUNT);
scrollIndex = 0; // 切换模式时重置滚动位置
} }
void switchToPrevMode() void switchToPrevMode()
{ {
currentMode = static_cast<NodeListMode>((currentMode + MODE_COUNT - 1) % MODE_COUNT); currentMode = static_cast<NodeListMode>((currentMode + MODE_COUNT - 1) % MODE_COUNT);
scrollIndex = 0; // 切换模式时重置滚动位置
} }
// 滚动控制函数 // 滚动控制函数(带边界检查)
void scrollUp() void scrollUp()
{ {
if (scrollIndex > 0) {
scrollIndex--; scrollIndex--;
if (scrollIndex < 0) {
scrollIndex = 0;
} }
} }
void scrollDown() void scrollDown()
{ {
// 获取节点数量并计算最大滚动值
int totalEntries = nodeDB->getNumMeshNodes();
int visibleNodeRows = 5; // 假设可见行数
int maxScroll = calculateMaxScroll(totalEntries, visibleNodeRows);
if (scrollIndex < maxScroll) {
scrollIndex++; scrollIndex++;
// 最大滚动值在渲染时计算,这里只做简单限制 }
} }
// 处理 UP 按键:单击滚动,双击切换模式 // 处理 UP 按键:单击滚动,双击切换模式
@@ -532,6 +539,13 @@ void drawNodeListScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t
int totalColumns = 2; int totalColumns = 2;
#endif #endif
int startIndex = scrollIndex * visibleNodeRows * totalColumns; int startIndex = scrollIndex * visibleNodeRows * totalColumns;
// 边界检查:确保 startIndex 不超出范围
if (startIndex >= totalEntries) {
startIndex = 0;
scrollIndex = 0; // 重置滚动位置
}
if (nodeDB->getMeshNodeByIndex(startIndex)->num == nodeDB->getNodeNum()) { if (nodeDB->getMeshNodeByIndex(startIndex)->num == nodeDB->getNodeNum()) {
startIndex++; // skip own node startIndex++; // skip own node
} }