[Feature] CodeBuity&Refactor #49

This commit is contained in:
Markus Hofknecht 2020-03-16 21:05:52 +01:00
parent 62cc2a7d6d
commit d4362bbef4
11 changed files with 116 additions and 71 deletions

View file

@ -45,7 +45,9 @@ namespace SystemTrayMenu
FadeForm FadeForm = null; FadeForm FadeForm = null;
bool autoResizeRowsDone = false; bool autoResizeRowsDone = false;
public Menu() public enum MenuType { Default, DisposedFake };
public Menu(MenuType menuType = MenuType.Default)
{ {
FadeForm = new FadeForm(this); FadeForm = new FadeForm(this);
InitializeComponent(); InitializeComponent();
@ -58,6 +60,11 @@ namespace SystemTrayMenu
VScrollBar scrollBar = dgv.Controls.OfType<VScrollBar>().First(); VScrollBar scrollBar = dgv.Controls.OfType<VScrollBar>().First();
scrollBar.MouseWheel += dgv_MouseWheel; scrollBar.MouseWheel += dgv_MouseWheel;
if (menuType == MenuType.DisposedFake)
{
Dispose();
}
} }
static void SetDoubleBuffer(Control ctl, bool DoubleBuffered) static void SetDoubleBuffer(Control ctl, bool DoubleBuffered)

50
Helper/AppRestart.cs Normal file
View file

@ -0,0 +1,50 @@
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace SystemTrayMenu.Helper
{
internal class AppRestart
{
public static event EventHandler BeforeRestarting;
static void Restart(string reason)
{
BeforeRestarting?.Invoke();
Log.Info($"Restart by '{reason}'");
Log.Close();
Process.Start(Assembly.GetExecutingAssembly().
ManifestModule.FullyQualifiedName);
}
[MethodImpl(MethodImplOptions.NoInlining)]
static string GetCurrentMethod()
{
var st = new StackTrace();
var sf = st.GetFrame(1);
return sf.GetMethod().Name;
}
internal static void ByThreadException()
{
Restart(GetCurrentMethod());
}
internal static void ByMenuNotifyIcon()
{
Restart(GetCurrentMethod());
}
internal static void ByDisplaySettings(object sender, EventArgs e)
{
Restart(GetCurrentMethod());
}
internal static void ByConfigChange()
{
Restart(GetCurrentMethod());
}
}
}

View file

@ -89,8 +89,7 @@ namespace SystemTrayMenu
timerFadeHalf.Stop(); timerFadeHalf.Stop();
timerFadeIn.Start(); timerFadeIn.Start();
} }
//see #35 [BUG], from late mouse events else
else if (!form.IsDisposed)
{ {
ShowInactiveTopmost(form); ShowInactiveTopmost(form);
timerFadeOut.Stop(); timerFadeOut.Stop();

View file

@ -1,5 +1,4 @@
using Clearcove.Logging; using System;
using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;

View file

@ -19,11 +19,12 @@ namespace SystemTrayMenu.Helper
log.Info(message); log.Info(message);
} }
internal static void Warn(string message) //internal static void Warn(string message)
{ //{
log.Warn($"{message}{Environment.NewLine}" + // log.Warn($"{message}{Environment.NewLine}" +
$"{Environment.StackTrace.ToString()}"); // $"{Environment.StackTrace.ToString()}");
} //}
internal static void Error(string message, Exception ex) internal static void Error(string message, Exception ex)
{ {
log.Error($"{message}{Environment.NewLine}" + log.Error($"{message}{Environment.NewLine}" +
@ -42,7 +43,7 @@ namespace SystemTrayMenu.Helper
Process.Start(GetLogFilePath()); Process.Start(GetLogFilePath());
} }
internal static void ApplicationRun() internal static void WriteApplicationRuns()
{ {
Assembly assembly = Assembly.GetExecutingAssembly(); Assembly assembly = Assembly.GetExecutingAssembly();
log.Info($"Application Start " + log.Info($"Application Start " +
@ -50,5 +51,10 @@ namespace SystemTrayMenu.Helper
assembly.GetName().Version.ToString() + " | " + assembly.GetName().Version.ToString() + " | " +
$"ScalingFactor={Scaling.Factor}"); $"ScalingFactor={Scaling.Factor}");
} }
internal static void Close()
{
Logger.ShutDown();
}
} }
} }

View file

