From f2f7573c3fc9ab04ae2a2db4686a6e70a905eee0 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 12 May 2024 20:14:29 +0100 Subject: [PATCH] Fix #12668: Crash opening picker window with filter when no results available. (#12669) When first opening the picker window, we attempt to find a valid class and type to select. If the picker window was closed with filters enabled, there may not be anything list that is usable. Resolve this by using callbacks to find the first usable type when no types are listed. --- src/picker_gui.cpp | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/picker_gui.cpp b/src/picker_gui.cpp index 4cb2f16c55..ba221d22f4 100644 --- a/src/picker_gui.cpp +++ b/src/picker_gui.cpp @@ -475,8 +475,20 @@ void PickerWindow::EnsureSelectedClassIsValid() int class_index = this->callbacks.GetSelectedClass(); if (std::binary_search(std::begin(this->classes), std::end(this->classes), class_index)) return; - class_index = this->classes.front(); + if (!this->classes.empty()) { + class_index = this->classes.front(); + } else { + /* Classes can be empty if filters are enabled, find the first usable class. */ + int count = this->callbacks.GetClassCount(); + for (int i = 0; i < count; i++) { + if (this->callbacks.GetClassName(i) == INVALID_STRING_ID) continue; + class_index = i; + break; + } + } + this->callbacks.SetSelectedClass(class_index); + this->types.ForceRebuild(); } void PickerWindow::EnsureSelectedClassIsVisible() @@ -569,8 +581,18 @@ void PickerWindow::EnsureSelectedTypeIsValid() int index = this->callbacks.GetSelectedType(); if (std::any_of(std::begin(this->types), std::end(this->types), [class_index, index](const auto &item) { return item.class_index == class_index && item.index == index; })) return; - class_index = this->types.front().class_index; - index = this->types.front().index; + if (!this->types.empty()) { + class_index = this->types.front().class_index; + index = this->types.front().index; + } else { + /* Types can be empty if filters are enabled, find the first usable type. */ + int count = this->callbacks.GetTypeCount(class_index); + for (int i = 0; i < count; i++) { + if (this->callbacks.GetTypeName(class_index, i) == INVALID_STRING_ID) continue; + index = i; + break; + } + } this->callbacks.SetSelectedClass(class_index); this->callbacks.SetSelectedType(index); }