Code Analyze and Refactor 0.12 (#109), version 0.11.4.5

This commit is contained in:
Markus Hofknecht 2020-07-08 17:54:19 +02:00
parent d38750c0b9
commit 14cc6af244
10 changed files with 150 additions and 95 deletions

View file

@ -38,6 +38,8 @@ namespace SystemTrayMenu.Business
private readonly WaitLeave waitLeave = new WaitLeave(MenuDefines.TimeUntilClose); private readonly WaitLeave waitLeave = new WaitLeave(MenuDefines.TimeUntilClose);
private DateTime deactivatedTime = DateTime.MinValue; private DateTime deactivatedTime = DateTime.MinValue;
private OpenCloseState openCloseState = OpenCloseState.Default; private OpenCloseState openCloseState = OpenCloseState.Default;
private RowData loadingRowData = null;
private bool showingMessageBox = false;
public Menus() public Menus()
{ {
@ -65,11 +67,40 @@ namespace SystemTrayMenu.Business
keyboardInput.ResetSelectedByKey(); keyboardInput.ResetSelectedByKey();
LoadStopped(); LoadStopped();
MenuData menuData = (MenuData)e.Result; MenuData menuData = (MenuData)e.Result;
if (menuData.Validity == MenuDataValidity.Valid) switch (menuData.Validity)
{ {
DisposeMenu(menus[menuData.Level]); case MenuDataValidity.Valid:
menus[0] = Create(menuData, Path.GetFileName(Config.Path)); DisposeMenu(menus[menuData.Level]);
AsEnumerable.ToList().ForEach(m => { m.ShowWithFade(); }); menus[0] = Create(menuData, Path.GetFileName(Config.Path));
AsEnumerable.ToList().ForEach(m => { m.ShowWithFade(); });
break;
case MenuDataValidity.Empty:
if (!showingMessageBox)
{
showingMessageBox = true;
MessageBox.Show(Translator.GetText(
"MessageRootFolderEmpty"));
OpenFolder();
showingMessageBox = false;
}
break;
case MenuDataValidity.NoAccess:
if (!showingMessageBox)
{
showingMessageBox = true;
MessageBox.Show(Translator.GetText(
"MessageRootFolderNoAccess"));
OpenFolder();
showingMessageBox = false;
}
break;
case MenuDataValidity.AbortedOrUnknown:
Log.Info("MenuDataValidity.AbortedOrUnknown");
break;
default:
break;
} }
} }
@ -87,9 +118,11 @@ namespace SystemTrayMenu.Business
void StartLoadMenu(RowData rowData) void StartLoadMenu(RowData rowData)
{ {
if (menus[0].IsUsable && if (menus[0].IsUsable &&
loadingRowData != rowData &&
(menus[rowData.MenuLevel + 1] == null || (menus[rowData.MenuLevel + 1] == null ||
menus[rowData.MenuLevel + 1].Tag as RowData != rowData)) menus[rowData.MenuLevel + 1].Tag as RowData != rowData))
{ {
loadingRowData = rowData;
LoadStarted(); LoadStarted();
BackgroundWorker workerSubMenu = workersSubMenu. BackgroundWorker workerSubMenu = workersSubMenu.
Where(w => !w.IsBusy).FirstOrDefault(); Where(w => !w.IsBusy).FirstOrDefault();
@ -135,6 +168,8 @@ namespace SystemTrayMenu.Business
ShowSubMenu(menu); ShowSubMenu(menu);
} }
} }
loadingRowData = null;
} }
} }
@ -229,33 +264,48 @@ namespace SystemTrayMenu.Business
directories = GetDirectoriesInNetworkLocation(path); directories = GetDirectoriesInNetworkLocation(path);
static string[] GetDirectoriesInNetworkLocation(string networkLocationRootPath) static string[] GetDirectoriesInNetworkLocation(string networkLocationRootPath)
{ {
Process cmd = new Process(); List<string> directories = new List<string>();
cmd.StartInfo.FileName = "cmd.exe"; try
cmd.StartInfo.RedirectStandardInput = true; {
cmd.StartInfo.RedirectStandardOutput = true; Process cmd = new Process();
cmd.StartInfo.CreateNoWindow = true; cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.UseShellExecute = false; cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; cmd.StartInfo.RedirectStandardOutput = true;
cmd.Start(); cmd.StartInfo.CreateNoWindow = true;
cmd.StandardInput.WriteLine($"net view {networkLocationRootPath}"); cmd.StartInfo.UseShellExecute = false;
cmd.StandardInput.Flush(); cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.StandardInput.Close(); cmd.Start();
cmd.StandardInput.WriteLine($"net view {networkLocationRootPath}");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
string output = cmd.StandardOutput.ReadToEnd(); string output = cmd.StandardOutput.ReadToEnd();
cmd.WaitForExit(); cmd.WaitForExit();
cmd.Close(); cmd.Close();
output = output.Substring(output.LastIndexOf('-') + 2); output = output.Substring(output.LastIndexOf('-') + 2);
output = output.Substring(0, output.IndexOf("The command completed successfully.", StringComparison.InvariantCulture)); output = output.Substring(0, output.IndexOf("The command completed successfully.", StringComparison.InvariantCulture));
return foreach (string line in output
output .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) {
.Select(x => Path.Combine( int indexOfSecondColumnTypeDisk = line.IndexOf("Disk", StringComparison.InvariantCulture);
networkLocationRootPath, if (indexOfSecondColumnTypeDisk > 0)
x.Substring(0, x.IndexOf(' ', StringComparison.InvariantCulture)))) {
.ToArray(); string directory = Path.Combine(
networkLocationRootPath,
line.Substring(0, indexOfSecondColumnTypeDisk));
directories.Add(directory);
}
}
}
catch (ArgumentOutOfRangeException ex)
{
Log.Warn($"Could not resolve network root folder: {networkLocationRootPath}", ex);
}
return directories.ToArray();
} }
} }
else else
@ -515,6 +565,11 @@ namespace SystemTrayMenu.Business
Form.ActiveForm is UserInterface.TaskbarForm; Form.ActiveForm is UserInterface.TaskbarForm;
} }
private static void OpenFolder()
{
Log.ProcessStart("explorer.exe", Config.Path);
}
private Menu Create(MenuData menuData, string title = null) private Menu Create(MenuData menuData, string title = null)
{ {
Menu menu = new Menu(); Menu menu = new Menu();
@ -528,10 +583,6 @@ namespace SystemTrayMenu.Business
menu.SetTitle(title); menu.SetTitle(title);
menu.UserClickedOpenFolder += OpenFolder; menu.UserClickedOpenFolder += OpenFolder;
static void OpenFolder()
{
Log.ProcessStart("explorer.exe", Config.Path);
}
} }
menu.Level = menuData.Level; menu.Level = menuData.Level;
@ -674,7 +725,7 @@ namespace SystemTrayMenu.Business
row.DefaultCellStyle.SelectionBackColor = Color.White; row.DefaultCellStyle.SelectionBackColor = Color.White;
row.Selected = false; row.Selected = false;
} }
else if (rowData.IsMenuOpen && rowData.IsSelected) else if (rowData.IsContextMenuOpen || (rowData.IsMenuOpen && rowData.IsSelected))
{ {
row.Selected = true; row.Selected = true;
} }
@ -709,7 +760,7 @@ namespace SystemTrayMenu.Business
int width = dgv.Columns[0].Width + dgv.Columns[1].Width; int width = dgv.Columns[0].Width + dgv.Columns[1].Width;
Rectangle rowBounds = new Rectangle(0, e.RowBounds.Top, width, e.RowBounds.Height); Rectangle rowBounds = new Rectangle(0, e.RowBounds.Top, width, e.RowBounds.Height);
if (rowData.IsMenuOpen && rowData.IsSelected) if (rowData.IsContextMenuOpen || (rowData.IsMenuOpen && rowData.IsSelected))
{ {
ControlPaint.DrawBorder(e.Graphics, rowBounds, MenuDefines.ColorSelectedItemBorder, ButtonBorderStyle.Solid); ControlPaint.DrawBorder(e.Graphics, rowBounds, MenuDefines.ColorSelectedItemBorder, ButtonBorderStyle.Solid);
row.DefaultCellStyle.SelectionBackColor = MenuDefines.ColorSelectedItem; row.DefaultCellStyle.SelectionBackColor = MenuDefines.ColorSelectedItem;

View file

@ -177,9 +177,6 @@ namespace SystemTrayMenu.DataClasses
(DateTime.Now - contextMenuClosed).TotalMilliseconds > 200) (DateTime.Now - contextMenuClosed).TotalMilliseconds > 200)
{ {
IsContextMenuOpen = true; IsContextMenuOpen = true;
Color colorbefore = dgv.Rows[RowIndex].DefaultCellStyle.SelectionBackColor;
dgv.Rows[RowIndex].DefaultCellStyle.SelectionBackColor =
MenuDefines.ColorSelectedItem;
ShellContextMenu ctxMnu = new ShellContextMenu(); ShellContextMenu ctxMnu = new ShellContextMenu();
Point location = dgv.FindForm().Location; Point location = dgv.FindForm().Location;
@ -199,11 +196,6 @@ namespace SystemTrayMenu.DataClasses
ctxMnu.ShowContextMenu(arrFI, point); ctxMnu.ShowContextMenu(arrFI, point);
} }
if (!dgv.IsDisposed)
{
dgv.Rows[RowIndex].DefaultCellStyle.SelectionBackColor = colorbefore;
}
IsContextMenuOpen = false; IsContextMenuOpen = false;
contextMenuClosed = DateTime.Now; contextMenuClosed = DateTime.Now;
} }

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 // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.11.4.4")] [assembly: AssemblyVersion("0.11.4.5")]
[assembly: AssemblyFileVersion("0.11.4.4")] [assembly: AssemblyFileVersion("0.11.4.5")]

