Browse Source

NotificationContent template works

Dan Walmsley 6 years ago
parent
commit
b6fc0d7857

+ 14 - 3
samples/ControlCatalog/ViewModels/NotificationViewModel.cs

@@ -1,12 +1,23 @@
-using System;
-using System.Collections.Generic;
-using System.Text;
+using System.Reactive;
+using Avalonia.Controls.Notifications;
+using ReactiveUI;
 
 namespace ControlCatalog.ViewModels
 {
     public class NotificationViewModel
     {
+        public NotificationViewModel()
+        {
+            OKCommand = ReactiveCommand.Create(() =>
+            {
+                NotificationManager.Instance.Show("Notification Accepted", "Main");
+            });
+        }
+
         public string Title { get; set; }
         public string Message { get; set; }
+
+        public ReactiveCommand<Unit, Unit> OKCommand { get; }
+
     }
 }

+ 5 - 9
samples/ControlCatalog/Views/NotificationView.xaml

@@ -2,19 +2,15 @@
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              x:Class="ControlCatalog.Views.NotificationView">
     <Border Padding="12" MinHeight="20" Background="#FF444444">
-        <Grid>
-            <Grid.ColumnDefinitions>
-                <ColumnDefinition Width="Auto"/>
-                <ColumnDefinition Width="*"/>
-            </Grid.ColumnDefinitions>
-            <ContentControl Margin="0,0,12,0" Width="25" Height="25" VerticalAlignment="Top">
+        <Grid ColumnDefinitions="Auto,*">
+            <Panel Margin="0,0,12,0" Width="25" Height="25" VerticalAlignment="Top">
                 <TextBlock Text="&#xE115;" FontFamily="Segoe UI Symbol" FontSize="20" TextAlignment="Center" VerticalAlignment="Center"/>
-            </ContentControl>
+            </Panel>
             <DockPanel Grid.Column="1">
                 <TextBlock DockPanel.Dock="Top" Text="{Binding Title}" FontWeight="Medium" />
                 <DockPanel LastChildFill="False" DockPanel.Dock="Bottom" Margin="0,8,0,0">
-                    <Button x:Name="Ok" Content="Ok" DockPanel.Dock="Right"  />
-                    <Button x:Name="Cancel" Content="Cancel" DockPanel.Dock="Right" Margin="0,0,8,0" />
+                    <Button Content="Ok" DockPanel.Dock="Right" Notification.CloseOnClick="True" Command="{Binding OKCommand}" />
+                    <Button Content="Cancel" DockPanel.Dock="Right" Notification.CloseOnClick="True" Margin="0,0,8,0" />
                 </DockPanel>
                 <TextBlock Text="{Binding Message}" TextWrapping="Wrap" Opacity=".8" Margin="0,8,0,0"/>
             </DockPanel>

+ 0 - 1
samples/ControlCatalog/Views/NotificationView.xaml.cs

@@ -1,5 +1,4 @@
 using Avalonia.Controls;
-using Avalonia.Controls.Primitives;
 using Avalonia.Markup.Xaml;
 
 namespace ControlCatalog.Views

+ 22 - 28
src/Avalonia.Controls/Notifications/Notification.cs

@@ -1,19 +1,16 @@
 using System;
+using System.Linq;
 using System.Reactive.Linq;
-using System.Threading.Tasks;
 using Avalonia.Controls.Primitives;
-using Avalonia.Data;
 using Avalonia.Interactivity;
+using Avalonia.LogicalTree;
 
 namespace Avalonia.Controls.Notifications
 {
     public class Notification : ContentControl
     {
-        // private TimeSpan _closingAnimationTime = TimeSpan.FromSeconds(1);
-
         static Notification()
         {
-            //CloseOnClickProperty.Changed.AddClassHandler<Button>(CloseOnClickChanged);
             IsClosedProperty.Changed.AddClassHandler<Notification>(IsClosedChanged);
         }
 
@@ -70,7 +67,7 @@ namespace Avalonia.Controls.Notifications
         }
 
         public static readonly DirectProperty<Notification, bool> IsClosedProperty =
-            AvaloniaProperty.RegisterDirect<Notification, bool>(nameof(IsClosed), o => o.IsClosed, (o,v) => o.IsClosed = v );
+            AvaloniaProperty.RegisterDirect<Notification, bool>(nameof(IsClosed), o => o.IsClosed, (o, v) => o.IsClosed = v);
 
         /// <summary>
         /// Defines the <see cref="NotificationCloseInvoked"/> event.
@@ -99,7 +96,7 @@ namespace Avalonia.Controls.Notifications
             remove { RemoveHandler(NotificationClosedEvent, value); }
         }
 
-        /*public static bool GetCloseOnClick(Notification obj)
+        public static bool GetCloseOnClick(Notification obj)
         {
             return (bool)obj.GetValue(CloseOnClickProperty);
         }
@@ -107,29 +104,30 @@ namespace Avalonia.Controls.Notifications
         public static void SetCloseOnClick(Notification obj, bool value)
         {
             obj.SetValue(CloseOnClickProperty, value);
-        }*/
+        }
 
