From a46fceeea6653bd20e67c7285be94e42f4885fca Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 17 May 2021 17:38:40 +0800 Subject: [PATCH] optimize: create formatted text only if fallback label changed --- src/Views/Controls/Avatar.cs | 51 +++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/src/Views/Controls/Avatar.cs b/src/Views/Controls/Avatar.cs index 78e59ef4..9b933ccf 100644 --- a/src/Views/Controls/Avatar.cs +++ b/src/Views/Controls/Avatar.cs @@ -61,7 +61,7 @@ namespace SourceGit.Views.Controls { "FallbackLabel", typeof(string), typeof(Avatar), - new PropertyMetadata("?")); + new PropertyMetadata("?", OnFallbackLabelChanged)); /// /// 下载头像失败时显示的Label属性 @@ -75,6 +75,9 @@ namespace SourceGit.Views.Controls { private static Dictionary loaded = new Dictionary(); private static Task loader = null; + private int colorIdx = 0; + private FormattedText label = null; + /// /// 渲染实现 /// @@ -82,17 +85,7 @@ namespace SourceGit.Views.Controls { protected override void OnRender(DrawingContext dc) { base.OnRender(dc); - if (Source == null) { - var placeholder = FallbackLabel.Length > 0 ? FallbackLabel.Substring(0, 1) : "?"; - var formatted = new FormattedText( - placeholder, - CultureInfo.CurrentCulture, - FlowDirection.LeftToRight, - new Typeface(new FontFamily("Consolas"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal), - Width * 0.65, - Brushes.White, - VisualTreeHelper.GetDpi(this).PixelsPerDip); - + if (Source == null && label != null) { var corner = Math.Max(2, Width / 16); var offsetX = (double)0; if (HorizontalAlignment == HorizontalAlignment.Right) { @@ -101,16 +94,38 @@ namespace SourceGit.Views.Controls { offsetX = Width * 0.5; } - var chars = placeholder.ToCharArray(); - var sum = 0; - foreach (var ch in chars) sum += Math.Abs(ch); - - Brush brush = BACKGROUND_BRUSHES[sum % BACKGROUND_BRUSHES.Length]; + Brush brush = BACKGROUND_BRUSHES[colorIdx]; dc.DrawRoundedRectangle(brush, null, new Rect(-Width * 0.5 + offsetX, -Height * 0.5, Width, Height), corner, corner); - dc.DrawText(formatted, new Point(formatted.Width * -0.5 + offsetX, formatted.Height * -0.5)); + dc.DrawText(label, new Point(label.Width * -0.5 + offsetX, label.Height * -0.5)); } } + /// + /// 显示文本变化时触发 + /// + /// + /// + private static void OnFallbackLabelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { + Avatar a = d as Avatar; + if (a == null) return; + + var placeholder = a.FallbackLabel.Length > 0 ? a.FallbackLabel.Substring(0, 1) : "?"; + + a.colorIdx = 0; + a.label = new FormattedText( + placeholder, + CultureInfo.CurrentCulture, + FlowDirection.LeftToRight, + new Typeface(new FontFamily("Consolas"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal), + a.Width * 0.65, + Brushes.White, + VisualTreeHelper.GetDpi(a).PixelsPerDip); + + var chars = placeholder.ToCharArray(); + foreach (var ch in chars) a.colorIdx += Math.Abs(ch); + a.colorIdx = a.colorIdx % BACKGROUND_BRUSHES.Length; + } + /// /// 邮件变化时触发 ///