TextElement.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using Avalonia.Media;
  2. namespace Avalonia.Controls.Documents
  3. {
  4. /// <summary>
  5. /// TextElement is an base class for content in text based controls.
  6. /// TextElements span other content, applying property values or providing structural information.
  7. /// </summary>
  8. public abstract class TextElement : StyledElement
  9. {
  10. /// <summary>
  11. /// Defines the <see cref="Background"/> property.
  12. /// </summary>
  13. public static readonly StyledProperty<IBrush?> BackgroundProperty =
  14. Border.BackgroundProperty.AddOwner<TextElement>();
  15. /// <summary>
  16. /// Defines the <see cref="FontFamily"/> property.
  17. /// </summary>
  18. public static readonly AttachedProperty<FontFamily> FontFamilyProperty =
  19. AvaloniaProperty.RegisterAttached<TextElement, Control, FontFamily>(
  20. nameof(FontFamily),
  21. defaultValue: FontFamily.Default,
  22. inherits: true);
  23. /// <summary>
  24. /// Defines the <see cref="FontSize"/> property.
  25. /// </summary>
  26. public static readonly AttachedProperty<double> FontSizeProperty =
  27. AvaloniaProperty.RegisterAttached<TextElement, Control, double>(
  28. nameof(FontSize),
  29. defaultValue: 12,
  30. inherits: true);
  31. /// <summary>
  32. /// Defines the <see cref="FontStyle"/> property.
  33. /// </summary>
  34. public static readonly AttachedProperty<FontStyle> FontStyleProperty =
  35. AvaloniaProperty.RegisterAttached<TextElement, Control, FontStyle>(
  36. nameof(FontStyle),
  37. inherits: true);
  38. /// <summary>
  39. /// Defines the <see cref="FontWeight"/> property.
  40. /// </summary>
  41. public static readonly AttachedProperty<FontWeight> FontWeightProperty =
  42. AvaloniaProperty.RegisterAttached<TextElement, Control, FontWeight>(
  43. nameof(FontWeight),
  44. inherits: true,
  45. defaultValue: FontWeight.Normal);
  46. /// <summary>
  47. /// Defines the <see cref="FontStretch"/> property.
  48. /// </summary>
  49. public static readonly AttachedProperty<FontStretch> FontStretchProperty =
  50. AvaloniaProperty.RegisterAttached<TextElement, Control, FontStretch>(
  51. nameof(FontStretch),
  52. inherits: true,
  53. defaultValue: FontStretch.Normal);
  54. /// <summary>
  55. /// Defines the <see cref="Foreground"/> property.
  56. /// </summary>
  57. public static readonly AttachedProperty<IBrush?> ForegroundProperty =
  58. AvaloniaProperty.RegisterAttached<TextElement, Control, IBrush?>(
  59. nameof(Foreground),
  60. Brushes.Black,
  61. inherits: true);
  62. private IInlineHost? _inlineHost;
  63. /// <summary>
  64. /// Gets or sets a brush used to paint the control's background.
  65. /// </summary>
  66. public IBrush? Background
  67. {
  68. get { return GetValue(BackgroundProperty); }
  69. set { SetValue(BackgroundProperty, value); }
  70. }
  71. /// <summary>
  72. /// Gets or sets the font family.
  73. /// </summary>
  74. public FontFamily FontFamily
  75. {
  76. get { return GetValue(FontFamilyProperty); }
  77. set { SetValue(FontFamilyProperty, value); }
  78. }
  79. /// <summary>
  80. /// Gets or sets the font size.
  81. /// </summary>
  82. public double FontSize
  83. {
  84. get { return GetValue(FontSizeProperty); }
  85. set { SetValue(FontSizeProperty, value); }
  86. }
  87. /// <summary>
  88. /// Gets or sets the font style.
  89. /// </summary>
  90. public FontStyle FontStyle
  91. {
  92. get { return GetValue(FontStyleProperty); }
  93. set { SetValue(FontStyleProperty, value); }
  94. }
  95. /// <summary>
  96. /// Gets or sets the font weight.
  97. /// </summary>
  98. public FontWeight FontWeight
  99. {
  100. get { return GetValue(FontWeightProperty); }
  101. set { SetValue(FontWeightProperty, value); }
  102. }
  103. /// <summary>
  104. /// Gets or sets the font stretch.
  105. /// </summary>
  106. public FontStretch FontStretch
  107. {
  108. get { return GetValue(FontStretchProperty); }
  109. set { SetValue(FontStretchProperty, value); }
  110. }
  111. /// <summary>
  112. /// Gets or sets a brush used to paint the text.
  113. /// </summary>
  114. public IBrush? Foreground
  115. {
  116. get { return GetValue(ForegroundProperty); }
  117. set { SetValue(ForegroundProperty, value); }
  118. }
  119. /// <summary>
  120. /// Gets the value of the attached <see cref="FontFamilyProperty"/> on a control.
  121. /// </summary>
  122. /// <param name="control">The control.</param>
  123. /// <returns>The font family.</returns>
  124. public static FontFamily GetFontFamily(Control control)
  125. {
  126. return control.GetValue(FontFamilyProperty);
  127. }
  128. /// <summary>
  129. /// Sets the value of the attached <see cref="FontFamilyProperty"/> on a control.
  130. /// </summary>
  131. /// <param name="control">The control.</param>
  132. /// <param name="value">The property value to set.</param>
  133. public static void SetFontFamily(Control control, FontFamily value)
  134. {
  135. control.SetValue(FontFamilyProperty, value);
  136. }
  137. /// <summary>
  138. /// Gets the value of the attached <see cref="FontSizeProperty"/> on a control.
  139. /// </summary>
  140. /// <param name="control">The control.</param>
  141. /// <returns>The font size.</returns>
  142. public static double GetFontSize(Control control)
  143. {
  144. return control.GetValue(FontSizeProperty);
  145. }
  146. /// <summary>
  147. /// Sets the value of the attached <see cref="FontSizeProperty"/> on a control.
  148. /// </summary>
  149. /// <param name="control">The control.</param>
  150. /// <param name="value">The property value to set.</param>
  151. public static void SetFontSize(Control control, double value)
  152. {
  153. control.SetValue(FontSizeProperty, value);
  154. }
  155. /// <summary>
  156. /// Gets the value of the attached <see cref="FontStyleProperty"/> on a control.
  157. /// </summary>
  158. /// <param name="control">The control.</param>
  159. /// <returns>The font style.</returns>
  160. public static FontStyle GetFontStyle(Control control)
  161. {
  162. return control.GetValue(FontStyleProperty);
  163. }
  164. /// <summary>
  165. /// Sets the value of the attached <see cref="FontStyleProperty"/> on a control.
  166. /// </summary>
  167. /// <param name="control">The control.</param>
  168. /// <param name="value">The property value to set.</param>
  169. public static void SetFontStyle(Control control, FontStyle value)
  170. {
  171. control.SetValue(FontStyleProperty, value);
  172. }
  173. /// <summary>
  174. /// Gets the value of the attached <see cref="FontWeightProperty"/> on a control.
  175. /// </summary>
  176. /// <param name="control">The control.</param>
  177. /// <returns>The font weight.</returns>
  178. public static FontWeight GetFontWeight(Control control)
  179. {
  180. return control.GetValue(FontWeightProperty);
  181. }
  182. /// <summary>
  183. /// Sets the value of the attached <see cref="FontWeightProperty"/> on a control.
  184. /// </summary>
  185. /// <param name="control">The control.</param>
  186. /// <param name="value">The property value to set.</param>
  187. public static void SetFontWeight(Control control, FontWeight value)
  188. {
  189. control.SetValue(FontWeightProperty, value);
  190. }
  191. /// <summary>
  192. /// Gets the value of the attached <see cref="FontStretchProperty"/> on a control.
  193. /// </summary>
  194. /// <param name="control">The control.</param>
  195. /// <returns>The font stretch.</returns>
  196. public static FontStretch GetFontStretch(Control control)
  197. {
  198. return control.GetValue(FontStretchProperty);
  199. }
  200. /// <summary>
  201. /// Sets the value of the attached <see cref="FontStretchProperty"/> on a control.
  202. /// </summary>
  203. /// <param name="control">The control.</param>
  204. /// <param name="value">The property value to set.</param>
  205. public static void SetFontStretch(Control control, FontStretch value)
  206. {
  207. control.SetValue(FontStretchProperty, value);
  208. }
  209. /// <summary>
  210. /// Gets the value of the attached <see cref="ForegroundProperty"/> on a control.
  211. /// </summary>
  212. /// <param name="control">The control.</param>
  213. /// <returns>The foreground.</returns>
  214. public static IBrush? GetForeground(Control control)
  215. {
  216. return control.GetValue(ForegroundProperty);
  217. }
  218. /// <summary>
  219. /// Sets the value of the attached <see cref="ForegroundProperty"/> on a control.
  220. /// </summary>
  221. /// <param name="control">The control.</param>
  222. /// <param name="value">The property value to set.</param>
  223. public static void SetForeground(Control control, IBrush? value)
  224. {
  225. control.SetValue(ForegroundProperty, value);
  226. }
  227. internal IInlineHost? InlineHost
  228. {
  229. get => _inlineHost;
  230. set
  231. {
  232. var oldValue = _inlineHost;
  233. _inlineHost = value;
  234. OnInlineHostChanged(oldValue, value);
  235. }
  236. }
  237. internal virtual void OnInlineHostChanged(IInlineHost? oldValue, IInlineHost? newValue)
  238. {
  239. }
  240. protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
  241. {
  242. base.OnPropertyChanged(change);
  243. switch (change.Property.Name)
  244. {
  245. case nameof(Background):
  246. case nameof(FontFamily):
  247. case nameof(FontSize):
  248. case nameof(FontStyle):
  249. case nameof(FontWeight):
  250. case nameof(FontStretch):
  251. case nameof(Foreground):
  252. InlineHost?.Invalidate();
  253. break;
  254. }
  255. }
  256. }
  257. }