From 6d18e980e1b19e2782b597cc8fbf1865698d103e Mon Sep 17 00:00:00 2001 From: Andrew Lindesay Date: Wed, 3 Oct 2018 15:38:45 +0200 Subject: [PATCH] HaikuDepot : Performance for Package List Updates Performance improvements for the application updating lists of packages when the user is operating with the locale set to Russian. fixes #14513 Change-Id: I1e2514a2afbd43503ac0edfe280a856411738026 Reviewed-on: https://review.haiku-os.org/612 Reviewed-by: waddlesplash --- src/apps/haikudepot/ui/MainWindow.cpp | 6 ++++ src/apps/haikudepot/ui/PackageListView.cpp | 33 ++++++++++++++-------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/apps/haikudepot/ui/MainWindow.cpp b/src/apps/haikudepot/ui/MainWindow.cpp index 7cfe02ca40..ef6f874940 100644 --- a/src/apps/haikudepot/ui/MainWindow.cpp +++ b/src/apps/haikudepot/ui/MainWindow.cpp @@ -936,6 +936,9 @@ MainWindow::_RefreshPackageList(bool force) if (fSinglePackageMode) return; + if (Logger::IsDebugEnabled()) + printf("will refresh the package list\n"); + BPackageRoster roster; BStringList repositoryNames; @@ -1221,6 +1224,9 @@ MainWindow::_RefreshPackageList(bool force) printf("Unknown exception occurred while resolving system " "dependencies.\n"); } + + if (Logger::IsDebugEnabled()) + printf("did refresh the package list\n"); } diff --git a/src/apps/haikudepot/ui/PackageListView.cpp b/src/apps/haikudepot/ui/PackageListView.cpp index c6106ad4d9..22851aa475 100644 --- a/src/apps/haikudepot/ui/PackageListView.cpp +++ b/src/apps/haikudepot/ui/PackageListView.cpp @@ -1,4 +1,5 @@ /* + * Copyright 2018, Andrew Lindesay, . * Copyright 2017, Julian Harnath, . * Copyright 2015, Axel Dörfler, . * Copyright 2013-2014, Stephan Aßmus . @@ -663,12 +664,16 @@ public: SetViewUIColor(B_PANEL_BACKGROUND_COLOR); SetLowUIColor(ViewUIColor()); SetHighUIColor(LowUIColor(), B_DARKEN_4_TINT); + + // constantly calculating the size is expensive so here a sensible + // upper limit on the number of packages is arbitrarily chosen. + fMinSize = BSize(StringWidth(_DeriveLabel(999999)) + 10, + B_H_SCROLL_BAR_HEIGHT); } virtual BSize MinSize() { - BString label(_GetLabel()); - return BSize(StringWidth(label) + 10, B_H_SCROLL_BAR_HEIGHT); + return fMinSize; } virtual BSize PreferredSize() @@ -685,13 +690,11 @@ public: { FillRect(updateRect, B_SOLID_LOW); - BString label(_GetLabel()); - font_height fontHeight; GetFontHeight(&fontHeight); BRect bounds(Bounds()); - float width = StringWidth(label); + float width = StringWidth(fLabel); BPoint offset; offset.x = bounds.left + (bounds.Width() - width) / 2.0f; @@ -699,32 +702,38 @@ public: - (fontHeight.ascent + fontHeight.descent)) / 2.0f + fontHeight.ascent; - DrawString(label, offset); + DrawString(fLabel, offset); } void SetItemCount(int32 count) { if (count == fItemCount) return; - BSize minSize = MinSize(); fItemCount = count; - if (minSize != MinSize()) - InvalidateLayout(); + fLabel = _DeriveLabel(fItemCount); Invalidate(); } private: - BString _GetLabel() const + +/*! This method is hit quite often when the list of packages in the + table-view are updated. Derivation of the plural for some + languages such as Russian can be slow so this method should be + called sparingly. +*/ + + BString _DeriveLabel(int32 count) const { static BStringFormat format(B_TRANSLATE("{0, plural, " "one{# item} other{# items}}")); - BString label; - format.Format(label, fItemCount); + format.Format(label, count); return label; } int32 fItemCount; + BString fLabel; + BSize fMinSize; };