From 310287b0af6ad0aef3d5aea7fe5b9fbf7f878690 Mon Sep 17 00:00:00 2001
From: Xjph <archseraphim@gmail.com>
Date: Sat, 23 Oct 2021 15:47:38 -0230
Subject: [PATCH] allow multiple simultaneous popup notifications to vertically
 stack

---
 .../NativeNotification/NativePopup.cs         | 14 +++++++---
 ObservatoryCore/UI/Views/BasicUIView.axaml.cs |  4 ++-
 .../UI/Views/NotificationView.axaml.cs        | 26 +++++++++++++++++++
 3 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/ObservatoryCore/NativeNotification/NativePopup.cs b/ObservatoryCore/NativeNotification/NativePopup.cs
index 4a4f2ba..3e40f26 100644
--- a/ObservatoryCore/NativeNotification/NativePopup.cs
+++ b/ObservatoryCore/NativeNotification/NativePopup.cs
@@ -22,6 +22,12 @@ namespace Observatory.NativeNotification
             {
                 var notifyWindow = new NotificationView(notificationGuid) { DataContext = new NotificationViewModel(notificationArgs) };
                 notifyWindow.Closed += NotifyWindow_Closed;
+
+                foreach (var notification in notifications)
+                {
+                    notification.Value.AdjustOffset(true);
+                }
+
                 notifications.Add(notificationGuid, notifyWindow);
                 notifyWindow.Show();
             });
@@ -31,10 +37,12 @@ namespace Observatory.NativeNotification
 
         private void NotifyWindow_Closed(object sender, EventArgs e)
         {
-            var notification = (NotificationView)sender;
+            var currentNotification = (NotificationView)sender;
 
-            if (notifications.ContainsKey(notification.Guid))
-                notifications.Remove(notification.Guid);
+            if (notifications.ContainsKey(currentNotification.Guid))
+            {
+                notifications.Remove(currentNotification.Guid);
+            }
         }
 
         public void CloseNotification(Guid guid)
diff --git a/ObservatoryCore/UI/Views/BasicUIView.axaml.cs b/ObservatoryCore/UI/Views/BasicUIView.axaml.cs
index c486b40..ba33d0f 100644
--- a/ObservatoryCore/UI/Views/BasicUIView.axaml.cs
+++ b/ObservatoryCore/UI/Views/BasicUIView.axaml.cs
@@ -21,10 +21,12 @@ namespace Observatory.UI.Views
     public class BasicUIView : UserControl
     {
         private DataGrid dataGrid;
+        private NativeNotification.NativePopup nativePopup;
 
         public BasicUIView()
         {
             InitializeComponent();
+            nativePopup = new();
         }
 
         private void InitializeComponent()
@@ -274,7 +276,7 @@ namespace Observatory.UI.Views
                         Detail = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras suscipit hendrerit libero ac scelerisque."
                     };
                     
-                    new NativeNotification.NativePopup().InvokeNativeNotification(notificationArgs);
+                    nativePopup.InvokeNativeNotification(notificationArgs);
                 });
             };
 
diff --git a/ObservatoryCore/UI/Views/NotificationView.axaml.cs b/ObservatoryCore/UI/Views/NotificationView.axaml.cs
index 4a343d4..292fd16 100644
--- a/ObservatoryCore/UI/Views/NotificationView.axaml.cs
+++ b/ObservatoryCore/UI/Views/NotificationView.axaml.cs
@@ -14,6 +14,8 @@ namespace Observatory.UI.Views
         private readonly double scale;
         private readonly Timer timer;
         private readonly Guid guid;
+        private bool defaultPosition = true;
+        private PixelPoint originalPosition;
 
         public NotificationView() : this(default)
         { }
@@ -48,6 +50,27 @@ namespace Observatory.UI.Views
 
         public Guid Guid { get => guid; }
 
+        public void AdjustOffset(bool increase)
+        {
+            if (defaultPosition)
+            {
+                if (increase || Position != originalPosition)
+                {
+                    var corner = Properties.Core.Default.NativeNotifyCorner;
+
+                    if ((corner >= 2 && increase) || (corner <= 1 && !increase))
+                    {
+                        Position += new PixelPoint(0, Convert.ToInt32(Height));
+                    }
+                    else
+                    {
+                        Position -= new PixelPoint(0, Convert.ToInt32(Height));
+                    }
+
+                }
+            }
+        }
+
         private void NotificationView_DataContextChanged(object sender, EventArgs e)
         {
             var notification = ((NotificationViewModel)DataContext).Notification;
@@ -117,10 +140,12 @@ namespace Observatory.UI.Views
 
             if (xOverride >= 0 && yOverride >= 0)
             {
+                defaultPosition = false;
                 Position = screenBounds.TopLeft + new PixelPoint(Convert.ToInt32(screenBounds.Width * xOverride), Convert.ToInt32(screenBounds.Height * yOverride));
             }
             else
             {
+                defaultPosition = true;
                 switch (corner)
                 {
                     default:
@@ -137,6 +162,7 @@ namespace Observatory.UI.Views
                         Position = screenBounds.TopLeft + new PixelPoint(50, 50);
                         break;
                 }
+                originalPosition = Position;
             }
         }