Browse Source

Add extra methods to LogicalExtensions and optimize a few methods as well.

pull/3869/head
Dariusz Komosinski 6 years ago
parent
commit
cd2b26bc94
  1. 2
      src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs
  2. 2
      src/Avalonia.Dialogs/ManagedFileChooser.xaml.cs
  3. 93
      src/Avalonia.Styling/LogicalTree/LogicalExtensions.cs
  4. 14
      src/Avalonia.Visuals/VisualTree/VisualExtensions.cs

2
src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs

@ -392,7 +392,7 @@ namespace Avalonia.Controls.Platform
{
var control = e.Source as ILogical;
if (!Menu.IsLogicalParentOf(control))
if (!Menu.IsLogicalAncestorOf(control))
{
Menu.Close();
}

2
src/Avalonia.Dialogs/ManagedFileChooser.xaml.cs

@ -34,7 +34,7 @@ namespace Avalonia.Dialogs
return;
}
var isQuickLink = _quickLinksRoot.IsLogicalParentOf(e.Source as Control);
var isQuickLink = _quickLinksRoot.IsLogicalAncestorOf(e.Source as Control);
if (e.ClickCount == 2 || isQuickLink)
{
if (model.ItemType == ManagedFileChooserItemType.File)

93
src/Avalonia.Styling/LogicalTree/LogicalExtensions.cs

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Avalonia.LogicalTree
{
@ -29,6 +28,35 @@ namespace Avalonia.LogicalTree
}
}
/// <summary>
/// Finds first ancestor of given type.
/// </summary>
/// <typeparam name="T">Ancestor type.</typeparam>
/// <param name="logical">The logical.</param>
/// <param name="includeSelf">If given logical should be included in search.</param>
/// <returns>First ancestor of given type.</returns>
public static T FindLogicalAncestorOfType<T>(this ILogical logical, bool includeSelf = false) where T : class
{
if (logical is null)
{
return null;
}
ILogical parent = includeSelf ? logical : logical.LogicalParent;
while (parent != null)
{
if (parent is T result)
{
return result;
}
parent = parent.LogicalParent;
}
return null;
}
public static IEnumerable<ILogical> GetLogicalChildren(this ILogical logical)
{
return logical.LogicalChildren;
@ -57,6 +85,28 @@ namespace Avalonia.LogicalTree
}
}
/// <summary>
/// Finds first descendant of given type.
/// </summary>
/// <typeparam name="T">Descendant type.</typeparam>
/// <param name="logical">The logical.</param>
/// <param name="includeSelf">If given logical should be included in search.</param>
/// <returns>First descendant of given type.</returns>
public static T FindLogicalDescendantOfType<T>(this ILogical logical, bool includeSelf = false) where T : class
{
if (logical is null)
{
return null;
}
if (includeSelf && logical is T result)
{
return result;
}
return FindDescendantOfTypeCore<T>(logical);
}
public static ILogical GetLogicalParent(this ILogical logical)
{
return logical.LogicalParent;
@ -80,9 +130,46 @@ namespace Avalonia.LogicalTree
}
}
public static bool IsLogicalParentOf(this ILogical logical, ILogical target)
public static bool IsLogicalAncestorOf(this ILogical logical, ILogical target)
{
return target.GetLogicalAncestors().Any(x => x == logical);
ILogical current = target?.LogicalParent;
while (current != null)
{
if (current == logical)
{
return true;
}
current = current.LogicalParent;
}
return false;
}
private static T FindDescendantOfTypeCore<T>(ILogical logical) where T : class
{
var logicalChildren = logical.LogicalChildren;
var logicalChildrenCount = logicalChildren.Count;
for (var i = 0; i < logicalChildrenCount; i++)
{
ILogical child = logicalChildren[i];
if (child is T result)
{
return result;
}
var childResult = FindDescendantOfTypeCore<T>(child);
if (!(childResult is null))
{
return childResult;
}
}
return null;
}
}
}

14
src/Avalonia.Visuals/VisualTree/VisualExtensions.cs

@ -377,7 +377,19 @@ namespace Avalonia.VisualTree
/// </returns>
public static bool IsVisualAncestorOf(this IVisual visual, IVisual target)
{
return target.GetVisualAncestors().Any(x => x == visual);
IVisual current = target;
while (current != null)
{
if (current == visual)
{
return true;
}
current = current.VisualParent;
}
return false;
}
public static IEnumerable<IVisual> SortByZIndex(this IEnumerable<IVisual> elements)

Loading…
Cancel
Save