-        //public static readonly AvaloniaProperty CloseOnClickProperty =
-        //  AvaloniaProperty.RegisterDirect<Notification, bool>("CloseOnClick", GetCloseOnClick, SetCloseOnClick);
+        public static readonly AvaloniaProperty CloseOnClickProperty =
+          AvaloniaProperty.RegisterAttached<Button, bool>("CloseOnClick", typeof(Notification), validate: CloseOnClickChanged);
 
-        private static void CloseOnClickChanged(Button dependencyObject, AvaloniaPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
+        private static bool CloseOnClickChanged(Button button, bool value)
         {
-            var button = dependencyObject as Button;
-            if (button == null)
+            if (value)
             {
-                return;
+                button.Click += Button_Click;
             }
-
-            var value = (bool)dependencyPropertyChangedEventArgs.NewValue;
-
-            if (value)
+            else
             {
-                button.Click += (sender, args) =>
-                {
-                    var notification = button.Parent as Notification;
-                    notification?.Close();
-                };
+                button.Click -= Button_Click;
             }
+
+            return true;
+        }
+
+        private static void Button_Click(object sender, RoutedEventArgs e)
+        {
+            var btn = sender as ILogical;
+            var notification = btn.GetLogicalAncestors().OfType<Notification>().FirstOrDefault();
+            notification?.Close();
         }
 
         protected override void OnTemplateApplied(TemplateAppliedEventArgs e)
@@ -139,9 +137,6 @@ namespace Avalonia.Controls.Notifications
             if (closeButton != null)
                 closeButton.Click += OnCloseButtonOnClick;
 
-            //var storyboards = Template.Triggers.OfType<EventTrigger>().FirstOrDefault(t => t.RoutedEvent == NotificationCloseInvokedEvent)?.Actions.OfType<BeginStoryboard>().Select(a => a.Storyboard);
-            //_closingAnimationTime = new TimeSpan(storyboards?.Max(s => Math.Min((s.Duration.HasTimeSpan ? s.Duration.TimeSpan + (s.BeginTime ?? TimeSpan.Zero) : TimeSpan.MaxValue).Ticks, s.Children.Select(ch => ch.Duration.TimeSpan + (s.BeginTime ?? TimeSpan.Zero)).Max().Ticks)) ?? 0);
-
         }
 
         private void OnCloseButtonOnClick(object sender, RoutedEventArgs args)
@@ -154,7 +149,7 @@ namespace Avalonia.Controls.Notifications
             Close();
         }
 
-        public async void Close()
+        public void Close()
         {
             if (IsClosing)
             {
@@ -166,7 +161,6 @@ namespace Avalonia.Controls.Notifications
             RaiseEvent(new RoutedEventArgs(NotificationCloseInvokedEvent));
         }
 
-
         private static void IsClosedChanged(Notification target, AvaloniaPropertyChangedEventArgs arg2)
         {
             if (!target.IsClosing & !target.IsClosed)

+ 0 - 12
src/Avalonia.Controls/Notifications/NotificationArea.cs

@@ -68,18 +68,6 @@ namespace Avalonia.Controls.Notifications
             notification.NotificationClosed += (sender, args) => onClose?.Invoke();
             notification.NotificationClosed += OnNotificationClosed;
 
-            /*if (!this.)
-            {
-                return;
-            }*/
-
-            /*var w = this.VisualRoot Window.GetWindow(this);
-            var x = PresentationSource.FromVisual(w);
-            if (x == null)
-            {
-                return;
-            }*/
-
             lock (_items)
             {
                 _items.Add(notification);

+ 1 - 2
src/Avalonia.Controls/Notifications/NotificationManager.cs

@@ -1,7 +1,6 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
-using System.Text;
 using Avalonia.Threading;
 
 namespace Avalonia.Controls.Notifications
@@ -32,7 +31,7 @@ namespace Avalonia.Controls.Notifications
 
                 _window = new Window
                 {
-                    Position =  workArea.Position,
+                    Position = workArea.Position,
                     Width = workArea.Width,
                     Height = workArea.Height
                 };

+ 17 - 1
src/Avalonia.Themes.Default/NotificationArea.xaml

@@ -3,7 +3,23 @@
         <Setter Property="Margin" Value="0 0 8 8"/>
         <Setter Property="Template">
             <ControlTemplate>
-                <StackPanel Name="PART_Items" />
+                <StackPanel Name="PART_Items">
+                    <StackPanel.DataTemplates>
+                        <DataTemplate DataType="NotificationContent">
+                            <Border Padding="12" MinHeight="80">
+                                <Grid ColumnDefinitions="Auto,*">
+                                    <ContentControl Margin="0,0,12,0" Width="25" Height="25" VerticalAlignment="Top">
+                                        <ContentControl HorizontalAlignment="Center" VerticalAlignment="Center"/>
+                                    </ContentControl>
+                                    <DockPanel Grid.Column="1">
+                                        <TextBlock DockPanel.Dock="Top" Text="{Binding Title}" FontWeight="Medium" />
+                                        <TextBlock Text="{Binding Message}" TextWrapping="Wrap" Opacity=".8" Margin="0,0,12,0"/>
+                                    </DockPanel>
+                                </Grid>
+                            </Border>
+                        </DataTemplate>
+                    </StackPanel.DataTemplates>
+                </StackPanel>
             </ControlTemplate>
         </Setter>
     </Style>