Text.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections.Immutable;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Data;
  5. using Avalonia.Media;
  6. using PixiEditor.Extensions.CommonApi.FlyUI.Properties;
  7. using PixiEditor.Extensions.Extensions;
  8. using PixiEditor.Extensions.FlyUI.Converters;
  9. using FontStyle = PixiEditor.Extensions.CommonApi.FlyUI.Properties.FontStyle;
  10. namespace PixiEditor.Extensions.FlyUI.Elements;
  11. public class Text : StatelessElement, IPropertyDeserializable
  12. {
  13. private TextWrap _textWrap = TextWrap.None;
  14. private string _value = null!;
  15. private FontStyle _fontStyle = FontStyle.Normal;
  16. private double _fontSize = 12;
  17. public string Value { get => _value; set => SetField(ref _value, value); }
  18. public TextWrap TextWrap { get => _textWrap; set => SetField(ref _textWrap, value); }
  19. public FontStyle FontStyle { get => _fontStyle; set => SetField(ref _fontStyle, value); }
  20. public double FontSize { get => _fontSize; set => SetField(ref _fontSize, value); }
  21. public Text()
  22. {
  23. }
  24. public Text(string value = "", TextWrap textWrap = TextWrap.None, FontStyle fontStyle = FontStyle.Normal, double fontSize = 12)
  25. {
  26. Value = value;
  27. TextWrap = textWrap;
  28. FontStyle = fontStyle;
  29. FontSize = fontSize;
  30. }
  31. public override Control BuildNative()
  32. {
  33. TextBlock textBlock = new();
  34. Binding valueBinding = new()
  35. {
  36. Source = this,
  37. Path = nameof(Value),
  38. };
  39. Binding textWrapBinding = new()
  40. {
  41. Source = this,
  42. Path = nameof(TextWrap),
  43. Converter = new EnumToEnumConverter<TextWrap, TextWrapping>(),
  44. };
  45. Binding fontStyleBinding = new()
  46. {
  47. Source = this,
  48. Path = nameof(FontStyle),
  49. Converter = new EnumToEnumConverter<FontStyle, Avalonia.Media.FontStyle>(),
  50. };
  51. Binding fontSizeBinding = new()
  52. {
  53. Source = this,
  54. Path = nameof(FontSize),
  55. };
  56. textBlock.Bind(TextBlock.TextProperty, valueBinding);
  57. textBlock.Bind(TextBlock.TextWrappingProperty, textWrapBinding);
  58. textBlock.Bind(TextBlock.FontStyleProperty, fontStyleBinding);
  59. textBlock.Bind(TextBlock.FontSizeProperty, fontSizeBinding);
  60. return textBlock;
  61. }
  62. public virtual IEnumerable<object> GetProperties()
  63. {
  64. yield return Value;
  65. yield return TextWrap;
  66. yield return FontStyle;
  67. yield return FontSize;
  68. }
  69. public virtual void DeserializeProperties(ImmutableList<object> values)
  70. {
  71. Value = (string)values.ElementAtOrDefault(0);
  72. TextWrap = (TextWrap)values.ElementAtOrDefault(1);
  73. FontStyle = (FontStyle)values.ElementAtOrDefault(2);
  74. FontSize = (double)values.ElementAtOrDefault(3, 12.0);
  75. }
  76. }