From d1d11c5e3567d01614235777afc9e6c066c04bf0 Mon Sep 17 00:00:00 2001 From: "oleg.shilo" Date: Fri, 6 Jan 2023 12:23:46 +1100 Subject: [PATCH] - Issue #12: Icon too big on nested context menu --- shell-x/GenericExtensions.cs | 8 +++++++- shell-x/Program.cs | 10 +++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/shell-x/GenericExtensions.cs b/shell-x/GenericExtensions.cs index cb1649f..e679eb2 100644 --- a/shell-x/GenericExtensions.cs +++ b/shell-x/GenericExtensions.cs @@ -95,7 +95,13 @@ public static string ToFileMenuText(this string path) => path.GetFileName() .Replace(".bat", "") .Replace(".ps1", ""); - public static string ToDirMenuText(this string path) => path.GetFileName().Split(new[] { '.' }, 2).Last(); + public static string ToDirMenuText(this string path) => path.GetFileName().Split(new[] { '.' }, 2).Last(); + + public static int ToStandardIconSize(this int customSize) + { + var standardSizes = new[] { 16, 32, 48, 256 }; + return standardSizes.OrderBy(x => Math.Abs((long)x - customSize)).First(); + } public static string GetFileNameWithoutExtension(this string path) => Path.GetFileNameWithoutExtension(path); diff --git a/shell-x/Program.cs b/shell-x/Program.cs index 70ee13d..da6eee7 100644 --- a/shell-x/Program.cs +++ b/shell-x/Program.cs @@ -293,6 +293,13 @@ internal static ToolStripItem[] BuildMenuFrom(string configDir, string invokeArg Image = LookupImageFor(item) }; + try + { + var size = parentMenu.ContentRectangle.Height.ToStandardIconSize(); + parentMenu.Image = parentMenu.Image?.Resize(size, size); + } + catch { } + current.AddItem(parentMenu); dirsToProcess.Enqueue(new BuildItem { @@ -316,7 +323,8 @@ internal static ToolStripItem[] BuildMenuFrom(string configDir, string invokeArg try { - menu.Image = menu.Image?.Resize(menu.ContentRectangle.Height, menu.ContentRectangle.Height); + var size = menu.ContentRectangle.Height.ToStandardIconSize(); + menu.Image = menu.Image?.Resize(size, size); } catch { }