Skip to content

Commit ee2edb0

Browse files
committed
Keep the selection when sorting
1 parent de9fa90 commit ee2edb0

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/fheroes2/dialog/dialog_selectfile.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ namespace
343343

344344
MapsFileInfoList::iterator it = lists.begin();
345345
for ( ; it != lists.end(); ++it ) {
346-
if ( ( *it ).filename == lastfile ) {
346+
if ( it->filename == lastfile ) {
347347
break;
348348
}
349349
}
@@ -422,12 +422,11 @@ namespace
422422
const size_t lengthLimit{ 255 };
423423

424424
LocalEvent & le = LocalEvent::Get();
425-
bool listUpdated = false;
426425

427426
while ( le.HandleEvents() && result.empty() ) {
428427
buttonOk.drawOnState( le.isMouseLeftButtonPressedAndHeldInArea( buttonOk.area() ) );
429428
buttonCancel.drawOnState( le.isMouseLeftButtonPressedAndHeldInArea( buttonCancel.area() ) );
430-
buttonSort.drawOnState(le.isMouseLeftButtonPressedAndHeldInArea(buttonSort.area()));
429+
buttonSort.drawOnState( le.isMouseLeftButtonPressedAndHeldInArea( buttonSort.area() ) );
431430
if ( isEditing ) {
432431
buttonVirtualKB->drawOnState( le.isMouseLeftButtonPressedAndHeldInArea( buttonVirtualKB->area() ) );
433432
}
@@ -443,6 +442,7 @@ namespace
443442
bool isListboxSelected = listbox.isSelected();
444443

445444
bool needRedraw = ( listId != listbox.getCurrentId() );
445+
bool listUpdated = false;
446446

447447
if ( le.isKeyPressed( fheroes2::Key::KEY_DELETE ) && isListboxSelected ) {
448448
listbox.SetCurrent( listId );
@@ -483,10 +483,31 @@ namespace
483483
}
484484
}
485485
else if ( le.MouseClickLeft( buttonSort.area() ) ) {
486+
const int currentId = listbox.getCurrentId();
487+
486488
settings.CycleSaveFileSortType();
487489
(void)settings.Save( Settings::configFileName );
488490
sortMapInfos( lists );
489491
listUpdated = true;
492+
493+
// re-select the last selected file if any, unless we're typing in the list box
494+
if ( !filename.empty() && !isListboxSelected ) {
495+
const std::string lastChoice = System::concatPath( Game::GetSaveDir(), filename + Game::GetSaveFileExtension() );
496+
497+
MapsFileInfoList::const_iterator it = lists.cbegin();
498+
for ( ; it != lists.end(); ++it ) {
499+
if ( it->filename == lastChoice ) {
500+
break;
501+
}
502+
}
503+
if ( it != lists.cend() ) {
504+
const int newId = static_cast<int>( std::distance( lists.cbegin(), it ) );
505+
if ( newId != currentId ) {
506+
listbox.SetCurrent( newId );
507+
needRedraw = true;
508+
}
509+
}
510+
}
490511
}
491512
else if ( isEditing ) {
492513
assert( textInput != nullptr );

0 commit comments

Comments
 (0)