Skip to content

Commit

Permalink
More specific placement
Browse files Browse the repository at this point in the history
  • Loading branch information
cwinland committed Oct 23, 2023
1 parent 2dff3e8 commit a6e781e
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 39 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,21 @@ Alert center Notification with collapsed message. | Alert center Notification wi
AlertMaxHeight | Double | 150 | The alert text content maximum height in the alert center.
AlertMaxWidth | Double | Auto | The alert maximum width property of the notification center popup.
ButtonHorizontalAlignment | HorizontalAlignment | Right | Indicates the placement of the Alert Center.
ButtonVerticalAlignment | VerticalAlignment | Top | Indicates the placement of the Alert Center.
ButtonZIndex | Integer | 999 | Indicates the order in which the button is drawn over content.
BorderBrush (Inherited) | Brushes | Transparent | Used for line colors in notification center and / or headers.
IsItemsAscending | Boolean | False | Indicates the order of alerts in the notification center.
MaxNotifications | Byte | 0 (Unlimited) | The upper limit of notifications allowed in the alert center. Oldest are removed when this number is exceeded.
NewAlertColor | Brushes | Goldenrod | Color of the icon when there is a new alert.
NewAlertIcon | PackIconKind | BellAlert | The icon when there is a new alert.
NoAlertColor | Brushes | Black | Color of the icon when there are no unread alerts.
NoAlertIcon | PackIconKind | Notifications | The icon when there are no unread alerts.
ShowNotificationCenterButton | Boolean | True | Sets whether the notification center button is visibile. Set to 'False' to hide the button.
PopupHorizontalPlacement | Double | 0 | Alert Center Popup Positioning.
PopupPlacement | PlacementMode | Bottom | Alert Center Popup Positioning.
PopupStaysOpen | Boolean | False | Indicates if the popup should stay open or automatically close when clicking away.
PopupVerticalPlacement | Double | 0 | Alert Center Popup Positioning.
ShowButtonInHeader | Boolean | True | Sets whether the notification center button is visibile. Set to 'False' to hide the button.
ShowButtonInContent | Boolean | False | Sets whether the notification center button is visibile. Set to 'False' to hide the button.

#### Example

Expand Down
183 changes: 157 additions & 26 deletions Wpf.NotificationCenter/NotificationCenter.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
Expand Down Expand Up @@ -32,6 +33,16 @@ public partial class NotificationCenter : INotifyPropertyChanged

#region Fields

/// <summary>
/// The button z index property
/// </summary>
public static readonly DependencyProperty ButtonZIndexProperty = DependencyProperty.Register(
nameof(ButtonZIndex),
typeof(int),
typeof(NotificationCenter),
new UIPropertyMetadata(999)
);

/// <summary>
/// Color of the icon when there is a new alert.
/// </summary>
Expand Down Expand Up @@ -79,7 +90,7 @@ public partial class NotificationCenter : INotifyPropertyChanged
nameof(AlertMaxHeight),
typeof(double),
typeof(NotificationCenter),
new PropertyMetadata(200d)
new UIPropertyMetadata(200d)
);

/// <summary>
Expand All @@ -89,17 +100,7 @@ public partial class NotificationCenter : INotifyPropertyChanged
nameof(AlertMaxWidth),
typeof(double),
typeof(NotificationCenter),
new PropertyMetadata(double.NaN, Refresh)
);

/// <summary>
/// The show notification center button property
/// </summary>
public static readonly DependencyProperty ShowNotificationCenterButtonProperty = DependencyProperty.Register(
nameof(ShowNotificationCenterButton),
typeof(bool),
typeof(NotificationCenter),
new PropertyMetadata(true)
new UIPropertyMetadata(double.NaN, Refresh)
);

