Skip to content

Commit

Permalink
feat: Add includeCurrent flag to FindFirstParent()
Browse files Browse the repository at this point in the history
Matches UWP version from toolkit (unfortunately the implicit default behaviour is the opposite of the UWP version...)
  • Loading branch information
davidjohnoliver committed Sep 4, 2020
1 parent 3314e05 commit e9f0c36
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 62 deletions.
16 changes: 11 additions & 5 deletions src/Uno.UI/Extensions/UIViewExtensions.iOSmacOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,16 @@ public static T FindFirstParent<T>(this _View view, Func<T, bool> predicate)
/// <typeparam name="T"></typeparam>
/// <param name="view"></param>
/// <returns>First parent of the view of specified T type.</returns>
public static T FindFirstParent<T>(this _View view)
public static T FindFirstParent<T>(this _View view) where T : class
=> FindFirstParent<T>(view, includeCurrent: false);

public static T FindFirstParent<T>(this _View view, bool includeCurrent)
where T : class
{
view = view?.Superview;
if (!includeCurrent)
{
view = view?.Superview;
}
while (view != null)
{
var typed = view as T;
Expand Down Expand Up @@ -352,7 +358,7 @@ public static void SetDimensions(this _View view, nfloat? width = null, nfloat?
/// <param name="view">View</param>
/// <returns>First responder view</returns>
public static _View FindFirstResponder(this _View view) =>
Uno.Extensions.UIViewExtensions.FindFirstResponder(view);
Uno.Extensions.UIViewExtensions.FindFirstResponder(view);

/// <summary>
/// Finds the nearest view controller for this _View.
Expand Down Expand Up @@ -586,7 +592,7 @@ public static _View FindFirstChild(this _View view)

return null;
}

/// <summary>
/// Returns the root of the view's local visual tree.
/// </summary>
Expand Down Expand Up @@ -682,6 +688,6 @@ public static void SetNeedsDisplay(this _View view)
#elif __MACOS__
view.NeedsDisplay = true;
#endif
}
}
}
}
15 changes: 11 additions & 4 deletions src/Uno.UI/Extensions/ViewExtensions.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Windows.UI.Core;
using System.Threading.Tasks;
using Android.Views.Animations;
using Windows.UI.Xaml.Controls;

namespace Uno.UI
{
Expand Down Expand Up @@ -45,25 +46,29 @@ public static bool HasParent(this View view)
return view.Parent != null;
}


/// <summary>
/// Return First parent of the view of specified T type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="view"></param>
/// <returns>First parent of the view of specified T type.</returns>
public static T FindFirstParent<T>(this IViewParent view) where T : class => FindFirstParentOfView<T>(view as View);
public static T FindFirstParent<T>(this IViewParent view) where T : class => FindFirstParent<T>(view, includeCurrent: false);

public static T FindFirstParent<T>(this IViewParent view, bool includeCurrent) where T : class => FindFirstParentOfView<T>(view as View, includeCurrent);

/// <summary>
/// Return First parent of the view of specified T type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="view"></param>
/// <returns>First parent of the view of specified T type.</returns>
public static T FindFirstParentOfView<T>(this View childView)
public static T FindFirstParentOfView<T>(this View childView) where T : class => FindFirstParentOfView<T>(childView, includeCurrent: false);

public static T FindFirstParentOfView<T>(this View childView, bool includeCurrent)
where T : class
{
var view = childView?.Parent;
var view = includeCurrent ? childView as IViewParent : null;
view ??= childView?.Parent;

while (view != null)
{
Expand Down Expand Up @@ -652,6 +657,8 @@ void AppendView(View innerView, string s)
.Append(u != null && u.RenderTransform != null ? $"RENDER_TRANSFORM({u.RenderTransform.MatrixCore})" : "")
.Append(u?.Clip != null ? $" Clip={u.Clip.Rect}" : "")
.Append(u == null && vg != null ? $" ClipChildren={vg.ClipChildren}" : "")
.Append($" IsLayoutRequested={innerView.IsLayoutRequested}")
.Append(innerView is TextBlock textBlock ? $" Text=\"{textBlock.Text}\"" : "")
.AppendLine();
}
}
Expand Down
98 changes: 50 additions & 48 deletions src/Uno.UI/UI/Xaml/FrameworkElement.Interface.Skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,34 @@

