From 59a7c74fd1ee7c56907633bccabcfd984e4d7ab2 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 13 Aug 2024 18:24:28 +0800 Subject: [PATCH] fix: try to fix issue #338 * use custom control to draw badges --- src/ViewModels/BranchTreeNode.cs | 5 -- src/Views/BranchTree.axaml | 21 +++---- src/Views/BranchTree.axaml.cs | 94 +++++++++++++++++++++++++++++++ src/Views/Repository.axaml | 34 ++++++----- src/Views/Repository.axaml.cs | 97 ++++++++++++++++++++++++++++++++ 5 files changed, 214 insertions(+), 37 deletions(-) diff --git a/src/ViewModels/BranchTreeNode.cs b/src/ViewModels/BranchTreeNode.cs index 33988725..71f96a90 100644 --- a/src/ViewModels/BranchTreeNode.cs +++ b/src/ViewModels/BranchTreeNode.cs @@ -36,11 +36,6 @@ namespace SourceGit.ViewModels get => Backend is Models.Branch; } - public string TrackStatus - { - get => Backend is Models.Branch { IsLocal: true } branch ? branch.TrackStatus.ToString() : string.Empty; - } - public FontWeight NameFontWeight { get => Backend is Models.Branch { IsCurrent: true } ? FontWeight.Bold : FontWeight.Regular; diff --git a/src/Views/BranchTree.axaml b/src/Views/BranchTree.axaml index 366814fe..59b0b609 100644 --- a/src/Views/BranchTree.axaml +++ b/src/Views/BranchTree.axaml @@ -83,20 +83,13 @@ FontWeight="{Binding NameFontWeight}"/> - - - + FontFamilyProperty = + TextBlock.FontFamilyProperty.AddOwner(); + + public FontFamily FontFamily + { + get => GetValue(FontFamilyProperty); + set => SetValue(FontFamilyProperty, value); + } + + public static readonly StyledProperty FontSizeProperty = + TextBlock.FontSizeProperty.AddOwner(); + + public double FontSize + { + get => GetValue(FontSizeProperty); + set => SetValue(FontSizeProperty, value); + } + + public static readonly StyledProperty ForegroundProperty = + AvaloniaProperty.Register(nameof(Foreground), Brushes.White); + + public IBrush Foreground + { + get => GetValue(ForegroundProperty); + set => SetValue(ForegroundProperty, value); + } + + public static readonly StyledProperty BackgroundProperty = + AvaloniaProperty.Register(nameof(Background), Brushes.White); + + public IBrush Background + { + get => GetValue(BackgroundProperty); + set => SetValue(BackgroundProperty, value); + } + + static BranchTreeNodeTrackStatusPresenter() + { + AffectsMeasure( + FontSizeProperty, + FontFamilyProperty, + ForegroundProperty); + + AffectsRender( + ForegroundProperty, + BackgroundProperty); + } + + public override void Render(DrawingContext context) + { + base.Render(context); + + if (_label != null) + { + context.DrawRectangle(Background, null, new RoundedRect(new Rect(0, 0, _label.Width + 18, 18), new CornerRadius(9))); + context.DrawText(_label, new Point(9, 9 - _label.Height * 0.5)); + } + } + + protected override void OnDataContextChanged(EventArgs e) + { + base.OnDataContextChanged(e); + InvalidateMeasure(); + InvalidateVisual(); + } + + protected override Size MeasureOverride(Size availableSize) + { + _label = null; + + if (DataContext is ViewModels.BranchTreeNode { Backend: Models.Branch branch }) + { + var status = branch.TrackStatus.ToString(); + if (!string.IsNullOrEmpty(status)) + { + _label = new FormattedText( + status, + CultureInfo.CurrentCulture, + FlowDirection.LeftToRight, + new Typeface(FontFamily), + FontSize, + Foreground); + } + } + + return _label != null ? new Size(_label.Width + 18, 18) : new Size(0, 0); + } + + private FormattedText _label = null; + } + public partial class BranchTree : UserControl { public static readonly StyledProperty> NodesProperty = diff --git a/src/Views/Repository.axaml b/src/Views/Repository.axaml index 65747a57..2bf003a7 100644 --- a/src/Views/Repository.axaml +++ b/src/Views/Repository.axaml @@ -91,15 +91,14 @@ - - - + @@ -107,15 +106,14 @@ - - - + diff --git a/src/Views/Repository.axaml.cs b/src/Views/Repository.axaml.cs index f6230f02..a83d24bd 100644 --- a/src/Views/Repository.axaml.cs +++ b/src/Views/Repository.axaml.cs @@ -1,12 +1,109 @@ using System; +using System.Globalization; using Avalonia; using Avalonia.Controls; using Avalonia.Input; using Avalonia.Interactivity; +using Avalonia.Media; namespace SourceGit.Views { + public class CounterPresenter : Control + { + public static readonly StyledProperty CountProperty = + AvaloniaProperty.Register(nameof(Count), 0); + + public int Count + { + get => GetValue(CountProperty); + set => SetValue(CountProperty, value); + } + + public static readonly StyledProperty FontFamilyProperty = + TextBlock.FontFamilyProperty.AddOwner(); + + public FontFamily FontFamily + { + get => GetValue(FontFamilyProperty); + set => SetValue(FontFamilyProperty, value); + } + + public static readonly StyledProperty FontSizeProperty = + TextBlock.FontSizeProperty.AddOwner(); + + public double FontSize + { + get => GetValue(FontSizeProperty); + set => SetValue(FontSizeProperty, value); + } + + public static readonly StyledProperty ForegroundProperty = + AvaloniaProperty.Register(nameof(Foreground), Brushes.White); + + public IBrush Foreground + { + get => GetValue(ForegroundProperty); + set => SetValue(ForegroundProperty, value); + } + + public static readonly StyledProperty BackgroundProperty = + AvaloniaProperty.Register(nameof(Background), Brushes.White); + + public IBrush Background + { + get => GetValue(BackgroundProperty); + set => SetValue(BackgroundProperty, value); + } + + static CounterPresenter() + { + AffectsMeasure( + FontSizeProperty, + FontFamilyProperty, + ForegroundProperty, + CountProperty); + + AffectsRender( + ForegroundProperty, + BackgroundProperty, + CountProperty); + } + + public override void Render(DrawingContext context) + { + base.Render(context); + + if (_label != null) + { + context.DrawRectangle(Background, null, new RoundedRect(new Rect(0, 0, _label.Width + 18, 18), new CornerRadius(9))); + context.DrawText(_label, new Point(9, 9 - _label.Height * 0.5)); + } + } + + protected override Size MeasureOverride(Size availableSize) + { + if (Count > 0) + { + _label = new FormattedText( + Count.ToString(), + CultureInfo.CurrentCulture, + FlowDirection.LeftToRight, + new Typeface(FontFamily), + FontSize, + Foreground); + } + else + { + _label = null; + } + + return _label != null ? new Size(_label.Width + 18, 18) : new Size(0, 0); + } + + private FormattedText _label = null; + } + public partial class Repository : UserControl { public Repository()