View file

@ -222,6 +222,24 @@ namespace SystemTrayMenu.Resources {
} }
} }
/// <summary>
/// Looks up a localized string similar to Your root folder for the SystemTrayMenu is empty! Put some files, folders or shortcuts into the folder or change the root folder..
/// </summary>
internal static string MessageRootFolderEmpty {
get {
return ResourceManager.GetString("MessageRootFolderEmpty", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to You have no access to the root folder for the SystemTrayMenu. Grant access to the folder or change the root folder..
/// </summary>
internal static string MessageRootFolderNoAccess {
get {
return ResourceManager.GetString("MessageRootFolderNoAccess", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to Restart. /// Looks up a localized string similar to Restart.
/// </summary> /// </summary>

View file

@ -189,4 +189,10 @@
<data name="Select Folder" xml:space="preserve"> <data name="Select Folder" xml:space="preserve">
<value>Select Folder</value> <value>Select Folder</value>
</data> </data>
<data name="MessageRootFolderEmpty" xml:space="preserve">
<value>Dein Hauptordner für die SystemTrayMenu ist leer! Pack ein paar Dateien, Ordner oder Verknüpfungen in den Hauptordner oder ändere den Hauptordner.</value>
</data>
<data name="MessageRootFolderNoAccess" xml:space="preserve">
<value>Du hast keine Rechte für den Hauptordner. Gewähre die Rechte oder ändere den Hauptordner.</value>
</data>
</root> </root>

View file

@ -189,4 +189,10 @@
<data name="Select Folder" xml:space="preserve"> <data name="Select Folder" xml:space="preserve">
<value>Select Folder</value> <value>Select Folder</value>
</data> </data>
<data name="MessageRootFolderEmpty" xml:space="preserve">
<value>Your root folder for the SystemTrayMenu is empty! Put some files, folders or shortcuts into the folder or change the root folder.</value>
</data>
<data name="MessageRootFolderNoAccess" xml:space="preserve">
<value>You have no access to the root folder for the SystemTrayMenu. Grant access to the folder or change the root folder.</value>
</data>
</root> </root>

View file

@ -197,6 +197,10 @@
</BootstrapperPackage> </BootstrapperPackage>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers.Unstable" Version="1.2.0.164"> <PackageReference Include="StyleCop.Analyzers.Unstable" Version="1.2.0.164">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View file

@ -24,8 +24,8 @@ namespace SystemTrayMenu.Utilities
public static class IconReader public static class IconReader
{ {
private static readonly ConcurrentDictionary<string, Icon> DictIconCache = new ConcurrentDictionary<string, Icon>(); private static readonly ConcurrentDictionary<string, Icon> DictIconCache = new ConcurrentDictionary<string, Icon>();
private static readonly object ReadIcon = new object();
// private static readonly object ReadIcon = new object();
public enum IconSize public enum IconSize
{ {
Large = 0, // 32x32 pixels Large = 0, // 32x32 pixels
@ -50,22 +50,23 @@ namespace SystemTrayMenu.Utilities
{ {
Icon icon = null; Icon icon = null;
string extension = Path.GetExtension(filePath); string extension = Path.GetExtension(filePath);
lock (ReadIcon)
// lock (ReadIcon)
// {
if (IsExtensionWitSameIcon(extension))
{ {
if (IsExtensionWitSameIcon(extension)) icon = DictIconCache.GetOrAdd(extension, GetIcon);
Icon GetIcon(string keyExtension)
{ {
icon = DictIconCache.GetOrAdd(extension, GetIcon); return GetFileIcon(filePath, linkOverlay, size);
Icon GetIcon(string keyExtension)
{
return GetFileIcon(filePath, linkOverlay, size);
}
}
else
{
icon = GetFileIcon(filePath, linkOverlay, size);
} }
} }
else
{
icon = GetFileIcon(filePath, linkOverlay, size);
}
// }
return icon; return icon;
} }

View file

@ -1,13 +0,0 @@
// <copyright file="Language.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.Utilities
{
internal class Language
{
public string Name { get; set; }
public string Value { get; set; }
}
}

View file

@ -65,39 +65,29 @@ namespace SystemTrayMenu.Utilities
internal static void ProcessStart(string fileName, string arguments = null) internal static void ProcessStart(string fileName, string arguments = null)
{ {
if (!string.IsNullOrEmpty(arguments) && try
!Directory.Exists(arguments))
{ {
Exception ex = new DirectoryNotFoundException(); using Process p = new Process
Warn($"path:'{arguments}'", ex); {
MessageBox.Show(ex.Message); StartInfo = new ProcessStartInfo(fileName)
{
Arguments = arguments,
UseShellExecute = true,
},
};
p.Start();
} }
else catch (Exception ex)
{ {
try if (ex is FileNotFoundException ||
ex is Win32Exception)
{ {
using Process p = new Process Warn($"fileName:'{fileName}' arguments:'{arguments}'", ex);
{ MessageBox.Show(ex.Message);
StartInfo = new ProcessStartInfo(fileName)
{
Arguments = arguments,
UseShellExecute = true,
},
};
p.Start();
} }
catch (Exception ex) else
{ {
if (ex is FileNotFoundException || throw;
ex is Win32Exception)
{
Warn($"fileName:'{fileName}' arguments:'{arguments}'", ex);
MessageBox.Show(ex.Message);
}
else
{
throw;
}
} }
} }
} }