GUI: Fix incorrect SaveLoad dialog state after updating the save list

Updating the list reset the selection in the list widget, however if
a save had previously been selected and the Choose button was enabled,
it remained enabled despite no save being selected. Trying to load
the game resulted in a crash. This was particularly an issue with
cloud enabled as if you are unlucky you could have tried to load a
save just as the cloud sync finished, which updated the list and
unselected the save. This change fixes bug #9766: Assert in
SaveLoadChooser dialog.

In addition to adding a sanity check on the selected index for the
Choose command, this commit also preserves the selection when
updating the list as I think this would be the expected behaviour
in this dialog.
This commit is contained in:
Thierry Crozat 2017-11-24 22:21:44 +00:00
parent 150f70f94c
commit 469e75cb64

View file

@ -428,11 +428,13 @@ void SaveLoadChooserSimple::handleCommand(CommandSender *sender, uint32 cmd, uin
break; break;
case kChooseCmd: case kChooseCmd:
_list->endEditMode(); _list->endEditMode();
if (selItem >= 0) {
if (!_saveList.empty()) { if (!_saveList.empty()) {
setResult(_saveList[selItem].getSaveSlot()); setResult(_saveList[selItem].getSaveSlot());
_resultString = _list->getSelectedString(); _resultString = _list->getSelectedString();
} }
close(); close();
}
break; break;
case kListSelectionChangedCmd: case kListSelectionChangedCmd:
updateSelection(true); updateSelection(true);
@ -694,7 +696,13 @@ void SaveLoadChooserSimple::updateSaveList() {
colors.push_back(ThemeEngine::kFontColorNormal); colors.push_back(ThemeEngine::kFontColorNormal);
} }
int selected = _list->getSelected();
_list->setList(saveNames, &colors); _list->setList(saveNames, &colors);
if (selected >= 0 && selected < saveNames.size())
_list->setSelected(selected);
else
_chooseButton->setEnabled(false);
draw(); draw();
} }