|
|
@ -7,12 +7,17 @@ |
|
|
namespace Perspex.Controls |
|
|
namespace Perspex.Controls |
|
|
{ |
|
|
{ |
|
|
using System; |
|
|
using System; |
|
|
|
|
|
using System.Reactive; |
|
|
|
|
|
using System.Reactive.Linq; |
|
|
using Perspex.Media; |
|
|
using Perspex.Media; |
|
|
using Perspex.Platform; |
|
|
using Perspex.Platform; |
|
|
using Splat; |
|
|
using Splat; |
|
|
|
|
|
|
|
|
public class TextBlock : Control |
|
|
public class TextBlock : Control |
|
|
{ |
|
|
{ |
|
|
|
|
|
public static readonly PerspexProperty<string> FontFamilyProperty = |
|
|
|
|
|
PerspexProperty.Register<Control, string>("FontFamily", "Segoe UI", inherits: true); |
|
|
|
|
|
|
|
|
public static readonly PerspexProperty<double> FontSizeProperty = |
|
|
public static readonly PerspexProperty<double> FontSizeProperty = |
|
|
PerspexProperty.Register<Control, double>( |
|
|
PerspexProperty.Register<Control, double>( |
|
|
"FontSize", |
|
|
"FontSize", |
|
|
@ -25,27 +30,24 @@ namespace Perspex.Controls |
|
|
public static readonly PerspexProperty<string> TextProperty = |
|
|
public static readonly PerspexProperty<string> TextProperty = |
|
|
PerspexProperty.Register<TextBlock, string>("Text"); |
|
|
PerspexProperty.Register<TextBlock, string>("Text"); |
|
|
|
|
|
|
|
|
private FormattedText formattedText = new FormattedText(); |
|
|
private FormattedText formattedText; |
|
|
|
|
|
|
|
|
public TextBlock() |
|
|
public TextBlock() |
|
|
{ |
|
|
{ |
|
|
this.GetObservable(TextProperty).Subscribe(x => |
|
|
Observable.Merge( |
|
|
{ |
|
|
this.GetObservable(TextProperty).Select(_ => Unit.Default), |
|
|
this.formattedText.Text = x; |
|
|
this.GetObservable(FontSizeProperty).Select(_ => Unit.Default), |
|
|
this.InvalidateMeasure(); |
|
|
this.GetObservable(FontStyleProperty).Select(_ => Unit.Default)) |
|
|
}); |
|
|
.Subscribe(_ => |
|
|
|
|
|
{ |
|
|
this.GetObservable(FontSizeProperty).Subscribe(x => |
|
|
if (this.formattedText != null) |
|
|
{ |
|
|
{ |
|
|
this.formattedText.FontSize = x; |
|
|
this.formattedText.Dispose(); |
|
|
this.InvalidateMeasure(); |
|
|
this.formattedText = null; |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
this.GetObservable(FontStyleProperty).Subscribe(x => |
|
|
this.InvalidateMeasure(); |
|
|
{ |
|
|
}); |
|
|
this.formattedText.FontStyle = x; |
|
|
|
|
|
this.InvalidateMeasure(); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public string Text |
|
|
public string Text |
|
|
@ -54,6 +56,12 @@ namespace Perspex.Controls |
|
|
set { this.SetValue(TextProperty, value); } |
|
|
set { this.SetValue(TextProperty, value); } |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public string FontFamily |
|
|
|
|
|
{ |
|
|
|
|
|
get { return this.GetValue(FontFamilyProperty); } |
|
|
|
|
|
set { this.SetValue(FontFamilyProperty, value); } |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public double FontSize |
|
|
public double FontSize |
|
|
{ |
|
|
{ |
|
|
get { return this.GetValue(FontSizeProperty); } |
|
|
get { return this.GetValue(FontSizeProperty); } |
|
|
@ -66,6 +74,24 @@ namespace Perspex.Controls |
|
|
set { this.SetValue(FontStyleProperty, value); } |
|
|
set { this.SetValue(FontStyleProperty, value); } |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected FormattedText FormattedText |
|
|
|
|
|
{ |
|
|
|
|
|
get |
|
|
|
|
|
{ |
|
|
|
|
|
if (this.formattedText == null) |
|
|
|
|
|
{ |
|
|
|
|
|
this.formattedText = new FormattedText( |
|
|
|
|
|
this.Text, |
|
|
|
|
|
this.FontFamily, |
|
|
|
|
|
this.FontSize, |
|
|
|
|
|
this.FontStyle); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return this.formattedText; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void Render(IDrawingContext context) |
|
|
public override void Render(IDrawingContext context) |
|
|
{ |
|
|
{ |
|
|
Brush background = this.Background; |
|
|
Brush background = this.Background; |
|
|
@ -75,18 +101,15 @@ namespace Perspex.Controls |
|
|
context.FillRectange(background, new Rect(this.ActualSize)); |
|
|
context.FillRectange(background, new Rect(this.ActualSize)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
context.DrawText( |
|
|
context.DrawText(this.Foreground, new Point(), this.FormattedText); |
|
|
this.Foreground, |
|
|
|
|
|
new Rect(this.ActualSize), |
|
|
|
|
|
this.formattedText); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected override Size MeasureOverride(Size availableSize) |
|
|
protected override Size MeasureOverride(Size availableSize) |
|
|
{ |
|
|
{ |
|
|
if (!string.IsNullOrEmpty(this.Text)) |
|
|
if (!string.IsNullOrEmpty(this.Text)) |
|
|
{ |
|
|
{ |
|
|
this.formattedText.Constraint = availableSize; |
|
|
this.FormattedText.Constraint = availableSize; |
|
|
return this.formattedText.Measure(); |
|
|
return this.FormattedText.Measure(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return new Size(); |
|
|
return new Size(); |
|
|
|