|
1 | 1 | using System; |
2 | | -using System.Globalization; |
3 | 2 |
|
4 | 3 | using Avalonia; |
5 | 4 | using Avalonia.Controls; |
6 | 5 | using Avalonia.Input; |
7 | 6 | using Avalonia.Interactivity; |
8 | | -using Avalonia.Media; |
9 | 7 |
|
10 | 8 | namespace SourceGit.Views |
11 | 9 | { |
12 | | - public class CounterPresenter : Control |
13 | | - { |
14 | | - public static readonly StyledProperty<int> CountProperty = |
15 | | - AvaloniaProperty.Register<CounterPresenter, int>(nameof(Count)); |
16 | | - |
17 | | - public int Count |
18 | | - { |
19 | | - get => GetValue(CountProperty); |
20 | | - set => SetValue(CountProperty, value); |
21 | | - } |
22 | | - |
23 | | - public static readonly StyledProperty<FontFamily> FontFamilyProperty = |
24 | | - TextBlock.FontFamilyProperty.AddOwner<CounterPresenter>(); |
25 | | - |
26 | | - public FontFamily FontFamily |
27 | | - { |
28 | | - get => GetValue(FontFamilyProperty); |
29 | | - set => SetValue(FontFamilyProperty, value); |
30 | | - } |
31 | | - |
32 | | - public static readonly StyledProperty<double> FontSizeProperty = |
33 | | - TextBlock.FontSizeProperty.AddOwner<CounterPresenter>(); |
34 | | - |
35 | | - public double FontSize |
36 | | - { |
37 | | - get => GetValue(FontSizeProperty); |
38 | | - set => SetValue(FontSizeProperty, value); |
39 | | - } |
40 | | - |
41 | | - public static readonly StyledProperty<IBrush> ForegroundProperty = |
42 | | - AvaloniaProperty.Register<CounterPresenter, IBrush>(nameof(Foreground), Brushes.White); |
43 | | - |
44 | | - public IBrush Foreground |
45 | | - { |
46 | | - get => GetValue(ForegroundProperty); |
47 | | - set => SetValue(ForegroundProperty, value); |
48 | | - } |
49 | | - |
50 | | - public static readonly StyledProperty<IBrush> BackgroundProperty = |
51 | | - AvaloniaProperty.Register<CounterPresenter, IBrush>(nameof(Background), Brushes.White); |
52 | | - |
53 | | - public IBrush Background |
54 | | - { |
55 | | - get => GetValue(BackgroundProperty); |
56 | | - set => SetValue(BackgroundProperty, value); |
57 | | - } |
58 | | - |
59 | | - static CounterPresenter() |
60 | | - { |
61 | | - AffectsMeasure<CounterPresenter>( |
62 | | - FontSizeProperty, |
63 | | - FontFamilyProperty, |
64 | | - ForegroundProperty, |
65 | | - CountProperty); |
66 | | - } |
67 | | - |
68 | | - public override void Render(DrawingContext context) |
69 | | - { |
70 | | - base.Render(context); |
71 | | - |
72 | | - if (_label != null) |
73 | | - { |
74 | | - context.DrawRectangle(Background, null, new RoundedRect(new Rect(0, 0, _label.Width + 18, 18), new CornerRadius(9))); |
75 | | - context.DrawText(_label, new Point(9, 9 - _label.Height * 0.5)); |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - protected override Size MeasureOverride(Size availableSize) |
80 | | - { |
81 | | - if (Count > 0) |
82 | | - { |
83 | | - _label = new FormattedText( |
84 | | - Count.ToString(), |
85 | | - CultureInfo.CurrentCulture, |
86 | | - FlowDirection.LeftToRight, |
87 | | - new Typeface(FontFamily), |
88 | | - FontSize, |
89 | | - Foreground); |
90 | | - } |
91 | | - else |
92 | | - { |
93 | | - _label = null; |
94 | | - } |
95 | | - |
96 | | - InvalidateVisual(); |
97 | | - return _label != null ? new Size(_label.Width + 18, 18) : new Size(0, 0); |
98 | | - } |
99 | | - |
100 | | - private FormattedText _label = null; |
101 | | - } |
102 | | - |
103 | 10 | public partial class Repository : UserControl |
104 | 11 | { |
105 | 12 | public Repository() |
|
0 commit comments