Skip to content

Commit

Permalink
Force to foreground when clicking notification
Browse files Browse the repository at this point in the history
  • Loading branch information
bagusnl committed Aug 19, 2024
1 parent c20943e commit f3093e3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CollapseLauncher/CollapseLauncher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<PackageReference Include="FFmpegInteropX" Version="*-*" Condition="$(DefineConstants.Contains('USEFFMPEGFORVIDEOBG'))" />

<PackageReference Include="GitInfo" Version="3.3.5" PrivateAssets="all" />
<PackageReference Include="H.NotifyIcon.WinUI" Version="2.1.2" />
<PackageReference Include="H.NotifyIcon.WinUI" Version="2.1.3" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.63" />
<PackageReference Include="ImageEx" Version="2.1.1" />
<PackageReference Include="Markdig.Signed" Version="0.37.0" />
Expand Down
6 changes: 3 additions & 3 deletions CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;
using Microsoft.UI.Xaml.Media;
using static CollapseLauncher.Dialogs.SimpleDialogs;
using static CollapseLauncher.InnerLauncherConfig;
Expand All @@ -53,6 +52,7 @@
using static Hi3Helper.Shared.Region.LauncherConfig;
using Brush = Microsoft.UI.Xaml.Media.Brush;
using Image = Microsoft.UI.Xaml.Controls.Image;
using Point = Windows.Foundation.Point;
using Size = System.Drawing.Size;
using Timer = System.Timers.Timer;
using UIElementExtensions = CollapseLauncher.Extension.UIElementExtensions;
Expand All @@ -69,8 +69,8 @@ public sealed partial class HomePage
private int barWidth;
private int consoleWidth;

public static int RefreshRateDefault { get; } = 500;
public static int RefreshRateSlow { get; } = 1000;
public static int RefreshRateDefault => 500;
public static int RefreshRateSlow => 1000;

private static int _refreshRate;

Expand Down
42 changes: 33 additions & 9 deletions CollapseLauncher/XAMLs/MainApp/TrayIcon.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using Hi3Helper;
using Hi3Helper.Shared.Region;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Drawing;
using System.Threading.Tasks;
using static CollapseLauncher.InnerLauncherConfig;
using static CollapseLauncher.Pages.HomePage;
using static Hi3Helper.InvokeProp;
Expand Down Expand Up @@ -69,18 +69,42 @@ public TrayIcon()
#endif
CloseButton.Text = _exitApp;

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

CollapseTaskbar.Icon = Icon.FromHandle(LauncherConfig.AppIconSmall);
CollapseTaskbar.Visibility = Visibility.Visible;

CollapseTaskbar.TrayIcon.MessageWindow.BalloonToolTipChanged += BalloonChangedEvent;
}

public void Dispose()
{
CollapseTaskbar.Dispose();
}

private void BalloonChangedEvent(object o, MessageWindow.BalloonToolTipChangedEventArgs args)
{
// Subscribe to the event when the Balloon is visible, and unsub when it's not.
// Due to bug, this MouseEvent is not available when the notification already went to the tray.
if (args.IsVisible)
CollapseTaskbar.TrayIcon.MessageWindow.MouseEventReceived += NotificationOnMouseEventReceived;
else
CollapseTaskbar.TrayIcon.MessageWindow.MouseEventReceived -= NotificationOnMouseEventReceived;
}

private bool _mouseEventProcessing;
private async void NotificationOnMouseEventReceived(object o, MessageWindow.MouseEventReceivedEventArgs args)
{
if (_mouseEventProcessing) return;
if (args.MouseEvent != MouseEvent.BalloonToolTipClicked) return;

_mouseEventProcessing = true;
BringToForeground();
await Task.Delay(250);
_mouseEventProcessing = false;
}
#endregion

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

