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
@@ -6,6 +6,7 @@
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
<Platforms>AnyCPU;x64</Platforms> <Platforms>AnyCPU;x64</Platforms>
<StartupObject>NotificationFlyoutSample.Host.Program</StartupObject> <StartupObject>NotificationFlyoutSample.Host.Program</StartupObject>
<AssetTargetFallback>uap10.0.19041</AssetTargetFallback>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@@ -172,14 +172,14 @@
</Page> </Page>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\TheXamlGuy.NotificationFlyout.Shared.UI\TheXamlGuy.NotificationFlyout.Shared.UI.csproj">
<Project>{7d6a151b-c021-4d34-adc1-0171a2f65a00}</Project>
<Name>TheXamlGuy.NotificationFlyout.Shared.UI</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\TheXamlGuy.NotificationFlyout.Uwp.UI.Controls\TheXamlGuy.NotificationFlyout.Uwp.UI.Controls.csproj"> <ProjectReference Include="..\..\src\TheXamlGuy.NotificationFlyout.Uwp.UI.Controls\TheXamlGuy.NotificationFlyout.Uwp.UI.Controls.csproj">
<Project>{8501316d-f994-4a29-9ec4-dc6c6b93f565}</Project> <Project>{8501316d-f994-4a29-9ec4-dc6c6b93f565}</Project>
<Name>TheXamlGuy.NotificationFlyout.Uwp.UI.Controls</Name> <Name>TheXamlGuy.NotificationFlyout.Uwp.UI.Controls</Name>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\..\src\TheXamlGuy.NotificationFlyout.Shared.UI\TheXamlGuy.NotificationFlyout.Shared.UI.csproj">
<Project>{8501316d-f994-4a29-9ec4-dc6c6b93f565}</Project>
<Name>TheXamlGuy.NotificationFlyout.Shared.UI</Name>
</ProjectReference>
</ItemGroup> </ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' "> <PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion> <VisualStudioVersion>14.0</VisualStudioVersion>
+1 -3
View File
@@ -3,9 +3,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:TheXamlGuy.NotificationFlyout.Uwp.UI.Controls" xmlns:controls="using:TheXamlGuy.NotificationFlyout.Uwp.UI.Controls"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls" xmlns:muxc="using:Microsoft.UI.Xaml.Controls">
IconSource="/Assets/Icon.ico"
LightIconSource="/Assets/Icon-Light.ico">
<controls:NotificationFlyout.FlyoutPresenterStyle> <controls:NotificationFlyout.FlyoutPresenterStyle>
<Style TargetType="controls:NotificationFlyoutPresenter"> <Style TargetType="controls:NotificationFlyoutPresenter">
<Setter Property="CornerRadius" Value="8" /> <Setter Property="CornerRadius" Value="8" />
@@ -1,6 +1,4 @@
using Windows.UI.Xaml.Controls; namespace NotificationFlyoutSample
namespace NotificationFlyoutSample
{ {
public sealed partial class Shell public sealed partial class Shell
{ {
@@ -21,4 +21,9 @@
<PackageReference Include="System.Drawing.Common" Version="6.0.0-preview.1.21102.12" /> <PackageReference Include="System.Drawing.Common" Version="6.0.0-preview.1.21102.12" />
<PackageReference Include="System.Memory" Version="4.5.4" /> <PackageReference Include="System.Memory" Version="4.5.4" />
</ItemGroup> </ItemGroup>
<PropertyGroup>
<EnableTypeInfoReflection>false</EnableTypeInfoReflection>
<EnableXBindDiagnostics>false</EnableXBindDiagnostics>
</PropertyGroup>
</Project> </Project>
@@ -46,11 +46,6 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls
flyout.Hide(); flyout.Hide();
} }
internal void UpdateThemeVisualState()
{
}
public void SetFlyoutPlacement(string placement) public void SetFlyoutPlacement(string placement)
{ {
if (!_isLoaded) if (!_isLoaded)
@@ -73,7 +68,7 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls
}); });
} }
internal void SetOwningFlyout(NotificationFlyout flyout) public void SetOwningFlyout(NotificationFlyout flyout)
{ {
_notificationFlyout = flyout; _notificationFlyout = flyout;
@@ -103,12 +98,8 @@ namespace TheXamlGuy.NotificationFlyout.Uwp.UI.Controls
}); });
} }
private NotificationFlyoutPresenter _flyoutPresenter;
protected override void OnApplyTemplate() protected override void OnApplyTemplate()
{ {
_flyoutPresenter = GetTemplateChild("FlyoutPresenter") as NotificationFlyoutPresenter;
_flyout = GetTemplateChild("Flyout") as Flyout; _flyout = GetTemplateChild("Flyout") as Flyout;
if (_flyout != null) 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;
using System.Windows; using System.Windows;
using Windows.UI.Xaml.Controls.Primitives; 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 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 OnTaskbarChanged(object sender, EventArgs args) => UpdateWindow();
private void OnThemeChanged(object sender, SystemPersonalisationChangedEventArgs args) private void OnThemeChanged(object sender, SystemPersonalisationChangedEventArgs args) => UpdateIcons();
{
UpdateFlyoutTheme(args.IsColorPrevalence);
UpdateIcons();
}
private void UpdateFlyoutTheme(bool isColorPrevalence)
{
var content = GetHostContent();
if (content != null)
{
// content.UpdateFlyoutTheme(isColorPrevalence);
}
}
private void PrepareContextMenu() private void PrepareContextMenu()
{ {
@@ -168,18 +158,23 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls
if (!IsLoaded) return; if (!IsLoaded) return;
if (_flyout == null) return; if (_flyout == null) return;
var iconSource = _flyout.IconSource;
var lightIconSource = _flyout.LightIconSource;
var shellTrayHandle = WindowHelper.GetHandle(ShellTrayHandleName); var shellTrayHandle = WindowHelper.GetHandle(ShellTrayHandleName);
if (shellTrayHandle == null) return; if (shellTrayHandle == null) return;
var desiredIconSource = _systemPersonalisationHelper.Theme == SystemTheme.Dark ? iconSource : lightIconSource;
if (desiredIconSource == null) return;
var dpi = WindowHelper.GetDpi(shellTrayHandle); 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() private void UpdateWindow()
@@ -12,6 +12,11 @@
<Version>1.0.0</Version> <Version>1.0.0</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Remove="Assets\notification-icon-default.ico" />
<None Remove="Assets\notification-icon-light.ico" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Toolkit.Wpf.UI.XamlHost" Version="6.1.2" /> <PackageReference Include="Microsoft.Toolkit.Wpf.UI.XamlHost" Version="6.1.2" />
</ItemGroup> </ItemGroup>
@@ -22,6 +27,11 @@
<ProjectReference Include="..\TheXamlGuy.NotificationFlyout.Wpf.UI\TheXamlGuy.NotificationFlyout.Wpf.UI.csproj" /> <ProjectReference Include="..\TheXamlGuy.NotificationFlyout.Wpf.UI\TheXamlGuy.NotificationFlyout.Wpf.UI.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Resource Include="Assets\notification-icon-light.ico" />
<Resource Include="Assets\notification-icon-default.ico" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Update="Microsoft.VCRTForwarders.140" Version="1.0.7" /> <PackageReference Update="Microsoft.VCRTForwarders.140" Version="1.0.7" />
</ItemGroup> </ItemGroup>
@@ -23,6 +23,7 @@ namespace TheXamlGuy.NotificationFlyout.Wpf.UI.Controls
protected virtual void OnContentLoaded() protected virtual void OnContentLoaded()
{ {
} }
protected virtual WindowsXamlHost OnPreparingXamlHost(WindowsXamlHost xamlHost) 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> <Version>1.0.0</Version>
</PropertyGroup> </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> <ItemGroup>
<ProjectReference Include="..\TheXamlGuy.NotificationFlyout.Shared.UI\TheXamlGuy.NotificationFlyout.Shared.UI.csproj" /> <ProjectReference Include="..\TheXamlGuy.NotificationFlyout.Shared.UI\TheXamlGuy.NotificationFlyout.Shared.UI.csproj" />
</ItemGroup> </ItemGroup>