Browse Source

Add padding

pull/3652/head
Benedikt Schroeder 6 years ago
parent
commit
2d78458f74
  1. 35
      src/Avalonia.Controls/TextBlock.cs

35
src/Avalonia.Controls/TextBlock.cs

@ -2,6 +2,7 @@
// Licensed under the MIT license. See licence.md file in the project root for full license information. // Licensed under the MIT license. See licence.md file in the project root for full license information.
using System.Reactive.Linq; using System.Reactive.Linq;
using Avalonia.Layout;
using Avalonia.LogicalTree; using Avalonia.LogicalTree;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Media.TextFormatting; using Avalonia.Media.TextFormatting;
@ -20,6 +21,12 @@ namespace Avalonia.Controls
public static readonly StyledProperty<IBrush> BackgroundProperty = public static readonly StyledProperty<IBrush> BackgroundProperty =
Border.BackgroundProperty.AddOwner<TextBlock>(); Border.BackgroundProperty.AddOwner<TextBlock>();
/// <summary>
/// Defines the <see cref="Padding"/> property.
/// </summary>
public static readonly StyledProperty<Thickness> PaddingProperty =
Decorator.PaddingProperty.AddOwner<TextBlock>();
// TODO: Define these attached properties elsewhere (e.g. on a Text class) and AddOwner // TODO: Define these attached properties elsewhere (e.g. on a Text class) and AddOwner
// them into TextBlock. // them into TextBlock.
@ -29,7 +36,7 @@ namespace Avalonia.Controls
public static readonly AttachedProperty<FontFamily> FontFamilyProperty = public static readonly AttachedProperty<FontFamily> FontFamilyProperty =
AvaloniaProperty.RegisterAttached<TextBlock, Control, FontFamily>( AvaloniaProperty.RegisterAttached<TextBlock, Control, FontFamily>(
nameof(FontFamily), nameof(FontFamily),
defaultValue: FontFamily.Default, defaultValue: FontFamily.Default,
inherits: true); inherits: true);
/// <summary> /// <summary>
@ -110,16 +117,13 @@ namespace Avalonia.Controls
static TextBlock() static TextBlock()
{ {
ClipToBoundsProperty.OverrideDefaultValue<TextBlock>(true); ClipToBoundsProperty.OverrideDefaultValue<TextBlock>(true);
AffectsRender<TextBlock>(
BackgroundProperty,
ForegroundProperty,
FontWeightProperty,
FontSizeProperty,
FontStyleProperty);
Observable.Merge( Observable.Merge(
TextProperty.Changed, TextProperty.Changed,
ForegroundProperty.Changed,
TextAlignmentProperty.Changed, TextAlignmentProperty.Changed,
TextWrappingProperty.Changed,
TextTrimmingProperty.Changed,
FontSizeProperty.Changed, FontSizeProperty.Changed,
FontStyleProperty.Changed, FontStyleProperty.Changed,
FontWeightProperty.Changed FontWeightProperty.Changed
@ -145,6 +149,15 @@ namespace Avalonia.Controls
} }
} }
/// <summary>
/// Gets or sets the padding to place around the <see cref="Text"/>.
/// </summary>
public Thickness Padding
{
get { return GetValue(PaddingProperty); }
set { SetValue(PaddingProperty, value); }
}
/// <summary> /// <summary>
/// Gets or sets a brush used to paint the control's background. /// Gets or sets a brush used to paint the control's background.
/// </summary> /// </summary>
@ -363,7 +376,9 @@ namespace Avalonia.Controls
context.FillRectangle(background, new Rect(Bounds.Size)); context.FillRectangle(background, new Rect(Bounds.Size));
} }
TextLayout?.Draw(context.PlatformImpl, new Point()); var padding = Padding;
TextLayout?.Draw(context.PlatformImpl, new Point(padding.Left, padding.Top));
} }
/// <summary> /// <summary>
@ -412,6 +427,10 @@ namespace Avalonia.Controls
return new Size(); return new Size();
} }
var padding = Padding;
availableSize = availableSize.Deflate(padding);
if (_constraint != availableSize) if (_constraint != availableSize)
{ {
InvalidateTextLayout(); InvalidateTextLayout();

Loading…
Cancel
Save