diff --git a/Business/Menus.cs b/Business/Menus.cs index 0bb0560..f216975 100644 --- a/Business/Menus.cs +++ b/Business/Menus.cs @@ -206,12 +206,12 @@ namespace SystemTrayMenu.Business waitToOpenMenu.CloseMenu += CloseMenu; void CloseMenu(int level) { - if (level < menus.Count() && menus[level] != null) + if (level < menus.Length && menus[level] != null) { HideOldMenu(menus[level]); } - if (level - 1 < menus.Count() && menus[level - 1] != null) + if (level - 1 < menus.Length && menus[level - 1] != null) { menus[level - 1].FocusTextBox(); } @@ -571,7 +571,7 @@ namespace SystemTrayMenu.Business if (FileUrl.GetDefaultBrowserPath(out string browserPath)) { - IconReader.GetFileIconWithCache(browserPath, true, true, out bool loading); + IconReader.GetFileIconWithCache(browserPath, true, true, out _); } } @@ -612,7 +612,7 @@ namespace SystemTrayMenu.Business { string path = rowData.FileInfo.FullName; int directoryNameBegin = path.LastIndexOf(@"\", StringComparison.InvariantCulture) + 1; - rowData.SetText(path.Substring(directoryNameBegin)); + rowData.SetText(path[directoryNameBegin..]); } else { @@ -1054,7 +1054,7 @@ namespace SystemTrayMenu.Business searchTextChanging = false; // if any open menu close - if (menu.Level + 1 < menus.Count()) + if (menu.Level + 1 < menus.Length) { Menu menuToClose = menus[menu.Level + 1]; if (menuToClose != null) diff --git a/Config/Config.cs b/Config/Config.cs index 2ca6b47..640b47b 100644 --- a/Config/Config.cs +++ b/Config/Config.cs @@ -73,17 +73,15 @@ namespace SystemTrayMenu public static void SetFolderByUser(bool save = true) { - using (FolderDialog dialog = new FolderDialog()) - { - dialog.InitialFolder = Path; + using FolderDialog dialog = new FolderDialog(); + dialog.InitialFolder = Path; - if (dialog.ShowDialog() == DialogResult.OK) + if (dialog.ShowDialog() == DialogResult.OK) + { + Settings.Default.PathDirectory = dialog.Folder; + if (save) { - Settings.Default.PathDirectory = dialog.Folder; - if (save) - { - Settings.Default.Save(); - } + Settings.Default.Save(); } } } @@ -424,12 +422,10 @@ namespace SystemTrayMenu str = str.Replace("#585858", htmlColorCode); byteArray = Encoding.UTF8.GetBytes(str); - using (MemoryStream stream = new MemoryStream(byteArray)) - { - SvgDocument svgDocument = SvgDocument.Open(stream); - svgDocument.Color = new SvgColourServer(Color.Black); - return svgDocument.Draw(); - } + using MemoryStream stream = new MemoryStream(byteArray); + SvgDocument svgDocument = SvgDocument.Open(stream); + svgDocument.Color = new SvgColourServer(Color.Black); + return svgDocument.Draw(); } private static bool IsRegistryValueThisValue(string keyName, string valueName, string value) diff --git a/DataClasses/RowData.cs b/DataClasses/RowData.cs index e48af33..b923cc3 100644 --- a/DataClasses/RowData.cs +++ b/DataClasses/RowData.cs @@ -144,7 +144,7 @@ namespace SystemTrayMenu.DataClasses try { icon = IconReader.GetFileIconWithCache( - TargetFilePath, + TargetFilePathOrig, showOverlay, true, out bool loading); @@ -153,7 +153,7 @@ namespace SystemTrayMenu.DataClasses } catch (Exception ex) { - Log.Warn($"path:'{TargetFilePath}'", ex); + Log.Warn($"path:'{TargetFilePathOrig}'", ex); } } } @@ -235,14 +235,8 @@ namespace SystemTrayMenu.DataClasses showOverlay = true; } - string path = TargetFilePath; - if (ContainsMenu) - { - path = TargetFilePathOrig; - } - icon = IconReader.GetFileIconWithCache( - path, + TargetFilePathOrig, showOverlay, false, out bool loading); diff --git a/Helpers/DragDropHelper.cs b/Helpers/DragDropHelper.cs index 5ad739e..8bc47a6 100644 --- a/Helpers/DragDropHelper.cs +++ b/Helpers/DragDropHelper.cs @@ -96,16 +96,14 @@ namespace SystemTrayMenu.Helpers return value; } - using (StreamWriter writer = new StreamWriter(pathToStoreFile + "\\" + title.Trim() + ".url")) - { - writer.WriteLine("[InternetShortcut]"); - writer.WriteLine($"URL={url.TrimEnd('\0')}"); - writer.WriteLine("IconIndex=0"); - writer.WriteLine($"HotKey=0"); - writer.WriteLine($"IDList="); - writer.WriteLine($"IconFile={pathIcon}"); - writer.Flush(); - } + using StreamWriter writer = new StreamWriter(pathToStoreFile + "\\" + title.Trim() + ".url"); + writer.WriteLine("[InternetShortcut]"); + writer.WriteLine($"URL={url.TrimEnd('\0')}"); + writer.WriteLine("IconIndex=0"); + writer.WriteLine($"HotKey=0"); + writer.WriteLine($"IDList="); + writer.WriteLine($"IconFile={pathIcon}"); + writer.Flush(); } } } diff --git a/Helpers/ImagingHelper.cs b/Helpers/ImagingHelper.cs index 2a7f8db..b6b5cf1 100644 --- a/Helpers/ImagingHelper.cs +++ b/Helpers/ImagingHelper.cs @@ -42,56 +42,54 @@ namespace SystemTrayMenu.Helpers if (newBitmap != null) { // save the resized png into a memory stream for future use - using (MemoryStream memoryStream = new MemoryStream()) + using MemoryStream memoryStream = new MemoryStream(); + newBitmap.Save(memoryStream, ImageFormat.Png); + + BinaryWriter iconWriter = new BinaryWriter(output); + if (output != null && iconWriter != null) { - newBitmap.Save(memoryStream, ImageFormat.Png); + // 0-1 reserved, 0 + iconWriter.Write((byte)0); + iconWriter.Write((byte)0); - BinaryWriter iconWriter = new BinaryWriter(output); - if (output != null && iconWriter != null) - { - // 0-1 reserved, 0 - iconWriter.Write((byte)0); - iconWriter.Write((byte)0); + // 2-3 image type, 1 = icon, 2 = cursor + iconWriter.Write((short)1); - // 2-3 image type, 1 = icon, 2 = cursor - iconWriter.Write((short)1); + // 4-5 number of images + iconWriter.Write((short)1); - // 4-5 number of images - iconWriter.Write((short)1); + // image entry 1 + // 0 image width + iconWriter.Write((byte)width); - // image entry 1 - // 0 image width - iconWriter.Write((byte)width); + // 1 image height + iconWriter.Write((byte)height); - // 1 image height - iconWriter.Write((byte)height); + // 2 number of colors + iconWriter.Write((byte)0); - // 2 number of colors - iconWriter.Write((byte)0); + // 3 reserved + iconWriter.Write((byte)0); - // 3 reserved - iconWriter.Write((byte)0); + // 4-5 color planes + iconWriter.Write((short)0); - // 4-5 color planes - iconWriter.Write((short)0); + // 6-7 bits per pixel + iconWriter.Write((short)32); - // 6-7 bits per pixel - iconWriter.Write((short)32); + // 8-11 size of image data + iconWriter.Write((int)memoryStream.Length); - // 8-11 size of image data - iconWriter.Write((int)memoryStream.Length); + // 12-15 offset of image data + iconWriter.Write(6 + 16); - // 12-15 offset of image data - iconWriter.Write(6 + 16); + // write image data + // png data must contain the whole png data file + iconWriter.Write(memoryStream.ToArray()); - // write image data - // png data must contain the whole png data file - iconWriter.Write(memoryStream.ToArray()); + iconWriter.Flush(); - iconWriter.Flush(); - - return true; - } + return true; } } @@ -111,11 +109,9 @@ namespace SystemTrayMenu.Helpers /// Wether or not the icon was succesfully generated. public static bool ConvertToIcon(string inputPath, string outputPath, int size = 16, bool preserveAspectRatio = false) { - using (FileStream inputStream = new FileStream(inputPath, FileMode.Open)) - using (FileStream outputStream = new FileStream(outputPath, FileMode.OpenOrCreate)) - { - return ConvertToIcon(inputStream, outputStream, size, preserveAspectRatio); - } + using FileStream inputStream = new FileStream(inputPath, FileMode.Open); + using FileStream outputStream = new FileStream(outputPath, FileMode.OpenOrCreate); + return ConvertToIcon(inputStream, outputStream, size, preserveAspectRatio); } public static Image RotateImage(Image img, float rotationAngle) diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 3350939..b119f4d 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -39,5 +39,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.20.5")] -[assembly: AssemblyFileVersion("1.0.20.5")] +[assembly: AssemblyVersion("1.0.21.0")] +[assembly: AssemblyFileVersion("1.0.21.0")] diff --git a/Properties/Resources.Designer.cs b/Properties/Resources.Designer.cs index 8fd10b9..24e81c0 100644 --- a/Properties/Resources.Designer.cs +++ b/Properties/Resources.Designer.cs @@ -140,16 +140,6 @@ namespace SystemTrayMenu.Properties { } } - /// - /// Looks up a localized resource of type System.Drawing.Icon similar to (Icon). - /// - public static System.Drawing.Icon search { - get { - object obj = ResourceManager.GetObject("search", resourceCulture); - return ((System.Drawing.Icon)(obj)); - } - } - /// /// Looks up a localized resource of type System.Drawing.Icon similar to (Icon). /// diff --git a/Properties/Resources.resx b/Properties/Resources.resx index 6ede001..556eced 100644 --- a/Properties/Resources.resx +++ b/Properties/Resources.resx @@ -124,9 +124,6 @@ ..\Resources\SystemTrayMenu.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a - - ..\Resources\search.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a - ..\Resources\ic_fluent_pin_48_filled.svg;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 diff --git a/Resources/search.ico b/Resources/search.ico deleted file mode 100644 index 8f22a4f..0000000 Binary files a/Resources/search.ico and /dev/null differ diff --git a/UserInterface/AppNotifyIcon.cs b/UserInterface/AppNotifyIcon.cs index 51dbb85..10fed5c 100644 --- a/UserInterface/AppNotifyIcon.cs +++ b/UserInterface/AppNotifyIcon.cs @@ -111,12 +111,10 @@ namespace SystemTrayMenu.UserInterface { if (threadsLoading) { - rotationAngle = rotationAngle + 5; - using (Bitmap bitmapLoading = new Bitmap(ImagingHelper.RotateImage(LoadingIcon.ToBitmap(), rotationAngle))) - { - DisposeIconIfNotDefaultIcon(); - notifyIcon.Icon = Icon.FromHandle(bitmapLoading.GetHicon()); - } + rotationAngle += 5; + using Bitmap bitmapLoading = new Bitmap(ImagingHelper.RotateImage(LoadingIcon.ToBitmap(), rotationAngle)); + DisposeIconIfNotDefaultIcon(); + notifyIcon.Icon = Icon.FromHandle(bitmapLoading.GetHicon()); } else { diff --git a/UserInterface/HotkeyTextboxControl/HotkeyControl.cs b/UserInterface/HotkeyTextboxControl/HotkeyControl.cs index a58f7a0..6caeb2a 100644 --- a/UserInterface/HotkeyTextboxControl/HotkeyControl.cs +++ b/UserInterface/HotkeyTextboxControl/HotkeyControl.cs @@ -393,7 +393,7 @@ namespace SystemTrayMenu.UserInterface.HotkeyTextboxControl return "* " + keyString; } - keyString = keyString.Substring(0, 1).ToUpperInvariant() + keyString.Substring(1).ToLowerInvariant(); + keyString = keyString.Substring(0, 1).ToUpperInvariant() + keyString[1..].ToLowerInvariant(); } return keyString + " *"; @@ -406,7 +406,7 @@ namespace SystemTrayMenu.UserInterface.HotkeyTextboxControl return "/ " + keyString; } - keyString = keyString.Substring(0, 1).ToUpperInvariant() + keyString.Substring(1).ToLowerInvariant(); + keyString = keyString.Substring(0, 1).ToUpperInvariant() + keyString[1..].ToLowerInvariant(); } return keyString + " /"; @@ -444,7 +444,7 @@ namespace SystemTrayMenu.UserInterface.HotkeyTextboxControl string visibleName = keyName.ToString(); if (visibleName.Length > 1) { - visibleName = visibleName.Substring(0, 1) + visibleName.Substring(1).ToLowerInvariant(); + visibleName = visibleName.Substring(0, 1) + visibleName[1..].ToLowerInvariant(); } return visibleName; diff --git a/UserInterface/Menu.cs b/UserInterface/Menu.cs index 5a7fddb..af43fbe 100644 --- a/UserInterface/Menu.cs +++ b/UserInterface/Menu.cs @@ -18,7 +18,6 @@ namespace SystemTrayMenu.UserInterface internal partial class Menu : Form { private static readonly Icon LoadingIcon = Properties.Resources.Loading; - private static readonly Icon Search = Properties.Resources.search; private readonly Fading fading = new Fading(); private bool isShowing; private bool directionToRight; @@ -789,7 +788,7 @@ namespace SystemTrayMenu.UserInterface private void LoadingMenu_Paint(object sender, PaintEventArgs e) { PictureBox pictureBox = (PictureBox)sender; - rotationAngle = rotationAngle + 5; + rotationAngle += 5; e.Graphics.DrawImage( ImagingHelper.RotateImage(LoadingIcon.ToBitmap(), rotationAngle), new Rectangle(Point.Empty, new Size(pictureBox.ClientSize.Width - 2, pictureBox.ClientSize.Height - 2))); diff --git a/UserInterface/SettingsForm.cs b/UserInterface/SettingsForm.cs index 2b92fec..80c571d 100644 --- a/UserInterface/SettingsForm.cs +++ b/UserInterface/SettingsForm.cs @@ -256,7 +256,7 @@ namespace SystemTrayMenu.UserInterface decimal newValue = numericUpDownSizeInPercentage.Value; if (e.Delta > 0) { - newValue = newValue + numericUpDownSizeInPercentage.Increment; + newValue += numericUpDownSizeInPercentage.Increment; if (newValue > numericUpDownSizeInPercentage.Maximum) { newValue = (int)numericUpDownSizeInPercentage.Maximum; @@ -264,7 +264,7 @@ namespace SystemTrayMenu.UserInterface } else { - newValue = newValue - numericUpDownSizeInPercentage.Increment; + newValue -= numericUpDownSizeInPercentage.Increment; if (newValue < numericUpDownSizeInPercentage.Minimum) { newValue = (int)numericUpDownSizeInPercentage.Minimum; @@ -473,6 +473,83 @@ namespace SystemTrayMenu.UserInterface return success; } + private static void AdjustControlMultilineIfNecessary(Control control) + { + if (control.Width > control.Parent.Width) + { + control.MaximumSize = new Size(control.Parent.Width, 0); + control.MinimumSize = new Size(0, control.Height * 2); + } + } + + private static void AddPossibilityToSelectFolderByWindowsContextMenu() + { + RegistryKey registryKeyContextMenu = null; + RegistryKey registryKeyContextMenuCommand = null; + + try + { + registryKeyContextMenu = Registry.CurrentUser.CreateSubKey(MenuName); + string binLocation = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; + if (registryKeyContextMenu != null) + { + registryKeyContextMenu.SetValue(string.Empty, Translator.GetText("Set as SystemTrayMenu folder")); + registryKeyContextMenu.SetValue("Icon", binLocation); + } + + registryKeyContextMenuCommand = Registry.CurrentUser.CreateSubKey(Command); + + if (registryKeyContextMenuCommand != null) + { + registryKeyContextMenuCommand.SetValue(string.Empty, binLocation + " \"%1\""); + } + + Settings.Default.PossibilityToSelectFolderByWindowsContextMenu = true; + } + catch (Exception ex) + { + Log.Warn("SavePossibilityToSelectFolderByWindowsContextMenu failed", ex); + } + finally + { + if (registryKeyContextMenu != null) + { + registryKeyContextMenu.Close(); + } + + if (registryKeyContextMenuCommand != null) + { + registryKeyContextMenuCommand.Close(); + } + } + } + + private static void RemovePossibilityToSelectFolderByWindowsContextMenu() + { + try + { + RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(Command); + if (registryKey != null) + { + registryKey.Close(); + Registry.CurrentUser.DeleteSubKey(Command); + } + + registryKey = Registry.CurrentUser.OpenSubKey(MenuName); + if (registryKey != null) + { + registryKey.Close(); + Registry.CurrentUser.DeleteSubKey(MenuName); + } + + Settings.Default.PossibilityToSelectFolderByWindowsContextMenu = false; + } + catch (Exception ex) + { + Log.Warn("DeletePossibilityToSelectFolderByWindowsContextMenu failed", ex); + } + } + private void SettingsForm_Load(object sender, EventArgs e) { AdjustControlMultilineIfNecessary(checkBoxStayOpenWhenFocusLost); @@ -482,15 +559,6 @@ namespace SystemTrayMenu.UserInterface tableLayoutPanelGeneral.Size.Height + 50); } - private void AdjustControlMultilineIfNecessary(Control control) - { - if (control.Width > control.Parent.Width) - { - control.MaximumSize = new Size(control.Parent.Width, 0); - control.MinimumSize = new Size(0, control.Height * 2); - } - } - private void ButtonOk_Click(object sender, EventArgs e) { Settings.Default.UseIconFromRootFolder = @@ -573,74 +641,6 @@ namespace SystemTrayMenu.UserInterface Close(); } - private void AddPossibilityToSelectFolderByWindowsContextMenu() - { - RegistryKey registryKeyContextMenu = null; - RegistryKey registryKeyContextMenuCommand = null; - - try - { - registryKeyContextMenu = Registry.CurrentUser.CreateSubKey(MenuName); - string binLocation = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; - if (registryKeyContextMenu != null) - { - registryKeyContextMenu.SetValue(string.Empty, Translator.GetText("Set as SystemTrayMenu folder")); - registryKeyContextMenu.SetValue("Icon", binLocation); - } - - registryKeyContextMenuCommand = Registry.CurrentUser.CreateSubKey(Command); - - if (registryKeyContextMenuCommand != null) - { - registryKeyContextMenuCommand.SetValue(string.Empty, binLocation + " \"%1\""); - } - - Settings.Default.PossibilityToSelectFolderByWindowsContextMenu = true; - } - catch (Exception ex) - { - Log.Warn("SavePossibilityToSelectFolderByWindowsContextMenu failed", ex); - } - finally - { - if (registryKeyContextMenu != null) - { - registryKeyContextMenu.Close(); - } - - if (registryKeyContextMenuCommand != null) - { - registryKeyContextMenuCommand.Close(); - } - } - } - - private void RemovePossibilityToSelectFolderByWindowsContextMenu() - { - try - { - RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(Command); - if (registryKey != null) - { - registryKey.Close(); - Registry.CurrentUser.DeleteSubKey(Command); - } - - registryKey = Registry.CurrentUser.OpenSubKey(MenuName); - if (registryKey != null) - { - registryKey.Close(); - Registry.CurrentUser.DeleteSubKey(MenuName); - } - - Settings.Default.PossibilityToSelectFolderByWindowsContextMenu = false; - } - catch (Exception ex) - { - Log.Warn("DeletePossibilityToSelectFolderByWindowsContextMenu failed", ex); - } - } - private void ButtonHotkeyDefault_Click(object sender, EventArgs e) { textBoxHotkey.SetHotkey("Ctrl+LWin"); diff --git a/Utilities/File/FileLnk.cs b/Utilities/File/FileLnk.cs index b1ca89f..23ac8ed 100644 --- a/Utilities/File/FileLnk.cs +++ b/Utilities/File/FileLnk.cs @@ -38,7 +38,7 @@ namespace SystemTrayMenu.Utilities public static bool IsNetworkRoot(string path) { return path.StartsWith(@"\\", StringComparison.InvariantCulture) && - !path.Substring(2).Contains(@"\", StringComparison.InvariantCulture); + !path[2..].Contains(@"\", StringComparison.InvariantCulture); } public static bool PingHost(string nameOrAddress) diff --git a/Utilities/File/IconReader.cs b/Utilities/File/IconReader.cs index bc8b004..4ccb93e 100644 --- a/Utilities/File/IconReader.cs +++ b/Utilities/File/IconReader.cs @@ -101,7 +101,6 @@ namespace SystemTrayMenu.Utilities return icon; } - [System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2008:Do not create tasks without passing a TaskScheduler", Justification = "todo")] public static Icon GetFolderIconSTA( string directoryPath, FolderType folderType, @@ -210,7 +209,6 @@ namespace SystemTrayMenu.Utilities return isExtensionWithSameIcon; } - [System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2008:Do not create tasks without passing a TaskScheduler", Justification = "todo")] private static Icon GetFileIconSTA(string filePath, bool linkOverlay, IconSize size = IconSize.Small) { Icon icon = null; diff --git a/Utilities/Log.cs b/Utilities/Log.cs index 0819902..93a22dd 100644 --- a/Utilities/Log.cs +++ b/Utilities/Log.cs @@ -110,7 +110,7 @@ namespace SystemTrayMenu.Utilities if (ex.Message == "The system cannot find the file specified.") { new Thread(ShowProblemWithShortcut).Start(); - void ShowProblemWithShortcut() + static void ShowProblemWithShortcut() { _ = MessageBox.Show( Translator.GetText("The item that this shortcut refers to has been changed or moved, so this shortcut will no longer work properly."),