Skip to content

Commit d096bc6

Browse files
committed
code_style: use OnPropertyChanged instead of static constructor
Signed-off-by: leo <longshuang@msn.cn>
1 parent 62d1232 commit d096bc6

4 files changed

Lines changed: 64 additions & 57 deletions

File tree

‎src/Views/BranchTree.axaml.cs‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,6 @@ public IBrush Background
147147
set => SetValue(BackgroundProperty, value);
148148
}
149149

150-
static BranchTreeNodeTrackStatusPresenter()
151-
{
152-
AffectsMeasure<BranchTreeNodeTrackStatusPresenter>(
153-
FontSizeProperty,
154-
FontFamilyProperty,
155-
ForegroundProperty);
156-
157-
AffectsRender<BranchTreeNodeTrackStatusPresenter>(
158-
ForegroundProperty,
159-
BackgroundProperty);
160-
}
161-
162150
public override void Render(DrawingContext context)
163151
{
164152
base.Render(context);
@@ -170,6 +158,18 @@ public override void Render(DrawingContext context)
170158
}
171159
}
172160

161+
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
162+
{
163+
base.OnPropertyChanged(change);
164+
165+
if (change.Property == FontSizeProperty ||
166+
change.Property == FontFamilyProperty ||
167+
change.Property == ForegroundProperty)
168+
InvalidateMeasure();
169+
else if (change.Property == BackgroundProperty)
170+
InvalidateVisual();
171+
}
172+
173173
protected override void OnDataContextChanged(EventArgs e)
174174
{
175175
base.OnDataContextChanged(e);

‎src/Views/CommitRefsPresenter.cs‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,6 @@ public bool ShowTags
148148
set => SetValue(ShowTagsProperty, value);
149149
}
150150

151-
static CommitRefsPresenter()
152-
{
153-
AffectsMeasure<CommitRefsPresenter>(
154-
FontFamilyProperty,
155-
FontSizeProperty,
156-
ForegroundProperty,
157-
UseGraphColorProperty,
158-
UseCompactBranchNamesProperty,
159-
BackgroundProperty,
160-
ShowTagsProperty);
161-
}
162-
163151
public Models.Decorator DecoratorAt(Point point)
164152
{
165153
var x = 0.0;
@@ -244,6 +232,20 @@ public override void Render(DrawingContext context)
244232
}
245233
}
246234

235+
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
236+
{
237+
base.OnPropertyChanged(change);
238+
239+
if (change.Property == FontFamilyProperty ||
240+
change.Property == FontSizeProperty ||
241+
change.Property == ForegroundProperty ||
242+
change.Property == UseGraphColorProperty ||
243+
change.Property == UseCompactBranchNamesProperty ||
244+
change.Property == BackgroundProperty ||
245+
change.Property == ShowTagsProperty)
246+
InvalidateMeasure();
247+
}
248+
247249
protected override void OnDataContextChanged(EventArgs e)
248250
{
249251
base.OnDataContextChanged(e);

‎src/Views/NameHighlightedTextBlock.cs‎

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,6 @@ public IBrush Foreground
4444
set => SetValue(ForegroundProperty, value);
4545
}
4646

47-
static NameHighlightedTextBlock()
48-
{
49-
AffectsMeasure<NameHighlightedTextBlock>(TextProperty);
50-
}
51-
52-
protected override Size MeasureOverride(Size availableSize)
53-
{
54-
var text = Text;
55-
if (string.IsNullOrEmpty(text))
56-
return base.MeasureOverride(availableSize);
57-
58-
var trimmed = text.Replace("$", "");
59-
var typeface = new Typeface(FontFamily);
60-
var formatted = new FormattedText(
61-
trimmed,
62-
CultureInfo.CurrentCulture,
63-
FlowDirection.LeftToRight,
64-
typeface,
65-
FontSize,
66-
Foreground);
67-
68-
return new Size(formatted.Width, formatted.Height);
69-
}
70-
7147
public override void Render(DrawingContext context)
7248
{
7349
var text = Text;
@@ -107,5 +83,32 @@ public override void Render(DrawingContext context)
10783
isName = !isName;
10884
}
10985
}
86+
87+
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
88+
{
89+
base.OnPropertyChanged(change);
90+
91+
if (change.Property == TextProperty)
92+
InvalidateMeasure();
93+
}
94+
95+
protected override Size MeasureOverride(Size availableSize)
96+
{
97+
var text = Text;
98+
if (string.IsNullOrEmpty(text))
99+
return base.MeasureOverride(availableSize);
100+
101+
var trimmed = text.Replace("$", "");
102+
var typeface = new Typeface(FontFamily);
103+
var formatted = new FormattedText(
104+
trimmed,
105+
CultureInfo.CurrentCulture,
106+
FlowDirection.LeftToRight,
107+
typeface,
108+
FontSize,
109+
Foreground);
110+
111+
return new Size(formatted.Width, formatted.Height);
112+
}
110113
}
111114
}

‎src/Views/TextDiffView.axaml.cs‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,15 +1424,6 @@ public ViewModels.TextLineRange DisplayRange
14241424
set => SetAndRaise(DisplayRangeProperty, ref _displayRange, value);
14251425
}
14261426

1427-
static TextDiffViewMinimap()
1428-
{
1429-
AffectsRender<TextDiffViewMinimap>(
1430-
AddedLineBrushProperty,
1431-
DeletedLineBrushProperty,
1432-
DisplayRangeProperty,
1433-
DisplayRangeColorProperty);
1434-
}
1435-
14361427
public override void Render(DrawingContext context)
14371428
{
14381429
context.DrawRectangle(Brushes.Transparent, null, new Rect(0, 0, Bounds.Width, Bounds.Height));
@@ -1468,6 +1459,17 @@ public override void Render(DrawingContext context)
14681459
context.DrawLine(pen, rect.BottomLeft, rect.BottomRight);
14691460
}
14701461

1462+
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
1463+
{
1464+
base.OnPropertyChanged(change);
1465+
1466+
if (change.Property == AddedLineBrushProperty ||
1467+
change.Property == DeletedLineBrushProperty ||
1468+
change.Property == DisplayRangeColorProperty ||
1469+
change.Property == DisplayRangeProperty)
1470+
InvalidateVisual();
1471+
}
1472+
14711473
protected override void OnDataContextChanged(EventArgs e)
14721474
{
14731475
base.OnDataContextChanged(e);

0 commit comments

Comments
 (0)