ControlLayoutViewModel.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using System.ComponentModel;
  3. using System.Text;
  4. using Avalonia.Controls;
  5. using Avalonia.Layout;
  6. using Avalonia.VisualTree;
  7. namespace Avalonia.Diagnostics.ViewModels
  8. {
  9. internal class ControlLayoutViewModel : ViewModelBase
  10. {
  11. private readonly IVisual _control;
  12. private Thickness _borderThickness;
  13. private double _height;
  14. private string? _heightConstraint;
  15. private HorizontalAlignment _horizontalAlignment;
  16. private Thickness _marginThickness;
  17. private Thickness _paddingThickness;
  18. private bool _updatingFromControl;
  19. private VerticalAlignment _verticalAlignment;
  20. private double _width;
  21. private string? _widthConstraint;
  22. public ControlLayoutViewModel(IVisual control)
  23. {
  24. _control = control;
  25. HasPadding = AvaloniaPropertyRegistry.Instance.IsRegistered(control, Decorator.PaddingProperty);
  26. HasBorder = AvaloniaPropertyRegistry.Instance.IsRegistered(control, Border.BorderThicknessProperty);
  27. if (control is AvaloniaObject ao)
  28. {
  29. MarginThickness = ao.GetValue(Layoutable.MarginProperty);
  30. if (HasPadding)
  31. {
  32. PaddingThickness = ao.GetValue(Decorator.PaddingProperty);
  33. }
  34. if (HasBorder)
  35. {
  36. BorderThickness = ao.GetValue(Border.BorderThicknessProperty);
  37. }
  38. HorizontalAlignment = ao.GetValue(Layoutable.HorizontalAlignmentProperty);
  39. VerticalAlignment = ao.GetValue(Layoutable.VerticalAlignmentProperty);
  40. }
  41. UpdateSize();
  42. UpdateSizeConstraints();
  43. }
  44. public Thickness MarginThickness
  45. {
  46. get => _marginThickness;
  47. set => RaiseAndSetIfChanged(ref _marginThickness, value);
  48. }
  49. public Thickness BorderThickness
  50. {
  51. get => _borderThickness;
  52. set => RaiseAndSetIfChanged(ref _borderThickness, value);
  53. }
  54. public Thickness PaddingThickness
  55. {
  56. get => _paddingThickness;
  57. set => RaiseAndSetIfChanged(ref _paddingThickness, value);
  58. }
  59. public double Width
  60. {
  61. get => _width;
  62. private set => RaiseAndSetIfChanged(ref _width, value);
  63. }
  64. public double Height
  65. {
  66. get => _height;
  67. private set => RaiseAndSetIfChanged(ref _height, value);
  68. }
  69. public string? WidthConstraint
  70. {
  71. get => _widthConstraint;
  72. private set => RaiseAndSetIfChanged(ref _widthConstraint, value);
  73. }
  74. public string? HeightConstraint
  75. {
  76. get => _heightConstraint;
  77. private set => RaiseAndSetIfChanged(ref _heightConstraint, value);
  78. }
  79. public HorizontalAlignment HorizontalAlignment
  80. {
  81. get => _horizontalAlignment;
  82. private set => RaiseAndSetIfChanged(ref _horizontalAlignment, value);
  83. }
  84. public VerticalAlignment VerticalAlignment
  85. {
  86. get => _verticalAlignment;
  87. private set => RaiseAndSetIfChanged(ref _verticalAlignment, value);
  88. }
  89. public bool HasPadding { get; }
  90. public bool HasBorder { get; }
  91. private void UpdateSizeConstraints()
  92. {
  93. if (_control is IAvaloniaObject ao)
  94. {
  95. string? CreateConstraintInfo(StyledProperty<double> minProperty, StyledProperty<double> maxProperty)
  96. {
  97. bool hasMin = ao.IsSet(minProperty);
  98. bool hasMax = ao.IsSet(maxProperty);
  99. if (hasMin || hasMax)
  100. {
  101. var builder = new StringBuilder();
  102. if (hasMin)
  103. {
  104. var minValue = ao.GetValue(minProperty);
  105. builder.AppendFormat("Min: {0}", Math.Round(minValue, 2));
  106. builder.AppendLine();
  107. }
  108. if (hasMax)
  109. {
  110. var maxValue = ao.GetValue(maxProperty);
  111. builder.AppendFormat("Max: {0}", Math.Round(maxValue, 2));
  112. }
  113. return builder.ToString();
  114. }
  115. return null;
  116. }
  117. WidthConstraint = CreateConstraintInfo(Layoutable.MinWidthProperty, Layoutable.MaxWidthProperty);
  118. HeightConstraint = CreateConstraintInfo(Layoutable.MinHeightProperty, Layoutable.MaxHeightProperty);
  119. }
  120. }
  121. protected override void OnPropertyChanged(PropertyChangedEventArgs e)
  122. {
  123. base.OnPropertyChanged(e);
  124. if (_updatingFromControl)
  125. {
  126. return;
  127. }
  128. if (_control is AvaloniaObject ao)
  129. {
  130. if (e.PropertyName == nameof(MarginThickness))
  131. {
  132. ao.SetValue(Layoutable.MarginProperty, MarginThickness);
  133. }
  134. else if (HasPadding && e.PropertyName == nameof(PaddingThickness))
  135. {
  136. ao.SetValue(Decorator.PaddingProperty, PaddingThickness);
  137. }
  138. else if (HasBorder && e.PropertyName == nameof(BorderThickness))
  139. {
  140. ao.SetValue(Border.BorderThicknessProperty, BorderThickness);
  141. }
  142. else if (e.PropertyName == nameof(HorizontalAlignment))
  143. {
  144. ao.SetValue(Layoutable.HorizontalAlignmentProperty, HorizontalAlignment);
  145. }
  146. else if (e.PropertyName == nameof(VerticalAlignment))
  147. {
  148. ao.SetValue(Layoutable.VerticalAlignmentProperty, VerticalAlignment);
  149. }
  150. }
  151. }
  152. public void ControlPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
  153. {
  154. try
  155. {
  156. _updatingFromControl = true;
  157. if (e.Property == Visual.BoundsProperty)
  158. {
  159. UpdateSize();
  160. }
  161. else
  162. {
  163. if (_control is IAvaloniaObject ao)
  164. {
  165. if (e.Property == Layoutable.MarginProperty)
  166. {
  167. MarginThickness = ao.GetValue(Layoutable.MarginProperty);
  168. }
  169. else if (e.Property == Decorator.PaddingProperty)
  170. {
  171. PaddingThickness = ao.GetValue(Decorator.PaddingProperty);
  172. }
  173. else if (e.Property == Border.BorderThicknessProperty)
  174. {
  175. BorderThickness = ao.GetValue(Border.BorderThicknessProperty);
  176. }
  177. else if (e.Property == Layoutable.MinWidthProperty ||
  178. e.Property == Layoutable.MaxWidthProperty ||
  179. e.Property == Layoutable.MinHeightProperty ||
  180. e.Property == Layoutable.MaxHeightProperty)
  181. {
  182. UpdateSizeConstraints();
  183. }
  184. else if (e.Property == Layoutable.HorizontalAlignmentProperty)
  185. {
  186. HorizontalAlignment = ao.GetValue(Layoutable.HorizontalAlignmentProperty);
  187. }
  188. else if (e.Property == Layoutable.VerticalAlignmentProperty)
  189. {
  190. VerticalAlignment = ao.GetValue(Layoutable.VerticalAlignmentProperty);
  191. }
  192. }
  193. }
  194. }
  195. finally
  196. {
  197. _updatingFromControl = false;
  198. }
  199. }
  200. private void UpdateSize()
  201. {
  202. var size = _control.Bounds;
  203. Width = Math.Round(size.Width, 2);
  204. Height = Math.Round(size.Height, 2);
  205. }
  206. }
  207. }