diff --git a/.ncrunch/Avalonia.Build.Tasks.v3.ncrunchproject b/.ncrunch/Avalonia.Build.Tasks.v3.ncrunchproject index 319cd523ce..95a483b433 100644 --- a/.ncrunch/Avalonia.Build.Tasks.v3.ncrunchproject +++ b/.ncrunch/Avalonia.Build.Tasks.v3.ncrunchproject @@ -1,5 +1,3 @@  - - True - + \ No newline at end of file diff --git a/.ncrunch/Avalonia.MicroCom.v3.ncrunchproject b/.ncrunch/Avalonia.MicroCom.v3.ncrunchproject new file mode 100644 index 0000000000..319cd523ce --- /dev/null +++ b/.ncrunch/Avalonia.MicroCom.v3.ncrunchproject @@ -0,0 +1,5 @@ + + + True + + \ No newline at end of file diff --git a/Avalonia.v3.ncrunchsolution b/Avalonia.v3.ncrunchsolution index afce1018ec..b97a8e54f5 100644 --- a/Avalonia.v3.ncrunchsolution +++ b/Avalonia.v3.ncrunchsolution @@ -4,6 +4,8 @@ tests\TestFiles\**.* src\Avalonia.Build.Tasks\bin\Debug\netstandard2.0\Avalonia.Build.Tasks.dll src\Avalonia.Build.Tasks\bin\Debug\netstandard2.0\Mono.Cecil.dll + src\Avalonia.Build.Tasks\bin\Debug\netstandard2.0\Mono.Cecil.Rocks.dll + src\Avalonia.Build.Tasks\bin\Debug\netstandard2.0\Mono.Cecil.Pdb.dll True diff --git a/Directory.Build.props b/Directory.Build.props index b41f8c488e..c6610695c4 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,5 +2,7 @@ $(MSBuildThisFileDirectory)build-intermediate/nuget $(MSBuildThisFileDirectory)\src\tools\Avalonia.Designer.HostApp\bin\$(Configuration)\netcoreapp2.0\Avalonia.Designer.HostApp.dll + + false diff --git a/Documentation/build.md b/Documentation/build.md index 8c2ef57b54..2f59146a48 100644 --- a/Documentation/build.md +++ b/Documentation/build.md @@ -30,7 +30,7 @@ The build process needs [Xcode](https://developer.apple.com/xcode/) to build the Linux operating systems ship with their own respective package managers however we will use [Homebrew](https://brew.sh/) to manage packages on macOS. To install follow the instructions [here](https://docs.brew.sh/Installation). -### Install CastXML +### Install CastXML (pre Nov 2020) Avalonia requires [CastXML](https://github.com/CastXML/CastXML) for XML processing during the build process. The easiest way to install this is via the operating system's package managers, such as below. @@ -61,6 +61,7 @@ git submodule update --init --recursive ### Build native libraries (macOS only) On macOS it is necessary to build and manually install the respective native libraries using [Xcode](https://developer.apple.com/xcode/). The steps to get this working correctly are: +- (for revisions after 2 Nov 2020) Run `./build.sh GenerateCppHeaders` to generate `avalonia-native.h` from `avn.idl` - Navigate to the Avalonia/native/Avalonia.Native/src/OSX folder and open the `Avalonia.Native.OSX.xcodeproj` project - Build the library via the Product->Build menu. This will generate binaries in your local path under ~/Library/Developer/Xcode/DerivedData/Avalonia.Native.OSX-*guid* where "guid" is uniquely generated every time you build. - Manually install the native library by copying it from the build artifacts folder into the shared dynamic library path: diff --git a/build/MicroCom.targets b/build/MicroCom.targets new file mode 100644 index 0000000000..3a07950616 --- /dev/null +++ b/build/MicroCom.targets @@ -0,0 +1,33 @@ + + + + + + false + all + true + + + + + + + + + + + + + + + + + + <_AvaloniaPatchComInterop>true + + + diff --git a/build/ReactiveUI.props b/build/ReactiveUI.props index d8e86e917e..f74ab07e31 100644 --- a/build/ReactiveUI.props +++ b/build/ReactiveUI.props @@ -1,5 +1,5 @@ - + diff --git a/nukebuild/Build.cs b/nukebuild/Build.cs index ecc01b4aab..97647a1c59 100644 --- a/nukebuild/Build.cs +++ b/nukebuild/Build.cs @@ -141,6 +141,7 @@ partial class Build : NukeBuild Target CompileNative => _ => _ .DependsOn(Clean) + .DependsOn(GenerateCppHeaders) .OnlyWhenStatic(() => EnvironmentInfo.IsOsx) .Executes(() => { diff --git a/src/Avalonia.Controls/ContextMenu.cs b/src/Avalonia.Controls/ContextMenu.cs index ec9649bc64..31071768c9 100644 --- a/src/Avalonia.Controls/ContextMenu.cs +++ b/src/Avalonia.Controls/ContextMenu.cs @@ -236,7 +236,7 @@ namespace Avalonia.Controls /// /// Opens the menu. /// - public override void Open() => Open(null); + public override void Open() => throw new NotSupportedException(); /// /// Opens a context menu on the specified control. diff --git a/src/Avalonia.Controls/Selection/SelectionModel.cs b/src/Avalonia.Controls/Selection/SelectionModel.cs index 054974e4f1..6ae53a4d59 100644 --- a/src/Avalonia.Controls/Selection/SelectionModel.cs +++ b/src/Avalonia.Controls/Selection/SelectionModel.cs @@ -443,6 +443,7 @@ namespace Avalonia.Controls.Selection } if ((e.Action == NotifyCollectionChangedAction.Remove && e.OldStartingIndex <= oldSelectedIndex) || + (e.Action == NotifyCollectionChangedAction.Replace && e.OldStartingIndex == oldSelectedIndex) || e.Action == NotifyCollectionChangedAction.Reset) { RaisePropertyChanged(nameof(SelectedItem)); diff --git a/src/Avalonia.Controls/TreeView.cs b/src/Avalonia.Controls/TreeView.cs index b2bd5ab2e5..09742412d9 100644 --- a/src/Avalonia.Controls/TreeView.cs +++ b/src/Avalonia.Controls/TreeView.cs @@ -378,10 +378,11 @@ namespace Avalonia.Controls { if (!this.IsVisualAncestorOf(element)) { - IControl result = _selectedItem != null ? + var result = _selectedItem != null ? ItemContainerGenerator.Index.ContainerFromItem(_selectedItem) : ItemContainerGenerator.ContainerFromIndex(0); - return (true, result); + + return (result != null, result); // SelectedItem may not be in the treeview. } return (true, null); 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"> - + value of this control and its parent controls. /// bool IsEffectivelyEnabled { get; } + + /// + /// Gets a value indicating whether keyboard focus is anywhere within the element or its visual tree child elements. + /// + bool IsKeyboardFocusWithin { get; } /// /// Gets a value indicating whether the control is focused. diff --git a/src/Avalonia.Input/InputElement.cs b/src/Avalonia.Input/InputElement.cs index 9ace7fd92d..66fb9cfb1c 100644 --- a/src/Avalonia.Input/InputElement.cs +++ b/src/Avalonia.Input/InputElement.cs @@ -42,6 +42,14 @@ namespace Avalonia.Input public static readonly StyledProperty CursorProperty = AvaloniaProperty.Register(nameof(Cursor), null, true); + /// + /// Defines the property. + /// + public static readonly DirectProperty IsKeyboardFocusWithinProperty = + AvaloniaProperty.RegisterDirect( + nameof(IsKeyboardFocusWithin), + o => o.IsKeyboardFocusWithin); + /// /// Defines the property. /// @@ -160,6 +168,7 @@ namespace Avalonia.Input private bool _isEffectivelyEnabled = true; private bool _isFocused; + private bool _isKeyboardFocusWithin; private bool _isFocusVisible; private bool _isPointerOver; private GestureRecognizerCollection? _gestureRecognizers; @@ -343,6 +352,15 @@ namespace Avalonia.Input get { return GetValue(CursorProperty); } set { SetValue(CursorProperty, value); } } + + /// + /// Gets a value indicating whether keyboard focus is anywhere within the element or its visual tree child elements. + /// + public bool IsKeyboardFocusWithin + { + get => _isKeyboardFocusWithin; + internal set => SetAndRaise(IsKeyboardFocusWithinProperty, ref _isKeyboardFocusWithin, value); + } /// /// Gets a value indicating whether the control is focused. @@ -544,6 +562,10 @@ namespace Avalonia.Input { UpdatePseudoClasses(null, change.NewValue.GetValueOrDefault()); } + else if (change.Property == IsKeyboardFocusWithinProperty) + { + PseudoClasses.Set(":focus-within", _isKeyboardFocusWithin); + } } /// diff --git a/src/Avalonia.Input/KeyboardDevice.cs b/src/Avalonia.Input/KeyboardDevice.cs index 187670a26b..6f4cb7a35c 100644 --- a/src/Avalonia.Input/KeyboardDevice.cs +++ b/src/Avalonia.Input/KeyboardDevice.cs @@ -9,6 +9,7 @@ namespace Avalonia.Input public class KeyboardDevice : IKeyboardDevice, INotifyPropertyChanged { private IInputElement? _focusedElement; + private IInputRoot? _focusedRoot; public event PropertyChangedEventHandler? PropertyChanged; @@ -28,10 +29,115 @@ namespace Avalonia.Input private set { _focusedElement = value; + + if (_focusedElement != null && _focusedElement.IsAttachedToVisualTree) + { + _focusedRoot = _focusedElement.VisualRoot as IInputRoot; + } + else + { + _focusedRoot = null; + } + RaisePropertyChanged(); } } + private void ClearFocusWithinAncestors(IInputElement? element) + { + var el = element; + + while (el != null) + { + if (el is InputElement ie) + { + ie.IsKeyboardFocusWithin = false; + } + + el = (IInputElement)el.VisualParent; + } + } + + private void ClearFocusWithin(IInputElement element, bool clearRoot) + { + foreach (var visual in element.VisualChildren) + { + if (visual is IInputElement el && el.IsKeyboardFocusWithin) + { + ClearFocusWithin(el, true); + break; + } + } + + if (clearRoot) + { + if (element is InputElement ie) + { + ie.IsKeyboardFocusWithin = false; + } + } + } + + private void SetIsFocusWithin(IInputElement? oldElement, IInputElement? newElement) + { + if (newElement == null && oldElement != null) + { + ClearFocusWithinAncestors(oldElement); + return; + } + + IInputElement? branch = null; + + var el = newElement; + + while (el != null) + { + if (el.IsKeyboardFocusWithin) + { + branch = el; + break; + } + + el = el.VisualParent as IInputElement; + } + + el = oldElement; + + if (el != null && branch != null) + { + ClearFocusWithin(branch, false); + } + + el = newElement; + + while (el != null && el != branch) + { + if (el is InputElement ie) + { + ie.IsKeyboardFocusWithin = true; + } + + el = el.VisualParent as IInputElement; + } + } + + private void ClearChildrenFocusWithin(IInputElement element, bool clearRoot) + { + foreach (var visual in element.VisualChildren) + { + if (visual is IInputElement el && el.IsKeyboardFocusWithin) + { + ClearChildrenFocusWithin(el, true); + break; + } + } + + if (clearRoot && element is InputElement ie) + { + ie.IsKeyboardFocusWithin = false; + } + } + public void SetFocusedElement( IInputElement? element, NavigationMethod method, @@ -40,6 +146,17 @@ namespace Avalonia.Input if (element != FocusedElement) { var interactive = FocusedElement as IInteractive; + + if (FocusedElement != null && + (!FocusedElement.IsAttachedToVisualTree || + _focusedRoot != element?.VisualRoot as IInputRoot) && + _focusedRoot != null) + { + ClearChildrenFocusWithin(_focusedRoot, true); + } + + SetIsFocusWithin(FocusedElement, element); + FocusedElement = element; interactive?.RaiseEvent(new RoutedEventArgs diff --git a/src/Avalonia.MicroCom/Avalonia.MicroCom.csproj b/src/Avalonia.MicroCom/Avalonia.MicroCom.csproj index 6af3b4347a..b796e173c4 100644 --- a/src/Avalonia.MicroCom/Avalonia.MicroCom.csproj +++ b/src/Avalonia.MicroCom/Avalonia.MicroCom.csproj @@ -10,7 +10,6 @@ false all - true diff --git a/src/Avalonia.Native/Avalonia.Native.csproj b/src/Avalonia.Native/Avalonia.Native.csproj index a08dc0f28f..3913484431 100644 --- a/src/Avalonia.Native/Avalonia.Native.csproj +++ b/src/Avalonia.Native/Avalonia.Native.csproj @@ -20,36 +20,7 @@ + - - - - - false - all - true - - - - - - - - - - - - - - - - - - <_AvaloniaPatchComInterop>true - - + diff --git a/src/Avalonia.Native/WindowImplBase.cs b/src/Avalonia.Native/WindowImplBase.cs index 2cf1fe2963..20b6b8ecc5 100644 --- a/src/Avalonia.Native/WindowImplBase.cs +++ b/src/Avalonia.Native/WindowImplBase.cs @@ -155,10 +155,10 @@ namespace Avalonia.Native } finally { + + _parent?.Dispose(); n?.Dispose(); } - - _parent._mouse.Dispose(); } void IAvnWindowBaseEvents.Activated() => _parent.Activated?.Invoke(); @@ -336,6 +336,7 @@ namespace Avalonia.Native _nativeControlHost = null; (Screen as ScreenImpl)?.Dispose(); + _mouse.Dispose(); } diff --git a/src/Avalonia.Visuals/Media/FontFamily.cs b/src/Avalonia.Visuals/Media/FontFamily.cs index 9db49200cd..f018733235 100644 --- a/src/Avalonia.Visuals/Media/FontFamily.cs +++ b/src/Avalonia.Visuals/Media/FontFamily.cs @@ -133,6 +133,16 @@ namespace Avalonia.Media } } + /// + /// Parses a string. + /// + /// The string. + /// + /// + /// Specified family is not supported. + /// + public static FontFamily Parse(string s) => Parse(s, null); + /// /// Parses a string. /// @@ -142,7 +152,7 @@ namespace Avalonia.Media /// /// Specified family is not supported. /// - public static FontFamily Parse(string s, Uri baseUri = null) + public static FontFamily Parse(string s, Uri baseUri) { if (string.IsNullOrEmpty(s)) { diff --git a/src/Avalonia.Visuals/Media/TextDecoration.cs b/src/Avalonia.Visuals/Media/TextDecoration.cs index d9b3f664ce..57936426f3 100644 --- a/src/Avalonia.Visuals/Media/TextDecoration.cs +++ b/src/Avalonia.Visuals/Media/TextDecoration.cs @@ -189,11 +189,11 @@ namespace Avalonia.Media break; case TextDecorationLocation.Strikethrough: origin += new Point(shapedTextCharacters.GlyphRun.BaselineOrigin.X, - shapedTextCharacters.GlyphRun.BaselineOrigin.Y - fontMetrics.StrikethroughPosition); + shapedTextCharacters.GlyphRun.BaselineOrigin.Y + fontMetrics.StrikethroughPosition); break; case TextDecorationLocation.Underline: origin += new Point(shapedTextCharacters.GlyphRun.BaselineOrigin.X, - shapedTextCharacters.GlyphRun.BaselineOrigin.Y - fontMetrics.UnderlinePosition); + shapedTextCharacters.GlyphRun.BaselineOrigin.Y + fontMetrics.UnderlinePosition); break; } diff --git a/src/Markup/Avalonia.Markup/Data/MultiBinding.cs b/src/Markup/Avalonia.Markup/Data/MultiBinding.cs index 7aa1eed890..cbc5f414f2 100644 --- a/src/Markup/Avalonia.Markup/Data/MultiBinding.cs +++ b/src/Markup/Avalonia.Markup/Data/MultiBinding.cs @@ -48,7 +48,7 @@ namespace Avalonia.Data /// Gets or sets the binding priority. /// public BindingPriority Priority { get; set; } - + /// /// Gets or sets the relative source for the binding. /// @@ -77,12 +77,12 @@ namespace Avalonia.Data // We only respect `StringFormat` if the type of the property we're assigning to will // accept a string. Note that this is slightly different to WPF in that WPF only applies // `StringFormat` for target type `string` (not `object`). - if (!string.IsNullOrWhiteSpace(StringFormat) && + if (!string.IsNullOrWhiteSpace(StringFormat) && (targetType == typeof(string) || targetType == typeof(object))) { converter = new StringFormatMultiValueConverter(StringFormat, converter); } - + var children = Bindings.Select(x => x.Initiate(target, null)); var input = children.Select(x => x.Observable) @@ -116,7 +116,16 @@ namespace Avalonia.Data } var culture = CultureInfo.CurrentCulture; - var converted = converter.Convert(values, targetType, ConverterParameter, culture); + values = new System.Collections.ObjectModel.ReadOnlyCollection(values); + object converted; + if (converter != null) + { + converted = converter.Convert(values, targetType, ConverterParameter, culture); + } + else + { + converted = values; + } if (converted == null) { diff --git a/src/Windows/Avalonia.Win32/Composition/CompositionConnector.cs b/src/Windows/Avalonia.Win32/Composition/CompositionConnector.cs index 5bdc0cd410..e0dfdf282c 100644 --- a/src/Windows/Avalonia.Win32/Composition/CompositionConnector.cs +++ b/src/Windows/Avalonia.Win32/Composition/CompositionConnector.cs @@ -127,17 +127,13 @@ namespace Avalonia.Win32 private SpriteVisual CreateBlur() { var blurEffect = new GaussianBlurEffect(new CompositionEffectSourceParameter("backdrop")); - var blurEffectFactory = _compositor.CreateEffectFactory(blurEffect); - var blurBrush = blurEffectFactory.CreateBrush(); var backDropBrush = _compositor.CreateBackdropBrush(); - blurBrush.SetSourceParameter("backdrop", backDropBrush); - var saturateEffect = new SaturationEffect(blurEffect); var satEffectFactory = _compositor.CreateEffectFactory(saturateEffect); - var satBrush = satEffectFactory.CreateBrush(); + satBrush.SetSourceParameter("backdrop", backDropBrush); var visual = _compositor.CreateSpriteVisual(); diff --git a/src/Windows/Avalonia.Win32/Composition/SaturationEffect.cs b/src/Windows/Avalonia.Win32/Composition/SaturationEffect.cs index 90eca22d8e..3495829f3a 100644 --- a/src/Windows/Avalonia.Win32/Composition/SaturationEffect.cs +++ b/src/Windows/Avalonia.Win32/Composition/SaturationEffect.cs @@ -6,7 +6,7 @@ namespace Avalonia.Win32 { class SaturationEffect : EffectBase { - public SaturationEffect(IGraphicsEffect source) : base(source) + public SaturationEffect(IGraphicsEffectSource source) : base(source) { } diff --git a/tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Multiple.cs b/tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Multiple.cs index 5d0c6d31e1..68bdbe51e8 100644 --- a/tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Multiple.cs +++ b/tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Multiple.cs @@ -1216,6 +1216,7 @@ namespace Avalonia.Controls.UnitTests.Selection var data = (AvaloniaList)target.Source!; var selectionChangedRaised = 0; var selectedIndexRaised = 0; + var selectedItemRaised = 0; var indexesChangedRaised = 0; target.Source = data; @@ -1227,6 +1228,11 @@ namespace Avalonia.Controls.UnitTests.Selection { ++selectedIndexRaised; } + + if (e.PropertyName == nameof(target.SelectedItem)) + { + ++selectedItemRaised; + } }; target.IndexesChanged += (s, e) => ++indexesChangedRaised; @@ -1249,6 +1255,7 @@ namespace Avalonia.Controls.UnitTests.Selection Assert.Equal(2, target.AnchorIndex); Assert.Equal(1, selectionChangedRaised); Assert.Equal(1, selectedIndexRaised); + Assert.Equal(1, selectedItemRaised); Assert.Equal(0, indexesChangedRaised); } diff --git a/tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Single.cs b/tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Single.cs index 66a2cef921..668af3b5d7 100644 --- a/tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Single.cs +++ b/tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Single.cs @@ -1040,6 +1040,7 @@ namespace Avalonia.Controls.UnitTests.Selection var data = (AvaloniaList)target.Source!; var selectionChangedRaised = 0; var selectedIndexRaised = 0; + var selectedItemRaised = 0; target.Source = data; target.Select(1); @@ -1050,6 +1051,11 @@ namespace Avalonia.Controls.UnitTests.Selection { ++selectedIndexRaised; } + + if (e.PropertyName == nameof(target.SelectedItem)) + { + ++selectedItemRaised; + } }; target.SelectionChanged += (s, e) => @@ -1070,6 +1076,7 @@ namespace Avalonia.Controls.UnitTests.Selection Assert.Equal(-1, target.AnchorIndex); Assert.Equal(1, selectionChangedRaised); Assert.Equal(1, selectedIndexRaised); + Assert.Equal(1, selectedItemRaised); } [Fact] diff --git a/tests/Avalonia.Controls.UnitTests/TreeViewTests.cs b/tests/Avalonia.Controls.UnitTests/TreeViewTests.cs index b805683393..cea77bb7c9 100644 --- a/tests/Avalonia.Controls.UnitTests/TreeViewTests.cs +++ b/tests/Avalonia.Controls.UnitTests/TreeViewTests.cs @@ -675,6 +675,50 @@ namespace Avalonia.Controls.UnitTests Assert.Same(node, focus.Current); } } + + [Fact] + public void Keyboard_Navigation_Should_Not_Crash_If_Selected_Item_Is_not_In_Tree() + { + using (Application()) + { + var focus = FocusManager.Instance; + var navigation = AvaloniaLocator.Current.GetService(); + var data = CreateTestTreeData(); + + var selectedNode = new Node { Value = "Out of Tree Selected Item" }; + + var target = new TreeView + { + Template = CreateTreeViewTemplate(), + Items = data, + SelectedItem = selectedNode + }; + + var button = new Button(); + + var root = new TestRoot + { + Child = new StackPanel + { + Children = { target, button }, + } + }; + + CreateNodeDataTemplate(target); + ApplyTemplates(target); + ExpandAll(target); + + var item = data[0].Children[0]; + var node = target.ItemContainerGenerator.Index.ContainerFromItem(item); + Assert.NotNull(node); + + target.SelectedItem = selectedNode; + node.Focus(); + Assert.Same(node, focus.Current); + + var next = KeyboardNavigationHandler.GetNext(node, NavigationDirection.Previous); + } + } [Fact] public void Pressing_SelectAll_Gesture_Should_Select_All_Nodes() diff --git a/tests/Avalonia.Input.UnitTests/InputElement_Focus.cs b/tests/Avalonia.Input.UnitTests/InputElement_Focus.cs index 09fae7207f..8b8f2fa775 100644 --- a/tests/Avalonia.Input.UnitTests/InputElement_Focus.cs +++ b/tests/Avalonia.Input.UnitTests/InputElement_Focus.cs @@ -121,5 +121,202 @@ namespace Avalonia.Input.UnitTests Assert.False(target2.Classes.Contains(":focus-visible")); } } + + [Fact] + public void Control_FocusWithin_PseudoClass_Should_Be_Applied() + { + using (UnitTestApplication.Start(TestServices.RealFocus)) + { + var target1 = new Decorator(); + var target2 = new Decorator(); + var root = new TestRoot + { + Child = new StackPanel + { + Children = + { + target1, + target2 + } + } + }; + + target1.ApplyTemplate(); + target2.ApplyTemplate(); + + FocusManager.Instance?.Focus(target1); + Assert.True(target1.IsFocused); + Assert.True(target1.Classes.Contains(":focus-within")); + Assert.True(target1.IsKeyboardFocusWithin); + Assert.True(root.Child.Classes.Contains(":focus-within")); + Assert.True(root.Child.IsKeyboardFocusWithin); + Assert.True(root.Classes.Contains(":focus-within")); + Assert.True(root.IsKeyboardFocusWithin); + } + } + + [Fact] + public void Control_FocusWithin_PseudoClass_Should_Be_Applied_and_Removed() + { + using (UnitTestApplication.Start(TestServices.RealFocus)) + { + var target1 = new Decorator(); + var target2 = new Decorator(); + var panel1 = new Panel { Children = { target1 } }; + var panel2 = new Panel { Children = { target2 } }; + var root = new TestRoot + { + Child = new StackPanel + { + Children = + { + panel1, + panel2 + } + } + }; + + target1.ApplyTemplate(); + target2.ApplyTemplate(); + + FocusManager.Instance?.Focus(target1); + Assert.True(target1.IsFocused); + Assert.True(target1.Classes.Contains(":focus-within")); + Assert.True(target1.IsKeyboardFocusWithin); + Assert.True(panel1.Classes.Contains(":focus-within")); + Assert.True(panel1.IsKeyboardFocusWithin); + Assert.True(root.Child.Classes.Contains(":focus-within")); + Assert.True(root.Child.IsKeyboardFocusWithin); + Assert.True(root.Classes.Contains(":focus-within")); + Assert.True(root.IsKeyboardFocusWithin); + + FocusManager.Instance?.Focus(target2); + + Assert.False(target1.IsFocused); + Assert.False(target1.Classes.Contains(":focus-within")); + Assert.False(target1.IsKeyboardFocusWithin); + Assert.False(panel1.Classes.Contains(":focus-within")); + Assert.False(panel1.IsKeyboardFocusWithin); + Assert.True(root.Child.Classes.Contains(":focus-within")); + Assert.True(root.Child.IsKeyboardFocusWithin); + Assert.True(root.Classes.Contains(":focus-within")); + Assert.True(root.IsKeyboardFocusWithin); + + Assert.True(target2.IsFocused); + Assert.True(target2.Classes.Contains(":focus-within")); + Assert.True(target2.IsKeyboardFocusWithin); + Assert.True(panel2.Classes.Contains(":focus-within")); + Assert.True(panel2.IsKeyboardFocusWithin); + } + } + + [Fact] + public void Control_FocusWithin_Pseudoclass_Should_Be_Removed_When_Removed_From_Tree() + { + using (UnitTestApplication.Start(TestServices.RealFocus)) + { + var target1 = new Decorator(); + var target2 = new Decorator(); + var root = new TestRoot + { + Child = new StackPanel + { + Children = + { + target1, + target2 + } + } + }; + + target1.ApplyTemplate(); + target2.ApplyTemplate(); + + FocusManager.Instance?.Focus(target1); + Assert.True(target1.IsFocused); + Assert.True(target1.Classes.Contains(":focus-within")); + Assert.True(target1.IsKeyboardFocusWithin); + Assert.True(root.Child.Classes.Contains(":focus-within")); + Assert.True(root.Child.IsKeyboardFocusWithin); + Assert.True(root.Classes.Contains(":focus-within")); + Assert.True(root.IsKeyboardFocusWithin); + + Assert.Equal(KeyboardDevice.Instance.FocusedElement, target1); + + root.Child = null; + + Assert.Null(KeyboardDevice.Instance.FocusedElement); + + Assert.False(target1.IsFocused); + Assert.False(target1.Classes.Contains(":focus-within")); + Assert.False(target1.IsKeyboardFocusWithin); + Assert.False(root.Classes.Contains(":focus-within")); + Assert.False(root.IsKeyboardFocusWithin); + } + } + + [Fact] + public void Control_FocusWithin_Pseudoclass_Should_Be_Removed_Focus_Moves_To_Different_Root() + { + using (UnitTestApplication.Start(TestServices.RealFocus)) + { + var target1 = new Decorator(); + var target2 = new Decorator(); + + var root1 = new TestRoot + { + Child = new StackPanel + { + Children = + { + target1, + } + } + }; + + var root2 = new TestRoot + { + Child = new StackPanel + { + Children = + { + target2, + } + } + }; + + target1.ApplyTemplate(); + target2.ApplyTemplate(); + + FocusManager.Instance?.Focus(target1); + Assert.True(target1.IsFocused); + Assert.True(target1.Classes.Contains(":focus-within")); + Assert.True(target1.IsKeyboardFocusWithin); + Assert.True(root1.Child.Classes.Contains(":focus-within")); + Assert.True(root1.Child.IsKeyboardFocusWithin); + Assert.True(root1.Classes.Contains(":focus-within")); + Assert.True(root1.IsKeyboardFocusWithin); + + Assert.Equal(KeyboardDevice.Instance.FocusedElement, target1); + + FocusManager.Instance?.Focus(target2); + + Assert.False(target1.IsFocused); + Assert.False(target1.Classes.Contains(":focus-within")); + Assert.False(target1.IsKeyboardFocusWithin); + Assert.False(root1.Child.Classes.Contains(":focus-within")); + Assert.False(root1.Child.IsKeyboardFocusWithin); + Assert.False(root1.Classes.Contains(":focus-within")); + Assert.False(root1.IsKeyboardFocusWithin); + + Assert.True(target2.IsFocused); + Assert.True(target2.Classes.Contains(":focus-within")); + Assert.True(target2.IsKeyboardFocusWithin); + Assert.True(root2.Child.Classes.Contains(":focus-within")); + Assert.True(root2.Child.IsKeyboardFocusWithin); + Assert.True(root2.Classes.Contains(":focus-within")); + Assert.True(root2.IsKeyboardFocusWithin); + } + } } } diff --git a/tests/Avalonia.Markup.UnitTests/Data/MultiBindingTests.cs b/tests/Avalonia.Markup.UnitTests/Data/MultiBindingTests.cs index 64f5eb2a0e..b87922aa1e 100644 --- a/tests/Avalonia.Markup.UnitTests/Data/MultiBindingTests.cs +++ b/tests/Avalonia.Markup.UnitTests/Data/MultiBindingTests.cs @@ -157,6 +157,29 @@ namespace Avalonia.Markup.UnitTests.Data Assert.Equal("1,2,Fallback", target.Text); } + [Fact] + public void MultiBinding_Without_StringFormat_And_Converter() + { + var source = new { A = 1, B = 2, C = 3 }; + var target = new ItemsControl { }; + + var binding = new MultiBinding + { + Bindings = new[] + { + new Binding { Path = "A", Source = source }, + new Binding { Path = "B", Source = source }, + new Binding { Path = "C", Source = source }, + }, + }; + + target.Bind(ItemsControl.ItemsProperty, binding); + Assert.Equal(target.ItemCount, 3); + Assert.Equal(target.Items.ElementAt(0), source.A); + Assert.Equal(target.Items.ElementAt(1), source.B); + Assert.Equal(target.Items.ElementAt(2), source.C); + } + private class ConcatConverter : IMultiValueConverter { public object Convert(IList values, Type targetType, object parameter, CultureInfo culture) diff --git a/tests/Avalonia.Markup.UnitTests/Extensions/IEnummerableExtension.cs b/tests/Avalonia.Markup.UnitTests/Extensions/IEnummerableExtension.cs new file mode 100644 index 0000000000..0d9e2969e1 --- /dev/null +++ b/tests/Avalonia.Markup.UnitTests/Extensions/IEnummerableExtension.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections; + +namespace Avalonia.Markup.UnitTests +{ + internal static class IEnumerableExtensions + { + public static object ElementAt(this IEnumerable source, int index) + { + var i = -1; + var enumerator = source.GetEnumerator(); + + while (enumerator.MoveNext() && ++i < index); + if (i == index) + { + return enumerator.Current; + } + throw new ArgumentOutOfRangeException(nameof(index)); + } + } +}