Browse Source

add notifcationmanager property to toplevel

pull/9277/head
Emmanuel Hansen 4 years ago
parent
commit
7ce869475e
  1. 15
      samples/ControlCatalog/Pages/NotificationsPage.xaml.cs
  2. 30
      samples/ControlCatalog/ViewModels/MainWindowViewModel.cs
  3. 29
      samples/ControlCatalog/ViewModels/NotificationViewModel.cs
  4. 7
      src/Avalonia.Controls/Notifications/WindowNotificationManager.cs
  5. 5
      src/Avalonia.Controls/TopLevel.cs

15
samples/ControlCatalog/Pages/NotificationsPage.xaml.cs

@ -1,18 +1,33 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using ControlCatalog.ViewModels;
namespace ControlCatalog.Pages
{
public class NotificationsPage : UserControl
{
private NotificationViewModel _viewModel;
public NotificationsPage()
{
this.InitializeComponent();
_viewModel = new NotificationViewModel();
DataContext = _viewModel;
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
_viewModel.NotificationManager = (VisualRoot as TopLevel)?.NotificationManager;
}
}
}

30
samples/ControlCatalog/ViewModels/MainWindowViewModel.cs

@ -26,23 +26,6 @@ namespace ControlCatalog.ViewModels
public MainWindowViewModel(IManagedNotificationManager notificationManager)
{
_notificationManager = notificationManager;
ShowCustomManagedNotificationCommand = MiniCommand.Create(() =>
{
NotificationManager.Show(new NotificationViewModel(NotificationManager) { Title = "Hey There!", Message = "Did you know that Avalonia now supports Custom In-Window Notifications?" });
});
ShowManagedNotificationCommand = MiniCommand.Create(() =>
{
NotificationManager.Show(new Avalonia.Controls.Notifications.Notification("Welcome", "Avalonia now supports Notifications.", NotificationType.Information));
});
ShowNativeNotificationCommand = MiniCommand.Create(() =>
{
NotificationManager.Show(new Avalonia.Controls.Notifications.Notification("Error", "Native Notifications are not quite ready. Coming soon.", NotificationType.Error));
});
AboutCommand = MiniCommand.CreateFromTask(async () =>
{
var dialog = new AboutAvaloniaDialog();
@ -52,7 +35,6 @@ namespace ControlCatalog.ViewModels
await dialog.ShowDialog(mainWindow);
}
});
ExitCommand = MiniCommand.Create(() =>
{
(App.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.Shutdown();
@ -143,24 +125,12 @@ namespace ControlCatalog.ViewModels
set { this.RaiseAndSetIfChanged(ref _windowStates, value); }
}
public IManagedNotificationManager NotificationManager
{
get { return _notificationManager; }
set { this.RaiseAndSetIfChanged(ref _notificationManager, value); }
}
public bool IsMenuItemChecked
{
get { return _isMenuItemChecked; }
set { this.RaiseAndSetIfChanged(ref _isMenuItemChecked, value); }
}
public MiniCommand ShowCustomManagedNotificationCommand { get; }
public MiniCommand ShowManagedNotificationCommand { get; }
public MiniCommand ShowNativeNotificationCommand { get; }
public MiniCommand AboutCommand { get; }
public MiniCommand ExitCommand { get; }

29
samples/ControlCatalog/ViewModels/NotificationViewModel.cs

@ -6,16 +6,33 @@ namespace ControlCatalog.ViewModels
{
public class NotificationViewModel
{
public NotificationViewModel(INotificationManager manager)
public WindowNotificationManager? NotificationManager { get; set; }
public NotificationViewModel()
{
ShowCustomManagedNotificationCommand = MiniCommand.Create(() =>
{
NotificationManager?.Show(new NotificationViewModel() { Title = "Hey There!", Message = "Did you know that Avalonia now supports Custom In-Window Notifications?" , NotificationManager = NotificationManager});
});
ShowManagedNotificationCommand = MiniCommand.Create(() =>
{
NotificationManager?.Show(new Avalonia.Controls.Notifications.Notification("Welcome", "Avalonia now supports Notifications.", NotificationType.Information));
});
ShowNativeNotificationCommand = MiniCommand.Create(() =>
{
NotificationManager?.Show(new Avalonia.Controls.Notifications.Notification("Error", "Native Notifications are not quite ready. Coming soon.", NotificationType.Error));
});
YesCommand = MiniCommand.Create(() =>
{
manager.Show(new Avalonia.Controls.Notifications.Notification("Avalonia Notifications", "Start adding notifications to your app today."));
NotificationManager?.Show(new Avalonia.Controls.Notifications.Notification("Avalonia Notifications", "Start adding notifications to your app today."));
});
NoCommand = MiniCommand.Create(() =>
{
manager.Show(new Avalonia.Controls.Notifications.Notification("Avalonia Notifications", "Start adding notifications to your app today. To find out more visit..."));
NotificationManager?.Show(new Avalonia.Controls.Notifications.Notification("Avalonia Notifications", "Start adding notifications to your app today. To find out more visit..."));
});
}
@ -26,5 +43,11 @@ namespace ControlCatalog.ViewModels
public MiniCommand NoCommand { get; }
public MiniCommand ShowCustomManagedNotificationCommand { get; }
public MiniCommand ShowManagedNotificationCommand { get; }
public MiniCommand ShowNativeNotificationCommand { get; }
}
}

7
src/Avalonia.Controls/Notifications/WindowNotificationManager.cs

@ -3,11 +3,10 @@ using System.Collections;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Rendering;
using Avalonia.Data;
using Avalonia.VisualTree;
using Avalonia.Controls.Metadata;
namespace Avalonia.Controls.Notifications
{
@ -55,7 +54,7 @@ namespace Avalonia.Controls.Notifications
/// Initializes a new instance of the <see cref="WindowNotificationManager"/> class.
/// </summary>
/// <param name="host">The window that will host the control.</param>
public WindowNotificationManager(Window host)
public WindowNotificationManager(TopLevel host)
{
if (VisualChildren.Count != 0)
{
@ -151,7 +150,7 @@ namespace Avalonia.Controls.Notifications
/// of the host <see cref="Window"/>.
/// </summary>
/// <param name="host">The <see cref="Window"/> that will be the host.</param>
private void Install(Window host)
private void Install(TemplatedControl host)
{
var adornerLayer = host.FindDescendantOfType<VisualLayerManager>()?.AdornerLayer;

5
src/Avalonia.Controls/TopLevel.cs

@ -1,6 +1,7 @@
using System;
using System.Reactive.Linq;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Notifications;
using Avalonia.Controls.Platform;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
@ -95,6 +96,7 @@ namespace Avalonia.Controls
private Border? _transparencyFallbackBorder;
private TargetWeakEventSubscriber<TopLevel, ResourcesChangedEventArgs>? _resourcesChangesSubscriber;
private IStorageProvider? _storageProvider;
private WindowNotificationManager _notificationManager;
/// <summary>
/// Initializes static members of the <see cref="TopLevel"/> class.
@ -151,6 +153,7 @@ namespace Avalonia.Controls
_keyboardNavigationHandler = TryGetService<IKeyboardNavigationHandler>(dependencyResolver);
_renderInterface = TryGetService<IPlatformRenderInterface>(dependencyResolver);
_globalStyles = TryGetService<IGlobalStyles>(dependencyResolver);
_notificationManager = new WindowNotificationManager(this);
Renderer = impl.CreateRenderer(this);
@ -328,6 +331,8 @@ namespace Avalonia.Controls
IRenderTarget IRenderRoot.CreateRenderTarget() => CreateRenderTarget();
public WindowNotificationManager NotificationManager => _notificationManager;
/// <inheritdoc/>
protected virtual IRenderTarget CreateRenderTarget()
{

Loading…
Cancel
Save