Skip to content

Commit

Permalink
Code Analyze and Refactor 0.12 (#109), version 0.11.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Hofknecht committed Jul 3, 2020
1 parent c4adc8c commit 98b648c
Show file tree
Hide file tree
Showing 23 changed files with 163 additions and 190 deletions.
5 changes: 0 additions & 5 deletions Business/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ internal class App : IDisposable

public App()
{
Screen screen = Screen.PrimaryScreen;
Statics.ScreenHeight = screen.Bounds.Height;
Statics.ScreenWidth = screen.Bounds.Width;
Statics.ScreenRight = screen.Bounds.Right;
Statics.TaskbarHeight = new WindowsTaskbar().Size.Height;
AppRestart.BeforeRestarting += Dispose;
SystemEvents.DisplaySettingsChanged += AppRestart.ByDisplaySettings;
menus.LoadStarted += menuNotifyIcon.LoadingStart;
Expand Down
76 changes: 20 additions & 56 deletions Business/KeyboardInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,28 @@ namespace SystemTrayMenu.Handler
{
internal class KeyboardInput : IDisposable
{
public event EventHandlerEmpty HotKeyPressed;
public event EventHandlerEmpty ClosePressed;
public event Action<DataGridView, int> RowSelected;
public event Action<int, int, DataGridView> RowDeselected;
internal event EventHandlerEmpty HotKeyPressed;
internal event EventHandlerEmpty ClosePressed;
internal event Action<DataGridView, int> RowSelected;
internal event Action<int, DataGridView> RowDeselected;
internal Action<DataGridView, int> EnterPressed;
public event EventHandlerEmpty Cleared;
internal event EventHandlerEmpty Cleared;

internal bool InUse = false;
internal int iRowKey = -1;
internal int iMenuKey = 0;

private readonly Menu[] menus;
private readonly KeyboardHook hook = new KeyboardHook();
private readonly Timer timerKeySearch = new Timer();
public int iRowKey = -1;
public int iMenuKey = 0;
private string KeySearchString = string.Empty;

public bool InUse = false;

public KeyboardInput(Menu[] menus)
{
this.menus = menus;

timerKeySearch.Interval = MenuDefines.KeySearchInterval;
timerKeySearch.Tick += TimerKeySearch_Tick;
void TimerKeySearch_Tick(object sender, EventArgs e)
{
// this search has expired, reset search
timerKeySearch.Stop();
KeySearchString = string.Empty;
}
}

public void Dispose()
{
hook.Dispose();
timerKeySearch.Dispose();
}

private int GetMenuIndex(in Menu currentMenu)
Expand All @@ -68,7 +56,7 @@ internal void RegisterHotKey()
{
try
{
hook.RegisterHotKey(Properties.Settings.Default.HotKey);
hook.RegisterHotKey();
hook.KeyPressed += hook_KeyPressed;
void hook_KeyPressed(object sender, KeyPressedEventArgs e)
{
Expand Down Expand Up @@ -178,29 +166,6 @@ internal void KeyPress(object sender, KeyPressEventArgs e)
Menu menu = menus[iMenuKey];
menu.KeyPressedSearch(letter);

#warning remove if not more needed
// Old Search by letters
//timerKeySearch.Stop();
//if (string.IsNullOrEmpty(KeySearchString))
//{
// // no search string set, start new search with initial letter
// KeySearchString += letter;
// SelectByKey(Keys.None, KeySearchString);
//}
//else if (KeySearchString.Length == 1 && KeySearchString.LastOrDefault().ToString(CultureInfo.InvariantCulture) == letter)
//{
// // initial letter pressed again, jump to next element
// SelectByKey(Keys.None, letter);
//}
//else
//{
// // new character for the search string, narrow down the search
// KeySearchString += letter;
// SelectByKey(Keys.None, KeySearchString, true);
//}
//// give user some time to continue with this search
//timerKeySearch.Start();

e.Handled = true;
}
}
Expand Down Expand Up @@ -295,7 +260,7 @@ private void SelectByKey(Keys keys, string keyInput = "", bool KeepSelection = f
}
else
{
RowDeselected(iMenuBefore, iRowBefore, dgvBefore);
RowDeselected(iRowBefore, dgvBefore);
SelectRow(dgv, iRowKey);
EnterPressed.Invoke(dgv, iRowKey);
}
Expand All @@ -305,7 +270,7 @@ private void SelectByKey(Keys keys, string keyInput = "", bool KeepSelection = f
if (SelectMatchedReverse(dgv, iRowKey) ||
SelectMatchedReverse(dgv, dgv.Rows.Count - 1))
{
RowDeselected(iMenuBefore, iRowBefore, dgvBefore);
RowDeselected(iRowBefore, dgvBefore);
SelectRow(dgv, iRowKey);
toClear = true;
}
Expand All @@ -314,7 +279,7 @@ private void SelectByKey(Keys keys, string keyInput = "", bool KeepSelection = f
if (SelectMatched(dgv, iRowKey) ||
SelectMatched(dgv, 0))
{
RowDeselected(iMenuBefore, iRowBefore, dgvBefore);
RowDeselected(iRowBefore, dgvBefore);
SelectRow(dgv, iRowKey);
toClear = true;
}
Expand All @@ -334,8 +299,7 @@ private void SelectByKey(Keys keys, string keyInput = "", bool KeepSelection = f
if (SelectMatched(dgv, iRowKey) ||
SelectMatched(dgv, 0))
{
RowDeselected(iMenuBefore,
iRowBefore, dgvBefore);
RowDeselected(iRowBefore, dgvBefore);
SelectRow(dgv, iRowKey);
toClear = true;
}
Expand All @@ -352,7 +316,7 @@ private void SelectByKey(Keys keys, string keyInput = "", bool KeepSelection = f
if (SelectMatched(dgv, iRowKey) ||
SelectMatched(dgv, 0))
{
RowDeselected(iMenuBefore, iRowBefore, dgvBefore);
RowDeselected(iRowBefore, dgvBefore);
SelectRow(dgv, iRowKey);
toClear = true;
}
Expand All @@ -371,23 +335,23 @@ private void SelectByKey(Keys keys, string keyInput = "", bool KeepSelection = f
if (SelectMatched(dgv, dgv.SelectedRows[0].Index) ||
SelectMatched(dgv, 0))
{
RowDeselected(iMenuBefore, iRowBefore, dgvBefore);
RowDeselected(iRowBefore, dgvBefore);
SelectRow(dgv, iRowKey);
toClear = true;
}
}
}
else
{
RowDeselected(iMenuBefore, iRowBefore, dgvBefore);
RowDeselected(iRowBefore, dgvBefore);
iMenuKey = 0;
iRowKey = -1;
toClear = true;
Cleared?.Invoke();
}
break;
case Keys.Escape:
RowDeselected(iMenuBefore, iRowBefore, dgvBefore);
RowDeselected(iRowBefore, dgvBefore);
iMenuKey = 0;
iRowKey = -1;
toClear = true;
Expand All @@ -399,7 +363,7 @@ private void SelectByKey(Keys keys, string keyInput = "", bool KeepSelection = f
if (SelectMatched(dgv, iRowKey, keyInput) ||
SelectMatched(dgv, 0, keyInput))
{
RowDeselected(iMenuBefore, iRowBefore, null);
RowDeselected(iRowBefore, null);
SelectRow(dgv, iRowKey);
toClear = true;
}
Expand All @@ -409,7 +373,7 @@ private void SelectByKey(Keys keys, string keyInput = "", bool KeepSelection = f
if (SelectMatched(dgv, iRowKey, keyInput) ||
SelectMatched(dgv, 0, keyInput))
{
RowDeselected(iMenuBefore, iRowBefore, null);
RowDeselected(iRowBefore, null);
SelectRow(dgv, iRowKey);
}
else
Expand Down
25 changes: 16 additions & 9 deletions Business/Menus.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Clearcove.Logging;
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
Expand Down Expand Up @@ -28,14 +27,19 @@ private enum OpenCloseState { Default, Opening, Closing };
private readonly List<BackgroundWorker> workersSubMenu = new List<BackgroundWorker>();

private readonly WaitToLoadMenu waitToOpenMenu = new WaitToLoadMenu();
private readonly KeyboardInput keyboardInput;
private readonly KeyboardInput keyboardInput = null;
private readonly Timer timerStillActiveCheck = new Timer();
private readonly WaitLeave waitLeave = new WaitLeave(MenuDefines.TimeUntilClose);
private DateTime deactivatedTime = DateTime.MinValue;

private IEnumerable<Menu> AsEnumerable => menus.Where(m => m != null && !m.IsDisposed);
private List<Menu> AsList => AsEnumerable.ToList();

private readonly int screenHeight = Screen.PrimaryScreen.Bounds.Height;
private readonly int screenWidth = Screen.PrimaryScreen.Bounds.Width;
private readonly int screenRight = Screen.PrimaryScreen.Bounds.Right;
private readonly int taskbarHeight = new WindowsTaskbar().Size.Height;

public Menus()
{
workerMainMenu.WorkerSupportsCancellation = true;
Expand Down Expand Up @@ -218,9 +222,10 @@ public void Dispose()
{
worker.Dispose();
}
waitLeave.Dispose();
waitToOpenMenu.Dispose();
keyboardInput.Dispose();
timerStillActiveCheck.Dispose();
waitLeave.Dispose();
IconReader.Dispose();
DisposeMenu(menus[0]);
}
Expand Down Expand Up @@ -374,7 +379,8 @@ internal void MainPreload()
{
menus[0] = Create(GetData(workerMainMenu, Config.Path, 0),
Path.GetFileName(Config.Path));
menus[0].AdjustSizeAndLocation();
menus[0].AdjustSizeAndLocation(screenHeight,
screenRight, taskbarHeight);
DisposeMenu(menus[0]);
}

Expand Down Expand Up @@ -737,7 +743,8 @@ private void AdjustMenusSizeAndLocation()
int widthPredecessors = -1; // -1 padding
bool directionToRight = false;

menus[0].AdjustSizeAndLocation();
menus[0].AdjustSizeAndLocation(screenHeight,
screenRight, taskbarHeight);

foreach (Menu menu in AsEnumerable.Where(m => m.Level > 0))
{
Expand All @@ -754,14 +761,14 @@ private void AdjustMenusSizeAndLocation()
widthPredecessors -= newWith;
}
}
else if (Statics.ScreenWidth <
widthPredecessors + menus[0].Width + menu.Width)
else if (screenWidth < widthPredecessors + menus[0].Width + menu.Width)
{
directionToRight = true;
widthPredecessors -= newWith;
}

menu.AdjustSizeAndLocation(menuPredecessor, directionToRight);
menu.AdjustSizeAndLocation(screenHeight, screenRight, taskbarHeight,
menuPredecessor, directionToRight);
widthPredecessors += menu.Width - menu.Padding.Left;
menuPredecessor = menu;
}
Expand Down
8 changes: 4 additions & 4 deletions Business/WaitToLoadMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ internal void MouseLeave(object sender, DataGridViewCellEventArgs e)
}
}

internal void RowDeselected(int iMenuBefore, int rowIndex, DataGridView dgv) //iMenuBefore not needed
internal void RowDeselected(int rowIndex, DataGridView dgv) //iMenuBefore not needed
{
timerStartLoad.Stop();
StopLoadMenu?.Invoke();
Expand Down Expand Up @@ -115,7 +115,7 @@ internal void MouseMove(object sender, MouseEventArgs e)
}
mouseMoveEvents = 0;
}
else if (DateTime.Now - dateTimeLastMouseMoveEvent <
else if (DateTime.Now - dateTimeLastMouseMoveEvent <
new TimeSpan(0, 0, 0, 0, 200))
{
mouseMoveEvents++;
Expand Down Expand Up @@ -189,8 +189,8 @@ public void Dispose()
{
timerStartLoad.Stop();
timerStartLoad.Dispose();
dgv.Dispose();
dgvTmp.Dispose();
dgv?.Dispose();
dgvTmp?.Dispose();
}
}
}
2 changes: 1 addition & 1 deletion Config/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using SystemTrayMenu.UserInterface.FolderDialog;
using SystemTrayMenu.UserInterface.Dialogs;
using SystemTrayMenu.Utilities;

namespace SystemTrayMenu
Expand Down
1 change: 0 additions & 1 deletion Config/MenuDefines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ internal static class MenuDefines
internal static readonly Color ColorTitleWarning = AppColors.Red;
internal static readonly Color ColorTitleSelected = AppColors.Yellow;
internal static readonly Color ColorTitleBackground = AppColors.Azure;
internal const int KeySearchInterval = 1000;
internal const int Scrollspeed = 4;
internal const int TimeUntilClose = 1000;
internal const int MenusMax = 50;
Expand Down
11 changes: 0 additions & 11 deletions DataClasses/Statics.cs

This file was deleted.

1 change: 0 additions & 1 deletion Helpers/Fading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ internal enum FadingState { Idle, Show, ShowTransparent, Hide };
private const double ShownMinus = 0.80; //Shown - StepIn

private readonly Timer timer = new Timer();
private static readonly object lockTimerEnable = new object();
private FadingState state = FadingState.Idle;
private double opacity = 0.00;
private bool visible = false;
Expand Down
10 changes: 5 additions & 5 deletions Helpers/KeyboardHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@ internal void RegisterHotKey(Keys key)
RegisterHotKey(keyModifiersNone, key);
}

internal void RegisterHotKey(string hotKeyString)
internal void RegisterHotKey()
{
KeyboardHookModifierKeys modifiers = KeyboardHookModifierKeys.None;
string modifiersString = Properties.Settings.Default.HotKey;
if (!string.IsNullOrEmpty(modifiersString))
{
if (modifiersString.ToLower().Contains("alt"))
if (modifiersString.ToUpperInvariant().Contains("ALT", StringComparison.InvariantCulture))
{
modifiers |= KeyboardHookModifierKeys.Alt;
}
if (modifiersString.ToLower().Contains("ctrl"))
if (modifiersString.ToUpperInvariant().Contains("CTRL", StringComparison.InvariantCulture))
{
modifiers |= KeyboardHookModifierKeys.Control;
}
if (modifiersString.ToLower().Contains("shift"))
if (modifiersString.ToUpperInvariant().Contains("SHIFT", StringComparison.InvariantCulture))
{
modifiers |= KeyboardHookModifierKeys.Shift;
}
if (modifiersString.ToLower().Contains("win"))
if (modifiersString.ToUpperInvariant().Contains("WIN", StringComparison.InvariantCulture))
{
modifiers |= KeyboardHookModifierKeys.Win;
}
Expand Down
6 changes: 3 additions & 3 deletions NativeDllImport/TrackPopupMenuEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ internal enum TPM : uint
{
LEFTBUTTON = 0x0000,
RIGHTBUTTON = 0x0002,
LEFTALIGN = 0x0000,
//LEFTALIGN = 0x0000,
CENTERALIGN = 0x0004,
RIGHTALIGN = 0x0008,
TOPALIGN = 0x0000,
//TOPALIGN = 0x0000,
VCENTERALIGN = 0x0010,
BOTTOMALIGN = 0x0020,
HORIZONTAL = 0x0000,
//HORIZONTAL = 0x0000,
VERTICAL = 0x0040,
NONOTIFY = 0x0080,
RETURNCMD = 0x0100,
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
// 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.11.2.4")]
[assembly: AssemblyFileVersion("0.11.2.4")]
[assembly: AssemblyVersion("0.11.3.0")]
[assembly: AssemblyFileVersion("0.11.3.0")]
Loading

0 comments on commit 98b648c

Please sign in to comment.