From e57a30304947c37cb6924c14f00e7bfaacbf06f4 Mon Sep 17 00:00:00 2001 From: BRIZEIUM <64617361+BRIZEIUM@users.noreply.github.com> Date: Fri, 23 Jun 2023 12:48:30 +0800 Subject: [PATCH] Add support for top centered display position. --- .../ConfigurationExample/MainViewModel.cs | 4 ++++ .../ConfigurationExample/MainWindow.xaml | 10 +++++---- .../Utilities/PositionProviderType.cs | 3 ++- .../Position/ControlPositionProvider.cs | 8 +++++++ Src/ToastNotifications/Position/Corner.cs | 3 ++- .../GdiPrimaryScreenPositionProvider.cs | 21 +++++++++++++++++++ .../Position/PrimaryScreenPositionProvider.cs | 21 +++++++++++++++++++ .../Position/WindowPositionProvider.cs | 8 +++++++ 8 files changed, 72 insertions(+), 6 deletions(-) diff --git a/Src/Examples/ConfigurationExample/MainViewModel.cs b/Src/Examples/ConfigurationExample/MainViewModel.cs index 328308f..66434b7 100644 --- a/Src/Examples/ConfigurationExample/MainViewModel.cs +++ b/Src/Examples/ConfigurationExample/MainViewModel.cs @@ -69,6 +69,10 @@ private static IPositionProvider CreatePositionProvider(Corner corner, PositionP { return new PrimaryScreenPositionProvider(corner, 5, 5); } + case PositionProviderType.ScreenGdi: + { + return new GdiPrimaryScreenPositionProvider(corner, 5, 5); + } case PositionProviderType.Control: { var mainWindow = Application.Current.MainWindow as MainWindow; diff --git a/Src/Examples/ConfigurationExample/MainWindow.xaml b/Src/Examples/ConfigurationExample/MainWindow.xaml index 7b0f9d0..28de10d 100644 --- a/Src/Examples/ConfigurationExample/MainWindow.xaml +++ b/Src/Examples/ConfigurationExample/MainWindow.xaml @@ -39,14 +39,15 @@ - - - + + + Window Main screen + Main screen(Gdi) Control @@ -55,7 +56,8 @@ BottomLeft TopRight BottomRight - BottomCenter + TopCenter + BottomCenter diff --git a/Src/Examples/ConfigurationExample/Utilities/PositionProviderType.cs b/Src/Examples/ConfigurationExample/Utilities/PositionProviderType.cs index 5ae66f8..fcdbe4c 100644 --- a/Src/Examples/ConfigurationExample/Utilities/PositionProviderType.cs +++ b/Src/Examples/ConfigurationExample/Utilities/PositionProviderType.cs @@ -4,6 +4,7 @@ public enum PositionProviderType { Window, Screen, - Control + Control, + ScreenGdi } } \ No newline at end of file diff --git a/Src/ToastNotifications/Position/ControlPositionProvider.cs b/Src/ToastNotifications/Position/ControlPositionProvider.cs index 7d1b12d..9ce80b2 100644 --- a/Src/ToastNotifications/Position/ControlPositionProvider.cs +++ b/Src/ToastNotifications/Position/ControlPositionProvider.cs @@ -51,6 +51,8 @@ public Point GetPosition(double actualPopupWidth, double actualPopupHeight) return GetPositionForBottomCenterCorner(location, actualPopupWidth, actualPopupHeight); case Corner.BottomLeft: return GetPositionForBottomLeftCorner(location, actualPopupWidth, actualPopupHeight); + case Corner.TopCenter: + return GetPositionForTopCenterCorner(location, actualPopupWidth, actualPopupHeight); default: throw new ArgumentOutOfRangeException(); } @@ -67,6 +69,7 @@ private void SetEjectDirection(Corner corner) { case Corner.TopRight: case Corner.TopLeft: + case Corner.TopCenter: EjectDirection = EjectDirection.ToBottom; break; case Corner.BottomRight: @@ -105,6 +108,11 @@ private Point GetPositionForTopRightCorner(Point location, double actualPopupWid return new Point(location.X + _element.ActualWidth - _offsetX - actualPopupWidth, location.Y + _offsetY); } + private Point GetPositionForTopCenterCorner(Point location, double actualPopupWidth, double actualPopupHeight) + { + return new Point(location.X + (_element.ActualWidth - _offsetX - actualPopupWidth) / 2, location.Y + _offsetY); + } + public void Dispose() { ParentWindow.LocationChanged -= ParentWindowOnLocationChanged; diff --git a/Src/ToastNotifications/Position/Corner.cs b/Src/ToastNotifications/Position/Corner.cs index 87f7ac0..4858db7 100644 --- a/Src/ToastNotifications/Position/Corner.cs +++ b/Src/ToastNotifications/Position/Corner.cs @@ -6,6 +6,7 @@ public enum Corner TopLeft, BottomRight, BottomLeft, - BottomCenter + BottomCenter, + TopCenter } } \ No newline at end of file diff --git a/Src/ToastNotifications/Position/GdiPrimaryScreenPositionProvider.cs b/Src/ToastNotifications/Position/GdiPrimaryScreenPositionProvider.cs index 252016b..502109e 100644 --- a/Src/ToastNotifications/Position/GdiPrimaryScreenPositionProvider.cs +++ b/Src/ToastNotifications/Position/GdiPrimaryScreenPositionProvider.cs @@ -55,6 +55,8 @@ public Point GetPosition(double actualPopupWidth, double actualPopupHeight) return GetPositionForBottomLeftCorner(actualPopupWidth, actualPopupHeight); case Corner.BottomCenter: return GetPositionForBottomCenterCorner(actualPopupWidth, actualPopupHeight); + case Corner.TopCenter: + return GetPositionForTopCenterCorner(actualPopupWidth, actualPopupHeight); default: throw new ArgumentOutOfRangeException(); } @@ -71,6 +73,7 @@ private void SetEjectDirection(Corner corner) { case Corner.TopRight: case Corner.TopLeft: + case Corner.TopCenter: EjectDirection = EjectDirection.ToBottom; break; case Corner.BottomRight: @@ -179,6 +182,24 @@ private Point GetPositionForTopRightCorner(double actualPopupWidth, double actua return new Point(pointX, pointY); } + private Point GetPositionForTopCenterCorner(double actualPopupWidth, double actualPopupHeight) + { + double pointX = (WorkAreaWidth - _offsetX - actualPopupWidth) / 2; + double pointY = _offsetY; + + switch (GetTaskBarLocation()) + { + case WindowsTaskBarLocation.Left: + pointX = (ScreenWidth - _offsetX - actualPopupWidth) / 2; + break; + + case WindowsTaskBarLocation.Top: + pointY = ScreenHeight - WorkAreaHeight + _offsetY; + break; + } + + return new Point(pointX, pointY); + } private WindowsTaskBarLocation GetTaskBarLocation() { diff --git a/Src/ToastNotifications/Position/PrimaryScreenPositionProvider.cs b/Src/ToastNotifications/Position/PrimaryScreenPositionProvider.cs index 250e029..8ee9e0c 100644 --- a/Src/ToastNotifications/Position/PrimaryScreenPositionProvider.cs +++ b/Src/ToastNotifications/Position/PrimaryScreenPositionProvider.cs @@ -47,6 +47,8 @@ public Point GetPosition(double actualPopupWidth, double actualPopupHeight) return GetPositionForBottomLeftCorner(actualPopupWidth, actualPopupHeight); case Corner.BottomCenter: return GetPositionForBottomCenterCorner(actualPopupWidth, actualPopupHeight); + case Corner.TopCenter: + return GetPositionForTopCenterCorner(actualPopupWidth, actualPopupHeight); default: throw new ArgumentOutOfRangeException(); } @@ -63,6 +65,7 @@ private void SetEjectDirection(Corner corner) { case Corner.TopRight: case Corner.TopLeft: + case Corner.TopCenter: EjectDirection = EjectDirection.ToBottom; break; case Corner.BottomRight: @@ -171,6 +174,24 @@ private Point GetPositionForTopRightCorner(double actualPopupWidth, double actua return new Point(pointX, pointY); } + private Point GetPositionForTopCenterCorner(double actualPopupWidth, double actualPopupHeight) + { + double pointX = (WorkAreaWidth - _offsetX - actualPopupWidth) / 2; + double pointY = _offsetY; + + switch (GetTaskBarLocation()) + { + case WindowsTaskBarLocation.Left: + pointX = (ScreenWidth - _offsetX - actualPopupWidth) / 2; + break; + + case WindowsTaskBarLocation.Top: + pointY = ScreenHeight - WorkAreaHeight + _offsetY; + break; + } + + return new Point(pointX, pointY); + } private WindowsTaskBarLocation GetTaskBarLocation() { diff --git a/Src/ToastNotifications/Position/WindowPositionProvider.cs b/Src/ToastNotifications/Position/WindowPositionProvider.cs index eff7eba..f22a0f4 100644 --- a/Src/ToastNotifications/Position/WindowPositionProvider.cs +++ b/Src/ToastNotifications/Position/WindowPositionProvider.cs @@ -44,6 +44,8 @@ public Point GetPosition(double actualPopupWidth, double actualPopupHeight) return GetPositionForBottomLeftCorner(parentPosition, actualPopupWidth, actualPopupHeight); case Corner.BottomCenter: return GetPositionForBottomCenter(parentPosition, actualPopupWidth, actualPopupHeight); + case Corner.TopCenter: + return GetPositionForTopCenter(parentPosition, actualPopupWidth, actualPopupHeight); default: throw new ArgumentOutOfRangeException(); } @@ -69,6 +71,7 @@ private void SetEjectDirection(Corner corner) { case Corner.TopRight: case Corner.TopLeft: + case Corner.TopCenter: EjectDirection = EjectDirection.ToBottom; break; case Corner.BottomRight: @@ -107,6 +110,11 @@ private Point GetPositionForTopRightCorner(Point parentPosition, double actualPo return new Point( parentPosition.X + GetWindowWidth() - _offsetX - actualPopupWidth, parentPosition.Y + _offsetY); } + private Point GetPositionForTopCenter(Point parentPosition, double actualPopupWidth, double actualPopupHeight) + { + return new Point(parentPosition.X + (GetWindowWidth() - actualPopupWidth) / 2, parentPosition.Y + _offsetY); + } + public void Dispose() { ParentWindow.LocationChanged -= ParentWindowOnLocationChanged;