/// <summary>
/// Toggle both main and console visibility while avoiding flip flop condition
/// Toggle both main and console visibility while avoiding flip-flop condition
/// </summary>
[RelayCommand]
private void ToggleAllVisibilityInvoke() => ToggleAllVisibility();
Expand Down Expand Up @@ -125,6 +149,7 @@ public void Dispose()
/// <summary>
/// Toggle console window visibility
/// </summary>
// ReSharper disable once MemberCanBePrivate.Global
public void ToggleConsoleVisibility(bool forceShow = false)
{
if (LauncherConfig.GetAppConfigValue("EnableConsole").ToBool())
Expand Down Expand Up @@ -162,7 +187,7 @@ public void ToggleMainVisibility(bool forceShow = false)
// Increase refresh rate to 1000ms when main window is hidden
RefreshRate = RefreshRateSlow;
LogWriteLine("Main window is hidden!");
ShowNotification("Main window is hidden!", "a");
WindowUtility.Tray_ShowNotification("test1", "neon stinki");
}
else
{
Expand Down Expand Up @@ -204,6 +229,7 @@ public void ToggleAllVisibility()
/// <summary>
/// Bring all windows into foreground, also brought all windows if they were hidden in taskbar.
/// </summary>
// ReSharper disable once MemberCanBePrivate.Global
public void BringToForeground()
{
IntPtr mainWindowHandle = WindowUtility.CurrentWindowPtr;
Expand Down Expand Up @@ -233,6 +259,7 @@ public void BringToForeground()
/// <summary>
/// Update tray context menu
/// </summary>
// ReSharper disable once MemberCanBePrivate.Global
public void UpdateContextMenu()
{
// Enable visibility toggle for console if the console is enabled
Expand All @@ -253,11 +280,8 @@ public void UpdateContextMenu()
bool isMainWindowVisible = IsWindowVisible(mainWindowHandle);
bool isConsoleVisible = LauncherConfig.GetAppConfigValue("EnableConsole").ToBool() && IsWindowVisible(consoleWindowHandle);

if (isConsoleVisible) ConsoleTaskbarToggle.Text = _hideConsole;
else ConsoleTaskbarToggle.Text = _showConsole;

if (isMainWindowVisible) MainTaskbarToggle.Text = _hideApp;
else MainTaskbarToggle.Text = _showApp;
ConsoleTaskbarToggle.Text = isConsoleVisible ? _hideConsole : _showConsole;
MainTaskbarToggle.Text = isMainWindowVisible ? _hideApp : _showApp;
}

/// <summary>
Expand Down
18 changes: 9 additions & 9 deletions CollapseLauncher/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@
},
"H.NotifyIcon.WinUI": {
"type": "Direct",
"requested": "[2.1.2, )",
"resolved": "2.1.2",
"contentHash": "Iwu5zEtP4ihPCUs3m0SowyKS6hA9AEcnvt0hQHZ9Tl4xu8O7lBfvmV1bomBpJPfPvpmrvTe9EfQcz+uRwiQm8A==",
"requested": "[2.1.3, )",
"resolved": "2.1.3",
"contentHash": "1tAYbvV2vyQ0lXKrMmNvpIiP5tXZibtyXCqlmH2Wjcc3i/W/EatihtVATpeuWZpxM6RA9T1H5pTZhT8+5y7wcQ==",
"dependencies": {
"H.NotifyIcon": "2.1.2",
"H.NotifyIcon": "2.1.3",
"Microsoft.WindowsAppSDK": "1.5.240627000"
}
},
Expand Down Expand Up @@ -376,18 +376,18 @@
},
"H.GeneratedIcons.System.Drawing": {
"type": "Transitive",
"resolved": "2.1.2",
"contentHash": "f2mF27nQRBGbXbEiSSxe6H12BElcH0WH3MI9lucID4Fa9I/WywFdhkFmPgALZTa2STi8rcsHTRWC7vuTfcblBQ==",
"resolved": "2.1.3",
"contentHash": "GH6MR2Qc/yyNaCj1205v2J3jT4siDmg5W+jlbJzxDuMZZtX20pEIb/QBABDS+l9brHlwPs7WygboRNeMii40Qw==",
"dependencies": {
"System.Drawing.Common": "8.0.7"
}
},
"H.NotifyIcon": {
"type": "Transitive",
"resolved": "2.1.2",
"contentHash": "5FllMntYHOHYQoLZGSMNUB2aSRrwciiQ3PT925NkI+XguZH5eDuXOQnOZvslHcmZ8VU0Qkp37XyG7agfP5nByw==",
"resolved": "2.1.3",
"contentHash": "aiCL1gjfKWh/jRlV4oDOycOqRaRj/C9E5cwHL2KCc+jre5vyUw5lHcHW6t4e/U3ssDmBKJgwhcgAW3JwMUP3ow==",
"dependencies": {
"H.GeneratedIcons.System.Drawing": "2.1.2"
"H.GeneratedIcons.System.Drawing": "2.1.3"
}
},
"Hi3Helper.ZstdNet": {
Expand Down

0 comments on commit f3093e3

Please sign in to comment.