Browse Source

dont use properties on toplevel

pull/2453/head
Dan Walmsley 8 years ago
parent
commit
e14cc277d7
  1. 5
      samples/ControlCatalog/MainWindow.xaml
  2. 18
      samples/ControlCatalog/MainWindow.xaml.cs
  3. 4
      samples/ControlCatalog/ViewModels/MainWindowViewModel.cs
  4. 18
      src/Avalonia.Controls/Notifications/NotificationArea.cs
  5. 57
      src/Avalonia.Controls/TopLevel.cs

5
samples/ControlCatalog/MainWindow.xaml

@ -5,10 +5,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:ControlCatalog.ViewModels" xmlns:vm="clr-namespace:ControlCatalog.ViewModels"
xmlns:v="clr-namespace:ControlCatalog.Views" xmlns:v="clr-namespace:ControlCatalog.Views"
x:Class="ControlCatalog.MainWindow" x:Class="ControlCatalog.MainWindow">
LocalNotificationManager="{Binding NotificationManager, Mode=OneWayToSource}"
NotificationArea.Position="TopRight"
NotificationArea.MaxItems="3">
<Window.DataTemplates> <Window.DataTemplates>
<DataTemplate DataType="vm:NotificationViewModel"> <DataTemplate DataType="vm:NotificationViewModel">
<v:NotificationView /> <v:NotificationView />

18
samples/ControlCatalog/MainWindow.xaml.cs

@ -1,6 +1,7 @@
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Notifications; using Avalonia.Controls.Notifications;
using Avalonia.Controls.Primitives;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using Avalonia.Threading; using Avalonia.Threading;
using ControlCatalog.ViewModels; using ControlCatalog.ViewModels;
@ -11,6 +12,8 @@ namespace ControlCatalog
{ {
public class MainWindow : Window public class MainWindow : Window
{ {
private NotificationArea _notificationArea;
public MainWindow() public MainWindow()
{ {
this.InitializeComponent(); this.InitializeComponent();
@ -18,7 +21,13 @@ namespace ControlCatalog
//Renderer.DrawFps = true; //Renderer.DrawFps = true;
//Renderer.DrawDirtyRects = Renderer.DrawFps = true; //Renderer.DrawDirtyRects = Renderer.DrawFps = true;
DataContext = new MainWindowViewModel(); _notificationArea = new NotificationArea
{
Position = NotificationPosition.TopRight,
MaxItems = 3
};
DataContext = new MainWindowViewModel(_notificationArea);
} }
private void InitializeComponent() private void InitializeComponent()
@ -30,5 +39,12 @@ namespace ControlCatalog
theme.TryGetResource("Button", out _); theme.TryGetResource("Button", out _);
AvaloniaXamlLoader.Load(this); AvaloniaXamlLoader.Load(this);
} }
protected override void OnTemplateApplied(TemplateAppliedEventArgs e)
{
base.OnTemplateApplied(e);
_notificationArea.Install(this);
}
} }
} }

4
samples/ControlCatalog/ViewModels/MainWindowViewModel.cs

