Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for top centered display position. #132

Open
wants to merge 1 commit into
base: master-v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Src/Examples/ConfigurationExample/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions Src/Examples/ConfigurationExample/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@

<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Text="Position provider:" VerticalAlignment="Center" Margin="5" FontWeight="Bold" />
<RadioButton VerticalAlignment="Center" IsChecked="{Binding Path=PositionProviderType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=Window}">Window</RadioButton>
<RadioButton VerticalAlignment="Center" IsChecked="{Binding Path=PositionProviderType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=Screen}" Margin="10 0">Main screen</RadioButton>
<RadioButton VerticalAlignment="Center" IsChecked="{Binding Path=PositionProviderType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=ScreenGdi}" Margin="10 0">Main screen(Gdi)</RadioButton>
<RadioButton VerticalAlignment="Center" IsChecked="{Binding Path=PositionProviderType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=Control}">Control</RadioButton>
</StackPanel>
<DockPanel Grid.Column="1" Grid.ColumnSpan="2" >
Expand All @@ -55,7 +56,8 @@
<RadioButton VerticalAlignment="Center" IsChecked="{Binding Path=Corner, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=BottomLeft}" Margin="20 0">BottomLeft</RadioButton>
<RadioButton VerticalAlignment="Center" IsChecked="{Binding Path=Corner, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=TopRight}">TopRight</RadioButton>
<RadioButton VerticalAlignment="Center" IsChecked="{Binding Path=Corner, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=BottomRight}" Margin="20 0">BottomRight</RadioButton>
<RadioButton VerticalAlignment="Center" IsChecked="{Binding Path=Corner, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=BottomCenter}">BottomCenter</RadioButton>
<RadioButton VerticalAlignment="Center" IsChecked="{Binding Path=Corner, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=TopCenter}">TopCenter</RadioButton>
<RadioButton VerticalAlignment="Center" IsChecked="{Binding Path=Corner, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=BottomCenter}" Margin="20 0">BottomCenter</RadioButton>
</DockPanel>
</Grid>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public enum PositionProviderType
{
Window,
Screen,
Control
Control,
ScreenGdi
}
}
8 changes: 8 additions & 0 deletions Src/ToastNotifications/Position/ControlPositionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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:
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion Src/ToastNotifications/Position/Corner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public enum Corner
TopLeft,
BottomRight,
BottomLeft,
BottomCenter
BottomCenter,
TopCenter
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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:
Expand Down Expand Up @@ -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()
{
Expand Down
21 changes: 21 additions & 0 deletions Src/ToastNotifications/Position/PrimaryScreenPositionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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:
Expand Down Expand Up @@ -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()
{
Expand Down
8 changes: 8 additions & 0 deletions Src/ToastNotifications/Position/WindowPositionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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:
Expand Down Expand Up @@ -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;
Expand Down