From d4362bbef4a0c21a5872fb1ac02ac5d75cdf6c62 Mon Sep 17 00:00:00 2001 From: Markus Hofknecht Date: Mon, 16 Mar 2020 21:05:52 +0100 Subject: [PATCH] [Feature] CodeBuity&Refactor #49 --- Controls/Menu.cs | 9 +++++- Helper/AppRestart.cs | 50 +++++++++++++++++++++++++++++++++ Helper/FadeForm.cs | 3 +- Helper/File/IconReader.cs | 3 +- Helper/Log.cs | 18 ++++++++---- Helper/MessageFilter.cs | 19 +++++++++++++ Helper/SingleAppInstance.cs | 9 ++---- Program.cs | 16 +++-------- Properties/AssemblyInfo.cs | 4 +-- SystemTrayMenu.cs | 55 ++++++++++--------------------------- SystemTrayMenu.csproj | 1 + 11 files changed, 116 insertions(+), 71 deletions(-) create mode 100644 Helper/AppRestart.cs diff --git a/Controls/Menu.cs b/Controls/Menu.cs index 5ce982a..860b4ee 100644 --- a/Controls/Menu.cs +++ b/Controls/Menu.cs @@ -45,7 +45,9 @@ namespace SystemTrayMenu FadeForm FadeForm = null; bool autoResizeRowsDone = false; - public Menu() + public enum MenuType { Default, DisposedFake }; + + public Menu(MenuType menuType = MenuType.Default) { FadeForm = new FadeForm(this); InitializeComponent(); @@ -58,6 +60,11 @@ namespace SystemTrayMenu VScrollBar scrollBar = dgv.Controls.OfType().First(); scrollBar.MouseWheel += dgv_MouseWheel; + + if (menuType == MenuType.DisposedFake) + { + Dispose(); + } } static void SetDoubleBuffer(Control ctl, bool DoubleBuffered) diff --git a/Helper/AppRestart.cs b/Helper/AppRestart.cs new file mode 100644 index 0000000..eb39a43 --- /dev/null +++ b/Helper/AppRestart.cs @@ -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()); + } + } +} diff --git a/Helper/FadeForm.cs b/Helper/FadeForm.cs index 6974cfc..a0f109f 100644 --- a/Helper/FadeForm.cs +++ b/Helper/FadeForm.cs @@ -89,8 +89,7 @@ namespace SystemTrayMenu timerFadeHalf.Stop(); timerFadeIn.Start(); } - //see #35 [BUG], from late mouse events - else if (!form.IsDisposed) + else { ShowInactiveTopmost(form); timerFadeOut.Stop(); diff --git a/Helper/File/IconReader.cs b/Helper/File/IconReader.cs index 8d608c7..c4de7ce 100644 --- a/Helper/File/IconReader.cs +++ b/Helper/File/IconReader.cs @@ -1,5 +1,4 @@ -using Clearcove.Logging; -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Drawing; diff --git a/Helper/Log.cs b/Helper/Log.cs index 45004a1..658a555 100644 --- a/Helper/Log.cs +++ b/Helper/Log.cs @@ -19,11 +19,12 @@ namespace SystemTrayMenu.Helper log.Info(message); } - internal static void Warn(string message) - { - log.Warn($"{message}{Environment.NewLine}" + - $"{Environment.StackTrace.ToString()}"); - } + //internal static void Warn(string message) + //{ + // log.Warn($"{message}{Environment.NewLine}" + + // $"{Environment.StackTrace.ToString()}"); + //} + internal static void Error(string message, Exception ex) { log.Error($"{message}{Environment.NewLine}" + @@ -42,7 +43,7 @@ namespace SystemTrayMenu.Helper Process.Start(GetLogFilePath()); } - internal static void ApplicationRun() + internal static void WriteApplicationRuns() { Assembly assembly = Assembly.GetExecutingAssembly(); log.Info($"Application Start " + @@ -50,5 +51,10 @@ namespace SystemTrayMenu.Helper assembly.GetName().Version.ToString() + " | " + $"ScalingFactor={Scaling.Factor}"); } + + internal static void Close() + { + Logger.ShutDown(); + } } } diff --git a/Helper/MessageFilter.cs b/Helper/MessageFilter.cs index f7da96b..a8748bf 100644 --- a/Helper/MessageFilter.cs +++ b/Helper/MessageFilter.cs @@ -15,6 +15,7 @@ namespace SystemTrayMenu public event EventHandler ScrollBarMouseMove; Point cursorPosition = new Point(); + bool messageFilterAdded = false; public bool PreFilterMessage(ref Message message) { @@ -37,5 +38,23 @@ namespace SystemTrayMenu } return false; } + + internal void StartListening() + { + if (!messageFilterAdded) + { + Application.AddMessageFilter(this); + messageFilterAdded = true; + } + } + + internal void StopListening() + { + if (messageFilterAdded) + { + Application.RemoveMessageFilter(this); + messageFilterAdded = false; + } + } } } diff --git a/Helper/SingleAppInstance.cs b/Helper/SingleAppInstance.cs index 45098a2..7165bf3 100644 --- a/Helper/SingleAppInstance.cs +++ b/Helper/SingleAppInstance.cs @@ -1,5 +1,4 @@ -using Clearcove.Logging; -using System; +using System; using System.Diagnostics; using System.Linq; @@ -9,8 +8,6 @@ namespace SystemTrayMenu.Helper { internal static void Initialize() { - Logger log = new Logger(nameof(SingleAppInstance)); - if (IsAnyOtherInstancesofAppAlreadyRunning()) { KillOtherInstancesOfApp(); @@ -30,9 +27,9 @@ namespace SystemTrayMenu.Helper killedAProcess = true; } } - catch (Exception exception) + catch (Exception ex) { - log.Warn($"{exception.ToString()}"); + Log.Error("Run as single instance failed", ex); } return killedAProcess; diff --git a/Program.cs b/Program.cs index d6e1036..9396827 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,4 @@ -using Clearcove.Logging; -using System; +using System; using System.Diagnostics; using System.Reflection; using System.Threading; @@ -35,7 +34,7 @@ namespace SystemTrayMenu using (new SystemTrayMenu()) { - Log.ApplicationRun(); + Log.WriteApplicationRuns(); Application.Run(); } } @@ -46,7 +45,7 @@ namespace SystemTrayMenu } finally { - Logger.ShutDown(); + Log.Close(); } void AskUserSendError(Exception ex) @@ -63,15 +62,8 @@ namespace SystemTrayMenu "&body=" + ex.ToString()); } - Logger.ShutDown(); - Restart(); + AppRestart.ByThreadException(); } } - - internal static void Restart() - { - Process.Start(Assembly.GetExecutingAssembly(). - ManifestModule.FullyQualifiedName); - } } } \ No newline at end of file diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 03dcf55..859e639 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -31,5 +31,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("0.9.2.4")] -[assembly: AssemblyFileVersion("0.9.2.4")] +[assembly: AssemblyVersion("0.9.2.5")] +[assembly: AssemblyFileVersion("0.9.2.5")] diff --git a/SystemTrayMenu.cs b/SystemTrayMenu.cs index 7b7d9f3..7975819 100644 --- a/SystemTrayMenu.cs +++ b/SystemTrayMenu.cs @@ -1,4 +1,4 @@ -using Clearcove.Logging; +using Microsoft.Win32; using System; using System.Collections.Generic; using System.ComponentModel; @@ -34,9 +34,13 @@ namespace SystemTrayMenu public SystemTrayMenu() { + AppRestart.BeforeRestarting += Dispose; + SystemEvents.DisplaySettingsChanged += AppRestart.ByDisplaySettings; + menus[0] = new Menu(Menu.MenuType.DisposedFake); + keyboardInput = new KeyboardInput(menus); keyboardInput.RegisterHotKey(); - keyboardInput.HotKeyPressed += SwitchOpenClose; + keyboardInput.HotKeyPressed -= SwitchOpenClose; keyboardInput.ClosePressed += MenusFadeOut; keyboardInput.RowSelected += KeyboardInputRowSelected; void KeyboardInputRowSelected(DataGridView dgv, int rowIndex) @@ -49,11 +53,8 @@ namespace SystemTrayMenu keyboardInput.Cleared += FadeHalfOrOutIfNeeded; menuNotifyIcon = new MenuNotifyIcon(); - - menus[0] = new Menu(); - menus[0].Dispose(); - menuNotifyIcon.Exit += Application.Exit; - + menuNotifyIcon.Exit += Application.Exit; + menuNotifyIcon.Restart += AppRestart.ByMenuNotifyIcon; menuNotifyIcon.HandleClick += SwitchOpenClose; void SwitchOpenClose() { @@ -132,11 +133,7 @@ namespace SystemTrayMenu menus[0].AdjustLocationAndSize(screen); ActivateMenu(); menus[0].AdjustLocationAndSize(screen); - if (!messageFilterAdded) - { - Application.AddMessageFilter(messageFilter); - messageFilterAdded = true; - } + messageFilter.StartListening(); } openCloseState = OpenCloseState.Default; @@ -144,11 +141,7 @@ namespace SystemTrayMenu void ActivateMenu() { - Menus().ToList().ForEach(menu => - { - menu.FadeIn(); - menu.FadeHalf(); - }); + Menus().ToList().ForEach(m =>{m.FadeIn();m.FadeHalf();}); menus[0].SetTitleColorActive(); menus[0].Activate(); WindowToTop.ForceForegroundWindow(menus[0].Handle); @@ -160,24 +153,10 @@ namespace SystemTrayMenu { 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 += MessageFilter_MouseMove; void MessageFilter_MouseMove() @@ -271,7 +250,7 @@ namespace SystemTrayMenu { DisposeMenu(menuToDispose); } - if (Menus().Any(m => m.IsDisposed)) + if (!Menus().Any(m => m.Visible)) { openCloseState = OpenCloseState.Default; } @@ -285,7 +264,7 @@ namespace SystemTrayMenu int widthPredecessors = -1; // -1 padding bool directionToRight = false; - foreach (Menu menu in Menus().Skip(1)) + foreach (Menu menu in Menus().Where(m=>m.Level > 0)) { // -1*2 padding int newWith = (menu.Width - 2 + menuPredecessor.Width); @@ -661,16 +640,12 @@ namespace SystemTrayMenu IEnumerable Menus() { - return menus.Where(m => m != null); + return menus.Where(m => m != null && !m.IsDisposed); } void MenusFadeOut() { - if (messageFilterAdded) - { - Application.RemoveMessageFilter(messageFilter); - messageFilterAdded = false; - } + messageFilter.StopListening(); Menus().ToList().ForEach(menu => { diff --git a/SystemTrayMenu.csproj b/SystemTrayMenu.csproj index 9a7288c..c6c8b8a 100644 --- a/SystemTrayMenu.csproj +++ b/SystemTrayMenu.csproj @@ -159,6 +159,7 @@ +