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.Controls/ComboBox.cs b/src/Avalonia.Controls/ComboBox.cs index 20ca41bc57..c5af5ffa7a 100644 --- a/src/Avalonia.Controls/ComboBox.cs +++ b/src/Avalonia.Controls/ComboBox.cs @@ -10,6 +10,7 @@ using Avalonia.Interactivity; using Avalonia.Layout; using Avalonia.LogicalTree; using Avalonia.Media; +using Avalonia.Threading; using Avalonia.VisualTree; namespace Avalonia.Controls @@ -76,6 +77,14 @@ namespace Avalonia.Controls public static readonly StyledProperty VerticalContentAlignmentProperty = ContentControl.VerticalContentAlignmentProperty.AddOwner(); + /// + /// Defines the property. + /// + public static readonly StyledProperty IsTextSearchEnabledProperty = + AvaloniaProperty.Register(nameof(IsTextSearchEnabled), true); + + private string _textSearchTerm = string.Empty; + private DispatcherTimer _textSearchTimer; private bool _isDropDownOpen; private Popup _popup; private object _selectionBoxItem; @@ -164,6 +173,15 @@ namespace Avalonia.Controls set { SetValue(VerticalContentAlignmentProperty, value); } } + /// + /// Gets or sets a value that specifies whether a user can jump to a value by typing. + /// + public bool IsTextSearchEnabled + { + get { return GetValue(IsTextSearchEnabledProperty); } + set { SetValue(IsTextSearchEnabledProperty, value); } + } + /// protected override IItemContainerGenerator CreateItemContainerGenerator() { @@ -229,6 +247,32 @@ namespace Avalonia.Controls } } + /// + protected override void OnTextInput(TextInputEventArgs e) + { + if (!IsTextSearchEnabled || e.Handled) + return; + + StopTextSearchTimer(); + + _textSearchTerm += e.Text; + + bool match(ItemContainerInfo info) => + info.ContainerControl is IContentControl control && + control.Content?.ToString()?.StartsWith(_textSearchTerm, StringComparison.OrdinalIgnoreCase) == true; + + var info = ItemContainerGenerator.Containers.FirstOrDefault(match); + + if (info != null) + { + SelectedIndex = info.Index; + } + + StartTextSearchTimer(); + + e.Handled = true; + } + /// protected override void OnPointerWheelChanged(PointerWheelEventArgs e) { @@ -426,5 +470,31 @@ namespace Avalonia.Controls SelectedIndex = prev; } + + private void StartTextSearchTimer() + { + _textSearchTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) }; + _textSearchTimer.Tick += TextSearchTimer_Tick; + _textSearchTimer.Start(); + } + + private void StopTextSearchTimer() + { + if (_textSearchTimer == null) + { + return; + } + + _textSearchTimer.Stop(); + _textSearchTimer.Tick -= TextSearchTimer_Tick; + + _textSearchTimer = null; + } + + private void TextSearchTimer_Tick(object sender, EventArgs e) + { + _textSearchTerm = string.Empty; + StopTextSearchTimer(); + } } } diff --git a/src/Avalonia.Controls/Control.cs b/src/Avalonia.Controls/Control.cs index 41370d8464..620f5afa81 100644 --- a/src/Avalonia.Controls/Control.cs +++ b/src/Avalonia.Controls/Control.cs @@ -8,6 +8,8 @@ using Avalonia.Rendering; using Avalonia.Styling; using Avalonia.VisualTree; +#nullable enable + namespace Avalonia.Controls { /// @@ -23,20 +25,20 @@ namespace Avalonia.Controls /// /// Defines the property. /// - public static readonly StyledProperty> FocusAdornerProperty = - AvaloniaProperty.Register>(nameof(FocusAdorner)); + public static readonly StyledProperty?> FocusAdornerProperty = + AvaloniaProperty.Register?>(nameof(FocusAdorner)); /// /// Defines the property. /// - public static readonly StyledProperty TagProperty = - AvaloniaProperty.Register(nameof(Tag)); + public static readonly StyledProperty TagProperty = + AvaloniaProperty.Register(nameof(Tag)); /// /// Defines the property. /// - public static readonly StyledProperty ContextMenuProperty = - AvaloniaProperty.Register(nameof(ContextMenu)); + public static readonly StyledProperty ContextMenuProperty = + AvaloniaProperty.Register(nameof(ContextMenu)); /// /// Event raised when an element wishes to be scrolled into view. @@ -44,16 +46,16 @@ namespace Avalonia.Controls public static readonly RoutedEvent RequestBringIntoViewEvent = RoutedEvent.Register("RequestBringIntoView", RoutingStrategies.Bubble); - private DataTemplates _dataTemplates; - private IControl _focusAdorner; + private DataTemplates? _dataTemplates; + private IControl? _focusAdorner; /// /// Gets or sets the control's focus adorner. /// - public ITemplate FocusAdorner + public ITemplate? FocusAdorner { - get { return GetValue(FocusAdornerProperty); } - set { SetValue(FocusAdornerProperty, value); } + get => GetValue(FocusAdornerProperty); + set => SetValue(FocusAdornerProperty, value); } /// @@ -63,27 +65,27 @@ namespace Avalonia.Controls /// Each control may define data templates which are applied to the control itself and its /// children. /// - public DataTemplates DataTemplates => _dataTemplates ?? (_dataTemplates = new DataTemplates()); + public DataTemplates DataTemplates => _dataTemplates ??= new DataTemplates(); /// /// Gets or sets a context menu to the control. /// - public ContextMenu ContextMenu + public ContextMenu? ContextMenu { - get { return GetValue(ContextMenuProperty); } - set { SetValue(ContextMenuProperty, value); } + get => GetValue(ContextMenuProperty); + set => SetValue(ContextMenuProperty, value); } /// /// Gets or sets a user-defined object attached to the control. /// - public object Tag + public object? Tag { - get { return GetValue(TagProperty); } - set { SetValue(TagProperty, value); } + get => GetValue(TagProperty); + set => SetValue(TagProperty, value); } - public new IControl Parent => (IControl)base.Parent; + public new IControl? Parent => (IControl?)base.Parent; /// bool IDataTemplateHost.IsDataTemplatesInitialized => _dataTemplates != null; @@ -106,15 +108,10 @@ namespace Avalonia.Controls { var c = i as IControl; - if (c?.IsInitialized == false) + if (c?.IsInitialized == false && c is ISupportInitialize init) { - var init = c as ISupportInitialize; - - if (init != null) - { - init.BeginInit(); - init.EndInit(); - } + init.BeginInit(); + init.EndInit(); } } } @@ -131,10 +128,7 @@ namespace Avalonia.Controls /// Gets the element that receives the focus adorner. /// /// The control that receives the focus adorner. - protected virtual IControl GetTemplateFocusTarget() - { - return this; - } + protected virtual IControl? GetTemplateFocusTarget() => this; /// protected sealed override void OnAttachedToVisualTreeCore(VisualTreeAttachmentEventArgs e) @@ -173,15 +167,10 @@ namespace Avalonia.Controls } } - if (_focusAdorner != null) + if (_focusAdorner != null && GetTemplateFocusTarget() is Visual target) { - var target = (Visual)GetTemplateFocusTarget(); - - if (target != null) - { - AdornerLayer.SetAdornedElement((Visual)_focusAdorner, target); - adornerLayer.Children.Add(_focusAdorner); - } + AdornerLayer.SetAdornedElement((Visual)_focusAdorner, target); + adornerLayer.Children.Add(_focusAdorner); } } } diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index ab99cfad17..98f4cadc13 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -4,10 +4,7 @@ using System.ComponentModel; using System.Linq; using System.Reactive.Linq; using System.Threading.Tasks; -using Avalonia.Controls.Chrome; using Avalonia.Controls.Platform; -using Avalonia.Controls.Primitives; -using Avalonia.Data; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Layout; @@ -482,10 +479,9 @@ namespace Avalonia.Controls try { - if (!ignoreCancel && HandleClosing()) + if (!ignoreCancel && ShouldCancelClose()) { close = false; - return; } } finally @@ -497,11 +493,25 @@ namespace Avalonia.Controls } } + /// + /// Handles a closing notification from . + /// true if closing is cancelled. Otherwise false. + /// + protected virtual bool HandleClosing() + { + if (!ShouldCancelClose()) + { + CloseInternal(); + return false; + } + + return true; + } + private void CloseInternal() { foreach (var (child, _) in _children.ToList()) { - // if we HandleClosing() before then there will be no children. child.CloseInternal(); } @@ -515,20 +525,18 @@ namespace Avalonia.Controls PlatformImpl?.Dispose(); } - /// - /// Handles a closing notification from . - /// - protected virtual bool HandleClosing() + private bool ShouldCancelClose(CancelEventArgs args = null) { + if (args is null) + { + args = new CancelEventArgs(); + } + bool canClose = true; foreach (var (child, _) in _children.ToList()) { - if (!child.HandleClosing()) - { - child.CloseInternal(); - } - else + if (child.ShouldCancelClose(args)) { canClose = false; } @@ -536,15 +544,12 @@ namespace Avalonia.Controls if (canClose) { - var args = new CancelEventArgs(); OnClosing(args); return args.Cancel; } - else - { - return !canClose; - } + + return true; } protected virtual void HandleWindowStateChanged(WindowState state) 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.Layout/ElementManager.cs b/src/Avalonia.Layout/ElementManager.cs index bf5a45966b..cb13deb15f 100644 --- a/src/Avalonia.Layout/ElementManager.cs +++ b/src/Avalonia.Layout/ElementManager.cs @@ -207,7 +207,7 @@ namespace Avalonia.Layout } } - public bool IsIndexValidInData(int currentIndex) => currentIndex >= 0 && currentIndex < _context.ItemCount; + public bool IsIndexValidInData(int currentIndex) => (uint)currentIndex < _context.ItemCount; public ILayoutable GetRealizedElement(int dataIndex) { diff --git a/src/Avalonia.Layout/UniformGridLayoutState.cs b/src/Avalonia.Layout/UniformGridLayoutState.cs index 62c5174775..282bbab1a8 100644 --- a/src/Avalonia.Layout/UniformGridLayoutState.cs +++ b/src/Avalonia.Layout/UniformGridLayoutState.cs @@ -44,7 +44,7 @@ namespace Avalonia.Layout Size availableSize, VirtualizingLayoutContext context, double layoutItemWidth, - double LayoutItemHeight, + double layoutItemHeight, UniformGridLayoutItemsStretch stretch, Orientation orientation, double minRowSpacing, @@ -63,7 +63,7 @@ namespace Avalonia.Layout if (realizedElement != null) { realizedElement.Measure(availableSize); - SetSize(realizedElement, layoutItemWidth, LayoutItemHeight, availableSize, stretch, orientation, minRowSpacing, minColumnSpacing, maxItemsPerLine); + SetSize(realizedElement, layoutItemWidth, layoutItemHeight, availableSize, stretch, orientation, minRowSpacing, minColumnSpacing, maxItemsPerLine); _cachedFirstElement = null; } else @@ -78,7 +78,7 @@ namespace Avalonia.Layout _cachedFirstElement.Measure(availableSize); - SetSize(_cachedFirstElement, layoutItemWidth, LayoutItemHeight, availableSize, stretch, orientation, minRowSpacing, minColumnSpacing, maxItemsPerLine); + SetSize(_cachedFirstElement, layoutItemWidth, layoutItemHeight, availableSize, stretch, orientation, minRowSpacing, minColumnSpacing, maxItemsPerLine); // See if we can move ownership to the flow algorithm. If we can, we do not need a local cache. bool added = FlowAlgorithm.TryAddElement0(_cachedFirstElement); @@ -93,7 +93,7 @@ namespace Avalonia.Layout private void SetSize( ILayoutable element, double layoutItemWidth, - double LayoutItemHeight, + double layoutItemHeight, Size availableSize, UniformGridLayoutItemsStretch stretch, Orientation orientation, @@ -107,7 +107,7 @@ namespace Avalonia.Layout } EffectiveItemWidth = (double.IsNaN(layoutItemWidth) ? element.DesiredSize.Width : layoutItemWidth); - EffectiveItemHeight = (double.IsNaN(LayoutItemHeight) ? element.DesiredSize.Height : LayoutItemHeight); + EffectiveItemHeight = (double.IsNaN(layoutItemHeight) ? element.DesiredSize.Height : layoutItemHeight); var availableSizeMinor = orientation == Orientation.Horizontal ? availableSize.Width : availableSize.Height; var minorItemSpacing = orientation == Orientation.Vertical ? minRowSpacing : minColumnSpacing; 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) { diff --git a/src/Avalonia.Visuals/Visual.cs b/src/Avalonia.Visuals/Visual.cs index 9327531b46..80d4195421 100644 --- a/src/Avalonia.Visuals/Visual.cs +++ b/src/Avalonia.Visuals/Visual.cs @@ -150,7 +150,7 @@ namespace Avalonia public TransformedBounds? TransformedBounds => _transformedBounds; /// - /// Gets a value indicating whether the control should be clipped to its bounds. + /// Gets or sets a value indicating whether the control should be clipped to its bounds. /// public bool ClipToBounds { @@ -191,7 +191,7 @@ namespace Avalonia } /// - /// Gets a value indicating whether this control is visible. + /// Gets or sets a value indicating whether this control is visible. /// public bool IsVisible { @@ -200,7 +200,7 @@ namespace Avalonia } /// - /// Gets the opacity of the control. + /// Gets or sets the opacity of the control. /// public double Opacity { @@ -209,7 +209,7 @@ namespace Avalonia } /// - /// Gets the opacity mask of the control. + /// Gets or sets the opacity mask of the control. /// public IBrush OpacityMask { @@ -218,7 +218,7 @@ namespace Avalonia } /// - /// Gets the render transform of the control. + /// Gets or sets the render transform of the control. /// public ITransform RenderTransform { @@ -227,7 +227,7 @@ namespace Avalonia } /// - /// Gets the transform origin of the control. + /// Gets or sets the transform origin of the control. /// public RelativePoint RenderTransformOrigin { @@ -236,7 +236,7 @@ namespace Avalonia } /// - /// Gets the Z index of the control. + /// Gets or sets the Z index of the control. /// /// /// Controls with a higher will appear in front of controls with diff --git a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs index c8a30a42e9..4ea838358c 100644 --- a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs @@ -1,7 +1,9 @@ +using System.Linq; using Avalonia.Controls.Presenters; using Avalonia.Controls.Primitives; using Avalonia.Controls.Shapes; using Avalonia.Controls.Templates; +using Avalonia.Input; using Avalonia.LogicalTree; using Avalonia.Media; using Avalonia.UnitTests; @@ -137,5 +139,39 @@ namespace Avalonia.Controls.UnitTests Assert.True(other.IsFocused); } } + + [Theory] + [InlineData(-1, 2, "c", "A item", "B item", "C item")] + [InlineData(0, 1, "b", "A item", "B item", "C item")] + [InlineData(2, 2, "x", "A item", "B item", "C item")] + public void TextSearch_Should_Have_Expected_SelectedIndex( + int initialSelectedIndex, + int expectedSelectedIndex, + string searchTerm, + params string[] items) + { + using (UnitTestApplication.Start(TestServices.MockThreadingInterface)) + { + var target = new ComboBox + { + Template = GetTemplate(), + Items = items.Select(x => new ComboBoxItem { Content = x }) + }; + + target.ApplyTemplate(); + target.Presenter.ApplyTemplate(); + target.SelectedIndex = initialSelectedIndex; + + var args = new TextInputEventArgs + { + Text = searchTerm, + RoutedEvent = InputElement.TextInputEvent + }; + + target.RaiseEvent(args); + + Assert.Equal(expectedSelectedIndex, target.SelectedIndex); + } + } } } diff --git a/tests/Avalonia.Controls.UnitTests/WindowTests.cs b/tests/Avalonia.Controls.UnitTests/WindowTests.cs index ba29001cf3..e8311b79ac 100644 --- a/tests/Avalonia.Controls.UnitTests/WindowTests.cs +++ b/tests/Avalonia.Controls.UnitTests/WindowTests.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; -using Avalonia.Layout; using Avalonia.Platform; using Avalonia.Rendering; using Avalonia.UnitTests; @@ -137,6 +136,129 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(1, count); } } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void Child_windows_should_be_closed_before_parent(bool programaticClose) + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var window = new Window(); + var child = new Window(); + + int count = 0; + int windowClosing = 0; + int childClosing = 0; + int windowClosed = 0; + int childClosed = 0; + + window.Closing += (sender, e) => + { + count++; + windowClosing = count; + }; + + child.Closing += (sender, e) => + { + count++; + childClosing = count; + }; + + window.Closed += (sender, e) => + { + count++; + windowClosed = count; + }; + + child.Closed += (sender, e) => + { + count++; + childClosed = count; + }; + + window.Show(); + child.Show(window); + + if (programaticClose) + { + window.Close(); + } + else + { + var cancel = window.PlatformImpl.Closing(); + + Assert.Equal(false, cancel); + } + + Assert.Equal(2, windowClosing); + Assert.Equal(1, childClosing); + Assert.Equal(4, windowClosed); + Assert.Equal(3, childClosed); + } + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void Child_windows_must_not_close_before_parent_has_chance_to_Cancel_OSCloseButton(bool programaticClose) + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var window = new Window(); + var child = new Window(); + + int count = 0; + int windowClosing = 0; + int childClosing = 0; + int windowClosed = 0; + int childClosed = 0; + + window.Closing += (sender, e) => + { + count++; + windowClosing = count; + e.Cancel = true; + }; + + child.Closing += (sender, e) => + { + count++; + childClosing = count; + }; + + window.Closed += (sender, e) => + { + count++; + windowClosed = count; + }; + + child.Closed += (sender, e) => + { + count++; + childClosed = count; + }; + + window.Show(); + child.Show(window); + + if (programaticClose) + { + window.Close(); + } + else + { + var cancel = window.PlatformImpl.Closing(); + + Assert.Equal(true, cancel); + } + + Assert.Equal(2, windowClosing); + Assert.Equal(1, childClosing); + Assert.Equal(0, windowClosed); + Assert.Equal(0, childClosed); + } + } [Fact] public void Showing_Should_Start_Renderer() diff --git a/tests/Avalonia.Markup.UnitTests/Parsers/ExpressionObserverBuilderTests_AttachedProperty.cs b/tests/Avalonia.Markup.UnitTests/Parsers/ExpressionObserverBuilderTests_AttachedProperty.cs index 7c48a975ef..e5e63b24d0 100644 --- a/tests/Avalonia.Markup.UnitTests/Parsers/ExpressionObserverBuilderTests_AttachedProperty.cs +++ b/tests/Avalonia.Markup.UnitTests/Parsers/ExpressionObserverBuilderTests_AttachedProperty.cs @@ -117,9 +117,11 @@ namespace Avalonia.Markup.UnitTests.Parsers var result = run(); result.Item1.Subscribe(x => { }); - GC.Collect(); + // Mono trickery + GC.Collect(2); GC.WaitForPendingFinalizers(); - GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(2); Assert.Null(result.Item2.Target); }