Skip to content

Commit f3093e3

Browse files
committed
Force to foreground when clicking notification
1 parent c20943e commit f3093e3

File tree

4 files changed

+46
-22
lines changed

4 files changed

+46
-22
lines changed

CollapseLauncher/CollapseLauncher.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
<PackageReference Include="FFmpegInteropX" Version="*-*" Condition="$(DefineConstants.Contains('USEFFMPEGFORVIDEOBG'))" />
9191

9292
<PackageReference Include="GitInfo" Version="3.3.5" PrivateAssets="all" />
93-
<PackageReference Include="H.NotifyIcon.WinUI" Version="2.1.2" />
93+
<PackageReference Include="H.NotifyIcon.WinUI" Version="2.1.3" />
9494
<PackageReference Include="HtmlAgilityPack" Version="1.11.63" />
9595
<PackageReference Include="ImageEx" Version="2.1.1" />
9696
<PackageReference Include="Markdig.Signed" Version="0.37.0" />

CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
using System.Text;
4444
using System.Threading;
4545
using System.Threading.Tasks;
46-
using Windows.Foundation;
4746
using Microsoft.UI.Xaml.Media;
4847
using static CollapseLauncher.Dialogs.SimpleDialogs;
4948
using static CollapseLauncher.InnerLauncherConfig;
@@ -53,6 +52,7 @@
5352
using static Hi3Helper.Shared.Region.LauncherConfig;
5453
using Brush = Microsoft.UI.Xaml.Media.Brush;
5554
using Image = Microsoft.UI.Xaml.Controls.Image;
55+
using Point = Windows.Foundation.Point;
5656
using Size = System.Drawing.Size;
5757
using Timer = System.Timers.Timer;
5858
using UIElementExtensions = CollapseLauncher.Extension.UIElementExtensions;
@@ -69,8 +69,8 @@ public sealed partial class HomePage
6969
private int barWidth;
7070
private int consoleWidth;
7171

72-
public static int RefreshRateDefault { get; } = 500;
73-
public static int RefreshRateSlow { get; } = 1000;
72+
public static int RefreshRateDefault => 500;
73+
public static int RefreshRateSlow => 1000;
7474

7575
private static int _refreshRate;
7676

CollapseLauncher/XAMLs/MainApp/TrayIcon.xaml.cs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
using Hi3Helper;
66
using Hi3Helper.Shared.Region;
77
using Microsoft.UI.Xaml;
8-
using Microsoft.UI.Xaml.Controls;
98
using System;
109
using System.Drawing;
10+
using System.Threading.Tasks;
1111
using static CollapseLauncher.InnerLauncherConfig;
1212
using static CollapseLauncher.Pages.HomePage;
1313
using static Hi3Helper.InvokeProp;
@@ -69,18 +69,42 @@ public TrayIcon()
6969
#endif
7070
CloseButton.Text = _exitApp;
7171

72-
// Switch toggle text to see if its started with Start
72+
// Switch toggle text to see if it's started with Start
7373
MainTaskbarToggle.Text = (m_appMode == AppMode.StartOnTray) ? _showApp : _hideApp;
7474
ConsoleTaskbarToggle.Text = (m_appMode == AppMode.StartOnTray) ? _showConsole : _hideConsole;
7575

7676
CollapseTaskbar.Icon = Icon.FromHandle(LauncherConfig.AppIconSmall);
7777
CollapseTaskbar.Visibility = Visibility.Visible;
78+
79+
CollapseTaskbar.TrayIcon.MessageWindow.BalloonToolTipChanged += BalloonChangedEvent;
7880
}
7981

8082
public void Dispose()
8183
{
8284
CollapseTaskbar.Dispose();
8385
}
86+
87+
private void BalloonChangedEvent(object o, MessageWindow.BalloonToolTipChangedEventArgs args)
88+
{
89+
// Subscribe to the event when the Balloon is visible, and unsub when it's not.
90+
// Due to bug, this MouseEvent is not available when the notification already went to the tray.
91+
if (args.IsVisible)
92+
CollapseTaskbar.TrayIcon.MessageWindow.MouseEventReceived += NotificationOnMouseEventReceived;
93+
else
94+
CollapseTaskbar.TrayIcon.MessageWindow.MouseEventReceived -= NotificationOnMouseEventReceived;
95+
}
96+
97+
private bool _mouseEventProcessing;
98+
private async void NotificationOnMouseEventReceived(object o, MessageWindow.MouseEventReceivedEventArgs args)
99+
{
100+
if (_mouseEventProcessing) return;
101+
if (args.MouseEvent != MouseEvent.BalloonToolTipClicked) return;
102+
103+
_mouseEventProcessing = true;
104+
BringToForeground();
105+
await Task.Delay(250);
106+
_mouseEventProcessing = false;
107+
}
84108
#endregion
85109

86110
#region Visibility Toggler
@@ -97,7 +121,7 @@ public void Dispose()
97121
private void ToggleConsoleVisibilityButton() => ToggleConsoleVisibility();
98122

