Skip to content

Commit

Permalink
Merge pull request #11 from Novack/main
Browse files Browse the repository at this point in the history
Added notification counter badge to app taskbar icon
  • Loading branch information
amilich authored Sep 23, 2023
2 parents 1e2f69a + 2585881 commit 8fc7563
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
30 changes: 30 additions & 0 deletions skiffWindowsApp/Skiff Desktop/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
using CustomNotificationsExample.CustomMessage;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Drawing;
using System.Windows.Media;
using System.Windows.Interop;
using System.Windows.Media.Imaging;

namespace Skiff_Desktop
{
Expand Down Expand Up @@ -57,6 +61,7 @@ public MainWindow()
StateChanged += OnWindowStateChanged;
SizeChanged += OnWindowSizeChanged;
RestoreWindow();
TaskbarItemInfo = new();
}

internal void RestoreWindow()
Expand Down Expand Up @@ -209,6 +214,31 @@ internal void UpdateUnreadCount(int newTotal)
{
UnreadCount = newTotal;
UnreadCounterChanged?.Invoke();

TaskbarItemInfo.Overlay = null;
if (UnreadCount > 0)
TaskbarItemInfo.Overlay = GetCounterBadge();
}

private ImageSource GetCounterBadge()
{
using Bitmap bitmap = Properties.Resources.badgebg;
using Graphics graphics = Graphics.FromImage(bitmap);

using StringFormat format = new()
{
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Center,
};
var point = new PointF(15f, 17f);
var font = new Font(System.Drawing.FontFamily.GenericSansSerif, emSize: 5f, System.Drawing.FontStyle.Bold);
string counterStr = Math.Clamp(UnreadCount, 0, 99).ToString();
graphics.DrawString(counterStr, font, System.Drawing.Brushes.White, point, format);

return Imaging.CreateBitmapSourceFromHIcon(
bitmap.GetHicon(),
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
}

Expand Down
10 changes: 10 additions & 0 deletions skiffWindowsApp/Skiff Desktop/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions skiffWindowsApp/Skiff Desktop/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="badgebg" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\badgebg.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="logo" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\logo.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
Expand Down
Binary file added skiffWindowsApp/Skiff Desktop/badgebg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8fc7563

Please sign in to comment.