From 1f188944083ca4a4fc79973da1fc9841d2a7f241 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 24 Nov 2024 06:51:58 +0000 Subject: [PATCH] Change: Determine industry directory width only on visible rows. (#13097) When rebuilding the industry directory list, the width of every item in the list is obtained to get the maximum width required for the horizontal scrollbar. This can take considerable time if there are a lot of industries. Instead, calculate only for the visible rows, and grow as needed. --- src/industry_gui.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 45c6569d90..737f224da7 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -1437,11 +1437,12 @@ protected: */ uint GetIndustryListWidth() const { - uint width = 0; - for (const Industry *i : this->industries) { - width = std::max(width, GetStringBoundingBox(this->GetIndustryString(i)).width); + uint width = this->hscroll->GetCount(); + auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->industries); + for (auto it = first; it != last; ++it) { + width = std::max(width, GetStringBoundingBox(this->GetIndustryString(*it)).width); } - return width + WidgetDimensions::scaled.framerect.Horizontal(); + return width; } /** (Re)Build industries list */ @@ -1467,7 +1468,6 @@ protected: this->industries.Filter(filter); - this->hscroll->SetCount(this->GetIndustryListWidth()); this->vscroll->SetCount(this->industries.size()); // Update scrollbar as well. } @@ -1682,6 +1682,7 @@ public: void OnInit() override { this->SetCargoFilterArray(); + this->hscroll->SetCount(0); } void SetStringParameters(WidgetID widget) const override @@ -1875,6 +1876,7 @@ public: void OnPaint() override { if (this->industries.NeedRebuild()) this->BuildSortIndustriesList(); + this->hscroll->SetCount(this->GetIndustryListWidth()); this->DrawWidgets(); }