/// <summary>
Expand Down Expand Up @@ -129,7 +130,47 @@ public partial class NotificationCenter : INotifyPropertyChanged
nameof(ButtonHorizontalAlignment),
typeof(HorizontalAlignment),
typeof(NotificationCenter),
new PropertyMetadata(HorizontalAlignment.Right)
new UIPropertyMetadata(HorizontalAlignment.Right)
);

/// <summary>
/// The show button in header property
/// </summary>
public static readonly DependencyProperty ShowButtonInHeaderProperty = DependencyProperty.Register(
nameof(ShowButtonInHeader),
typeof(bool),
typeof(NotificationCenter),
new PropertyMetadata(true)
);

/// <summary>
/// The show button in content property
/// </summary>
public static readonly DependencyProperty ShowButtonInContentProperty = DependencyProperty.Register(
nameof(ShowButtonInContent),
typeof(bool),
typeof(NotificationCenter),
new PropertyMetadata(false)
);

/// <summary>
/// The button vertical alignment property
/// </summary>
public static readonly DependencyProperty ButtonVerticalAlignmentProperty = DependencyProperty.Register(
nameof(ButtonVerticalAlignment),
typeof(VerticalAlignment),
typeof(NotificationCenter),
new UIPropertyMetadata(default(VerticalAlignment))
);

/// <summary>
/// The popup placement property
/// </summary>
public static readonly DependencyProperty PopupPlacementProperty = DependencyProperty.Register(
nameof(PopupPlacement),
typeof(PlacementMode),
typeof(NotificationCenter),
new FrameworkPropertyMetadata(PlacementMode.Bottom)
);

private readonly SolidColorBrush defaultColor = Brushes.Black;
Expand Down Expand Up @@ -169,6 +210,26 @@ public HorizontalAlignment ButtonHorizontalAlignment
set => SetValue(ButtonHorizontalAlignmentProperty, value);
}

/// <summary>
/// Gets or sets the button vertical alignment.
/// </summary>
/// <value>The button vertical alignment.</value>
public VerticalAlignment ButtonVerticalAlignment
{
get => (VerticalAlignment) GetValue(ButtonVerticalAlignmentProperty);
set => SetValue(ButtonVerticalAlignmentProperty, value);
}

/// <summary>
/// Gets or sets the index of the button z.
/// </summary>
/// <value>The index of the button z.</value>
public int ButtonZIndex
{
get => (int) GetValue(ButtonZIndexProperty);
set => SetValue(ButtonZIndexProperty, value);
}

/// <summary>
/// Gets the data visibility.
/// </summary>
Expand Down Expand Up @@ -283,13 +344,6 @@ public PackIconKind NoAlertIcon
/// <value>The notifications.</value>
public ObservableCollection<Note> Notifications { get; } = new();

/// <summary>
/// Gets or sets the notifications visibility.
/// </summary>
/// <value>The notifications visibility.</value>
/// <exclude />
public Visibility NotificationsVisibility => NotificationsVisible ? Visibility.Visible : Visibility.Collapsed;

/// <summary>
/// Gets or sets a value indicating whether [notifications visible].
/// </summary>
Expand All @@ -301,18 +355,95 @@ public bool NotificationsVisible
{
notificationsVisible = value;
OnPropertyChanged();
OnPropertyChanged(nameof(NotificationsVisibility));
}
}

/// <summary>
/// Gets or sets a value indicating whether [show notification center button].
/// Gets or sets the popup placement.
/// </summary>
/// <value>The popup placement.</value>
public PlacementMode PopupPlacement
{
get => (PlacementMode) GetValue(PopupPlacementProperty);
set => SetValue(PopupPlacementProperty, value);
}

/// <summary>
/// The popup stays open property
/// </summary>
public static readonly DependencyProperty PopupStaysOpenProperty = DependencyProperty.Register(
nameof(PopupStaysOpen),
typeof(bool),
typeof(NotificationCenter),
new FrameworkPropertyMetadata(false)
);