@ -15,6 +15,7 @@ namespace SystemTrayMenu
public event EventHandler ScrollBarMouseMove; public event EventHandler ScrollBarMouseMove;
Point cursorPosition = new Point(); Point cursorPosition = new Point();
bool messageFilterAdded = false;
public bool PreFilterMessage(ref Message message) public bool PreFilterMessage(ref Message message)
{ {
@ -37,5 +38,23 @@ namespace SystemTrayMenu
} }
return false; return false;
} }
internal void StartListening()
{
if (!messageFilterAdded)
{
Application.AddMessageFilter(this);
messageFilterAdded = true;
}
}
internal void StopListening()
{
if (messageFilterAdded)
{
Application.RemoveMessageFilter(this);
messageFilterAdded = false;
}
}
} }
} }

View file

@ -1,5 +1,4 @@
using Clearcove.Logging; using System;
using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
@ -9,8 +8,6 @@ namespace SystemTrayMenu.Helper
{ {
internal static void Initialize() internal static void Initialize()
{ {
Logger log = new Logger(nameof(SingleAppInstance));
if (IsAnyOtherInstancesofAppAlreadyRunning()) if (IsAnyOtherInstancesofAppAlreadyRunning())
{ {
KillOtherInstancesOfApp(); KillOtherInstancesOfApp();
@ -30,9 +27,9 @@ namespace SystemTrayMenu.Helper
killedAProcess = true; killedAProcess = true;
} }
} }
catch (Exception exception) catch (Exception ex)
{ {
log.Warn($"{exception.ToString()}"); Log.Error("Run as single instance failed", ex);
} }
return killedAProcess; return killedAProcess;

View file

@ -1,5 +1,4 @@
using Clearcove.Logging; using System;
using System;
using System.Diagnostics; using System.Diagnostics;
using System.Reflection; using System.Reflection;
using System.Threading; using System.Threading;
@ -35,7 +34,7 @@ namespace SystemTrayMenu
using (new SystemTrayMenu()) using (new SystemTrayMenu())
{ {
Log.ApplicationRun(); Log.WriteApplicationRuns();
Application.Run(); Application.Run();
} }
} }
@ -46,7 +45,7 @@ namespace SystemTrayMenu
} }
finally finally
{ {
Logger.ShutDown(); Log.Close();
} }
void AskUserSendError(Exception ex) void AskUserSendError(Exception ex)
@ -63,15 +62,8 @@ namespace SystemTrayMenu
"&body=" + ex.ToString()); "&body=" + ex.ToString());
} }
Logger.ShutDown(); AppRestart.ByThreadException();
Restart();
} }
} }
internal static void Restart()
{
Process.Start(Assembly.GetExecutingAssembly().
ManifestModule.FullyQualifiedName);
}
} }
} }

View file

@ -31,5 +31,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.9.2.4")] [assembly: AssemblyVersion("0.9.2.5")]
[assembly: AssemblyFileVersion("0.9.2.4")] [assembly: AssemblyFileVersion("0.9.2.5")]

View file