@ -9,8 +9,10 @@ namespace ControlCatalog.ViewModels
{ {
class MainWindowViewModel : ViewModelBase class MainWindowViewModel : ViewModelBase
{ {
public MainWindowViewModel() public MainWindowViewModel(INotificationManager notificationManager)
{ {
_notificationManager = notificationManager;
ShowCustomManagedNotificationCommand = ReactiveCommand.Create(() => ShowCustomManagedNotificationCommand = ReactiveCommand.Create(() =>
{ {
NotificationManager.Show(new NotificationViewModel(NotificationManager) { Title = "Hey There!", Message = "Did you know that Avalonia now supports Custom In-Window Notifications?" }); NotificationManager.Show(new NotificationViewModel(NotificationManager) { Title = "Hey There!", Message = "Did you know that Avalonia now supports Custom In-Window Notifications?" });

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

@ -4,6 +4,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.VisualTree;
namespace Avalonia.Controls.Notifications namespace Avalonia.Controls.Notifications
{ {
@ -22,7 +23,7 @@ namespace Avalonia.Controls.Notifications
} }
public static readonly AvaloniaProperty<NotificationPosition> PositionProperty = public static readonly AvaloniaProperty<NotificationPosition> PositionProperty =
AvaloniaProperty.RegisterAttached<NotificationArea, TopLevel, NotificationPosition>("Position", defaultValue: NotificationPosition.TopLeft , inherits: true); AvaloniaProperty.RegisterAttached<NotificationArea, TopLevel, NotificationPosition>("Position", defaultValue: NotificationPosition.TopLeft, inherits: true);
public NotificationPosition Position public NotificationPosition Position
{ {
@ -70,7 +71,7 @@ namespace Avalonia.Controls.Notifications
public void Show(NotificationContent content, TimeSpan? expirationTime, Action onClick, Action onClose) public void Show(NotificationContent content, TimeSpan? expirationTime, Action onClick, Action onClose)
{ {
Show(content as object, expirationTime, onClick, onClose); Show(content as object, expirationTime, onClick, onClose);
} }
public async void Show(object content, TimeSpan? expirationTime, Action onClick, Action onClose) public async void Show(object content, TimeSpan? expirationTime, Action onClick, Action onClose)
@ -116,6 +117,19 @@ namespace Avalonia.Controls.Notifications
var notification = sender as Notification; var notification = sender as Notification;
_items.Remove(notification); _items.Remove(notification);
} }
public void Install(TopLevel host)
{
var adornerLayer = host.GetVisualDescendants()
.OfType<AdornerDecorator>()
.FirstOrDefault()
?.AdornerLayer;
if (adornerLayer != null)
{
adornerLayer.Children.Add(this as IControl);
}
}
} }
public enum NotificationPosition public enum NotificationPosition

57
src/Avalonia.Controls/TopLevel.cs

@ -48,26 +48,12 @@ namespace Avalonia.Controls
public static readonly StyledProperty<IInputElement> PointerOverElementProperty = public static readonly StyledProperty<IInputElement> PointerOverElementProperty =
AvaloniaProperty.Register<TopLevel, IInputElement>(nameof(IInputRoot.PointerOverElement)); AvaloniaProperty.Register<TopLevel, IInputElement>(nameof(IInputRoot.PointerOverElement));
/// <summary>
/// Defines the <see cref="LocalNotificationManager"/> property.
/// </summary>
public static readonly DirectProperty<TopLevel, NotificationArea> LocalNotificationManagerProperty =
AvaloniaProperty.RegisterDirect<TopLevel, NotificationArea>(nameof(LocalNotificationManager), o => o.LocalNotificationManager, (o, v) => o.LocalNotificationManager = v);
/// <summary>
/// Defines the <see cref="SystemNotificationManager"/> property.
/// </summary>
public static readonly DirectProperty<TopLevel, INotificationManager> SystemNotificationManagerProperty =
AvaloniaProperty.RegisterDirect<TopLevel, INotificationManager>(nameof(SystemNotificationManager), o => o.SystemNotificationManager, (o, v) => o.SystemNotificationManager = v);
private readonly IInputManager _inputManager; private readonly IInputManager _inputManager;
private readonly IAccessKeyHandler _accessKeyHandler; private readonly IAccessKeyHandler _accessKeyHandler;
private readonly IKeyboardNavigationHandler _keyboardNavigationHandler; private readonly IKeyboardNavigationHandler _keyboardNavigationHandler;
private readonly IApplicationLifecycle _applicationLifecycle; private readonly IApplicationLifecycle _applicationLifecycle;
private readonly IPlatformRenderInterface _renderInterface; private readonly IPlatformRenderInterface _renderInterface;
private Size _clientSize; private Size _clientSize;
private NotificationArea _localNotificationManager;
private INotificationManager _systemNotificationManager;
private ILayoutManager _layoutManager; private ILayoutManager _layoutManager;
/// <summary> /// <summary>
@ -104,8 +90,6 @@ namespace Avalonia.Controls
PlatformImpl = impl; PlatformImpl = impl;
LocalNotificationManager = new NotificationArea();
dependencyResolver = dependencyResolver ?? AvaloniaLocator.Current; dependencyResolver = dependencyResolver ?? AvaloniaLocator.Current;
var styler = TryGetService<IStyler>(dependencyResolver); var styler = TryGetService<IStyler>(dependencyResolver);
@ -115,13 +99,6 @@ namespace Avalonia.Controls
_applicationLifecycle = TryGetService<IApplicationLifecycle>(dependencyResolver); _applicationLifecycle = TryGetService<IApplicationLifecycle>(dependencyResolver);
_renderInterface = TryGetService<IPlatformRenderInterface>(dependencyResolver); _renderInterface = TryGetService<IPlatformRenderInterface>(dependencyResolver);
_systemNotificationManager = TryGetService<INotificationManager>(dependencyResolver);
if(_systemNotificationManager == null)
{
SystemNotificationManager = _localNotificationManager;
}
Renderer = impl.CreateRenderer(this); Renderer = impl.CreateRenderer(this);
if (Renderer != null) if (Renderer != null)
@ -181,25 +158,6 @@ namespace Avalonia.Controls
protected set { SetAndRaise(ClientSizeProperty, ref _clientSize, value); } protected set { SetAndRaise(ClientSizeProperty, ref _clientSize, value); }
} }
/// <summary>
/// Gets or sets the Local (in window managed) notification manager.
/// </summary>
public NotificationArea LocalNotificationManager
{
get => _localNotificationManager;
protected set => SetAndRaise(LocalNotificationManagerProperty, ref _localNotificationManager, value);
}
/// <summary>
/// Gets or sets the System (native os) notification manager. If the system doesnt natively support notifications then this property
/// will match the <see cref="LocalNotificationManager"/>.
/// </summary>
public INotificationManager SystemNotificationManager
{
get => _systemNotificationManager;
protected set => SetAndRaise(SystemNotificationManagerProperty, ref _systemNotificationManager, value);
}
public ILayoutManager LayoutManager public ILayoutManager LayoutManager
{ {
get get
@ -415,20 +373,5 @@ namespace Avalonia.Controls
{ {
(this as IInputRoot).MouseDevice.SceneInvalidated(this, e.DirtyRect); (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