From 66987ef42a777cbfabd90359dbcb54e8fab81066 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sun, 10 Apr 2016 16:02:55 +0200 Subject: [PATCH] Added caching of menu icon sizes --- Site5/Controls/v5/Base/MainMenu.ascx.cs | 27 ++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/Site5/Controls/v5/Base/MainMenu.ascx.cs b/Site5/Controls/v5/Base/MainMenu.ascx.cs index 872303ad..3615f9f2 100644 --- a/Site5/Controls/v5/Base/MainMenu.ascx.cs +++ b/Site5/Controls/v5/Base/MainMenu.ascx.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Web.UI; @@ -19,6 +20,8 @@ namespace Swarmops.Controls.Base private string _regularsLocalized; private string _regularshipLocalized; + private static Dictionary _imageSizeLookup = new Dictionary(); + protected void Page_Load (object sender, EventArgs e) { ParticipantTitle regular = CurrentOrganization.RegularLabel; @@ -98,11 +101,29 @@ namespace Swarmops.Controls.Base cssClass = " dir=\"ltr\""; } - string iconSize = "40px"; + string iconSize = string.Empty; - if (File.Exists (Server.MapPath ("~/Images/PageIcons/" + menuItem.ImageUrl + "-20px.png"))) + if (menuItem.ImageUrl != null && _imageSizeLookup.ContainsKey (menuItem.ImageUrl)) { - iconSize = "20px"; + iconSize = _imageSizeLookup[menuItem.ImageUrl]; // from cache + } + else if (menuItem.ImageUrl != null) + { + string[] iconSizePreferences = { "40", "96", "128", "20", "16" }; + + foreach (string testSize in iconSizePreferences) + { + if ( + File.Exists ( + Server.MapPath ("~/Images/PageIcons/" + menuItem.ImageUrl + "-" + testSize + "px.png"))) + { + iconSize = testSize + "px"; + _imageSizeLookup[menuItem.ImageUrl] = iconSize; + break; // break at first located preference + } + } + + // Let it break if none is found, that'll generate a broken image and be visible immediately } switch (menuItem.Type)