namespace Windows.UI.Xaml
{
public partial class FrameworkElement : UIElement, IFrameworkElement
{
public T FindFirstParent<T>() where T : class
{
var view = this.Parent;
while (view != null)
{
var typed = view as T;
if (typed != null)
{
return typed;
}
view = view.GetParent() as DependencyObject;
}
return null;
}
public partial class FrameworkElement : UIElement, IFrameworkElement
{
public T FindFirstParent<T>() where T : class => FindFirstParent<T>(includeCurrent: false);

public T FindFirstParent<T>(bool includeCurrent) where T : class
{
var view = includeCurrent ? (DependencyObject)this : this.Parent;
while (view != null)
{
var typed = view as T;
if (typed != null)
{
return typed;
}
view = view.GetParent() as DependencyObject;
}
return null;
}

private protected virtual void OnLoaded()
{
{

}
}

private protected virtual void OnUnloaded()
{
{

}
}


#region Transitions Dependency Property
Expand All @@ -57,18 +59,18 @@ private void OnTransitionsChanged(DependencyPropertyChangedEventArgs args)
#endregion

public object FindName(string name)
=> IFrameworkElementHelper.FindName(this, GetChildren(), name);
=> IFrameworkElementHelper.FindName(this, GetChildren(), name);


public void Dispose()
{
throw new NotImplementedException();
}
public void Dispose()
{
throw new NotImplementedException();
}

public Size AdjustArrange(Size finalSize)
{
return finalSize;
}
public Size AdjustArrange(Size finalSize)
{
return finalSize;
}


#region IsEnabled DependencyProperty
Expand Down Expand Up @@ -96,11 +98,11 @@ protected virtual void OnIsEnabledChanged(bool oldValue, bool newValue)

#endregion

public int? RenderPhase
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public int? RenderPhase
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}

private static readonly Uri DefaultBaseUri = new Uri("ms-appx://local");
public global::System.Uri BaseUri
Expand All @@ -111,24 +113,24 @@ protected virtual void OnIsEnabledChanged(bool oldValue, bool newValue)

public void ApplyBindingPhase(int phase) => throw new NotImplementedException();

#region Background DependencyProperty
#region Background DependencyProperty

public Brush Background
{
get => (Brush)GetValue(BackgroundProperty);
set => SetValue(BackgroundProperty, value);
}
public Brush Background
{
get => (Brush)GetValue(BackgroundProperty);
set => SetValue(BackgroundProperty, value);
}

// Using a DependencyProperty as the backing store for Background. This enables animation, styling, binding, etc...
public static DependencyProperty BackgroundProperty { get; } =
DependencyProperty.Register("Background", typeof(Brush), typeof(FrameworkElement), new PropertyMetadata(null, (s, e) => ((FrameworkElement)s)?.OnBackgroundChanged(e)));
// Using a DependencyProperty as the backing store for Background. This enables animation, styling, binding, etc...
public static DependencyProperty BackgroundProperty { get; } =
DependencyProperty.Register("Background", typeof(Brush), typeof(FrameworkElement), new PropertyMetadata(null, (s, e) => ((FrameworkElement)s)?.OnBackgroundChanged(e)));


protected virtual void OnBackgroundChanged(DependencyPropertyChangedEventArgs e)
{
protected virtual void OnBackgroundChanged(DependencyPropertyChangedEventArgs e)
{

}
}

#endregion
}
#endregion
}
}
6 changes: 4 additions & 2 deletions src/Uno.UI/UI/Xaml/FrameworkElement.Interface.netstdref.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ namespace Windows.UI.Xaml
{
public partial class FrameworkElement : UIElement, IFrameworkElement
{
public T FindFirstParent<T>() where T : class
public T FindFirstParent<T>() where T : class => FindFirstParent<T>(includeCurrent: false);

public T FindFirstParent<T>(bool includeCurrent) where T : class
{
var view = this.Parent;
var view = includeCurrent ? (DependencyObject)this : this.Parent;
while (view != null)
{
var typed = view as T;
Expand Down
8 changes: 5 additions & 3 deletions src/Uno.UI/UI/Xaml/FrameworkElement.Interface.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ namespace Windows.UI.Xaml
public partial class FrameworkElement : UIElement, IFrameworkElement
{
private readonly SerialDisposable _backgroundSubscription = new SerialDisposable();
public T FindFirstParent<T>() where T : class
public T FindFirstParent<T>() where T : class => FindFirstParent<T>(includeCurrent: false);

public T FindFirstParent<T>(bool includeCurrent) where T : class
{
var view = this.Parent;
var view = includeCurrent ? (DependencyObject)this : this.Parent;
while (view != null)
{
var typed = view as T;
Expand Down Expand Up @@ -177,7 +179,7 @@ private protected void SetBackgroundBrush(Brush brush)
ResetStyle("background-color");
SetStyle("background-image", gradientBrush.ToCssString(RenderSize));
RecalculateBrushOnSizeChanged(true);
break;
break;
default:
ResetStyle("background-color", "background-image", "background-size");
RecalculateBrushOnSizeChanged(false);
Expand Down

0 comments on commit e9f0c36

Please sign in to comment.