From 961bade055fdc4f00bcff378441fc3c74eea9009 Mon Sep 17 00:00:00 2001 From: Dariusz Komosinski Date: Sun, 28 Jul 2019 22:43:28 +0200 Subject: [PATCH] Do not allocate as much in TextBlock and TextPresenter constructors. --- .../Presenters/TextPresenter.cs | 19 +++++------- src/Avalonia.Controls/TextBlock.cs | 29 +++++++++---------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index b3345ec101..debbb81264 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -49,6 +49,14 @@ namespace Avalonia.Controls.Presenters AffectsRender(PasswordCharProperty, SelectionBrushProperty, SelectionForegroundBrushProperty, SelectionStartProperty, SelectionEndProperty); + + Observable.Merge( + SelectionStartProperty.Changed, + SelectionEndProperty.Changed, + PasswordCharProperty.Changed + ).AddClassHandler((x,_) => x.InvalidateFormattedText()); + + CaretIndexProperty.Changed.AddClassHandler((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 diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index 6b0c48b97b..b9603b91ed 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/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((x,_) => x.OnTextPropertiesChanged()); } /// @@ -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(); - }); } /// @@ -408,5 +401,11 @@ namespace Avalonia.Controls InvalidateFormattedText(); InvalidateMeasure(); } + + private void OnTextPropertiesChanged() + { + InvalidateFormattedText(); + InvalidateMeasure(); + } } }