[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;
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<VScrollBar>().First();
scrollBar.MouseWheel += dgv_MouseWheel;
if (menuType == MenuType.DisposedFake)
{
Dispose();
}
}
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();
timerFadeIn.Start();
}
//see #35 [BUG], from late mouse events
else if (!form.IsDisposed)
else
{
ShowInactiveTopmost(form);
timerFadeOut.Stop();

View file

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

View file

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

View file

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

View file

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

View file

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

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

View file

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

View file

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