Browse Source

remove need for seperate NotificationManager class.

pull/2453/head
Dan Walmsley 7 years ago
parent
commit
962ed29bb6
  1. 12
      samples/ControlCatalog/ViewModels/MainWindowViewModel.cs
  2. 3
      samples/ControlCatalog/ViewModels/NotificationViewModel.cs
  3. 2
      src/Avalonia.Controls/Notifications/INotificationManager.cs
  4. 8
      src/Avalonia.Controls/Notifications/NotificationArea.cs
  5. 53
      src/Avalonia.Controls/Notifications/NotificationManager.cs
  6. 18
      src/Avalonia.Controls/TopLevel.cs

12
samples/ControlCatalog/ViewModels/MainWindowViewModel.cs

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls.Notifications;
using Avalonia.Threading;
@ -15,19 +16,20 @@ namespace ControlCatalog.ViewModels
{
await Task.Delay(5000);
NotificationManager.Instance.Show(new NotificationViewModel { Title = "Warning", Message = "Please save your work before closing." }, "Main");
Application.Current.MainWindow.LocalNotificationManager.Show(new NotificationViewModel { Title = "Warning", Message = "Please save your work before closing." });
await Task.Delay(1500);
NotificationManager.Instance.Show(new NotificationContent { Message = "Test2", Type = NotificationType.Error }, "Main");
Application.Current.MainWindow.LocalNotificationManager.Show(new NotificationContent { Message = "Test2", Type = NotificationType.Error });
await Task.Delay(2000);
NotificationManager.Instance.Show(new NotificationContent { Message = "Test3", Type = NotificationType.Warning }, "Main");
Application.Current.MainWindow.LocalNotificationManager.Show(new NotificationContent { Message = "Test3", Type = NotificationType.Warning });
await Task.Delay(2500);
NotificationManager.Instance.Show(new NotificationContent { Message = "Test4", Type = NotificationType.Success }, "Main");
Application.Current.MainWindow.LocalNotificationManager.Show(new NotificationContent { Message = "Test4", Type = NotificationType.Success });
await Task.Delay(500);
NotificationManager.Instance.Show("Test5", "Main");
Application.Current.MainWindow.LocalNotificationManager.Show("Test5");
});
}

3
samples/ControlCatalog/ViewModels/NotificationViewModel.cs

@ -1,4 +1,5 @@
using System.Reactive;
using Avalonia;
using Avalonia.Controls.Notifications;
using ReactiveUI;
@ -10,7 +11,7 @@ namespace ControlCatalog.ViewModels
{
OKCommand = ReactiveCommand.Create(() =>
{
NotificationManager.Instance.Show("Notification Accepted", "Main");
Application.Current.MainWindow.LocalNotificationManager.Show("Notification Accepted");
});
}

2
src/Avalonia.Controls/Notifications/INotificationManager.cs

@ -4,6 +4,6 @@ namespace Avalonia.Controls.Notifications
{
public interface INotificationManager
{
void Show(object content, string areaName = "", TimeSpan? expirationTime = null, Action onClick = null, Action onClose = null);
void Show(object content, TimeSpan? expirationTime = null, Action onClick = null, Action onClose = null);
}
}

8
src/Avalonia.Controls/Notifications/NotificationArea.cs

@ -7,7 +7,7 @@ using Avalonia.Interactivity;
namespace Avalonia.Controls.Notifications
{
public class NotificationArea : TemplatedControl
public class NotificationArea : TemplatedControl, INotificationManager
{
private IList _items;
@ -31,7 +31,6 @@ namespace Avalonia.Controls.Notifications
public NotificationArea()
{
NotificationManager.AddArea(this);
}
static NotificationArea()
@ -50,7 +49,7 @@ namespace Avalonia.Controls.Notifications
_items = itemsControl?.Children;
}
public async void Show(object content, TimeSpan expirationTime, Action onClick, Action onClose)
public async void Show(object content, TimeSpan? expirationTime, Action onClick, Action onClose)
{
var notification = new Notification
{
@ -82,7 +81,8 @@ namespace Avalonia.Controls.Notifications
{
return;
}
await Task.Delay(expirationTime);
await Task.Delay(expirationTime ?? TimeSpan.FromSeconds(5));
notification.Close();
}

53
src/Avalonia.Controls/Notifications/NotificationManager.cs

@ -1,53 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Threading;
namespace Avalonia.Controls.Notifications
{
public class NotificationManager : INotificationManager
{
private static readonly List<NotificationArea> Areas = new List<NotificationArea>();
private static Window _window;
public static NotificationManager Instance = new NotificationManager();
public void Show(object content, string areaName = "", TimeSpan? expirationTime = null, Action onClick = null,
Action onClose = null)
{
if (!Dispatcher.UIThread.CheckAccess())
{
Dispatcher.UIThread.Post(
new Action(() => Show(content, areaName, expirationTime, onClick, onClose)));
return;
}
if (expirationTime == null)
expirationTime = TimeSpan.FromSeconds(5);
if (areaName == string.Empty && _window == null)
{
var workArea = Application.Current.MainWindow.Screens.Primary.WorkingArea;
_window = new Window
{
Position = workArea.Position,
Width = workArea.Width,
Height = workArea.Height
};
_window.Show();
}
foreach (var area in Areas.Where(a => a.Name == areaName))
{
area.Show(content, (TimeSpan)expirationTime, onClick, onClose);
}
}
internal static void AddArea(NotificationArea area)
{
Areas.Add(area);
}
}
}

18
src/Avalonia.Controls/TopLevel.cs

@ -2,6 +2,7 @@
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Linq;
using System.Reactive.Linq;
using Avalonia.Controls.Notifications;
using Avalonia.Controls.Primitives;
@ -103,7 +104,7 @@ namespace Avalonia.Controls
PlatformImpl = impl;
LocalNotificationManager = new NotificationManager();
LocalNotificationManager = new NotificationArea();
dependencyResolver = dependencyResolver ?? AvaloniaLocator.Current;
var styler = TryGetService<IStyler>(dependencyResolver);
@ -414,5 +415,20 @@ namespace Avalonia.Controls
{
(this as IInputRoot).MouseDevice.SceneInvalidated(this, e.DirtyRect);
}
protected override void OnTemplateApplied(TemplateAppliedEventArgs e)
{
var adornerLayer = this.GetVisualDescendants()
.OfType<AdornerDecorator>()
.FirstOrDefault()
?.AdornerLayer;
if (adornerLayer != null)
{
adornerLayer.Children.Add(LocalNotificationManager as IControl);
}
base.OnTemplateApplied(e);
}
}
}

Loading…
Cancel
Save