[Feature] Fix warnings / clean code (#215), version 1.0.21.0

This commit is contained in:
Markus Hofknecht 2021-10-10 17:33:54 +02:00
parent 30312cc025
commit 4919aa9afd
16 changed files with 155 additions and 189 deletions

View file

@ -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)

View file

@ -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<SvgDocument>(stream);
svgDocument.Color = new SvgColourServer(Color.Black);
return svgDocument.Draw();
}
using MemoryStream stream = new MemoryStream(byteArray);
SvgDocument svgDocument = SvgDocument.Open<SvgDocument>(stream);
svgDocument.Color = new SvgColourServer(Color.Black);
return svgDocument.Draw();
}
private static bool IsRegistryValueThisValue(string keyName, string valueName, string value)

View file

@ -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);

View file

@ -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();
}
}
}

View file

@ -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
/// <returns>Wether or not the icon was succesfully generated.</returns>
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)

View file

@ -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")]

View file

@ -140,16 +140,6 @@ namespace SystemTrayMenu.Properties {
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Icon similar to (Icon).
/// </summary>
public static System.Drawing.Icon search {
get {
object obj = ResourceManager.GetObject("search", resourceCulture);
return ((System.Drawing.Icon)(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Icon similar to (Icon).
/// </summary>

View file

@ -124,9 +124,6 @@
<data name="SystemTrayMenu" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\SystemTrayMenu.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="search" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\search.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="ic_fluent_pin_48_filled" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\ic_fluent_pin_48_filled.svg;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 114 KiB

View file

@ -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
{

View file

@ -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;

View file

@ -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)));

View file

@ -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");

View file

@ -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)

View file

@ -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;

View file

@ -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."),