using System.Windows.Controls; using System.Windows.Documents; using FontFamily = System.Windows.Media.FontFamily; using FontStyle = System.Windows.FontStyle; using SystemFonts = System.Windows.SystemFonts; // ReSharper disable once CheckNamespace namespace WPFluent.Controls; /// /// Represents an icon that uses a glyph from the specified font. /// public class FontIcon : IconElement { /// /// Identifies the dependency property. /// public static readonly DependencyProperty FontFamilyProperty = DependencyProperty.Register( nameof(FontFamily), typeof(FontFamily), typeof(FontIcon), new FrameworkPropertyMetadata(new FontFamily("Segoe MDL2 Assets"), OnFontFamilyChanged)); /// /// Identifies the dependency property. /// public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty .AddOwner( typeof(FontIcon), new FrameworkPropertyMetadata( SystemFonts.MessageFontSize, FrameworkPropertyMetadataOptions.Inherits, OnFontSizeChanged)); /// /// Identifies the dependency property. /// public static readonly DependencyProperty FontStyleProperty = DependencyProperty.Register( nameof(FontStyle), typeof(FontStyle), typeof(FontIcon), new FrameworkPropertyMetadata(FontStyles.Normal, OnFontStyleChanged)); /// /// Identifies the dependency property. /// public static readonly DependencyProperty FontWeightProperty = DependencyProperty.Register( nameof(FontWeight), typeof(FontWeight), typeof(FontIcon), new FrameworkPropertyMetadata(FontWeights.Normal, OnFontWeightChanged)); /// /// Identifies the dependency property. /// public static readonly DependencyProperty GlyphProperty = DependencyProperty.Register( nameof(Glyph), typeof(string), typeof(FontIcon), new FrameworkPropertyMetadata(string.Empty, OnGlyphChanged)); private static void OnFontFamilyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var self = (FontIcon)d; if (self.TextBlock is null) { return; } self.TextBlock.SetCurrentValue(System.Windows.Controls.TextBlock.FontFamilyProperty, (FontFamily)e.NewValue); } private static void OnFontSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var self = (FontIcon)d; if (self.TextBlock is null) { return; } self.TextBlock.SetCurrentValue(System.Windows.Controls.TextBlock.FontSizeProperty, (double)e.NewValue); } private static void OnFontStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var self = (FontIcon)d; if (self.TextBlock is null) { return; } self.TextBlock.SetCurrentValue(System.Windows.Controls.TextBlock.FontStyleProperty, (FontStyle)e.NewValue); } private static void OnFontWeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var self = (FontIcon)d; if (self.TextBlock is null) { return; } self.TextBlock.SetCurrentValue(System.Windows.Controls.TextBlock.FontWeightProperty, (FontWeight)e.NewValue); } private static void OnGlyphChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var self = (FontIcon)d; if (self.TextBlock is null) { return; } self.TextBlock.SetCurrentValue(System.Windows.Controls.TextBlock.TextProperty, (string)e.NewValue); } protected override UIElement InitializeChildren() { if (FontSize.Equals(SystemFonts.MessageFontSize)) { // SetResourceReference(FontSizeProperty, "DefaultIconFontSize"); // If the FontSize is the default, set it to the parent's FontSize. if (VisualParent is not null && TextElement.GetFontSize(VisualParent) != SystemFonts.MessageFontSize) { SetCurrentValue(FontSizeProperty, TextElement.GetFontSize(VisualParent)); } } TextBlock = new TextBlock { Style = null, HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Center, TextAlignment = TextAlignment.Center, FontFamily = FontFamily, FontSize = FontSize, FontStyle = FontStyle, FontWeight = FontWeight, Text = Glyph, Visibility = Visibility.Visible, Focusable = false, }; SetCurrentValue(FocusableProperty, false); return TextBlock; } protected TextBlock? TextBlock { get; set; } /// [Bindable(true)] [Category("Appearance")] [Localizability(LocalizationCategory.Font)] public FontFamily FontFamily { get => (FontFamily)GetValue(FontFamilyProperty); set => SetValue(FontFamilyProperty, value); } /// [TypeConverter(typeof(FontSizeConverter))] [Bindable(true)] [Category("Appearance")] [Localizability(LocalizationCategory.None)] public double FontSize { get => (double)GetValue(FontSizeProperty); set => SetValue(FontSizeProperty, value); } /// [Bindable(true)] [Category("Appearance")] public FontStyle FontStyle { get => (FontStyle)GetValue(FontStyleProperty); set => SetValue(FontStyleProperty, value); } /// [Bindable(true)] [Category("Appearance")] public FontWeight FontWeight { get => (FontWeight)GetValue(FontWeightProperty); set => SetValue(FontWeightProperty, value); } /// /// Gets or sets the character code that identifies the icon glyph. /// /// The hexadecimal character code for the icon glyph. public string Glyph { get => (string)GetValue(GlyphProperty); set => SetValue(GlyphProperty, value); } }