99123
/// <summary>
100-
/// Toggle both main and console visibility while avoiding flip flop condition
124+
/// Toggle both main and console visibility while avoiding flip-flop condition
101125
/// </summary>
102126
[RelayCommand]
103127
private void ToggleAllVisibilityInvoke() => ToggleAllVisibility();
@@ -125,6 +149,7 @@ public void Dispose()
125149
/// <summary>
126150
/// Toggle console window visibility
127151
/// </summary>
152+
// ReSharper disable once MemberCanBePrivate.Global
128153
public void ToggleConsoleVisibility(bool forceShow = false)
129154
{
130155
if (LauncherConfig.GetAppConfigValue("EnableConsole").ToBool())
@@ -162,7 +187,7 @@ public void ToggleMainVisibility(bool forceShow = false)
162187
// Increase refresh rate to 1000ms when main window is hidden
163188
RefreshRate = RefreshRateSlow;
164189
LogWriteLine("Main window is hidden!");
165-
ShowNotification("Main window is hidden!", "a");
190+
WindowUtility.Tray_ShowNotification("test1", "neon stinki");
166191
}
167192
else
168193
{
@@ -204,6 +229,7 @@ public void ToggleAllVisibility()
204229
/// <summary>
205230
/// Bring all windows into foreground, also brought all windows if they were hidden in taskbar.
206231
/// </summary>
232+
// ReSharper disable once MemberCanBePrivate.Global
207233
public void BringToForeground()
208234
{
209235
IntPtr mainWindowHandle = WindowUtility.CurrentWindowPtr;
@@ -233,6 +259,7 @@ public void BringToForeground()
233259
/// <summary>
234260
/// Update tray context menu
235261
/// </summary>
262+
// ReSharper disable once MemberCanBePrivate.Global
236263
public void UpdateContextMenu()
237264
{
238265
// Enable visibility toggle for console if the console is enabled
@@ -253,11 +280,8 @@ public void UpdateContextMenu()
253280
bool isMainWindowVisible = IsWindowVisible(mainWindowHandle);
254281
bool isConsoleVisible = LauncherConfig.GetAppConfigValue("EnableConsole").ToBool() && IsWindowVisible(consoleWindowHandle);
255282

256-
if (isConsoleVisible) ConsoleTaskbarToggle.Text = _hideConsole;
257-
else ConsoleTaskbarToggle.Text = _showConsole;
258-
259-
if (isMainWindowVisible) MainTaskbarToggle.Text = _hideApp;
260-
else MainTaskbarToggle.Text = _showApp;
283+
ConsoleTaskbarToggle.Text = isConsoleVisible ? _hideConsole : _showConsole;
284+
MainTaskbarToggle.Text = isMainWindowVisible ? _hideApp : _showApp;
261285
}
262286

263287
/// <summary>

CollapseLauncher/packages.lock.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@
123123
},
124124
"H.NotifyIcon.WinUI": {
125125
"type": "Direct",
126-
"requested": "[2.1.2, )",
127-
"resolved": "2.1.2",
128-
"contentHash": "Iwu5zEtP4ihPCUs3m0SowyKS6hA9AEcnvt0hQHZ9Tl4xu8O7lBfvmV1bomBpJPfPvpmrvTe9EfQcz+uRwiQm8A==",
126+
"requested": "[2.1.3, )",
127+
"resolved": "2.1.3",
128+
"contentHash": "1tAYbvV2vyQ0lXKrMmNvpIiP5tXZibtyXCqlmH2Wjcc3i/W/EatihtVATpeuWZpxM6RA9T1H5pTZhT8+5y7wcQ==",
129129
"dependencies": {
130-
"H.NotifyIcon": "2.1.2",
130+
"H.NotifyIcon": "2.1.3",
131131
"Microsoft.WindowsAppSDK": "1.5.240627000"
132132
}
133133
},
@@ -376,18 +376,18 @@
376376
},
377377
"H.GeneratedIcons.System.Drawing": {
378378
"type": "Transitive",
379-
"resolved": "2.1.2",
380-
"contentHash": "f2mF27nQRBGbXbEiSSxe6H12BElcH0WH3MI9lucID4Fa9I/WywFdhkFmPgALZTa2STi8rcsHTRWC7vuTfcblBQ==",
379+
"resolved": "2.1.3",
380+
"contentHash": "GH6MR2Qc/yyNaCj1205v2J3jT4siDmg5W+jlbJzxDuMZZtX20pEIb/QBABDS+l9brHlwPs7WygboRNeMii40Qw==",
381381
"dependencies": {
382382
"System.Drawing.Common": "8.0.7"
383383
}
384384
},
385385
"H.NotifyIcon": {
386386
"type": "Transitive",
387-
"resolved": "2.1.2",
388-
"contentHash": "5FllMntYHOHYQoLZGSMNUB2aSRrwciiQ3PT925NkI+XguZH5eDuXOQnOZvslHcmZ8VU0Qkp37XyG7agfP5nByw==",
387+
"resolved": "2.1.3",
388+
"contentHash": "aiCL1gjfKWh/jRlV4oDOycOqRaRj/C9E5cwHL2KCc+jre5vyUw5lHcHW6t4e/U3ssDmBKJgwhcgAW3JwMUP3ow==",
389389
"dependencies": {
390-
"H.GeneratedIcons.System.Drawing": "2.1.2"
390+
"H.GeneratedIcons.System.Drawing": "2.1.3"
391391
}
392392
},
393393
"Hi3Helper.ZstdNet": {

0 commit comments

Comments
 (0)