diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ControlDetailsViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ControlDetailsViewModel.cs index d4b988acd4..fa41eacbeb 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ControlDetailsViewModel.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ControlDetailsViewModel.cs @@ -32,6 +32,8 @@ namespace Avalonia.Diagnostics.ViewModels view.Filter = FilterProperty; PropertiesView = view; + Layout = new ControlLayoutViewModel(control); + if (control is INotifyPropertyChanged inpc) { inpc.PropertyChanged += ControlPropertyChanged; @@ -52,6 +54,8 @@ namespace Avalonia.Diagnostics.ViewModels get => _selectedProperty; set => RaiseAndSetIfChanged(ref _selectedProperty, value); } + + public ControlLayoutViewModel Layout { get; } public void Dispose() { @@ -112,6 +116,8 @@ namespace Avalonia.Diagnostics.ViewModels property.Update(); } } + + Layout.ControlPropertyChanged(sender, e); } private void ControlPropertyChanged(object sender, PropertyChangedEventArgs e) diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ControlLayoutViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ControlLayoutViewModel.cs new file mode 100644 index 0000000000..fd2e4c3355 --- /dev/null +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ControlLayoutViewModel.cs @@ -0,0 +1,190 @@ +using System.ComponentModel; +using Avalonia.Controls; +using Avalonia.Layout; +using Avalonia.VisualTree; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal class ControlLayoutViewModel : ViewModelBase + { + private readonly IVisual _control; + private Thickness _marginThickness; + private Thickness _borderThickness; + private Thickness _paddingThickness; + private double _width; + private double _height; + private string _widthConstraint; + private string _heightConstraint; + private bool _updatingFromControl; + + public Thickness MarginThickness + { + get => _marginThickness; + set => RaiseAndSetIfChanged(ref _marginThickness, value); + } + + public Thickness BorderThickness + { + get => _borderThickness; + set => RaiseAndSetIfChanged(ref _borderThickness, value); + } + + public Thickness PaddingThickness + { + get => _paddingThickness; + set => RaiseAndSetIfChanged(ref _paddingThickness, value); + } + + public double Width + { + get => _width; + private set => RaiseAndSetIfChanged(ref _width, value); + } + + public double Height + { + get => _height; + private set => RaiseAndSetIfChanged(ref _height, value); + } + + public string WidthConstraint + { + get => _widthConstraint; + private set => RaiseAndSetIfChanged(ref _widthConstraint, value); + } + + public string HeightConstraint + { + get => _heightConstraint; + private set => RaiseAndSetIfChanged(ref _heightConstraint, value); + } + + public bool HasPadding { get; } + + public bool HasBorder { get; } + + public ControlLayoutViewModel(IVisual control) + { + _control = control; + + HasPadding = AvaloniaPropertyRegistry.Instance.IsRegistered(control, Decorator.PaddingProperty); + HasBorder = AvaloniaPropertyRegistry.Instance.IsRegistered(control, Border.BorderThicknessProperty); + + if (control is AvaloniaObject ao) + { + MarginThickness = ao.GetValue(Layoutable.MarginProperty); + + if (HasPadding) + { + PaddingThickness = ao.GetValue(Decorator.PaddingProperty); + } + + if (HasBorder) + { + BorderThickness = ao.GetValue(Border.BorderThicknessProperty); + } + } + + UpdateSize(); + UpdateSizeConstraints(); + } + + private void UpdateSizeConstraints() + { + if (_control is IAvaloniaObject ao) + { + string CreateConstraintInfo(StyledProperty minProperty, StyledProperty maxProperty) + { + if (ao.IsSet(minProperty) || ao.IsSet(maxProperty)) + { + var minValue = ao.GetValue(minProperty); + var maxValue = ao.GetValue(maxProperty); + + return $"{minValue} < size < {maxValue}"; + } + + return null; + } + + WidthConstraint = CreateConstraintInfo(Layoutable.MinWidthProperty, Layoutable.MaxWidthProperty); + HeightConstraint = CreateConstraintInfo(Layoutable.MinHeightProperty, Layoutable.MaxHeightProperty); + } + } + + protected override void OnPropertyChanged(PropertyChangedEventArgs e) + { + base.OnPropertyChanged(e); + + if (_updatingFromControl) + { + return; + } + + if (_control is AvaloniaObject ao) + { + if (e.PropertyName == nameof(MarginThickness)) + { + ao.SetValue(Layoutable.MarginProperty, MarginThickness); + } + else if (HasPadding && e.PropertyName == nameof(PaddingThickness)) + { + ao.SetValue(Decorator.PaddingProperty, PaddingThickness); + } + else if (HasBorder && e.PropertyName == nameof(BorderThickness)) + { + ao.SetValue(Border.BorderThicknessProperty, BorderThickness); + } + } + } + + public void ControlPropertyChanged(object sender, AvaloniaPropertyChangedEventArgs e) + { + try + { + _updatingFromControl = true; + + if (e.Property == Visual.BoundsProperty) + { + UpdateSize(); + } + else + { + if (_control is IAvaloniaObject ao) + { + if (e.Property == Layoutable.MarginProperty) + { + MarginThickness = ao.GetValue(Layoutable.MarginProperty); + } + else if (e.Property == Decorator.PaddingProperty) + { + PaddingThickness = ao.GetValue(Decorator.PaddingProperty); + } + else if (e.Property == Border.BorderThicknessProperty) + { + BorderThickness = ao.GetValue(Border.BorderThicknessProperty); + } + else if (e.Property == Layoutable.MinWidthProperty || + e.Property == Layoutable.MaxWidthProperty || + e.Property == Layoutable.MinHeightProperty || + e.Property == Layoutable.MaxHeightProperty) + { + UpdateSizeConstraints(); + } + } + } + } + finally + { + _updatingFromControl = false; + } + } + + private void UpdateSize() + { + var size = _control.Bounds; + + Width = size.Width; + Height = size.Height; + } + } +} diff --git a/src/Avalonia.Diagnostics/Diagnostics/Views/ControlDetailsView.xaml b/src/Avalonia.Diagnostics/Diagnostics/Views/ControlDetailsView.xaml index 8aad5fffd8..2e0b6813ba 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/Views/ControlDetailsView.xaml +++ b/src/Avalonia.Diagnostics/Diagnostics/Views/ControlDetailsView.xaml @@ -1,38 +1,155 @@  - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Avalonia.Diagnostics/Diagnostics/Views/ControlDetailsView.xaml.cs b/src/Avalonia.Diagnostics/Diagnostics/Views/ControlDetailsView.xaml.cs index c6bd5a18aa..c9568509f6 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/Views/ControlDetailsView.xaml.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/Views/ControlDetailsView.xaml.cs @@ -1,10 +1,24 @@ +using System; using Avalonia.Controls; +using Avalonia.Controls.Shapes; using Avalonia.Markup.Xaml; +using Avalonia.VisualTree; namespace Avalonia.Diagnostics.Views { internal class ControlDetailsView : UserControl { + private ThicknessEditor _borderArea; + private ThicknessEditor _paddingArea; + private Rectangle _horizontalSizeBegin; + private Rectangle _horizontalSizeEnd; + private Rectangle _verticalSizeBegin; + private Rectangle _verticalSizeEnd; + private Grid _layoutRoot; + private Border _horizontalSize; + private Border _verticalSize; + private Border _contentArea; + public ControlDetailsView() { InitializeComponent(); @@ -13,6 +27,101 @@ namespace Avalonia.Diagnostics.Views private void InitializeComponent() { AvaloniaXamlLoader.Load(this); + + _borderArea = this.FindControl("BorderArea"); + _paddingArea = this.FindControl("PaddingArea"); + + _horizontalSizeBegin = this.FindControl("HorizontalSizeBegin"); + _horizontalSizeEnd = this.FindControl("HorizontalSizeEnd"); + _verticalSizeBegin = this.FindControl("VerticalSizeBegin"); + _verticalSizeEnd = this.FindControl("VerticalSizeEnd"); + + _horizontalSize = this.FindControl("HorizontalSize"); + _verticalSize = this.FindControl("VerticalSize"); + + _contentArea = this.FindControl("ContentArea"); + + _layoutRoot = this.FindControl("LayoutRoot"); + + void SubscribeToBounds(Visual visual) + { + visual.GetPropertyChangedObservable(TransformedBoundsProperty) + .Subscribe(UpdateSizeGuidelines); + } + + SubscribeToBounds(_borderArea); + SubscribeToBounds(_paddingArea); + SubscribeToBounds(_contentArea); + } + + private void UpdateSizeGuidelines(AvaloniaPropertyChangedEventArgs e) + { + void UpdateGuidelines(Visual area) + { + if (area.TransformedBounds is TransformedBounds bounds) + { + // Horizontal guideline + { + var sizeArea = TranslateToRoot((_horizontalSize.TransformedBounds ?? default).Bounds.BottomLeft, + _horizontalSize); + + var start = TranslateToRoot(bounds.Bounds.BottomLeft, area); + + SetPosition(_horizontalSizeBegin, start); + + var end = TranslateToRoot(bounds.Bounds.BottomRight, area); + + SetPosition(_horizontalSizeEnd, end.WithX(end.X - 1)); + + var height = sizeArea.Y - start.Y + 2; + + _horizontalSizeBegin.Height = height; + _horizontalSizeEnd.Height = height; + } + + // Vertical guideline + { + var sizeArea = TranslateToRoot((_verticalSize.TransformedBounds ?? default).Bounds.TopRight, _verticalSize); + + var start = TranslateToRoot(bounds.Bounds.TopRight, area); + + SetPosition(_verticalSizeBegin, start); + + var end = TranslateToRoot(bounds.Bounds.BottomRight, area); + + SetPosition(_verticalSizeEnd, end.WithY(end.Y - 1)); + + var width = sizeArea.X - start.X + 2; + + _verticalSizeBegin.Width = width; + _verticalSizeEnd.Width = width; + } + } + } + + Point TranslateToRoot(Point point, IVisual from) + { + return from.TranslatePoint(point, _layoutRoot) ?? default; + } + + static void SetPosition(Rectangle rect, Point start) + { + Canvas.SetLeft(rect, start.X); + Canvas.SetTop(rect, start.Y); + } + + if (_borderArea.IsPresent) + { + UpdateGuidelines(_borderArea); + } + else if (_paddingArea.IsPresent) + { + UpdateGuidelines(_paddingArea); + } + else + { + UpdateGuidelines(_contentArea); + } } } } diff --git a/src/Avalonia.Diagnostics/Diagnostics/Views/ThicknessEditor.cs b/src/Avalonia.Diagnostics/Diagnostics/Views/ThicknessEditor.cs new file mode 100644 index 0000000000..c7611c8c46 --- /dev/null +++ b/src/Avalonia.Diagnostics/Diagnostics/Views/ThicknessEditor.cs @@ -0,0 +1,130 @@ +using Avalonia.Controls; +using Avalonia.Data; +using Avalonia.Data.Converters; +using Avalonia.Media; + +namespace Avalonia.Diagnostics.Views +{ + internal static class Converters + { + public static IValueConverter HasConstraintConverter = + new FuncValueConverter(ConvertToDecoration); + + private static TextDecorationCollection ConvertToDecoration(object arg) + { + return arg != null ? TextDecorations.Underline : null; + } + } + + internal class ThicknessEditor : ContentControl + { + public static readonly DirectProperty ThicknessProperty = + AvaloniaProperty.RegisterDirect(nameof(Thickness), o => o.Thickness, + (o, v) => o.Thickness = v, defaultBindingMode: BindingMode.TwoWay); + + public static readonly DirectProperty HeaderProperty = + AvaloniaProperty.RegisterDirect(nameof(Header), o => o.Header, + (o, v) => o.Header = v); + + public static readonly DirectProperty IsPresentProperty = + AvaloniaProperty.RegisterDirect(nameof(Header), o => o.IsPresent, + (o, v) => o.IsPresent = v); + + public static readonly DirectProperty LeftProperty = + AvaloniaProperty.RegisterDirect(nameof(Left), o => o.Left, (o, v) => o.Left = v); + + public static readonly DirectProperty TopProperty = + AvaloniaProperty.RegisterDirect(nameof(Top), o => o.Top, (o, v) => o.Top = v); + + public static readonly DirectProperty RightProperty = + AvaloniaProperty.RegisterDirect(nameof(Right), o => o.Right, + (o, v) => o.Right = v); + + public static readonly DirectProperty BottomProperty = + AvaloniaProperty.RegisterDirect(nameof(Bottom), o => o.Bottom, + (o, v) => o.Bottom = v); + + + private Thickness _thickness; + private string _header; + private bool _isPresent = true; + private double _left; + private double _top; + private double _right; + private double _bottom; + + private bool _isUpdatingThickness; + + public Thickness Thickness + { + get => _thickness; + set => SetAndRaise(ThicknessProperty, ref _thickness, value); + } + + public string Header + { + get => _header; + set => SetAndRaise(HeaderProperty, ref _header, value); + } + + public bool IsPresent + { + get => _isPresent; + set => SetAndRaise(IsPresentProperty, ref _isPresent, value); + } + + public double Left + { + get => _left; + set => SetAndRaise(LeftProperty, ref _left, value); + } + + public double Top + { + get => _top; + set => SetAndRaise(TopProperty, ref _top, value); + } + + public double Right + { + get => _right; + set => SetAndRaise(RightProperty, ref _right, value); + } + + public double Bottom + { + get => _bottom; + set => SetAndRaise(BottomProperty, ref _bottom, value); + } + + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) + { + base.OnPropertyChanged(change); + + if (change.Property == ThicknessProperty) + { + try + { + _isUpdatingThickness = true; + + var value = change.NewValue.GetValueOrDefault(); + + Left = value.Left; + Top = value.Top; + Right = value.Right; + Bottom = value.Bottom; + } + finally + { + _isUpdatingThickness = false; + } + } + else if (!_isUpdatingThickness && + (change.Property == LeftProperty || change.Property == TopProperty || + change.Property == RightProperty || change.Property == BottomProperty)) + { + Thickness = new Thickness(Left, Top, Right, Bottom); + } + } + } +} diff --git a/src/Avalonia.Diagnostics/Diagnostics/Views/TreePageView.xaml b/src/Avalonia.Diagnostics/Diagnostics/Views/TreePageView.xaml index 98de9b611e..86137dfc57 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/Views/TreePageView.xaml +++ b/src/Avalonia.Diagnostics/Diagnostics/Views/TreePageView.xaml @@ -2,7 +2,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:Avalonia.Diagnostics.ViewModels" x:Class="Avalonia.Diagnostics.Views.TreePageView"> - +