| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- // Copyright (c) The Perspex 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.Linq;
- using System.Reactive.Linq;
- using Perspex.Media;
- using Perspex.Threading;
- using Perspex.VisualTree;
- namespace Perspex.Controls.Presenters
- {
- public class TextPresenter : TextBlock
- {
- public static readonly PerspexProperty<int> CaretIndexProperty =
- TextBox.CaretIndexProperty.AddOwner<TextPresenter>();
- public static readonly PerspexProperty<int> SelectionStartProperty =
- TextBox.SelectionStartProperty.AddOwner<TextPresenter>();
- public static readonly PerspexProperty<int> SelectionEndProperty =
- TextBox.SelectionEndProperty.AddOwner<TextPresenter>();
- private readonly DispatcherTimer _caretTimer;
- private bool _caretBlink;
- private IObservable<bool> _canScrollHorizontally;
- public TextPresenter()
- {
- _caretTimer = new DispatcherTimer();
- _caretTimer.Interval = TimeSpan.FromMilliseconds(500);
- _caretTimer.Tick += CaretTimerTick;
- _canScrollHorizontally = this.GetObservable(TextWrappingProperty)
- .Select(x => x == TextWrapping.NoWrap);
- Observable.Merge(
- this.GetObservable(SelectionStartProperty),
- this.GetObservable(SelectionEndProperty))
- .Subscribe(_ => InvalidateFormattedText());
- this.GetObservable(CaretIndexProperty)
- .Subscribe(CaretIndexChanged);
- }
- public int CaretIndex
- {
- get { return GetValue(CaretIndexProperty); }
- set { SetValue(CaretIndexProperty, value); }
- }
- public int SelectionStart
- {
- get { return GetValue(SelectionStartProperty); }
- set { SetValue(SelectionStartProperty, value); }
- }
- public int SelectionEnd
- {
- get { return GetValue(SelectionEndProperty); }
- set { SetValue(SelectionEndProperty, value); }
- }
- public int GetCaretIndex(Point point)
- {
- var hit = FormattedText.HitTestPoint(point);
- return hit.TextPosition + (hit.IsTrailing ? 1 : 0);
- }
- public override void Render(DrawingContext context)
- {
- var selectionStart = SelectionStart;
- var selectionEnd = SelectionEnd;
- if (selectionStart != selectionEnd)
- {
- var start = Math.Min(selectionStart, selectionEnd);
- var length = Math.Max(selectionStart, selectionEnd) - start;
- var rects = FormattedText.HitTestTextRange(start, length);
- var brush = new SolidColorBrush(0xff086f9e);
- foreach (var rect in rects)
- {
- context.FillRectangle(brush, rect);
- }
- }
- base.Render(context);
- if (selectionStart == selectionEnd)
- {
- var backgroundColor = (((Control)TemplatedParent).GetValue(BackgroundProperty) as SolidColorBrush)?.Color;
- var caretBrush = Brushes.Black;
- if(backgroundColor.HasValue)
- {
- byte red = (byte)~(backgroundColor.Value.R);
- byte green = (byte)~(backgroundColor.Value.G);
- byte blue = (byte)~(backgroundColor.Value.B);
- caretBrush = new SolidColorBrush(Color.FromRgb(red, green, blue));
- }
-
- if (_caretBlink)
- {
- var charPos = FormattedText.HitTestTextPosition(CaretIndex);
- var x = Math.Floor(charPos.X) + 0.5;
- var y = Math.Floor(charPos.Y) + 0.5;
- var b = Math.Ceiling(charPos.Bottom) - 0.5;
- context.DrawLine(
- new Pen(caretBrush, 1),
- new Point(x, y),
- new Point(x, b));
- }
- }
- }
- public void ShowCaret()
- {
- _caretBlink = true;
- _caretTimer.Start();
- InvalidateVisual();
- }
- public void HideCaret()
- {
- _caretBlink = false;
- _caretTimer.Stop();
- InvalidateVisual();
- }
- internal void CaretIndexChanged(int caretIndex)
- {
- if (this.GetVisualParent() != null)
- {
- _caretBlink = true;
- _caretTimer.Stop();
- _caretTimer.Start();
- InvalidateVisual();
- if (IsMeasureValid)
- {
- var rect = FormattedText.HitTestTextPosition(caretIndex);
- this.BringIntoView(rect);
- }
- else
- {
- // The measure is currently invalid so there's no point trying to bring the
- // current char into view until a measure has been carried out as the scroll
- // viewer extents may not be up-to-date.
- Dispatcher.UIThread.InvokeAsync(
- () =>
- {
- var rect = FormattedText.HitTestTextPosition(caretIndex);
- this.BringIntoView(rect);
- },
- DispatcherPriority.Normal);
- }
- }
- }
- protected override FormattedText CreateFormattedText(Size constraint)
- {
- var result = base.CreateFormattedText(constraint);
- var selectionStart = SelectionStart;
- var selectionEnd = SelectionEnd;
- var start = Math.Min(selectionStart, selectionEnd);
- var length = Math.Max(selectionStart, selectionEnd) - start;
- if (length > 0)
- {
- result.SetForegroundBrush(Brushes.White, start, length);
- }
- return result;
- }
- protected override Size MeasureOverride(Size availableSize)
- {
- var text = Text;
- if (!string.IsNullOrWhiteSpace(text))
- {
- return base.MeasureOverride(availableSize);
- }
- else
- {
- // TODO: Pretty sure that measuring "X" isn't the right way to do this...
- using (var formattedText = new FormattedText(
- "X",
- FontFamily,
- FontSize,
- FontStyle,
- TextAlignment,
- FontWeight))
- {
- return formattedText.Measure();
- }
- }
- }
- private void CaretTimerTick(object sender, EventArgs e)
- {
- _caretBlink = !_caretBlink;
- InvalidateVisual();
- }
- }
- }
|