diff --git a/src/Avalonia.Base/Data/Converters/BoolConverters.cs b/src/Avalonia.Base/Data/Converters/BoolConverters.cs index 6740c2168f..9329cdd6af 100644 --- a/src/Avalonia.Base/Data/Converters/BoolConverters.cs +++ b/src/Avalonia.Base/Data/Converters/BoolConverters.cs @@ -12,5 +12,11 @@ namespace Avalonia.Data.Converters /// public static readonly IMultiValueConverter And = new FuncMultiValueConverter(x => x.All(y => y)); + + /// + /// A multi-value converter that returns true if any of the inputs is true. + /// + public static readonly IMultiValueConverter Or = + new FuncMultiValueConverter(x => x.Any(y => y)); } } diff --git a/src/Avalonia.Diagnostics/Diagnostics/Converters/BoolToOpacityConverter.cs b/src/Avalonia.Diagnostics/Diagnostics/Converters/BoolToOpacityConverter.cs new file mode 100644 index 0000000000..63ac3ab62f --- /dev/null +++ b/src/Avalonia.Diagnostics/Diagnostics/Converters/BoolToOpacityConverter.cs @@ -0,0 +1,21 @@ +using System; +using System.Globalization; +using Avalonia.Data.Converters; + +namespace Avalonia.Diagnostics.Converters +{ + internal class BoolToOpacityConverter : IValueConverter + { + public double Opacity { get; set; } + + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + return (bool)value ? 1d : Opacity; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ControlDetailsViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ControlDetailsViewModel.cs index fa41eacbeb..32592559e5 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ControlDetailsViewModel.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ControlDetailsViewModel.cs @@ -1,8 +1,15 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; using System.ComponentModel; using System.Linq; +using System.Reflection; using Avalonia.Collections; +using Avalonia.Controls; +using Avalonia.Controls.Metadata; +using Avalonia.Markup.Xaml.MarkupExtensions; +using Avalonia.Styling; using Avalonia.VisualTree; namespace Avalonia.Diagnostics.ViewModels @@ -12,6 +19,10 @@ namespace Avalonia.Diagnostics.ViewModels private readonly IVisual _control; private readonly IDictionary> _propertyIndex; private AvaloniaPropertyViewModel _selectedProperty; + private string _styleFilter; + private bool _snapshotStyles; + private bool _showInactiveStyles; + private string _styleStatus; public ControlDetailsViewModel(TreePageViewModel treePage, IVisual control) { @@ -43,20 +54,160 @@ namespace Avalonia.Diagnostics.ViewModels { ao.PropertyChanged += ControlPropertyChanged; } + + AppliedStyles = new ObservableCollection(); + PseudoClasses = new ObservableCollection(); + + if (control is StyledElement styledElement) + { + styledElement.Classes.CollectionChanged += OnClassesChanged; + + var pseudoClassAttributes = styledElement.GetType().GetCustomAttributes(true); + + foreach (var classAttribute in pseudoClassAttributes) + { + foreach (var className in classAttribute.PseudoClasses) + { + PseudoClasses.Add(new PseudoClassViewModel(className, styledElement)); + } + } + + var styleDiagnostics = styledElement.GetStyleDiagnostics(); + + foreach (var appliedStyle in styleDiagnostics.AppliedStyles) + { + var styleSource = appliedStyle.Source; + + var setters = new List(); + + if (styleSource is Style style) + { + foreach (var setter in style.Setters) + { + if (setter is Setter regularSetter) + { + var setterValue = regularSetter.Value; + + var resourceInfo = GetResourceInfo(setterValue); + + SetterViewModel setterVm; + + if (resourceInfo.HasValue) + { + var resourceKey = resourceInfo.Value.resourceKey; + var resourceValue = styledElement.FindResource(resourceKey); + + setterVm = new ResourceSetterViewModel(regularSetter.Property, resourceKey, resourceValue, resourceInfo.Value.isDynamic); + } + else + { + setterVm = new SetterViewModel(regularSetter.Property, setterValue); + } + + setters.Add(setterVm); + } + } + + AppliedStyles.Add(new StyleViewModel(appliedStyle, style.Selector?.ToString() ?? "No selector", setters)); + } + } + + UpdateStyles(); + } + } + + private (object resourceKey, bool isDynamic)? GetResourceInfo(object value) + { + if (value is StaticResourceExtension staticResource) + { + return (staticResource.ResourceKey, false); + } + else if (value is DynamicResourceExtension dynamicResource) + { + return (dynamicResource.ResourceKey, true); + } + + return null; } public TreePageViewModel TreePage { get; } public DataGridCollectionView PropertiesView { get; } + public ObservableCollection AppliedStyles { get; } + + public ObservableCollection PseudoClasses { get; } + public AvaloniaPropertyViewModel SelectedProperty { get => _selectedProperty; set => RaiseAndSetIfChanged(ref _selectedProperty, value); } - + + public string StyleFilter + { + get => _styleFilter; + set => RaiseAndSetIfChanged(ref _styleFilter, value); + } + + public bool SnapshotStyles + { + get => _snapshotStyles; + set => RaiseAndSetIfChanged(ref _snapshotStyles, value); + } + + public bool ShowInactiveStyles + { + get => _showInactiveStyles; + set => RaiseAndSetIfChanged(ref _showInactiveStyles, value); + } + + public string StyleStatus + { + get => _styleStatus; + set => RaiseAndSetIfChanged(ref _styleStatus, value); + } + public ControlLayoutViewModel Layout { get; } + protected override void OnPropertyChanged(PropertyChangedEventArgs e) + { + base.OnPropertyChanged(e); + + if (e.PropertyName == nameof(StyleFilter)) + { + UpdateStyleFilters(); + } + else if (e.PropertyName == nameof(SnapshotStyles)) + { + if (!SnapshotStyles) + { + UpdateStyles(); + } + } + } + + private void UpdateStyleFilters() + { + var filter = StyleFilter; + bool hasFilter = !string.IsNullOrEmpty(filter); + + foreach (var style in AppliedStyles) + { + var hasVisibleSetter = false; + + foreach (var setter in style.Setters) + { + setter.IsVisible = + !hasFilter || setter.Name.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0; + + hasVisibleSetter |= setter.IsVisible; + } + + style.IsVisible = hasVisibleSetter; + } + } + public void Dispose() { if (_control is INotifyPropertyChanged inpc) @@ -68,6 +219,11 @@ namespace Avalonia.Diagnostics.ViewModels { ao.PropertyChanged -= ControlPropertyChanged; } + + if (_control is StyledElement se) + { + se.Classes.CollectionChanged -= OnClassesChanged; + } } private IEnumerable GetAvaloniaProperties(object o) @@ -129,6 +285,74 @@ namespace Avalonia.Diagnostics.ViewModels property.Update(); } } + + if (!SnapshotStyles) + { + UpdateStyles(); + } + } + + private void OnClassesChanged(object sender, NotifyCollectionChangedEventArgs e) + { + if (!SnapshotStyles) + { + UpdateStyles(); + } + } + + private void UpdateStyles() + { + int activeCount = 0; + + foreach (var style in AppliedStyles) + { + style.Update(); + + if (style.IsActive) + { + activeCount++; + } + } + + var propertyBuckets = new Dictionary>(); + + foreach (var style in AppliedStyles) + { + if (!style.IsActive) + { + continue; + } + + foreach (var setter in style.Setters) + { + if (propertyBuckets.TryGetValue(setter.Property, out var setters)) + { + foreach (var otherSetter in setters) + { + otherSetter.IsActive = false; + } + + setter.IsActive = true; + + setters.Add(setter); + } + else + { + setter.IsActive = true; + + setters = new List { setter }; + + propertyBuckets.Add(setter.Property, setters); + } + } + } + + foreach (var pseudoClass in PseudoClasses) + { + pseudoClass.Update(); + } + + StyleStatus = $"Styles ({activeCount}/{AppliedStyles.Count} active)"; } private bool FilterProperty(object arg) diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs index bf7d0e232a..3049431361 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs @@ -163,6 +163,14 @@ namespace Avalonia.Diagnostics.ViewModels tree?.SelectControl(control); } + public void EnableSnapshotStyles(bool enable) + { + if (Content is TreePageViewModel treeVm && treeVm.Details != null) + { + treeVm.Details.SnapshotStyles = enable; + } + } + public void Dispose() { KeyboardDevice.Instance.PropertyChanged -= KeyboardPropertyChanged; diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/PseudoClassViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/PseudoClassViewModel.cs new file mode 100644 index 0000000000..69126c2e2f --- /dev/null +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/PseudoClassViewModel.cs @@ -0,0 +1,51 @@ +using Avalonia.Controls; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal class PseudoClassViewModel : ViewModelBase + { + private readonly IPseudoClasses _pseudoClasses; + private readonly StyledElement _source; + private bool _isActive; + private bool _isUpdating; + + public PseudoClassViewModel(string name, StyledElement source) + { + Name = name; + _source = source; + _pseudoClasses = _source.Classes; + + Update(); + } + + public string Name { get; } + + public bool IsActive + { + get => _isActive; + set + { + RaiseAndSetIfChanged(ref _isActive, value); + + if (!_isUpdating) + { + _pseudoClasses.Set(Name, value); + } + } + } + + public void Update() + { + try + { + _isUpdating = true; + + IsActive = _source.Classes.Contains(Name); + } + finally + { + _isUpdating = false; + } + } + } +} diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ResourceSetterViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ResourceSetterViewModel.cs new file mode 100644 index 0000000000..a82e13fcfa --- /dev/null +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ResourceSetterViewModel.cs @@ -0,0 +1,27 @@ +using Avalonia.Media; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal class ResourceSetterViewModel : SetterViewModel + { + public object Key { get; } + + public IBrush Tint { get; } + + public ResourceSetterViewModel(AvaloniaProperty property, object resourceKey, object resourceValue, bool isDynamic) : base(property, resourceValue) + { + Key = resourceKey; + Tint = isDynamic ? Brushes.Orange : Brushes.Brown; + } + + public void CopyResourceKey() + { + if (Key is null) + { + return; + } + + CopyToClipboard(Key.ToString()); + } + } +} diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/SetterViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/SetterViewModel.cs new file mode 100644 index 0000000000..e835f5a878 --- /dev/null +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/SetterViewModel.cs @@ -0,0 +1,59 @@ +using Avalonia.Input.Platform; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal class SetterViewModel : ViewModelBase + { + private bool _isActive; + private bool _isVisible; + + public AvaloniaProperty Property { get; } + + public string Name { get; } + + public object Value { get; } + + public bool IsActive + { + get => _isActive; + set => RaiseAndSetIfChanged(ref _isActive, value); + } + + public bool IsVisible + { + get => _isVisible; + set => RaiseAndSetIfChanged(ref _isVisible, value); + } + + public SetterViewModel(AvaloniaProperty property, object value) + { + Property = property; + Name = property.Name; + Value = value; + IsActive = true; + IsVisible = true; + } + + public void CopyValue() + { + if (Value is null) + { + return; + } + + CopyToClipboard(Value.ToString()); + } + + public void CopyPropertyName() + { + CopyToClipboard(Property.Name); + } + + protected static void CopyToClipboard(string value) + { + var clipboard = AvaloniaLocator.Current.GetService(); + + clipboard?.SetTextAsync(value); + } + } +} diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/StyleViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/StyleViewModel.cs new file mode 100644 index 0000000000..06e2409800 --- /dev/null +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/StyleViewModel.cs @@ -0,0 +1,43 @@ +using System.Collections.Generic; +using Avalonia.Styling; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal class StyleViewModel : ViewModelBase + { + private readonly IStyleInstance _styleInstance; + private bool _isActive; + private bool _isVisible; + + public StyleViewModel(IStyleInstance styleInstance, string name, List setters) + { + _styleInstance = styleInstance; + IsVisible = true; + Name = name; + Setters = setters; + + Update(); + } + + public bool IsActive + { + get => _isActive; + set => RaiseAndSetIfChanged(ref _isActive, value); + } + + public bool IsVisible + { + get => _isVisible; + set => RaiseAndSetIfChanged(ref _isVisible, value); + } + + public string Name { get; } + + public List Setters { get; } + + public void Update() + { + IsActive = _styleInstance.IsActive; + } + } +} diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreePageViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreePageViewModel.cs index bd65a3b06b..6b779cd6ac 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreePageViewModel.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreePageViewModel.cs @@ -31,11 +31,18 @@ namespace Avalonia.Diagnostics.ViewModels get => _selectedNode; private set { + var oldDetails = Details; + if (RaiseAndSetIfChanged(ref _selectedNode, value)) { Details = value != null ? new ControlDetailsViewModel(this, value.Visual) : null; + + if (Details != null && oldDetails != null) + { + Details.StyleFilter = oldDetails.StyleFilter; + } } } } diff --git a/src/Avalonia.Diagnostics/Diagnostics/Views/ControlDetailsView.xaml b/src/Avalonia.Diagnostics/Diagnostics/Views/ControlDetailsView.xaml index 2e0b6813ba..9ba576c826 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/Views/ControlDetailsView.xaml +++ b/src/Avalonia.Diagnostics/Diagnostics/Views/ControlDetailsView.xaml @@ -2,7 +2,9 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:conv="clr-namespace:Avalonia.Diagnostics.Converters" xmlns:local="clr-namespace:Avalonia.Diagnostics.Views" - x:Class="Avalonia.Diagnostics.Views.ControlDetailsView"> + xmlns:vm="clr-namespace:Avalonia.Diagnostics.ViewModels" + x:Class="Avalonia.Diagnostics.Views.ControlDetailsView" + x:Name="Main"> @@ -11,6 +13,7 @@ + @@ -105,7 +108,7 @@ - + @@ -148,7 +151,122 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ( + + + ) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Avalonia.Diagnostics/Diagnostics/Views/MainWindow.xaml.cs b/src/Avalonia.Diagnostics/Diagnostics/Views/MainWindow.xaml.cs index c4f9185728..330121321a 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/Views/MainWindow.xaml.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/Views/MainWindow.xaml.cs @@ -90,6 +90,16 @@ namespace Avalonia.Diagnostics.Views var vm = (MainViewModel)DataContext; vm.SelectControl((IControl)control); } + } + else if (e.Modifiers == RawInputModifiers.Alt) + { + if (e.Key == Key.S || e.Key == Key.D) + { + var enable = e.Key == Key.S; + + var vm = (MainViewModel)DataContext; + vm.EnableSnapshotStyles(enable); + } } } diff --git a/src/Avalonia.Styling/ApiCompatBaseline.txt b/src/Avalonia.Styling/ApiCompatBaseline.txt new file mode 100644 index 0000000000..0eedc3e360 --- /dev/null +++ b/src/Avalonia.Styling/ApiCompatBaseline.txt @@ -0,0 +1,4 @@ +Compat issues with assembly Avalonia.Styling: +InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Styling.IStyleInstance.IsActive' is present in the implementation but not in the contract. +InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Styling.IStyleInstance.IsActive.get()' is present in the implementation but not in the contract. +Total Issues: 2 diff --git a/src/Avalonia.Styling/Diagnostics/StyleDiagnostics.cs b/src/Avalonia.Styling/Diagnostics/StyleDiagnostics.cs new file mode 100644 index 0000000000..984b145e68 --- /dev/null +++ b/src/Avalonia.Styling/Diagnostics/StyleDiagnostics.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; +using Avalonia.Styling; + +namespace Avalonia.Diagnostics +{ + /// + /// Contains information about style related diagnostics of a control. + /// + public class StyleDiagnostics + { + /// + /// Currently applied styles. + /// + public IReadOnlyList AppliedStyles { get; } + + public StyleDiagnostics(IReadOnlyList appliedStyles) + { + AppliedStyles = appliedStyles; + } + } +} diff --git a/src/Avalonia.Styling/Diagnostics/StyledElementExtensions.cs b/src/Avalonia.Styling/Diagnostics/StyledElementExtensions.cs new file mode 100644 index 0000000000..d7bcc1aa47 --- /dev/null +++ b/src/Avalonia.Styling/Diagnostics/StyledElementExtensions.cs @@ -0,0 +1,17 @@ +namespace Avalonia.Diagnostics +{ + /// + /// Defines diagnostic extensions on s. + /// + public static class StyledElementExtensions + { + /// + /// Gets a style diagnostics for a . + /// + /// The element. + public static StyleDiagnostics GetStyleDiagnostics(this StyledElement styledElement) + { + return styledElement.GetStyleDiagnosticsInternal(); + } + } +} diff --git a/src/Avalonia.Styling/StyledElement.cs b/src/Avalonia.Styling/StyledElement.cs index cc8d91462d..fad281244f 100644 --- a/src/Avalonia.Styling/StyledElement.cs +++ b/src/Avalonia.Styling/StyledElement.cs @@ -356,6 +356,18 @@ namespace Avalonia } } + internal StyleDiagnostics GetStyleDiagnosticsInternal() + { + IReadOnlyList? appliedStyles = _appliedStyles; + + if (appliedStyles is null) + { + appliedStyles = Array.Empty(); + } + + return new StyleDiagnostics(appliedStyles); + } + /// void ILogical.NotifyAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e) { diff --git a/src/Avalonia.Styling/Styling/IStyleInstance.cs b/src/Avalonia.Styling/Styling/IStyleInstance.cs index cb094badd2..8ddb989bc0 100644 --- a/src/Avalonia.Styling/Styling/IStyleInstance.cs +++ b/src/Avalonia.Styling/Styling/IStyleInstance.cs @@ -14,6 +14,11 @@ namespace Avalonia.Styling /// IStyle Source { get; } + /// + /// Gets a value indicating whether this style is active. + /// + bool IsActive { get; } + /// /// Instructs the style to start acting upon the control. /// diff --git a/src/Avalonia.Styling/Styling/StyleInstance.cs b/src/Avalonia.Styling/Styling/StyleInstance.cs index 8ca31d654f..830cf49a0d 100644 --- a/src/Avalonia.Styling/Styling/StyleInstance.cs +++ b/src/Avalonia.Styling/Styling/StyleInstance.cs @@ -17,7 +17,6 @@ namespace Avalonia.Styling private readonly List? _animations; private readonly IStyleActivator? _activator; private readonly Subject? _animationTrigger; - private bool _active; public StyleInstance( IStyle source, @@ -29,6 +28,7 @@ namespace Avalonia.Styling Source = source ?? throw new ArgumentNullException(nameof(source)); Target = target ?? throw new ArgumentNullException(nameof(target)); _activator = activator; + IsActive = _activator is null; if (setters is object) { @@ -56,6 +56,7 @@ namespace Avalonia.Styling } } + public bool IsActive { get; private set; } public IStyle Source { get; } public IStyleable Target { get; } @@ -104,15 +105,15 @@ namespace Avalonia.Styling private void ActivatorChanged(bool value) { - if (_active != value) + if (IsActive != value) { - _active = value; + IsActive = value; _animationTrigger?.OnNext(value); if (_setters is object) { - if (_active) + if (IsActive) { foreach (var setter in _setters) {