-
Notifications
You must be signed in to change notification settings - Fork 110
Implement badge component #463
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
Merged
SKProCH
merged 17 commits into
AvaloniaCommunity:master
from
Hyz-sui:feature-badge-control
Mar 21, 2026
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
de668c5
define MaterialValidationErrorForegroundBrush brush
Hyz-sui 961f601
implement badge control
Hyz-sui 2f95c4f
add demo for Badge and Badged
Hyz-sui f6786d3
Merge branch 'master' into fork/Hyz-sui/feature-badge-control
SKProCH 4c3e143
Add badge increment sample in demo
SKProCH c2cc515
Set BadgeWidth and BadgeHeight default to NaN
SKProCH 9f3e6f2
Fix ValidationError assignment in ReadOnlyTheme.cs
SKProCH ab64cea
Rollback ValidationError to Color, setup MaterialValidationErrorForeg…
SKProCH fe4617e
Move BadgePlacement to proper namespace
SKProCH 4071f77
Fix MaterialValidationErrorForegroundBrush
SKProCH 068b78a
Fix demo
SKProCH 61541cf
Remove unnecessary Panel from Badge template
SKProCH 0d572c3
Make FontSize inherited by default, some padding coerce
SKProCH 0c41100
Rename HasContent to DisplayContent, make it bool?, make content driv…
SKProCH dfca36f
Merge branch 'master' into fork/Hyz-sui/feature-badge-control
SKProCH aac148f
Add Error mode to ColorZoneMode
SKProCH 8db3d70
Add ColorMode property to Badge and Badged
SKProCH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using System; | ||
| using Avalonia.Controls; | ||
| using Avalonia.Interactivity; | ||
| using Material.Styles.Controls; | ||
| using Material.Styles.Enums; | ||
|
|
||
| namespace Material.Avalonia.Demo.Pages; | ||
|
|
||
| public partial class BadgesDemo : UserControl { | ||
| private int _inboxCount; | ||
|
|
||
| public BadgesDemo() { | ||
| InitializeComponent(); | ||
| badgePlacementSelector.ItemsSource = Enum.GetValues<BadgePlacement>(); | ||
| badgePlacementSelector.SelectedItem = BadgePlacement.TopRight; | ||
|
|
||
| badgeColorModeSelector.ItemsSource = Enum.GetValues<ColorZoneMode>(); | ||
| badgeColorModeSelector.SelectedItem = ColorZoneMode.Error; | ||
| } | ||
|
|
||
| private void OnIncrementBadgeClick(object? sender, RoutedEventArgs e) { | ||
| _inboxCount++; | ||
| InteractiveBadged.BadgeContent = _inboxCount.ToString(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| <ResourceDictionary xmlns="https://github.com/avaloniaui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
| xmlns:assists="clr-namespace:Material.Styles.Assists" | ||
| xmlns:controls="clr-namespace:Material.Styles.Controls"> | ||
| <ControlTheme x:Key="MaterialBadge" TargetType="controls:Badge"> | ||
| <Setter Property="CornerRadius" Value="9999" /> | ||
| <Setter Property="Padding" Value="3 0" /> | ||
| <Setter Property="MinHeight" Value="19" /> | ||
| <Setter Property="MinWidth" Value="19" /> | ||
| <Setter Property="HorizontalAlignment" Value="Center" /> | ||
| <Setter Property="VerticalAlignment" Value="Center" /> | ||
| <Setter Property="Template"> | ||
| <Setter.Value> | ||
| <ControlTemplate> | ||
| <Border Width="{TemplateBinding Width}" | ||
| Height="{TemplateBinding Height}" | ||
| MinWidth="{TemplateBinding MinWidth}" | ||
| MinHeight="{TemplateBinding MinHeight}" | ||
| Padding="{TemplateBinding Padding}" | ||
| HorizontalAlignment="{TemplateBinding HorizontalAlignment}" | ||
| VerticalAlignment="{TemplateBinding VerticalAlignment}" | ||
| Background="{TemplateBinding Background}" | ||
| CornerRadius="{TemplateBinding CornerRadius}" | ||
| IsHitTestVisible="False"> | ||
| <ContentPresenter Name="PART_ContentPresenter" | ||
| HorizontalAlignment="Center" | ||
| VerticalAlignment="Center" | ||
| Content="{TemplateBinding Content}" | ||
| FontSize="{TemplateBinding FontSize}" | ||
| Foreground="{TemplateBinding Foreground}" /> | ||
| </Border> | ||
| </ControlTemplate> | ||
| </Setter.Value> | ||
| </Setter> | ||
|
|
||
| <Style Selector="^:content-hidden"> | ||
| <Setter Property="MinWidth" Value="6" /> | ||
| <Setter Property="MinHeight" Value="6" /> | ||
| <Setter Property="Padding" Value="0" /> | ||
| <Style Selector="^ /template/ ContentPresenter#PART_ContentPresenter"> | ||
| <Setter Property="IsVisible" Value="False" /> | ||
| </Style> | ||
| </Style> | ||
|
|
||
| <Style Selector="^[ColorMode=Standard]"> | ||
| <Setter Property="Background" Value="{DynamicResource MaterialPaperBrush}" /> | ||
| <Setter Property="Foreground" Value="{DynamicResource MaterialBodyBrush}" /> | ||
| </Style> | ||
|
|
||
| <Style Selector="^[ColorMode=Inverted]"> | ||
| <Setter Property="Background" Value="{DynamicResource MaterialBodyBrush}" /> | ||
| <Setter Property="Foreground" Value="{DynamicResource MaterialPaperBrush}" /> | ||
| </Style> | ||
|
|
||
| <Style Selector="^[ColorMode=PrimaryLight]"> | ||
| <Setter Property="Background" Value="{DynamicResource MaterialPrimaryLightBrush}" /> | ||
| <Setter Property="Foreground" Value="{DynamicResource MaterialPrimaryLightForegroundBrush}" /> | ||
| </Style> | ||
|
|
||
| <Style Selector="^[ColorMode=PrimaryMid]"> | ||
| <Setter Property="Background" Value="{DynamicResource MaterialPrimaryMidBrush}" /> | ||
| <Setter Property="Foreground" Value="{DynamicResource MaterialPrimaryMidForegroundBrush}" /> | ||
| </Style> | ||
|
|
||
| <Style Selector="^[ColorMode=PrimaryDark]"> | ||
| <Setter Property="Background" Value="{DynamicResource MaterialPrimaryDarkBrush}" /> | ||
| <Setter Property="Foreground" Value="{DynamicResource MaterialPrimaryForegroundBrush}" /> | ||
| </Style> | ||
|
|
||
| <Style Selector="^[ColorMode=Accent]"> | ||
| <Setter Property="Background" Value="{DynamicResource MaterialSecondaryMidBrush}" /> | ||
| <Setter Property="Foreground" Value="{DynamicResource MaterialSecondaryMidForegroundBrush}" /> | ||
| </Style> | ||
|
|
||
| <Style Selector="^[ColorMode=Light]"> | ||
| <Setter Property="Background" Value="{DynamicResource MaterialLightBackgroundBrush}" /> | ||
| <Setter Property="Foreground" Value="{DynamicResource MaterialLightForegroundBrush}" /> | ||
| </Style> | ||
|
|
||
| <Style Selector="^[ColorMode=Dark]"> | ||
| <Setter Property="Background" Value="{DynamicResource MaterialDarkBackgroundBrush}" /> | ||
| <Setter Property="Foreground" Value="{DynamicResource MaterialDarkForegroundBrush}" /> | ||
| </Style> | ||
|
|
||
| <Style Selector="^[ColorMode=Error]"> | ||
| <Setter Property="Background" Value="{DynamicResource MaterialValidationErrorBrush}" /> | ||
| <Setter Property="Foreground" Value="{DynamicResource MaterialValidationErrorForegroundBrush}" /> | ||
| </Style> | ||
| </ControlTheme> | ||
|
|
||
| <ControlTheme x:Key="{x:Type controls:Badge}" | ||
| BasedOn="{StaticResource MaterialBadge}" | ||
| TargetType="controls:Badge" /> | ||
| </ResourceDictionary> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| using Avalonia; | ||
| using Avalonia.Controls; | ||
| using Avalonia.Controls.Metadata; | ||
|
|
||
| namespace Material.Styles.Controls; | ||
|
|
||
| [PseudoClasses(":display-content")] | ||
| public class Badge : ContentControl { | ||
| /// <summary> | ||
| /// Identifies the <see cref="ColorMode"/> property. | ||
| /// </summary> | ||
| public static readonly StyledProperty<ColorZoneMode> ColorModeProperty = | ||
| AvaloniaProperty.Register<Badge, ColorZoneMode>(nameof(ColorMode), ColorZoneMode.Error); | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the color zone mode | ||
| /// </summary> | ||
| public ColorZoneMode ColorMode { | ||
| get => GetValue(ColorModeProperty); | ||
| set => SetValue(ColorModeProperty, value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Identifies the <see cref="DisplayContent"/> property. | ||
| /// </summary> | ||
| public static readonly StyledProperty<bool?> DisplayContentProperty = | ||
| AvaloniaProperty.Register<Badge, bool?>(nameof(DisplayContent)); | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets whether the badge content is displayed. | ||
| /// </summary> | ||
| public bool? DisplayContent { | ||
| get => GetValue(DisplayContentProperty); | ||
| set => SetValue(DisplayContentProperty, value); | ||
| } | ||
|
|
||
| protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { | ||
| base.OnPropertyChanged(change); | ||
| if (change.Property == DisplayContentProperty || change.Property == ContentProperty) { | ||
| UpdatePseudoClasses(); | ||
| } | ||
| } | ||
|
|
||
| private void UpdatePseudoClasses() { | ||
| var displayContent = (DisplayContent, Content) switch { | ||
| (true, _) => true, | ||
| (null, 0) => false, | ||
| (null, "0") => false, | ||
| (null, "") => false, | ||
| (null, not null) => true, | ||
| (_, _) => false, | ||
| }; | ||
| PseudoClasses.Set(":content-hidden", !displayContent); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <ResourceDictionary xmlns="https://github.com/avaloniaui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
| xmlns:assists="clr-namespace:Material.Styles.Assists" | ||
| xmlns:controls="clr-namespace:Material.Styles.Controls"> | ||
| <ControlTheme x:Key="MaterialBadged" TargetType="controls:Badged"> | ||
| <Setter Property="BadgePlacement" Value="TopRight" /> | ||
| <Setter Property="BadgeCornerRadius" Value="9999" /> | ||
| <Setter Property="BadgeFontSize" Value="{Binding $self.FontSize}" /> | ||
| <Setter Property="IsBadgeVisible" Value="True" /> | ||
| <Setter Property="Template"> | ||
| <Setter.Value> | ||
| <ControlTemplate> | ||
| <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> | ||
| <ContentPresenter Margin="8" Content="{TemplateBinding Content}" /> | ||
| <controls:Badge Width="{TemplateBinding BadgeWidth}" | ||
| Height="{TemplateBinding BadgeHeight}" | ||
| Background="{TemplateBinding BadgeBackground}" | ||
| Content="{TemplateBinding BadgeContent}" | ||
| CornerRadius="{TemplateBinding BadgeCornerRadius}" | ||
| FontSize="{TemplateBinding BadgeFontSize}" | ||
| Foreground="{TemplateBinding BadgeForeground}" | ||
| ColorMode="{TemplateBinding BadgeColorMode}" | ||
| DisplayContent="{TemplateBinding BadgeDisplayContent}" | ||
| IsVisible="{TemplateBinding IsBadgeVisible}" /> | ||
| </Grid> | ||
| </ControlTemplate> | ||
| </Setter.Value> | ||
| </Setter> | ||
|
|
||
| <Style Selector="^[BadgeDisplayContent=False] /template/ ContentPresenter"> | ||
| <Setter Property="Margin" Value="2" /> | ||
| </Style> | ||
|
|
||
| <Style Selector="^[BadgePlacement=Left] /template/ controls|Badge"> | ||
| <Setter Property="HorizontalAlignment" Value="Left" /> | ||
| <Setter Property="VerticalAlignment" Value="Center" /> | ||
| </Style> | ||
| <Style Selector="^[BadgePlacement=TopLeft] /template/ controls|Badge"> | ||
| <Setter Property="HorizontalAlignment" Value="Left" /> | ||
| <Setter Property="VerticalAlignment" Value="Top" /> | ||
| </Style> | ||
| <Style Selector="^[BadgePlacement=Top] /template/ controls|Badge"> | ||
| <Setter Property="HorizontalAlignment" Value="Center" /> | ||
| <Setter Property="VerticalAlignment" Value="Top" /> | ||
| </Style> | ||
| <Style Selector="^[BadgePlacement=TopRight] /template/ controls|Badge"> | ||
| <Setter Property="HorizontalAlignment" Value="Right" /> | ||
| <Setter Property="VerticalAlignment" Value="Top" /> | ||
| </Style> | ||
| <Style Selector="^[BadgePlacement=Right] /template/ controls|Badge"> | ||
| <Setter Property="HorizontalAlignment" Value="Right" /> | ||
| <Setter Property="VerticalAlignment" Value="Center" /> | ||
| </Style> | ||
| <Style Selector="^[BadgePlacement=BottomRight] /template/ controls|Badge"> | ||
| <Setter Property="HorizontalAlignment" Value="Right" /> | ||
| <Setter Property="VerticalAlignment" Value="Bottom" /> | ||
| </Style> | ||
| <Style Selector="^[BadgePlacement=Bottom] /template/ controls|Badge"> | ||
| <Setter Property="HorizontalAlignment" Value="Center" /> | ||
| <Setter Property="VerticalAlignment" Value="Bottom" /> | ||
| </Style> | ||
| <Style Selector="^[BadgePlacement=BottomLeft] /template/ controls|Badge"> | ||
| <Setter Property="HorizontalAlignment" Value="Left" /> | ||
| <Setter Property="VerticalAlignment" Value="Bottom" /> | ||
| </Style> | ||
| <Style Selector="^[BadgePlacement=Center] /template/ controls|Badge"> | ||
| <Setter Property="HorizontalAlignment" Value="Center" /> | ||
| <Setter Property="VerticalAlignment" Value="Center" /> | ||
| </Style> | ||
| </ControlTheme> | ||
|
|
||
| <ControlTheme x:Key="{x:Type controls:Badged}" | ||
| BasedOn="{StaticResource MaterialBadged}" | ||
| TargetType="controls:Badged" /> | ||
| </ResourceDictionary> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.