Skip to content

Commit

Permalink
- Investigation for #22
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-shilo committed Feb 4, 2023
1 parent 6de21a4 commit 5342cbe
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 21 deletions.
Binary file modified bin/shell-x.exe
Binary file not shown.
85 changes: 64 additions & 21 deletions shell-x/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,34 @@ protected override ContextMenuStrip CreateMenu()

var menu = new ContextMenuStrip();
menu.Items.AddRange(items);

// DisposeLastMenu();
// lastOpenedMenu = menu;

return menu;
}

static ContextMenuStrip lastOpenedMenu;

void DisposeLastMenu()
{
if (lastOpenedMenu != null)
{
foreach (var item in lastOpenedMenu.Items.OfType<ToolStripMenuItem>())
{
try
{
var img = item.Image;
item.Image = null;
img?.Dispose();
}
catch { }
}
// lastOpenedMenu.Close();
// lastOpenedMenu.Dispose();
}
}

static Image LookupImageFor(string path)
{
return LookupImageFor(path, ".ico") ??
Expand All @@ -273,8 +298,6 @@ static Image LookupImageFor(string path, string imgExtension)
return img;
}

static List<Image> LoadedImages = new List<Image>();

internal static ToolStripItem[] BuildMenuFrom(string configDir, string invokeArguments)
{
var menus = new List<ToolStripItem>();
Expand Down Expand Up @@ -306,7 +329,7 @@ internal static ToolStripItem[] BuildMenuFrom(string configDir, string invokeArg
try
{
var size = parentMenu.ContentRectangle.Height.ToStandardIconSize();
parentMenu.Image = parentMenu.Image?.Resize(size, size);
parentMenu.Image = parentMenu.Image?.Resize(size, size, dispose: true);
}
catch { }

Expand Down Expand Up @@ -334,7 +357,8 @@ internal static ToolStripItem[] BuildMenuFrom(string configDir, string invokeArg
try
{
var size = menu.ContentRectangle.Height.ToStandardIconSize();
menu.Image = menu.Image?.Resize(size, size);

menu.Image = menu.Image?.Resize(size, size, dispose: true);
}
catch { }

Expand All @@ -353,21 +377,23 @@ internal static ToolStripItem[] BuildMenuFrom(string configDir, string invokeArg

public static void Cleanup()
{
LoadedImages.ForEach(i => i.Dispose());

var except = Process.GetCurrentProcess().Id.ToString();
Directory.GetFiles(App.ConfigDir.PathJoin(".run").EnsureDirectory(), "*.*.ps1")
.Select(x => new { path = x, pid = x.GetFileName().Split('.').First() })
.Where(x => x.pid != except)
.ToList()
.ForEach(x =>
{
try
try
{
var except = Process.GetCurrentProcess().Id.ToString();
Directory.GetFiles(App.ConfigDir.PathJoin(".run").EnsureDirectory(), "*.*.ps1")
.Select(x => new { path = x, pid = x.GetFileName().Split('.').First() })
.Where(x => x.pid != except)
.ToList()
.ForEach(x =>
{
File.Delete(x.path);
}
catch { }
});
try
{
File.Delete(x.path);
}
catch { }
});
}
catch { }
}

public static string CloneScript(string script)
Expand Down Expand Up @@ -487,18 +513,33 @@ string GetConfigDirFor(string file)

static class Utils
{
static Dictionary<string, Image> loadedImages = new Dictionary<string, Image>();

public static Image ReadImage(this string file)
{
var imageId = $"{file}{File.GetLastWriteTimeUtc(file).ToFileTimeUtc()}";

if (loadedImages.ContainsKey(imageId))
return loadedImages[imageId];

using (var ms = new MemoryStream(File.ReadAllBytes(file)))
{
Image image;
if (string.Compare(Path.GetExtension(file), ".ico", StringComparison.OrdinalIgnoreCase) == 0)
return new Icon(ms).ToBitmap();
{
using (var temp = new Icon(ms))
image = temp.ToBitmap();
}
else
return Image.FromStream(ms);
{
image = Image.FromStream(ms);
}
loadedImages[imageId] = image;
return image;
}
}

public static Bitmap Resize(this Image image, int width, int height)
public static Bitmap Resize(this Image image, int width, int height, bool dispose)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
Expand All @@ -520,6 +561,8 @@ public static Bitmap Resize(this Image image, int width, int height)
}
}

if (dispose)
image.Dispose();
return destImage;
}
}

0 comments on commit 5342cbe

Please sign in to comment.