Skip to content

Cache icons so .exe files are not needed to be scanned on loading #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions Mint/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,38 @@ private void LoadOptions()
checkAutoStart.Checked = Options.CurrentOptions.AutoStart;
}

static string Sha256(string randomString)
{
var crypt = new System.Security.Cryptography.SHA256Managed();
var hash = new System.Text.StringBuilder();
byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(randomString));
foreach (byte theByte in crypto)
{
hash.Append(theByte.ToString("x2"));
}
return hash.ToString();
}

readonly static string _iconCache = Application.StartupPath + "\\.mint_cache";

private Bitmap ExtractOrLoadIcon(string exeFilePath)
{
_ = Directory.CreateDirectory(_iconCache);
string pathHash = Sha256(exeFilePath);
string iconPath = _iconCache + "\\" + pathHash + ".png";
if (File.Exists(iconPath))
{
return new Bitmap(iconPath);
}
else
{
Bitmap extracted = Icon.ExtractAssociatedIcon(exeFilePath).ToBitmap();
extracted.Save(iconPath);
return extracted;
}

}

private void BuildLauncherMenu()
{
launcherMenu.Items.Clear();
Expand Down Expand Up @@ -185,7 +217,7 @@ private void BuildLauncherMenu()

if (!string.IsNullOrEmpty(x.AppGroup))
{
subItem = new ToolStripMenuItem(x.AppTitle, !isDeadItem ? Icon.ExtractAssociatedIcon(x.AppLink).ToBitmap() : null);
subItem = new ToolStripMenuItem(x.AppTitle, !isDeadItem ? ExtractOrLoadIcon(x.AppLink) : null);
subItem.Click += subItem_Click;
if (!isDeadItem)
{
Expand All @@ -201,7 +233,7 @@ private void BuildLauncherMenu()
}
else
{
i = new ToolStripMenuItem(x.AppTitle, !isDeadItem ? (Icon.ExtractAssociatedIcon(x.AppLink)).ToBitmap() : null);
i = new ToolStripMenuItem(x.AppTitle, !isDeadItem ? (ExtractOrLoadIcon(x.AppLink)) : null);
if (!isDeadItem)
{
i.ForeColor = Color.GhostWhite;
Expand Down