@ -1,4 +1,4 @@
using Clearcove.Logging; using Microsoft.Win32;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@ -34,9 +34,13 @@ namespace SystemTrayMenu
public SystemTrayMenu() public SystemTrayMenu()
{ {
AppRestart.BeforeRestarting += Dispose;
SystemEvents.DisplaySettingsChanged += AppRestart.ByDisplaySettings;
menus[0] = new Menu(Menu.MenuType.DisposedFake);
keyboardInput = new KeyboardInput(menus); keyboardInput = new KeyboardInput(menus);
keyboardInput.RegisterHotKey(); keyboardInput.RegisterHotKey();
keyboardInput.HotKeyPressed += SwitchOpenClose; keyboardInput.HotKeyPressed -= SwitchOpenClose;
keyboardInput.ClosePressed += MenusFadeOut; keyboardInput.ClosePressed += MenusFadeOut;
keyboardInput.RowSelected += KeyboardInputRowSelected; keyboardInput.RowSelected += KeyboardInputRowSelected;
void KeyboardInputRowSelected(DataGridView dgv, int rowIndex) void KeyboardInputRowSelected(DataGridView dgv, int rowIndex)
@ -49,11 +53,8 @@ namespace SystemTrayMenu
keyboardInput.Cleared += FadeHalfOrOutIfNeeded; keyboardInput.Cleared += FadeHalfOrOutIfNeeded;
menuNotifyIcon = new MenuNotifyIcon(); menuNotifyIcon = new MenuNotifyIcon();
menuNotifyIcon.Exit += Application.Exit;
menus[0] = new Menu(); menuNotifyIcon.Restart += AppRestart.ByMenuNotifyIcon;
menus[0].Dispose();
menuNotifyIcon.Exit += Application.Exit;
menuNotifyIcon.HandleClick += SwitchOpenClose; menuNotifyIcon.HandleClick += SwitchOpenClose;
void SwitchOpenClose() void SwitchOpenClose()
{ {
@ -132,11 +133,7 @@ namespace SystemTrayMenu
menus[0].AdjustLocationAndSize(screen); menus[0].AdjustLocationAndSize(screen);
ActivateMenu(); ActivateMenu();
menus[0].AdjustLocationAndSize(screen); menus[0].AdjustLocationAndSize(screen);
if (!messageFilterAdded) messageFilter.StartListening();
{
Application.AddMessageFilter(messageFilter);
messageFilterAdded = true;
}
} }
openCloseState = OpenCloseState.Default; openCloseState = OpenCloseState.Default;
@ -144,11 +141,7 @@ namespace SystemTrayMenu
void ActivateMenu() void ActivateMenu()
{ {
Menus().ToList().ForEach(menu => Menus().ToList().ForEach(m =>{m.FadeIn();m.FadeHalf();});
{
menu.FadeIn();
menu.FadeHalf();
});
menus[0].SetTitleColorActive(); menus[0].SetTitleColorActive();
menus[0].Activate(); menus[0].Activate();
WindowToTop.ForceForegroundWindow(menus[0].Handle); WindowToTop.ForceForegroundWindow(menus[0].Handle);
@ -160,24 +153,10 @@ namespace SystemTrayMenu
{ {
if (Config.SetFolderByUser()) if (Config.SetFolderByUser())
{ {
ApplicationRestart(); AppRestart.ByConfigChange();
} }
} }
Microsoft.Win32.SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
Log.Info("SystemEvents_DisplaySettingsChanged");
ApplicationRestart();
}
menuNotifyIcon.Restart += ApplicationRestart;
void ApplicationRestart()
{
Dispose();
Program.Restart();
}
messageFilter.MouseMove += FadeInIfNeeded; messageFilter.MouseMove += FadeInIfNeeded;
messageFilter.MouseMove += MessageFilter_MouseMove; messageFilter.MouseMove += MessageFilter_MouseMove;
void MessageFilter_MouseMove() void MessageFilter_MouseMove()
@ -271,7 +250,7 @@ namespace SystemTrayMenu
{ {
DisposeMenu(menuToDispose); DisposeMenu(menuToDispose);
} }
if (Menus().Any(m => m.IsDisposed)) if (!Menus().Any(m => m.Visible))
{ {
openCloseState = OpenCloseState.Default; openCloseState = OpenCloseState.Default;
} }
@ -285,7 +264,7 @@ namespace SystemTrayMenu
int widthPredecessors = -1; // -1 padding int widthPredecessors = -1; // -1 padding
bool directionToRight = false; bool directionToRight = false;
foreach (Menu menu in Menus().Skip(1)) foreach (Menu menu in Menus().Where(m=>m.Level > 0))
{ {
// -1*2 padding // -1*2 padding
int newWith = (menu.Width - 2 + menuPredecessor.Width); int newWith = (menu.Width - 2 + menuPredecessor.Width);
@ -661,16 +640,12 @@ namespace SystemTrayMenu
IEnumerable<Menu> Menus() IEnumerable<Menu> Menus()
{ {
return menus.Where(m => m != null); return menus.Where(m => m != null && !m.IsDisposed);
} }
void MenusFadeOut() void MenusFadeOut()
{ {
if (messageFilterAdded) messageFilter.StopListening();
{
Application.RemoveMessageFilter(messageFilter);
messageFilterAdded = false;
}
Menus().ToList().ForEach(menu => Menus().ToList().ForEach(menu =>
{ {

View file

@ -159,6 +159,7 @@
<Compile Include="Controls\MenuData.cs" /> <Compile Include="Controls\MenuData.cs" />
<Compile Include="Controls\RowData.cs" /> <Compile Include="Controls\RowData.cs" />
<Compile Include="Controls\AppContextMenu.cs" /> <Compile Include="Controls\AppContextMenu.cs" />
<Compile Include="Helper\AppRestart.cs" />
<Compile Include="Helper\BringWindowToTop.cs" /> <Compile Include="Helper\BringWindowToTop.cs" />
<Compile Include="Helper\DataGridViewExtensions.cs" /> <Compile Include="Helper\DataGridViewExtensions.cs" />
<Compile Include="Helper\File\IconReader.cs" /> <Compile Include="Helper\File\IconReader.cs" />