|
| 1 | +#nullable enable |
| 2 | +using Microsoft.UI.Xaml; |
| 3 | +using Microsoft.UI.Xaml.Controls; |
| 4 | +using WBrush = Microsoft.UI.Xaml.Media.Brush; |
| 5 | +using WContentPresenter = Microsoft.UI.Xaml.Controls.ContentPresenter; |
| 6 | + |
| 7 | +namespace Microsoft.Maui |
| 8 | +{ |
| 9 | + public class MauiButton : Button |
| 10 | + { |
| 11 | + public static readonly DependencyProperty BorderRadiusProperty = |
| 12 | + DependencyProperty.Register(nameof(BorderRadius), typeof(int), typeof(MauiButton), |
| 13 | + new PropertyMetadata(default(int), OnBorderRadiusChanged)); |
| 14 | + |
| 15 | + public static readonly DependencyProperty BackgroundColorProperty = |
| 16 | + DependencyProperty.Register(nameof(BackgroundColor), typeof(WBrush), typeof(MauiButton), |
| 17 | + new PropertyMetadata(default(WBrush), OnBackgroundColorChanged)); |
| 18 | + |
| 19 | + WContentPresenter? _contentPresenter; |
| 20 | + Grid? _rootGrid; |
| 21 | + |
| 22 | + public WBrush BackgroundColor |
| 23 | + { |
| 24 | + get |
| 25 | + { |
| 26 | + return (WBrush)GetValue(BackgroundColorProperty); |
| 27 | + } |
| 28 | + set |
| 29 | + { |
| 30 | + SetValue(BackgroundColorProperty, value); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public int BorderRadius |
| 35 | + { |
| 36 | + get |
| 37 | + { |
| 38 | + return (int)GetValue(BorderRadiusProperty); |
| 39 | + } |
| 40 | + set |
| 41 | + { |
| 42 | + SetValue(BorderRadiusProperty, value); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + protected override void OnApplyTemplate() |
| 47 | + { |
| 48 | + base.OnApplyTemplate(); |
| 49 | + |
| 50 | + _contentPresenter = GetTemplateChild("ContentPresenter") as WContentPresenter; |
| 51 | + _rootGrid = GetTemplateChild("RootGrid") as Grid; |
| 52 | + |
| 53 | + UpdateBackgroundColor(); |
| 54 | + UpdateBorderRadius(); |
| 55 | + } |
| 56 | + |
| 57 | + static void OnBackgroundColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 58 | + { |
| 59 | + ((MauiButton)d).UpdateBackgroundColor(); |
| 60 | + } |
| 61 | + |
| 62 | + static void OnBorderRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 63 | + { |
| 64 | + ((MauiButton)d).UpdateBorderRadius(); |
| 65 | + } |
| 66 | + |
| 67 | + void UpdateBackgroundColor() |
| 68 | + { |
| 69 | + if (BackgroundColor == null) |
| 70 | + BackgroundColor = Background; |
| 71 | + |
| 72 | + if (_contentPresenter != null) |
| 73 | + _contentPresenter.Background = BackgroundColor; |
| 74 | + |
| 75 | + Background = new UI.Xaml.Media.SolidColorBrush(UI.Colors.Transparent); |
| 76 | + } |
| 77 | + |
| 78 | + void UpdateBorderRadius() |
| 79 | + { |
| 80 | + var radius = BorderRadius == -1 ? 0 : BorderRadius; |
| 81 | + var cornerRadius = WinUIHelpers.CreateCornerRadius(radius); |
| 82 | + |
| 83 | + if (_contentPresenter != null) |
| 84 | + _contentPresenter.CornerRadius = cornerRadius; |
| 85 | + |
| 86 | + if (_rootGrid != null) |
| 87 | + _rootGrid.CornerRadius = cornerRadius; |
| 88 | + } |
| 89 | + |
| 90 | + public void UpdateCharacterSpacing(double characterSpacing) |
| 91 | + { |
| 92 | + CharacterSpacing = characterSpacing.ToEm(); |
| 93 | + |
| 94 | + if (_contentPresenter != null) |
| 95 | + _contentPresenter.CharacterSpacing = CharacterSpacing; |
| 96 | + |
| 97 | + var textBlock = GetTextBlock(Content); |
| 98 | + |
| 99 | + if (textBlock != null) |
| 100 | + textBlock.CharacterSpacing = CharacterSpacing; |
| 101 | + } |
| 102 | + |
| 103 | + public TextBlock? GetTextBlock(object content) |
| 104 | + { |
| 105 | + if (content is TextBlock tb) |
| 106 | + { |
| 107 | + return tb; |
| 108 | + } |
| 109 | + |
| 110 | + if (content is StackPanel sp) |
| 111 | + { |
| 112 | + foreach (var item in sp.Children) |
| 113 | + { |
| 114 | + if (item is TextBlock textBlock) |
| 115 | + { |
| 116 | + return textBlock; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return null; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments