Skip to content

Commit

Permalink
Improve CheckIfMouseOver performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliviaophia committed Apr 16, 2022
1 parent e01cace commit 3477859
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
4 changes: 3 additions & 1 deletion Sources/SmartTaskbar.Win10/Worker/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal sealed class Engine
private static int _timerCount;
private static TaskbarInfo _taskbar;

private static readonly HashSet<IntPtr> NonMouseOverShowHandleSet = new HashSet<IntPtr>();
private static readonly HashSet<IntPtr> DesktopHandleSet = new HashSet<IntPtr>();
private static readonly Stack<IntPtr> LastHideForegroundHandle = new Stack<IntPtr>();
private static ForegroundWindowInfo _currentForegroundWindow;
Expand Down Expand Up @@ -53,7 +54,7 @@ private static void Timer_Tick(object sender, EventArgs e)
Hooker.SetHook(_taskbar.Handle);
}

switch (_taskbar.CheckIfMouseOver())
switch (_taskbar.CheckIfMouseOver(NonMouseOverShowHandleSet))
{
case TaskbarBehavior.DoNothing:
break;
Expand All @@ -78,6 +79,7 @@ private static void Timer_Tick(object sender, EventArgs e)
_timerCount = 0;

DesktopHandleSet.Clear();
NonMouseOverShowHandleSet.Clear();
Hooker.ResetHook();
}

Expand Down
22 changes: 15 additions & 7 deletions Sources/SmartTaskbar.Win10/Worker/TaskbarHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ public static void ShowTaskar(this in TaskbarInfo taskbar)
/// Mouse over the taskbar or a specific window,
/// it will only cause the taskbar to show or do nothing.
/// </summary>
/// <param name="taskbar"></param>
/// <returns></returns>
public static TaskbarBehavior CheckIfMouseOver(this in TaskbarInfo taskbar)
public static TaskbarBehavior CheckIfMouseOver(this in TaskbarInfo taskbar,
HashSet<IntPtr> nonMouseOverShowHandleSet)
{
// Get mouse coordinates
if (!GetCursorPos(out var point))
Expand Down Expand Up @@ -214,11 +214,19 @@ public static TaskbarBehavior CheckIfMouseOver(this in TaskbarInfo taskbar)
&& mouseOverRect.right <= taskbar.Rect.right + TrayTolerance)
return TaskbarBehavior.DoNothing;

// If it is a thumbnail of the floating taskbar icon,
// the taskbar needs to be displayed.
return mouseOverHandle.GetClassName() is TrayTaskListThumbnailWnd
? TaskbarBehavior.Show
: TaskbarBehavior.Pending;
if (nonMouseOverShowHandleSet.Contains(mouseOverHandle))
return TaskbarBehavior.Pending;

switch (mouseOverHandle.GetClassName())
{
// If it is a thumbnail of the floating taskbar icon,
// the taskbar needs to be displayed.
case TrayTaskListThumbnailWnd:
return TaskbarBehavior.Show;
default:
nonMouseOverShowHandleSet.Add(mouseOverHandle);
return TaskbarBehavior.Pending;
}
}

public static bool CheckIfWindowShouldHideTaskbar(this in TaskbarInfo taskbar, IntPtr foregroundHandle)
Expand Down
4 changes: 3 additions & 1 deletion Sources/SmartTaskbar/Worker/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal sealed class Engine
private static int _timerCount;
private static TaskbarInfo _taskbar;

private static readonly HashSet<IntPtr> NonMouseOverShowHandleSet = new();
private static readonly HashSet<IntPtr> DesktopHandleSet = new();
private static readonly Stack<IntPtr> LastHideForegroundHandle = new();
private static ForegroundWindowInfo _currentForegroundWindow;
Expand Down Expand Up @@ -46,7 +47,7 @@ private static void Timer_Tick(object sender, EventArgs e)
return;
}

switch (_taskbar.CheckIfMouseOver())
switch (_taskbar.CheckIfMouseOver(NonMouseOverShowHandleSet))
{
case TaskbarBehavior.DoNothing:
break;
Expand All @@ -71,6 +72,7 @@ private static void Timer_Tick(object sender, EventArgs e)
_timerCount = 0;

DesktopHandleSet.Clear();
NonMouseOverShowHandleSet.Clear();
}

private static void CheckCurrentWindow()
Expand Down
22 changes: 15 additions & 7 deletions Sources/SmartTaskbar/Worker/TaskbarHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ public static void ShowTaskar(this in TaskbarInfo taskbar)
/// Mouse over the taskbar or a specific window,
/// it will only cause the taskbar to show or do nothing.
/// </summary>
/// <param name="taskbar"></param>
/// <returns></returns>
public static TaskbarBehavior CheckIfMouseOver(this in TaskbarInfo taskbar)
public static TaskbarBehavior CheckIfMouseOver(this in TaskbarInfo taskbar,
HashSet<IntPtr> nonMouseOverShowHandleSet)
{
// Get mouse coordinates
if (!GetCursorPos(out var point))
Expand Down Expand Up @@ -210,11 +210,19 @@ public static TaskbarBehavior CheckIfMouseOver(this in TaskbarInfo taskbar)
&& mouseOverRect.right <= taskbar.Rect.right + TrayTolerance)
return TaskbarBehavior.DoNothing;

// If it is a thumbnail of the floating taskbar icon,
// the taskbar needs to be displayed.
return mouseOverHandle.GetClassName() is TrayTaskListThumbnailWnd
? TaskbarBehavior.Show
: TaskbarBehavior.Pending;
if (nonMouseOverShowHandleSet.Contains(mouseOverHandle))
return TaskbarBehavior.Pending;

switch (mouseOverHandle.GetClassName())
{
// If it is a thumbnail of the floating taskbar icon,
// the taskbar needs to be displayed.
case TrayTaskListThumbnailWnd:
return TaskbarBehavior.Show;
default:
nonMouseOverShowHandleSet.Add(mouseOverHandle);
return TaskbarBehavior.Pending;
}
}

public static bool CheckIfWindowShouldHideTaskbar(this in TaskbarInfo taskbar, IntPtr foregroundHandle)
Expand Down

0 comments on commit 3477859

Please sign in to comment.