6 changed files with 179 additions and 153 deletions
@ -1,126 +1,126 @@ |
|||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||
// <copyright file="TextBoxView.cs" company="Steven Kirk">
|
// <copyright file="TextBoxView.cs" company="Steven Kirk">
|
||||
// Copyright 2013 MIT Licence. See licence.md for more information.
|
// Copyright 2013 MIT Licence. See licence.md for more information.
|
||||
// </copyright>
|
// </copyright>
|
||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
namespace Perspex.Controls |
namespace Perspex.Controls |
||||
{ |
{ |
||||
using System; |
using System; |
||||
using Perspex.Media; |
using Perspex.Media; |
||||
using Perspex.Platform; |
using Perspex.Platform; |
||||
using Perspex.Threading; |
using Perspex.Threading; |
||||
using Splat; |
using Splat; |
||||
|
|
||||
internal class TextBoxView : Control |
internal class TextBoxView : Control |
||||
{ |
{ |
||||
private TextBox parent; |
private TextBox parent; |
||||
|
|
||||
private FormattedText formattedText; |
private FormattedText formattedText; |
||||
|
|
||||
private DispatcherTimer caretTimer; |
private DispatcherTimer caretTimer; |
||||
|
|
||||
private bool caretBlink; |
private bool caretBlink; |
||||
|
|
||||
public TextBoxView(TextBox parent) |
public TextBoxView(TextBox parent) |
||||
{ |
{ |
||||
this.caretTimer = new DispatcherTimer(); |
this.caretTimer = new DispatcherTimer(); |
||||
this.caretTimer.Interval = TimeSpan.FromMilliseconds(500); |
this.caretTimer.Interval = TimeSpan.FromMilliseconds(500); |
||||
this.caretTimer.Tick += this.CaretTimerTick; |
this.caretTimer.Tick += this.CaretTimerTick; |
||||
this.parent = parent; |
this.parent = parent; |
||||
} |
} |
||||
|
|
||||
public FormattedText FormattedText |
public FormattedText FormattedText |
||||
{ |
{ |
||||
get |
get |
||||
{ |
{ |
||||
if (this.formattedText == null) |
if (this.formattedText == null) |
||||
{ |
{ |
||||
this.formattedText = this.CreateFormattedText(); |
this.formattedText = this.CreateFormattedText(); |
||||
} |
} |
||||
|
|
||||
return this.formattedText; |
return this.formattedText; |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
public new void GotFocus() |
public new void GotFocus() |
||||
{ |
{ |
||||
this.caretBlink = true; |
this.caretBlink = true; |
||||
this.caretTimer.Start(); |
this.caretTimer.Start(); |
||||
} |
} |
||||
|
|
||||
public new void LostFocus() |
public new void LostFocus() |
||||
{ |
{ |
||||
this.caretTimer.Stop(); |
this.caretTimer.Stop(); |
||||
this.InvalidateVisual(); |
this.InvalidateVisual(); |
||||
} |
} |
||||
|
|
||||
public void InvalidateText() |
public void InvalidateText() |
||||
{ |
{ |
||||
this.formattedText = null; |
this.formattedText = null; |
||||
this.InvalidateMeasure(); |
this.InvalidateMeasure(); |
||||
} |
} |
||||
|
|
||||
internal void CaretMoved() |
internal void CaretMoved() |
||||
{ |
{ |
||||
this.caretBlink = true; |
this.caretBlink = true; |
||||
this.caretTimer.Stop(); |
this.caretTimer.Stop(); |
||||
this.caretTimer.Start(); |
this.caretTimer.Start(); |
||||
this.InvalidateVisual(); |
this.InvalidateVisual(); |
||||
} |
} |
||||
|
|
||||
public override void Render(IDrawingContext context) |
public override void Render(IDrawingContext context) |
||||
{ |
{ |
||||
Rect rect = new Rect(this.ActualSize); |
Rect rect = new Rect(this.ActualSize); |
||||
|
|
||||
context.DrawText(Brushes.Black, rect, this.FormattedText); |
context.DrawText(Brushes.Black, rect, this.FormattedText); |
||||
|
|
||||
if (this.parent.IsFocused) |
if (this.parent.IsFocused) |
||||
{ |
{ |
||||
ITextService textService = Locator.Current.GetService<ITextService>(); |
ITextService textService = Locator.Current.GetService<ITextService>(); |
||||
Point caretPos = textService.GetCaretPosition( |
Point caretPos = textService.GetCaretPosition( |
||||
this.formattedText, |
this.formattedText, |
||||
this.parent.CaretIndex, |
this.parent.CaretIndex, |
||||
this.ActualSize); |
this.ActualSize); |
||||
double[] lineHeights = textService.GetLineHeights(this.formattedText, this.ActualSize); |
double[] lineHeights = textService.GetLineHeights(this.formattedText, this.ActualSize); |
||||
Brush caretBrush = Brushes.Black; |
Brush caretBrush = Brushes.Black; |
||||
|
|
||||
if (this.caretBlink) |
if (this.caretBlink) |
||||
{ |
{ |
||||
context.DrawLine( |
context.DrawLine( |
||||
new Pen(caretBrush, 1), |
new Pen(caretBrush, 1), |
||||
caretPos, |
caretPos, |
||||
new Point(caretPos.X, caretPos.Y + lineHeights[0])); |
new Point(caretPos.X, caretPos.Y + lineHeights[0])); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
protected override Size MeasureOverride(Size constraint) |
protected override Size MeasureOverride(Size constraint) |
||||
{ |
{ |
||||
if (!string.IsNullOrEmpty(this.parent.Text)) |
if (!string.IsNullOrEmpty(this.parent.Text)) |
||||
{ |
{ |
||||
ITextService textService = Locator.Current.GetService<ITextService>(); |
ITextService textService = Locator.Current.GetService<ITextService>(); |
||||
return textService.Measure(this.FormattedText, constraint); |
return textService.Measure(this.FormattedText, constraint); |
||||
} |
} |
||||
|
|
||||
return new Size(); |
return new Size(); |
||||
} |
} |
||||
|
|
||||
private FormattedText CreateFormattedText() |
private FormattedText CreateFormattedText() |
||||
{ |
{ |
||||
return new FormattedText |
return new FormattedText |
||||
{ |
{ |
||||
FontFamilyName = "Segoe UI", |
FontFamilyName = "Segoe UI", |
||||
FontSize = this.GetValue(TextBlock.FontSizeProperty), |
FontSize = this.GetValue(TextBlock.FontSizeProperty), |
||||
FontStyle = this.GetValue(TextBlock.FontStyleProperty), |
FontStyle = this.GetValue(TextBlock.FontStyleProperty), |
||||
Text = this.parent.Text, |
Text = this.parent.Text, |
||||
}; |
}; |
||||
} |
} |
||||
|
|
||||
private void CaretTimerTick(object sender, EventArgs e) |
private void CaretTimerTick(object sender, EventArgs e) |
||||
{ |
{ |
||||
this.caretBlink = !this.caretBlink; |
this.caretBlink = !this.caretBlink; |
||||
this.InvalidateVisual(); |
this.InvalidateVisual(); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -1,21 +1,21 @@ |
|||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||
// <copyright file="ITextService.cs" company="Steven Kirk">
|
// <copyright file="ITextService.cs" company="Steven Kirk">
|
||||
// Copyright 2014 MIT Licence. See licence.md for more information.
|
// Copyright 2014 MIT Licence. See licence.md for more information.
|
||||
// </copyright>
|
// </copyright>
|
||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
namespace Perspex.Platform |
namespace Perspex.Platform |
||||
{ |
{ |
||||
using Perspex.Media; |
using Perspex.Media; |
||||
|
|
||||
public interface ITextService |
public interface ITextService |
||||
{ |
{ |
||||
int GetCaretIndex(FormattedText text, Point point, Size constraint); |
int GetCaretIndex(FormattedText text, Point point, Size constraint); |
||||
|
|
||||
Point GetCaretPosition(FormattedText text, int caretIndex, Size constraint); |
Point GetCaretPosition(FormattedText text, int caretIndex, Size constraint); |
||||
|
|
||||
double[] GetLineHeights(FormattedText text, Size constraint); |
double[] GetLineHeights(FormattedText text, Size constraint); |
||||
|
|
||||
Size Measure(FormattedText text, Size constraint); |
Size Measure(FormattedText text, Size constraint); |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue