TextPresenter.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Reactive.Linq;
  5. using Avalonia.Media;
  6. using Avalonia.Styling;
  7. using Avalonia.Threading;
  8. using Avalonia.VisualTree;
  9. namespace Avalonia.Controls.Presenters
  10. {
  11. public class TextPresenter : TextBlock
  12. {
  13. public static readonly DirectProperty<TextPresenter, int> CaretIndexProperty =
  14. TextBox.CaretIndexProperty.AddOwner<TextPresenter>(
  15. o => o.CaretIndex,
  16. (o, v) => o.CaretIndex = v);
  17. public static readonly DirectProperty<TextPresenter, int> SelectionStartProperty =
  18. TextBox.SelectionStartProperty.AddOwner<TextPresenter>(
  19. o => o.SelectionStart,
  20. (o, v) => o.SelectionStart = v);
  21. public static readonly DirectProperty<TextPresenter, int> SelectionEndProperty =
  22. TextBox.SelectionEndProperty.AddOwner<TextPresenter>(
  23. o => o.SelectionEnd,
  24. (o, v) => o.SelectionEnd = v);
  25. private readonly DispatcherTimer _caretTimer;
  26. private int _caretIndex;
  27. private int _selectionStart;
  28. private int _selectionEnd;
  29. private bool _caretBlink;
  30. private IBrush _highlightBrush;
  31. public TextPresenter()
  32. {
  33. _caretTimer = new DispatcherTimer();
  34. _caretTimer.Interval = TimeSpan.FromMilliseconds(500);
  35. _caretTimer.Tick += CaretTimerTick;
  36. Observable.Merge(
  37. this.GetObservable(SelectionStartProperty),
  38. this.GetObservable(SelectionEndProperty))
  39. .Subscribe(_ => InvalidateFormattedText());
  40. this.GetObservable(CaretIndexProperty)
  41. .Subscribe(CaretIndexChanged);
  42. }
  43. public int CaretIndex
  44. {
  45. get
  46. {
  47. return _caretIndex;
  48. }
  49. set
  50. {
  51. value = CoerceCaretIndex(value);
  52. SetAndRaise(CaretIndexProperty, ref _caretIndex, value);
  53. }
  54. }
  55. public int SelectionStart
  56. {
  57. get
  58. {
  59. return _selectionStart;
  60. }
  61. set
  62. {
  63. value = CoerceCaretIndex(value);
  64. SetAndRaise(SelectionStartProperty, ref _selectionStart, value);
  65. }
  66. }
  67. public int SelectionEnd
  68. {
  69. get
  70. {
  71. return _selectionEnd;
  72. }
  73. set
  74. {
  75. value = CoerceCaretIndex(value);
  76. SetAndRaise(SelectionEndProperty, ref _selectionEnd, value);
  77. }
  78. }
  79. public int GetCaretIndex(Point point)
  80. {
  81. var hit = FormattedText.HitTestPoint(point);
  82. return hit.TextPosition + (hit.IsTrailing ? 1 : 0);
  83. }
  84. public override void Render(DrawingContext context)
  85. {
  86. var selectionStart = SelectionStart;
  87. var selectionEnd = SelectionEnd;
  88. if (selectionStart != selectionEnd)
  89. {
  90. var start = Math.Min(selectionStart, selectionEnd);
  91. var length = Math.Max(selectionStart, selectionEnd) - start;
  92. // issue #600: set constaint before any FormattedText manipulation
  93. // see base.Render(...) implementation
  94. FormattedText.Constraint = Bounds.Size;
  95. var rects = FormattedText.HitTestTextRange(start, length);
  96. if (_highlightBrush == null)
  97. {
  98. _highlightBrush = (IBrush)this.FindResource("HighlightBrush");
  99. }
  100. foreach (var rect in rects)
  101. {
  102. context.FillRectangle(_highlightBrush, rect);
  103. }
  104. }
  105. base.Render(context);
  106. if (selectionStart == selectionEnd)
  107. {
  108. var backgroundColor = (((Control)TemplatedParent).GetValue(BackgroundProperty) as SolidColorBrush)?.Color;
  109. var caretBrush = Brushes.Black;
  110. if (backgroundColor.HasValue)
  111. {
  112. byte red = (byte)~(backgroundColor.Value.R);
  113. byte green = (byte)~(backgroundColor.Value.G);
  114. byte blue = (byte)~(backgroundColor.Value.B);
  115. caretBrush = new SolidColorBrush(Color.FromRgb(red, green, blue));
  116. }
  117. if (_caretBlink)
  118. {
  119. var charPos = FormattedText.HitTestTextPosition(CaretIndex);
  120. var x = Math.Floor(charPos.X) + 0.5;
  121. var y = Math.Floor(charPos.Y) + 0.5;
  122. var b = Math.Ceiling(charPos.Bottom) - 0.5;
  123. context.DrawLine(
  124. new Pen(caretBrush, 1),
  125. new Point(x, y),
  126. new Point(x, b));
  127. }
  128. }
  129. }
  130. public void ShowCaret()
  131. {
  132. _caretBlink = true;
  133. _caretTimer.Start();
  134. InvalidateVisual();
  135. }
  136. public void HideCaret()
  137. {
  138. _caretBlink = false;
  139. _caretTimer.Stop();
  140. InvalidateVisual();
  141. }
  142. internal void CaretIndexChanged(int caretIndex)
  143. {
  144. if (this.GetVisualParent() != null)
  145. {
  146. if (_caretTimer.IsEnabled)
  147. {
  148. _caretBlink = true;
  149. _caretTimer.Stop();
  150. _caretTimer.Start();
  151. InvalidateVisual();
  152. }
  153. if (IsMeasureValid)
  154. {
  155. var rect = FormattedText.HitTestTextPosition(caretIndex);
  156. this.BringIntoView(rect);
  157. }
  158. else
  159. {
  160. // The measure is currently invalid so there's no point trying to bring the
  161. // current char into view until a measure has been carried out as the scroll
  162. // viewer extents may not be up-to-date.
  163. Dispatcher.UIThread.Post(
  164. () =>
  165. {
  166. var rect = FormattedText.HitTestTextPosition(caretIndex);
  167. this.BringIntoView(rect);
  168. },
  169. DispatcherPriority.Normal);
  170. }
  171. }
  172. }
  173. protected override FormattedText CreateFormattedText(Size constraint)
  174. {
  175. var result = base.CreateFormattedText(constraint);
  176. var selectionStart = SelectionStart;
  177. var selectionEnd = SelectionEnd;
  178. var start = Math.Min(selectionStart, selectionEnd);
  179. var length = Math.Max(selectionStart, selectionEnd) - start;
  180. if (length > 0)
  181. {
  182. result.Spans = new[]
  183. {
  184. new FormattedTextStyleSpan(start, length, foregroundBrush: Brushes.White),
  185. };
  186. }
  187. return result;
  188. }
  189. protected override Size MeasureOverride(Size availableSize)
  190. {
  191. var text = Text;
  192. if (!string.IsNullOrEmpty(text))
  193. {
  194. return base.MeasureOverride(availableSize);
  195. }
  196. else
  197. {
  198. return new FormattedText
  199. {
  200. Text = "X",
  201. Typeface = new Typeface(FontFamily, FontSize, FontStyle, FontWeight),
  202. TextAlignment = TextAlignment,
  203. Constraint = availableSize,
  204. }.Measure();
  205. }
  206. }
  207. private int CoerceCaretIndex(int value)
  208. {
  209. var text = Text;
  210. var length = text?.Length ?? 0;
  211. return Math.Max(0, Math.Min(length, value));
  212. }
  213. private void CaretTimerTick(object sender, EventArgs e)
  214. {
  215. _caretBlink = !_caretBlink;
  216. InvalidateVisual();
  217. }
  218. }
  219. }