浏览代码

Do not allocate as much in TextBlock and TextPresenter constructors.

Dariusz Komosinski 6 年之前
父节点
当前提交
961bade055
共有 2 个文件被更改,包括 22 次插入26 次删除
  1. 8 11
      src/Avalonia.Controls/Presenters/TextPresenter.cs
  2. 14 15
      src/Avalonia.Controls/TextBlock.cs

+ 8 - 11
src/Avalonia.Controls/Presenters/TextPresenter.cs

@@ -49,6 +49,14 @@ namespace Avalonia.Controls.Presenters
             AffectsRender<TextPresenter>(PasswordCharProperty,
                 SelectionBrushProperty, SelectionForegroundBrushProperty,
                 SelectionStartProperty, SelectionEndProperty);
+
+            Observable.Merge(
+                SelectionStartProperty.Changed,
+                SelectionEndProperty.Changed,
+                PasswordCharProperty.Changed
+            ).AddClassHandler<TextPresenter>((x,_) => x.InvalidateFormattedText());
+
+            CaretIndexProperty.Changed.AddClassHandler<TextPresenter>((x, e) => x.CaretIndexChanged((int)e.NewValue));
         }
 
         public TextPresenter()
@@ -56,17 +64,6 @@ namespace Avalonia.Controls.Presenters
             _caretTimer = new DispatcherTimer();
             _caretTimer.Interval = TimeSpan.FromMilliseconds(500);
             _caretTimer.Tick += CaretTimerTick;
-
-            Observable.Merge(
-                this.GetObservable(SelectionStartProperty),
-                this.GetObservable(SelectionEndProperty))
-                .Subscribe(_ => InvalidateFormattedText());
-
-            this.GetObservable(CaretIndexProperty)
-                .Subscribe(CaretIndexChanged);
-
-            this.GetObservable(PasswordCharProperty)
-                .Subscribe(_ => InvalidateFormattedText());
         }
 
         public int CaretIndex

+ 14 - 15
src/Avalonia.Controls/TextBlock.cs

@@ -1,12 +1,9 @@
 // Copyright (c) The Avalonia Project. All rights reserved.
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
-using System;
-using System.Reactive;
 using System.Reactive.Linq;
 using Avalonia.LogicalTree;
 using Avalonia.Media;
-using Avalonia.Media.Immutable;
 using Avalonia.Metadata;
 
 namespace Avalonia.Controls
@@ -106,6 +103,14 @@ namespace Avalonia.Controls
                 FontWeightProperty,
                 FontSizeProperty,
                 FontStyleProperty);
+
+            Observable.Merge(
+                TextProperty.Changed,
+                TextAlignmentProperty.Changed,
+                FontSizeProperty.Changed,
+                FontStyleProperty.Changed,
+                FontWeightProperty.Changed
+            ).AddClassHandler<TextBlock>((x,_) => x.OnTextPropertiesChanged());
         }
 
         /// <summary>
@@ -114,18 +119,6 @@ namespace Avalonia.Controls
         public TextBlock()
         {
             _text = string.Empty;
-
-            Observable.Merge(
-                this.GetObservable(TextProperty).Select(_ => Unit.Default),
-                this.GetObservable(TextAlignmentProperty).Select(_ => Unit.Default),
-                this.GetObservable(FontSizeProperty).Select(_ => Unit.Default),
-                this.GetObservable(FontStyleProperty).Select(_ => Unit.Default),
-                this.GetObservable(FontWeightProperty).Select(_ => Unit.Default))
-                .Subscribe(_ =>
-                {
-                    InvalidateFormattedText();
-                    InvalidateMeasure();
-                });
         }
 
         /// <summary>
@@ -408,5 +401,11 @@ namespace Avalonia.Controls
             InvalidateFormattedText();
             InvalidateMeasure();
         }
+
+        private void OnTextPropertiesChanged()
+        {
+            InvalidateFormattedText();
+            InvalidateMeasure();
+        }
     }
 }