mirror of
https://github.com/HarbourMasters/Shipwright
synced 2026-08-20 14:34:50 -04:00
Keep an unlisted combobox value from killing the menu (#7062)
A stored value with no entry in the combo map threw out of map::at while drawing, which took the whole menu down as soon as a search matched the widget. Fall back to the default and log the offender. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -107,6 +107,18 @@ template <typename T>
|
||||
bool Combobox(std::string label, T* value, const std::map<T, const char*>& comboMap,
|
||||
const ComboboxOptions& options = {}) {
|
||||
bool dirty = false;
|
||||
|
||||
if (comboMap.empty()) {
|
||||
return dirty;
|
||||
}
|
||||
// A value with no entry (a stale config, a map that has since changed) must not throw out of at() below.
|
||||
if (!comboMap.contains(*value)) {
|
||||
SPDLOG_WARN("Combobox \"{}\" holds unlisted value {}, showing the default instead", label,
|
||||
static_cast<int32_t>(*value));
|
||||
T fallback = static_cast<T>(options.defaultIndex);
|
||||
*value = comboMap.contains(fallback) ? fallback : comboMap.begin()->first;
|
||||
}
|
||||
|
||||
float startX = ImGui::GetCursorPosX();
|
||||
std::string invisibleLabelStr = "##" + std::string(label);
|
||||
const char* invisibleLabel = invisibleLabelStr.c_str();
|
||||
|
||||
Reference in New Issue
Block a user