-
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e7e4e3
commit 907a3c3
Showing
5 changed files
with
99 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System.Windows.Forms; | ||
|
||
namespace Bloxstrap.UI | ||
{ | ||
public class NotifyIconWrapper : IDisposable | ||
{ | ||
bool _disposed = false; | ||
|
||
private readonly NotifyIcon _notifyIcon; | ||
EventHandler? _alertClickHandler; | ||
|
||
public NotifyIconWrapper() | ||
{ | ||
App.Logger.WriteLine("[NotifyIconWrapper::NotifyIconWrapper] Initializing notification area icon"); | ||
|
||
_notifyIcon = new() | ||
{ | ||
Icon = Properties.Resources.IconBloxstrap, | ||
Text = App.ProjectName, | ||
Visible = true | ||
}; | ||
} | ||
|
||
public void ShowAlert(string caption, string message, int duration, EventHandler? clickHandler) | ||
{ | ||
string id = Guid.NewGuid().ToString()[..8]; | ||
|
||
App.Logger.WriteLine($"[NotifyIconWrapper::ShowAlert] [{id}] Showing alert for {duration} seconds (clickHandler={clickHandler is not null})"); | ||
App.Logger.WriteLine($"[NotifyIconWrapper::ShowAlert] [{id}] {caption}: {message.Replace("\n", "\\n")}"); | ||
|
||
_notifyIcon.BalloonTipTitle = caption; | ||
_notifyIcon.BalloonTipText = message; | ||
|
||
if (_alertClickHandler is not null) | ||
{ | ||
App.Logger.WriteLine($"[NotifyIconWrapper::ShowAlert] [{id}] Previous alert still present, erasing click handler"); | ||
_notifyIcon.BalloonTipClicked -= _alertClickHandler; | ||
} | ||
|
||
_alertClickHandler = clickHandler; | ||
_notifyIcon.BalloonTipClicked += clickHandler; | ||
|
||
_notifyIcon.ShowBalloonTip(duration); | ||
|
||
Task.Run(async () => | ||
{ | ||
await Task.Delay(duration * 1000); | ||
_notifyIcon.BalloonTipClicked -= clickHandler; | ||
App.Logger.WriteLine($"[NotifyIconWrapper::ShowAlert] [{id}] Duration over, erasing current click handler"); | ||
if (_alertClickHandler == clickHandler) | ||
_alertClickHandler = null; | ||
else | ||
App.Logger.WriteLine($"[NotifyIconWrapper::ShowAlert] [{id}] Click handler has been overriden by another alert"); | ||
}); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_disposed) | ||
return; | ||
|
||
_notifyIcon.Dispose(); | ||
|
||
_disposed = true; | ||
|
||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters