Added notification icon fallback support

This commit is contained in:
Daniel Clark
2021-02-14 20:59:45 +00:00
parent 9869470c24
commit 50033d9aa3
14 changed files with 80 additions and 42 deletions
@@ -21,4 +21,9 @@
<PackageReference Include="System.Drawing.Common" Version="6.0.0-preview.1.21102.12" />
<PackageReference Include="System.Memory" Version="4.5.4" />
</ItemGroup>
<PropertyGroup>
<EnableTypeInfoReflection>false</EnableTypeInfoReflection>
<EnableXBindDiagnostics>false</EnableXBindDiagnostics>
</PropertyGroup>
</Project>
@@ -46,11 +46,6 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls
flyout.Hide();
}
internal void UpdateThemeVisualState()
{
}
public void SetFlyoutPlacement(string placement)
{
if (!_isLoaded)
@@ -73,7 +68,7 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls
});
}
internal void SetOwningFlyout(NotificationFlyout flyout)
public void SetOwningFlyout(NotificationFlyout flyout)
{
_notificationFlyout = flyout;
@@ -103,12 +98,8 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls
});
}
private NotificationFlyoutPresenter _flyoutPresenter;
protected override void OnApplyTemplate()
{
_flyoutPresenter = GetTemplateChild("FlyoutPresenter") as NotificationFlyoutPresenter;
_flyout = GetTemplateChild("Flyout") as Flyout;
if (_flyout != null)
{
Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

@@ -5,6 +5,9 @@ using TheXamlGuy.NotificationFlyout.Wpf.UI.Extensions;
using System;
using System.Windows;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls
{
@@ -105,20 +108,7 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls
private void OnTaskbarChanged(object sender, EventArgs args) => UpdateWindow();
private void OnThemeChanged(object sender, SystemPersonalisationChangedEventArgs args)
{
UpdateFlyoutTheme(args.IsColorPrevalence);
UpdateIcons();
}
private void UpdateFlyoutTheme(bool isColorPrevalence)
{
var content = GetHostContent();
if (content != null)
{
// content.UpdateFlyoutTheme(isColorPrevalence);
}
}
private void OnThemeChanged(object sender, SystemPersonalisationChangedEventArgs args) => UpdateIcons();
private void PrepareContextMenu()
{
@@ -168,18 +158,23 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls
if (!IsLoaded) return;
if (_flyout == null) return;
var iconSource = _flyout.IconSource;
var lightIconSource = _flyout.LightIconSource;
var shellTrayHandle = WindowHelper.GetHandle(ShellTrayHandleName);
if (shellTrayHandle == null) return;
var desiredIconSource = _systemPersonalisationHelper.Theme == SystemTheme.Dark ? iconSource : lightIconSource;
if (desiredIconSource == null) return;
var dpi = WindowHelper.GetDpi(shellTrayHandle);
using var icon = await desiredIconSource.ConvertToIconAsync(dpi);
_notificationIconHelper.SetIcon(icon.Handle);
var desiredIconSource = _systemPersonalisationHelper.Theme == SystemTheme.Dark ? _flyout.IconSource : _flyout.LightIconSource;
if (desiredIconSource == null)
{
var fallbackIconSource = new BitmapImage(new Uri($"pack://application:,,,/{GetType().Namespace};component/Assets/notification-icon-{(_systemPersonalisationHelper.Theme == SystemTheme.Dark ? "default" : "light")}.ico"));
using var icon = fallbackIconSource.ConvertToIcon(dpi);
_notificationIconHelper.SetIcon(icon.Handle);
}
else
{
using var icon = await desiredIconSource.ConvertToIconAsync(dpi);
_notificationIconHelper.SetIcon(icon.Handle);
}
}
private void UpdateWindow()
@@ -12,6 +12,11 @@
<Version>1.0.0</Version>
</PropertyGroup>
<ItemGroup>
<None Remove="Assets\notification-icon-default.ico" />
<None Remove="Assets\notification-icon-light.ico" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Toolkit.Wpf.UI.XamlHost" Version="6.1.2" />
</ItemGroup>
@@ -22,6 +27,11 @@
<ProjectReference Include="..\TheXamlGuy.NotificationFlyout.Wpf.UI\TheXamlGuy.NotificationFlyout.Wpf.UI.csproj" />
</ItemGroup>
<ItemGroup>
<Resource Include="Assets\notification-icon-light.ico" />
<Resource Include="Assets\notification-icon-default.ico" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="Microsoft.VCRTForwarders.140" Version="1.0.7" />
</ItemGroup>
@@ -23,6 +23,7 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls
protected virtual void OnContentLoaded()
{
}
protected virtual WindowsXamlHost OnPreparingXamlHost(WindowsXamlHost xamlHost)
@@ -0,0 +1,31 @@
using System;
using System.Drawing;
using System.Windows;
using System.Windows.Media;
using Microsoft.Windows.Sdk;
namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Extensions
{
public static class ImageSourceExtensions
{
public static Icon ConvertToIcon(this ImageSource imageSource, uint dpi)
{
if (imageSource == null) return null;
var uri = new Uri(imageSource.ToString(), UriKind.RelativeOrAbsolute);
var streamResource = Application.GetResourceStream(uri);
if (streamResource == null) throw new ArgumentException(nameof(streamResource));
return new Icon(streamResource.Stream, new System.Drawing.Size(PInvoke.GetSystemMetricsForDpi((int)SystemMetricFlag.SM_CXICON, dpi), PInvoke.GetSystemMetricsForDpi((int)SystemMetricFlag.SM_CYICON, dpi)));
}
private enum SystemMetricFlag : int
{
SM_CXICON = 11,
SM_CYICON = 12,
SM_CXSMICON = 49,
SM_CYSMICON = 50
}
}
}
@@ -0,0 +1 @@

@@ -11,6 +11,13 @@
<Version>1.0.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.1.319-beta">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TheXamlGuy.NotificationFlyout.Shared.UI\TheXamlGuy.NotificationFlyout.Shared.UI.csproj" />
</ItemGroup>