Move *everything* from setting flyout content and the flyout icons to a UWP control and simplified creation of the wpf host via a NotificationFlyoutApplication where you only need to set the UWP flyout

This commit is contained in:
Daniel Clark
2021-02-10 11:49:16 +00:00
parent 083c7b9365
commit b5b8641527
27 changed files with 315 additions and 166 deletions
@@ -11,11 +11,6 @@
<EnableXBindDiagnostics>false</EnableXBindDiagnostics>
</PropertyGroup>
<ItemGroup>
<None Remove="Assets\Icon-Light.ico" />
<None Remove="Assets\Icon.ico" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Toolkit.Wpf.UI.XamlHost" Version="6.1.2" />
</ItemGroup>
@@ -27,9 +22,4 @@
<ProjectReference Include="..\NotificationFlyoutSample\NotificationFlyoutSample.csproj" />
</ItemGroup>
<ItemGroup>
<Resource Include="Assets\Icon-Light.ico" />
<Resource Include="Assets\Icon.ico" />
</ItemGroup>
</Project>
@@ -1,4 +1,5 @@
using System;
using NotificationFlyout.Wpf.UI.Controls;
using System;
namespace NotificationFlyoutSample.Host
{
@@ -10,7 +11,10 @@ namespace NotificationFlyoutSample.Host
using (new NotificationFlyoutSample.App())
{
var app = new App();
new SampleNotificationFlyout();
new NotificationFlyoutApplication
{
Flyout = new Shell()
};
app.Run();
}
}
@@ -1,9 +0,0 @@
<NotificationFlyout
x:Class="NotificationFlyoutSample.Host.SampleNotificationFlyout"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uwp="clr-namespace:NotificationFlyoutSample;assembly=NotificationFlyoutSample"
IconSource="/Assets/Icon.ico"
LightIconSource="/Assets/Icon-Light.ico">
<uwp:Shell />
</NotificationFlyout>
@@ -1,10 +0,0 @@
namespace NotificationFlyoutSample.Host
{
public partial class SampleNotificationFlyout
{
public SampleNotificationFlyout()
{
InitializeComponent();
}
}
}

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