/// <summary>
/// Gets or sets a value indicating whether [popup stays open].
/// </summary>
/// <value><c>true</c> if [popup stays open]; otherwise, <c>false</c>.</value>
public bool PopupStaysOpen
{
get => (bool) GetValue(PopupStaysOpenProperty);
set => SetValue(PopupStaysOpenProperty, value);
}
/// <summary>
/// The popup horizontal placement property
/// </summary>
public static readonly DependencyProperty PopupHorizontalPlacementProperty = DependencyProperty.Register(
nameof(PopupHorizontalPlacement),
typeof(double),
typeof(NotificationCenter),
new FrameworkPropertyMetadata(default(double))
);

/// <summary>
/// Gets or sets the popup horizontal placement.
/// </summary>
/// <value>The popup horizontal placement.</value>
public double PopupHorizontalPlacement
{
get => (double) GetValue(PopupHorizontalPlacementProperty);
set => SetValue(PopupHorizontalPlacementProperty, value);
}

/// <summary>
/// The popup vertical placement property
/// </summary>
public static readonly DependencyProperty PopupVerticalPlacementProperty = DependencyProperty.Register(
nameof(PopupVerticalPlacement),
typeof(double),
typeof(NotificationCenter),
new FrameworkPropertyMetadata(default(double))
);

/// <summary>
/// Gets or sets the popup vertical placement.
/// </summary>
/// <value>The popup vertical placement.</value>
public double PopupVerticalPlacement
{
get => (double) GetValue(PopupVerticalPlacementProperty);
set => SetValue(PopupVerticalPlacementProperty, value);
}
/// <summary>
/// Gets or sets a value indicating whether [show button in content].
/// </summary>
/// <value><c>true</c> if [show button in content]; otherwise, <c>false</c>.</value>
public bool ShowButtonInContent
{
get => (bool) GetValue(ShowButtonInContentProperty);
set => SetValue(ShowButtonInContentProperty, value);
}

/// <summary>
/// Gets or sets a value indicating whether [show button in header].
/// </summary>
/// <value><c>true</c> if [show notification center button]; otherwise, <c>false</c>.</value>
public bool ShowNotificationCenterButton
/// <value><c>true</c> if [show button in header]; otherwise, <c>false</c>.</value>
public bool ShowButtonInHeader
{
get => (bool) GetValue(ShowNotificationCenterButtonProperty);
set => SetValue(ShowNotificationCenterButtonProperty, value);
get => (bool) GetValue(ShowButtonInHeaderProperty);
set => SetValue(ShowButtonInHeaderProperty, value);
}

/// <summary>
Expand Down
8 changes: 1 addition & 7 deletions Wpf.NotificationCenter/NotificationCenterButton.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
xmlns:sys="clr-namespace:System;assembly=System.Runtime"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid Background="Transparent" ZIndex="900">
<Grid Background="Transparent" ZIndex="{Binding ButtonZIndex, RelativeSource={RelativeSource AncestorType={x:Type nc:NotificationCenter}}}">
<!-- Notification Center Button -->
<Button Height="Auto"
Margin="0"
Expand Down Expand Up @@ -39,11 +39,5 @@
<Label Content="{Binding NewNotificationCount, RelativeSource={RelativeSource AncestorType={x:Type nc:NotificationCenter}}}" />
</StackPanel>
</Button>

