Added API for opening a window from flyout w/ sample

This commit is contained in:
Daniel Clark
2021-02-11 15:09:20 +00:00
parent 0d28dad842
commit 4a6c6ca4a5
12 changed files with 182 additions and 77 deletions
@@ -1,7 +1,11 @@
namespace NotificationFlyout.Uwp.UI
using Windows.UI.Xaml;
namespace NotificationFlyout.Uwp.UI
{
public interface INotificationFlyoutApplication
{
void Exit();
void OpenAsWindow<TUIElement>() where TUIElement : UIElement;
}
}
@@ -48,6 +48,12 @@ namespace NotificationFlyout.Wpf.UI.Controls
_notificationFlyoutXamlHost.HideFlyout();
}
public void OpenAsWindow<TUIElement>() where TUIElement : Windows.UI.Xaml.UIElement
{
var window = new XamlHost<TUIElement>();
window.Show();
}
public void ShowFlyout()
{
_notificationFlyoutXamlHost.ShowFlyout();
@@ -1,70 +0,0 @@
using Microsoft.Toolkit.Wpf.UI.XamlHost;
using NotificationFlyout.Wpf.UI.Extensions;
using System;
using System.Windows;
using System.Windows.Media;
namespace NotificationFlyout.Wpf.UI.Controls
{
internal class XamlHostWindow<TXamlContent> : Window where TXamlContent : Windows.UI.Xaml.UIElement
{
internal const double WindowSize = 5;
protected new bool IsLoaded;
private WindowsXamlHost _xamlHost;
public XamlHostWindow()
{
PrepareDefaultWindow();
PrepareWindowsXamlHost();
Loaded += OnLoaded;
ContentRendered += OnContentRendered;
}
internal TXamlContent GetHostContent()
{
if (_xamlHost == null) return null;
return _xamlHost.GetUwpInternalObject() as TXamlContent;
}
protected virtual void OnContentLoaded()
{
}
private void OnContentRendered(object sender, EventArgs args)
{
IsLoaded = true;
OnContentLoaded();
}
private void OnLoaded(object sender, RoutedEventArgs args)
{
this.Hidden();
}
private void PrepareDefaultWindow()
{
ShowInTaskbar = false;
ShowActivated = false;
WindowStyle = WindowStyle.None;
ResizeMode = ResizeMode.NoResize;
AllowsTransparency = true;
Background = new SolidColorBrush(Colors.Transparent);
Height = WindowSize;
Width = WindowSize;
}
private void PrepareWindowsXamlHost()
{
_xamlHost = new WindowsXamlHost
{
InitialTypeName = typeof(TXamlContent).FullName,
Height = 0,
Width = 0,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch
};
Content = _xamlHost;
}
}
}
@@ -7,7 +7,7 @@ using Windows.UI.Xaml.Controls;
namespace NotificationFlyout.Wpf.UI.Controls
{
internal class ContextMenuXamlHost : XamlHostWindow<ContextMenuFlyoutHost>
internal class ContextMenuXamlHost : TransparentXamlHost<ContextMenuFlyoutHost>
{
private Uwp.UI.Controls.NotificationFlyout _flyout;
@@ -1,15 +1,15 @@
using NotificationFlyout.Uwp.UI.Controls;
using NotificationFlyout.Wpf.UI.Extensions;
using NotificationFlyout.Uwp.UI.Extensions;
using NotificationFlyout.Wpf.UI.Extensions;
using NotificationFlyout.Wpf.UI.Helpers;
using System;
using System.Windows;
using Windows.UI.Xaml.Controls.Primitives;
using System.Windows.Input;
using Windows.UI.Xaml.Controls.Primitives;
namespace NotificationFlyout.Wpf.UI.Controls
{
internal class NotificationFlyoutXamlHost : XamlHostWindow<NotificationFlyoutHost>
internal class NotificationFlyoutXamlHost : TransparentXamlHost<NotificationFlyoutHost>
{
private const string ShellTrayHandleName = "Shell_TrayWnd";
@@ -176,7 +176,6 @@ namespace NotificationFlyout.Wpf.UI.Controls
_notificationIconHelper.SetIcon(icon.Handle);
}
private void UpdateRequestedTheme()
{
if (_flyout == null) return;
@@ -216,24 +215,28 @@ namespace NotificationFlyout.Wpf.UI.Controls
height = windowHeight;
width = windowWidth;
break;
case TaskbarPosition.Top:
top = taskbarRect.Bottom;
left = FlowDirection == FlowDirection.RightToLeft ? taskbarRect.Left : taskbarRect.Right - windowWidth;
height = windowHeight;
width = windowWidth;
break;
case TaskbarPosition.Right:
top = taskbarRect.Bottom - windowHeight;
left = taskbarRect.Left - windowWidth;
height = windowHeight;
width = windowWidth;
break;
case TaskbarPosition.Bottom:
top = taskbarRect.Top - windowHeight;
left = FlowDirection == FlowDirection.RightToLeft ? taskbarRect.Left : taskbarRect.Right - windowWidth;
height = windowHeight;
width = windowWidth;
break;
default:
throw new ArgumentOutOfRangeException();
}
@@ -0,0 +1,43 @@
using Microsoft.Toolkit.Wpf.UI.XamlHost;
using NotificationFlyout.Wpf.UI.Extensions;
using System.Windows;
using System.Windows.Media;
namespace NotificationFlyout.Wpf.UI.Controls
{
internal class TransparentXamlHost<TXamlContent> : XamlHost<TXamlContent> where TXamlContent : Windows.UI.Xaml.UIElement
{
internal const double WindowSize = 5;
public TransparentXamlHost()
{
Loaded += OnLoaded;
PrepareDefaultWindow();
}
protected override WindowsXamlHost OnPreparingXamlHost(WindowsXamlHost xamlHost)
{
xamlHost.Height = 0;
xamlHost.Width = 0;
return base.OnPreparingXamlHost(xamlHost);
}
private void OnLoaded(object sender, RoutedEventArgs args)
{
this.Hidden();
}
private void PrepareDefaultWindow()
{
ShowInTaskbar = false;
ShowActivated = false;
WindowStyle = WindowStyle.None;
ResizeMode = ResizeMode.NoResize;
AllowsTransparency = true;
Background = new SolidColorBrush(Colors.Transparent);
Height = WindowSize;
Width = WindowSize;
}
}
}
@@ -0,0 +1,52 @@
using Microsoft.Toolkit.Wpf.UI.XamlHost;
using NotificationFlyout.Wpf.UI.Extensions;
using System;
using System.Windows;
namespace NotificationFlyout.Wpf.UI.Controls
{
internal class XamlHost<TXamlContent> : Window where TXamlContent : Windows.UI.Xaml.UIElement
{
protected new bool IsLoaded;
private WindowsXamlHost _xamlHost;
public XamlHost()
{
PrepareWindowsXamlHost();
ContentRendered += OnContentRendered;
}
protected TXamlContent GetHostContent()
{
if (_xamlHost == null) return null;
return _xamlHost.GetUwpInternalObject() as TXamlContent;
}
protected virtual void OnContentLoaded()
{
}
protected virtual WindowsXamlHost OnPreparingXamlHost(WindowsXamlHost xamlHost)
{
xamlHost.InitialTypeName = typeof(TXamlContent).FullName;
xamlHost.HorizontalAlignment = HorizontalAlignment.Stretch;
xamlHost.VerticalAlignment = VerticalAlignment.Stretch;
return xamlHost;
}
private void OnContentRendered(object sender, EventArgs args)
{
IsLoaded = true;
OnContentLoaded();
}
private void PrepareWindowsXamlHost()
{
_xamlHost = new WindowsXamlHost();
OnPreparingXamlHost(_xamlHost);
Content = _xamlHost;
}
}
}