Skip to content

Commit

Permalink
fix: Adjust VisualParent property to return Selector for SelectorItems
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Mar 2, 2022
1 parent 583ec45 commit bdfc675
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 19 deletions.
25 changes: 14 additions & 11 deletions src/Uno.UI/Extensions/UIViewExtensions.iOSmacOS.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
using Uno.Disposables;
using Uno;
using Uno;
using Uno.Extensions;
using Uno.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Uno.UI.DataBinding;
using Uno.UI.Extensions;
using Windows.UI.Xaml;
using System.Diagnostics.CodeAnalysis;
using Uno.Foundation.Logging;
using Windows.UI.Core;
using Uno.UI.Controls;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

#if NET6_0_OR_GREATER
using ObjCRuntime;
#endif

#if XAMARIN_IOS_UNIFIED
using Foundation;
using UIKit;
using CoreGraphics;
using _View = UIKit.UIView;
using _Controller = UIKit.UIViewController;
using _Responder = UIKit.UIResponder;
using _Color = UIKit.UIColor;
using _Event = UIKit.UIEvent;
using System.Security.Principal;
#elif __MACOS__
using Foundation;
using AppKit;
Expand Down Expand Up @@ -295,7 +287,18 @@ public static void AddChild(this _View parent, _View child)
/// <summary>
/// Get the parent view in the visual tree. This may differ from the logical <see cref="FrameworkElement.Parent"/>.
/// </summary>
public static _View GetVisualTreeParent(this _View child) => child?.Superview;
public static _View GetVisualTreeParent(this _View child)
{
var visualParent = child?.Superview;

if (visualParent is null &&
child is FrameworkElement fw)
{
visualParent = fw.VisualParent;
}

return visualParent;
}

public static IEnumerable<T> FindSubviewsOfType<T>(this _View view, int maxDepth = 20) where T : class
{
Expand Down
13 changes: 12 additions & 1 deletion src/Uno.UI/Extensions/ViewExtensions.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,18 @@ public static void AddChild(this ViewGroup parent, View child)
/// <summary>
/// Get the parent view in the visual tree. This may differ from the logical <see cref="FrameworkElement.Parent"/>.
/// </summary>
public static ViewGroup GetVisualTreeParent(this View child) => child?.Parent as ViewGroup;
public static ViewGroup GetVisualTreeParent(this View child)
{
var visualParent = child?.Parent as ViewGroup;

if (visualParent is null &&
child is FrameworkElement fe)
{
visualParent = fe.VisualParent;
}

return visualParent;
}

/// <summary>
/// Removes a child view from the specified view, and disposes it if the specified view is the owner.
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/Popup/PopupPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected override Size ArrangeOverride(Size finalSize)
// for android, the above line returns the absolute coordinates of anchor on the screen
// because the parent view of this PopupPanel is a PopupWindow and GetLocationInWindow will be (0,0)
// therefore, we need to make the relative adjustment
if (this.VisualParent is Android.Views.View view)
if (this.NativeVisualParent is Android.Views.View view)
{
var windowLocation = Point.From(view.GetLocationInWindow);
var screenLocation = Point.From(view.GetLocationOnScreen);
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/Primitives/SelectorItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public SelectorItem()

private Selector Selector => ItemsControl.ItemsControlFromItemContainer(this) as Selector;

internal override UIElement VisualParent => Selector ?? base.VisualParent;

private bool IsItemClickEnabled
{
get
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UI/UI/Xaml/FrameworkElement.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public partial class FrameworkElement
/// <summary>
/// The parent of the <see cref="FrameworkElement"/> in the visual tree, which may differ from its <see cref="Parent"/> (ie if it's a child of a native view).
/// </summary>
internal IViewParent VisualParent => (this as View).Parent;
internal IViewParent NativeVisualParent => (this as View).Parent;

public FrameworkElement()
{
Expand Down Expand Up @@ -158,7 +158,7 @@ partial void OnLoadedPartial()
// see StretchAffectsMeasure for details.
this.SetValue(
StretchAffectsMeasureProperty,
!(VisualParent is DependencyObject),
!(NativeVisualParent is DependencyObject),
DependencyPropertyValuePrecedences.DefaultValue
);
}
Expand Down
9 changes: 7 additions & 2 deletions src/Uno.UI/UI/Xaml/FrameworkElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,15 @@ Windows.UI.Xaml.ResourceDictionary Resources
/// Allows to override the publicly-visible <see cref="Parent"/> without modifying DP propagation.
/// </summary>
internal DependencyObject LogicalParentOverride { get; set; }

internal UIElement VisualParent => ((IDependencyObjectStoreProvider)this).Store.Parent as UIElement;
#endif

/// <summary>
/// Provides the managed visual parent of the element. This property can be overriden for specific
/// scenarios, for example in case of SelectorItem, where actual parent is null, but visual parent
/// is the list.
/// </summary>
internal virtual UIElement VisualParent => ((IDependencyObjectStoreProvider)this).Store.Parent as UIElement;

private bool _isParsing;
/// <summary>
/// True if the element is in the process of being parsed from Xaml.
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UI/UI/Xaml/Media/VisualTreeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ public static IReadOnlyList<Popup> GetOpenPopupsForXamlRoot(XamlRoot xamlRoot)
realParent = reference.GetParent() as DependencyObject;
#endif

if (realParent is null && reference is SelectorItem selectorItem)
if (realParent is null && reference is _ViewGroup uiElement)
{
return ItemsControl.ItemsControlFromItemContainer(selectorItem);
return uiElement.GetVisualTreeParent() as DependencyObject;
}

return realParent;
Expand Down

0 comments on commit bdfc675

Please sign in to comment.