<!-- Alert Center Popup -->
<nc:NotificationPopup StaysOpen="False"
Placement="Bottom"
Width="{Binding AlertMaxWidth, RelativeSource={RelativeSource AncestorType={x:Type nc:NotificationCenter}}}"
IsOpen="{Binding NotificationsVisible, RelativeSource={RelativeSource AncestorType={x:Type nc:NotificationCenter}}}" />
</Grid>
</ContentControl>
4 changes: 3 additions & 1 deletion Wpf.NotificationCenter/NotificationPopup.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
mc:Ignorable="d" AllowsTransparency="True" PopupAnimation="Slide"
d:DesignHeight="450" d:DesignWidth="800">
<Popup.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<CollectionViewSource x:Key="DataSource"
Source="{Binding Notifications, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}}"
behaviors:CollectionViewSourceBehavior.IsAscending="{Binding IsItemsAscending, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}} }"
Expand All @@ -24,7 +25,8 @@
<Border.Effect>
<DropShadowEffect BlurRadius="15" Opacity="0.8" ShadowDepth="10" Direction="-90" RenderingBias="Quality"></DropShadowEffect>
</Border.Effect>
<Grid Visibility="{Binding NotificationsVisibility, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}}">
<Grid
Visibility="{Binding NotificationsVisible, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}, Converter={StaticResource BooleanToVisibilityConverter}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
Expand Down
17 changes: 15 additions & 2 deletions Wpf.NotificationCenter/Themes/Generic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,17 @@
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ContentControl Canvas.ZIndex="0" Content="{Binding }" FontWeight="Bold" FontSize="14" />
<local:NotificationCenterButton HorizontalAlignment="{Binding ButtonHorizontalAlignment, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}}"
Visibility="{Binding ShowNotificationCenterButton, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}, Converter={StaticResource BooleanToVisibilityConverter}}"
<local:NotificationCenterButton HorizontalAlignment="{Binding ButtonHorizontalAlignment, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}}"
VerticalAlignment="{Binding ButtonVerticalAlignment, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}}"
Visibility="{Binding ShowButtonInHeader, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}, Converter={StaticResource BooleanToVisibilityConverter}}"
/>
<!-- Alert Center Popup -->
<local:NotificationPopup StaysOpen="{Binding PopupStaysOpen, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}}"
HorizontalOffset="{Binding PopupHorizontalPlacement, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}}"
VerticalOffset="{Binding PopupVerticalPlacement, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}}"
Placement="{Binding PopupPlacement, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}}"
Width="{Binding AlertMaxWidth, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}}"
IsOpen="{Binding NotificationsVisible, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}}" />
</Grid>
</DataTemplate>
</Setter.Value>
Expand All @@ -425,6 +433,11 @@
</DataTemplate.Resources>
<Grid Background="Transparent">
<ContentControl Canvas.ZIndex="0" Content="{Binding }" FontWeight="Bold" FontSize="14" />
<local:NotificationCenterButton HorizontalAlignment="{Binding ButtonHorizontalAlignment, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}}"
VerticalAlignment="{Binding ButtonVerticalAlignment, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}}"
Visibility="{Binding ShowButtonInContent, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}, Converter={StaticResource BooleanToVisibilityConverter}}"
/>

<!-- Toast Container -->
<Border x:Name="ToastContainer" HorizontalAlignment="{Binding ButtonHorizontalAlignment, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationCenter}}}"
Margin="4 0 0 4"
Expand Down
8 changes: 6 additions & 2 deletions WpfNotificationCenter/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
BorderBrush="Blue"
IsItemsAscending="False"
ButtonHorizontalAlignment="Right"
ButtonVerticalAlignment="Bottom"
PopupPlacement="Bottom"
NewAlertIcon="BellAlert"
NoAlertIcon="BellCancel"
AlertMaxHeight="200"
ShowNotificationCenterButton="True"
ShowButtonInHeader="True"
ShowButtonInContent="False"
ButtonZIndex="5000"
Grid.Row="0">
<notificationCenter:NotificationCenter.Header>
<Grid>
Expand All @@ -35,7 +39,7 @@
</notificationCenter:NotificationCenter.Header>
<notificationCenter:NotificationCenter.Content>
<StackPanel>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<StackPanel Orientation="Horizontal">
<ComboBox SelectedItem="{Binding DataContext.SelectedAlertType, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"
ItemsSource="{Binding DataContext.AlertTypes, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}" />
<ComboBox SelectedItem="{Binding DataContext.SelectedNotificationType, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"
Expand Down

0 comments on commit a6e781e

Please sign in to comment.