@@ -130,6 +130,8 @@
</AppxManifest>
</ItemGroup>
<ItemGroup>
<Content Include="Assets\Icon-Light.ico" />
<Content Include="Assets\Icon.ico" />
<Content Include="Properties\Default.rd.xml" />
<Content Include="Assets\LockScreenLogo.scale-200.png" />
<Content Include="Assets\SplashScreen.scale-200.png" />
+7 -3
View File
@@ -1,8 +1,11 @@
<UserControl
<controls:NotificationFlyout
x:Class="NotificationFlyoutSample.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:winui="using:Microsoft.UI.Xaml.Controls">
xmlns:controls="using:NotificationFlyout.Uwp.UI.Controls"
xmlns:winui="using:Microsoft.UI.Xaml.Controls"
LightIconSource="/Assets/Icon.ico">
<controls:NotificationFlyout.Content>
<StackPanel
Width="330"
Height="500"
@@ -18,4 +21,5 @@
<ToggleButton Margin="0,0,0,8" />
<CalendarView Margin="0,0,0,8" />
</StackPanel>
</UserControl>
</controls:NotificationFlyout.Content>
</controls:NotificationFlyout>
+12 -12
View File
@@ -9,18 +9,18 @@
private void Theme_SelectionChanged(object sender, Windows.UI.Xaml.Controls.SelectionChangedEventArgs e)
{
switch (Theme.SelectedIndex)
{
case 0:
RequestedTheme = Windows.UI.Xaml.ElementTheme.Default;
break;
case 1:
RequestedTheme = Windows.UI.Xaml.ElementTheme.Dark;
break;
case 2:
RequestedTheme = Windows.UI.Xaml.ElementTheme.Light;
break;
}
//switch (Theme.SelectedIndex)
//{
// case 0:
// RequestedTheme = Windows.UI.Xaml.ElementTheme.Default;
// break;
// case 1:
// RequestedTheme = Windows.UI.Xaml.ElementTheme.Dark;
// break;
// case 2:
// RequestedTheme = Windows.UI.Xaml.ElementTheme.Light;
// break;
//}
}
}
}
@@ -0,0 +1,29 @@
using System;
using System.Drawing;
namespace NotificationFlyout.Shared.UI
{
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,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
@@ -8,11 +8,11 @@
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<Page Include="NotificationFlyoutHost\NotificationFlyoutHost.xaml">
<Page Include="NotificationFlyout\NotificationFlyoutHost.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="NotificationFlyoutPresenter\NotificationFlyoutPresenter.xaml">
<Page Include="NotificationFlyout\NotificationFlyoutPresenter.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
@@ -0,0 +1,56 @@
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
namespace NotificationFlyout.Uwp.UI.Controls
{
public class NotificationFlyout : DependencyObject
{
public static readonly DependencyProperty IconSourceProperty =
DependencyProperty.Register(nameof(IconSource),
typeof(ImageSource), typeof(NotificationFlyout),
new PropertyMetadata(null, OnIconPropertyChanged));
public static readonly DependencyProperty LightIconSourceProperty =
DependencyProperty.Register(nameof(LightIconSource),
typeof(ImageSource), typeof(NotificationFlyout),
new PropertyMetadata(null, OnIconPropertyChanged));
public static DependencyProperty ContentProperty =
DependencyProperty.Register(nameof(Content),
typeof(UIElement), typeof(NotificationFlyout),
new PropertyMetadata(null));
internal event EventHandler ContentChanged;
internal event EventHandler IconSourceChanged;
public UIElement Content
{
get => (UIElement)GetValue(ContentProperty);
set => SetValue(ContentProperty, value);
}
public ImageSource IconSource
{
get => (ImageSource)GetValue(IconSourceProperty);
set => SetValue(IconSourceProperty, value);
}
public ImageSource LightIconSource
{
get => (ImageSource)GetValue(LightIconSourceProperty);
set => SetValue(LightIconSourceProperty, value);
}
private static void OnIconPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
var sender = dependencyObject as NotificationFlyout;
sender?.OnIconPropertyChanged();
}
private void OnIconPropertyChanged()
{
IconSourceChanged?.Invoke(this, EventArgs.Empty);
}
}
}
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("NotificationFlyout.Uwp.UI.Controls")]
@@ -12,3 +13,4 @@ using System.Runtime.InteropServices;
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: ComVisible(false)]
[assembly: InternalsVisibleTo("NotificationFlyout.Wpf.UI.Controls")]
@@ -1,6 +1,6 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///NotificationFlyout.Uwp.UI.Controls/NotificationFlyoutHost/NotificationFlyoutHost.xaml" />
<ResourceDictionary Source="ms-appx:///NotificationFlyout.Uwp.UI.Controls/NotificationFlyoutPresenter/NotificationFlyoutPresenter.xaml" />
<ResourceDictionary Source="ms-appx:///NotificationFlyout.Uwp.UI.Controls/NotificationFlyout/NotificationFlyoutHost.xaml" />
<ResourceDictionary Source="ms-appx:///NotificationFlyout.Uwp.UI.Controls/NotificationFlyout/NotificationFlyoutPresenter.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
@@ -0,0 +1,29 @@
using Microsoft.Windows.Sdk;
using System;
using System.Drawing;
using System.IO;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
namespace NotificationFlyout.Uwp.UI.Extensions
{
public static class ImageSourceExtensions
{
public static Icon ConvertToIcon(this ImageSource imageSource, uint dpi)
{
var bitmapImage = (BitmapImage)imageSource;
var uri = $"{AppDomain.CurrentDomain.BaseDirectory}{bitmapImage.UriSource}".Replace("ms-appx:///", "").Replace("/", "\\");
using var stream = File.OpenRead(uri);
return new Icon(stream, new 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 @@
GetSystemMetricsForDpi
@@ -0,0 +1,28 @@
<Project Sdk="MSBuild.Sdk.Extras">
<PropertyGroup>
<TargetFrameworks>uap10.0.19041</TargetFrameworks>
<Platforms>AnyCPU;x64</Platforms>
<EnableTypeInfoReflection>false</EnableTypeInfoReflection>
<EnableXBindDiagnostics>false</EnableXBindDiagnostics>
<LangVersion>9.0</LangVersion>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<Compile Remove="NotificationFlyout\**" />
<EmbeddedResource Remove="NotificationFlyout\**" />
<None Remove="NotificationFlyout\**" />
</ItemGroup>
<ItemGroup>
<None Remove="NativeMethods.txt" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="NativeMethods.txt" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.1.319-beta">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
</ItemGroup>
</Project>
@@ -13,6 +13,7 @@
<ItemGroup>
<ProjectReference Include="..\NotificationFlyout.Uwp.UI.Controls\NotificationFlyout.Uwp.UI.Controls.csproj" />
<ProjectReference Include="..\NotificationFlyout.Uwp.UI\NotificationFlyout.Uwp.UI.csproj" />
<ProjectReference Include="..\NotificationFlyout.Wpf.UI\NotificationFlyout.Wpf.UI.csproj" />
</ItemGroup>
@@ -1,83 +0,0 @@
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;
namespace NotificationFlyout.Wpf.UI.Controls
{
[ContentProperty(nameof(Content))]
public class NotificationFlyout : DependencyObject
{
public static readonly DependencyProperty IconSourceProperty =
DependencyProperty.Register(nameof(IconSource),
typeof(ImageSource), typeof(NotificationFlyout),
new PropertyMetadata(null, OnIconPropertyChanged));
public static readonly DependencyProperty LightIconSourceProperty =
DependencyProperty.Register(nameof(LightIconSource),
typeof(ImageSource), typeof(NotificationFlyout),
new PropertyMetadata(null, OnIconPropertyChanged));
public static DependencyProperty ContentProperty =
DependencyProperty.Register(nameof(Content),
typeof(Windows.UI.Xaml.UIElement), typeof(NotificationFlyout),
new PropertyMetadata(null, OnFlyoutPresenterPropertyChanged));
private readonly NotificationFlyoutXamlHost _xamlHost;
public NotificationFlyout()
{
_xamlHost = new NotificationFlyoutXamlHost();
_xamlHost.Show();
}
public Windows.UI.Xaml.UIElement Content
{
get => (Windows.UI.Xaml.UIElement)GetValue(ContentProperty);
set => SetValue(ContentProperty, value);
}
public ImageSource IconSource
{
get => (ImageSource)GetValue(IconSourceProperty);
set => SetValue(IconSourceProperty, value);
}
public ImageSource LightIconSource
{
get => (ImageSource)GetValue(LightIconSourceProperty);
set => SetValue(LightIconSourceProperty, value);
}
public void HideFlyout()
{
_xamlHost.HideFlyout();
}
public void ShowFlyout()
{
_xamlHost.ShowFlyout();
}
private static void OnFlyoutPresenterPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var sender = dependencyObject as NotificationFlyout;
sender?.OnFlyoutPresenterPropertyChanged();
}
private static void OnIconPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
var sender = dependencyObject as NotificationFlyout;
sender?.OnIconPropertyChanged();
}
private void OnFlyoutPresenterPropertyChanged()
{
_xamlHost.SetFlyoutContent(Content);
}
private void OnIconPropertyChanged()
{
_xamlHost.SetIcons(IconSource, LightIconSource);
}
}
}
@@ -0,0 +1,49 @@
using System.Windows;
using System.Windows.Markup;
namespace NotificationFlyout.Wpf.UI.Controls
{
[ContentProperty(nameof(Flyout))]
public class NotificationFlyoutApplication : DependencyObject
{
public static DependencyProperty FlyoutProperty =
DependencyProperty.Register(nameof(Flyout),
typeof(Uwp.UI.Controls.NotificationFlyout), typeof(NotificationFlyoutApplication),
new PropertyMetadata(null, OnFlyoutPropertyChanged));
private readonly NotificationFlyoutXamlHost _xamlHost;
public NotificationFlyoutApplication()
{
_xamlHost = new NotificationFlyoutXamlHost();
_xamlHost.Show();
}
public Uwp.UI.Controls.NotificationFlyout Flyout
{
get => (Uwp.UI.Controls.NotificationFlyout)GetValue(FlyoutProperty);
set => SetValue(FlyoutProperty, value);
}
public void HideFlyout()
{
_xamlHost.HideFlyout();
}
public void ShowFlyout()
{
_xamlHost.ShowFlyout();
}
private static void OnFlyoutPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var sender = dependencyObject as NotificationFlyoutApplication;
sender?.OnFlyoutPropertyChanged();
}
private void OnFlyoutPropertyChanged()
{
_xamlHost.SetFlyout(Flyout);
}
}
}
@@ -1,6 +1,7 @@
using Microsoft.Toolkit.Wpf.UI.XamlHost;
using NotificationFlyout.Uwp.UI.Controls;
using NotificationFlyout.Wpf.UI.Extensions;
using NotificationFlyout.Uwp.UI.Extensions;
using NotificationFlyout.Wpf.UI.Helpers;
using System;
using System.Windows;
@@ -14,9 +15,8 @@ namespace NotificationFlyout.Wpf.UI.Controls
private const string ShellTrayHandleName = "Shell_TrayWnd";
private const double WindowSize = 5;
private ImageSource _defaultIconSource;
private Uwp.UI.Controls.NotificationFlyout _flyout;
private bool _isLoaded;
private ImageSource _lightIconSource;
private NotificationIconHelper _notificationIconHelper;
private SystemPersonalisationHelper _systemPersonalisationHelper;
private TaskbarHelper _taskbarHelper;
@@ -30,13 +30,25 @@ namespace NotificationFlyout.Wpf.UI.Controls
Loaded += OnLoaded;
}
public void SetFlyoutContent(Windows.UI.Xaml.UIElement content)
public void SetFlyout(Uwp.UI.Controls.NotificationFlyout flyout)
{
var flyoutHost = GetFlyoutHost();
if (flyoutHost != null)
if (_flyout != null)
{
flyoutHost.Content = content;
_flyout.ContentChanged -= OnFlyoutContentChanged;
_flyout.IconSourceChanged -= OnFlyoutIconSourceChanged;
}
_flyout = flyout;
_flyout.ContentChanged += OnFlyoutContentChanged;
_flyout.IconSourceChanged += OnFlyoutIconSourceChanged;
UpdateFlyoutContent();
UpdateIcons();
}
private void OnFlyoutIconSourceChanged(object sender, EventArgs args)
{
UpdateIcons();
}
internal void HideFlyout()
@@ -48,14 +60,6 @@ namespace NotificationFlyout.Wpf.UI.Controls
}
}
internal void SetIcons(ImageSource defaultIconSource, ImageSource lightIconSource)
{
_defaultIconSource = defaultIconSource;
_lightIconSource = lightIconSource;
UpdateIcon();
}
internal void ShowFlyout()
{
var flyoutHost = GetFlyoutHost();
@@ -82,6 +86,11 @@ namespace NotificationFlyout.Wpf.UI.Controls
return _xamlHost.GetUwpInternalObject() as NotificationFlyoutHost;
}
private void OnFlyoutContentChanged(object sender, EventArgs args)
{
UpdateFlyoutContent();
}
private void OnIconInvoked(object sender, NotificationIconInvokedEventArgs args)
{
ShowFlyout();
@@ -95,7 +104,6 @@ namespace NotificationFlyout.Wpf.UI.Controls
_isLoaded = true;
UpdateWindow();
UpdateIcon();
this.Hidden();
}
@@ -106,7 +114,7 @@ namespace NotificationFlyout.Wpf.UI.Controls
private void OnThemeChanged(object sender, SystemPersonalisationChangedEventArgs args)
{
UpdateIcon();
UpdateIcons();
}
private void PrepareDefaultWindow()
@@ -149,10 +157,29 @@ namespace NotificationFlyout.Wpf.UI.Controls
Content = _xamlHost;
}
private void UpdateIcon()
private void UpdateFlyoutContent()
{
if (_flyout == null) return;
var content = _flyout.Content;
if (content == null) return;
var flyoutHost = GetFlyoutHost();
if (flyoutHost != null)
{
flyoutHost.Content = content;
}
}
private void UpdateIcons()
{
if (!_isLoaded) return;
if (_flyout == null) return;
var _defaultIconSource = _flyout.IconSource;
var _lightIconSource = _flyout.LightIconSource;
var shellTrayHandle = WindowHelper.GetHandle(ShellTrayHandleName);
if (shellTrayHandle == null) return;
+22
View File
@@ -14,6 +14,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
global.json = global.json
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NotificationFlyout.Uwp.UI", "NotificationFlyout.Uwp.UI\NotificationFlyout.Uwp.UI.csproj", "{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -88,6 +90,26 @@ Global
{29430194-7EDE-4C33-AF59-CE121F48F66E}.Release|x64.Build.0 = Release|x64
{29430194-7EDE-4C33-AF59-CE121F48F66E}.Release|x86.ActiveCfg = Release|Any CPU
{29430194-7EDE-4C33-AF59-CE121F48F66E}.Release|x86.Build.0 = Release|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Debug|ARM.ActiveCfg = Debug|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Debug|ARM.Build.0 = Debug|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Debug|ARM64.Build.0 = Debug|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Debug|x64.ActiveCfg = Debug|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Debug|x64.Build.0 = Debug|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Debug|x86.ActiveCfg = Debug|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Debug|x86.Build.0 = Debug|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Release|Any CPU.Build.0 = Release|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Release|ARM.ActiveCfg = Release|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Release|ARM.Build.0 = Release|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Release|ARM64.ActiveCfg = Release|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Release|ARM64.Build.0 = Release|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Release|x64.ActiveCfg = Release|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Release|x64.Build.0 = Release|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Release|x86.ActiveCfg = Release|Any CPU
{3FE0D7F7-1173-4989-BE6C-AD28FE0D4AC9}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE