This change should make the UI Icon Size setting dynamic, clearing all cached icons when the size changes (in the settings UI) so if they have a dynamic size the best is selected.

This commit is contained in:
RKrom 2014-11-11 13:23:59 +01:00
parent c50e5fa5ab
commit 61c2921b2a
16 changed files with 140 additions and 123 deletions

View file

@ -25,6 +25,7 @@ using GreenshotPlugin.UnmanagedHelpers;
using log4net;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
@ -37,6 +38,7 @@ namespace GreenshotPlugin.Core {
private static readonly ILog LOG = LogManager.GetLogger(typeof(PluginUtils));
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private const string PATH_KEY = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\";
private static IDictionary<string, Image> exeIconCache = new Dictionary<string, Image>();
/// <summary>
/// Simple global property to get the Greenshot host
@ -46,6 +48,21 @@ namespace GreenshotPlugin.Core {
set;
}
public static void ClearExeIconCache() {
List<Image> cachedImages = new List<Image>();
lock (exeIconCache) {
foreach (string key in exeIconCache.Keys) {
cachedImages.Add(exeIconCache[key]);
}
exeIconCache.Clear();
}
foreach (Image cachedImage in cachedImages) {
if (cachedImage != null) {
cachedImage.Dispose();
}
}
}
/// <summary>
/// Get the path of an executable
/// </summary>
@ -70,6 +87,28 @@ namespace GreenshotPlugin.Core {
}
return null;
}
/// <summary>
/// Get icon for executable, from the cache
/// </summary>
/// <param name="path">path to the exe or dll</param>
/// <param name="index">index of the icon</param>
/// <returns>Bitmap with the icon or null if something happended</returns>
public static Image GetCachedExeIcon(string path, int index) {
string cacheKey = string.Format("{0}:{1}", path, index);
Image returnValue;
if (!exeIconCache.TryGetValue(cacheKey, out returnValue)) {
lock (exeIconCache) {
if (!exeIconCache.TryGetValue(cacheKey, out returnValue)) {
returnValue = GetExeIcon(path, index);
if (returnValue != null) {
exeIconCache.Add(cacheKey, returnValue);
}
}
}
}
return returnValue;
}
/// <summary>
/// Get icon for executable
@ -77,7 +116,7 @@ namespace GreenshotPlugin.Core {
/// <param name="path">path to the exe or dll</param>
/// <param name="index">index of the icon</param>
/// <returns>Bitmap with the icon or null if something happended</returns>
public static Bitmap GetExeIcon(string path, int index) {
private static Bitmap GetExeIcon(string path, int index) {
if (!File.Exists(path)) {
return null;
}