-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WinUI] Allocate less when updating gestures (#21450)
* `EnumerableExtensions`: Enable nullable * Add `EnumerableExtensions.HasAnyGesturesFor` * Sandbox measure code * Revert "Sandbox measure code" This reverts commit e984695. * Do not create lambdas + apply MSVS suggestion regarding pattern matching + add braces * Make `HasAnyGesturesFor` `internal` * Reimplement by adding `FirstGestureOrDefault`
- Loading branch information
Showing
2 changed files
with
64 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,86 @@ | ||
#nullable disable | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
|
||
namespace Microsoft.Maui.Controls.Internals | ||
{ | ||
static class EnumerableExtensions | ||
{ | ||
public static bool HasChildGesturesFor<T>(this IEnumerable<GestureElement> elements, Func<T, bool> predicate = null) where T : GestureRecognizer | ||
public static bool HasChildGesturesFor<T>(this IEnumerable<GestureElement>? elements, Func<T, bool>? predicate = null) where T : GestureRecognizer | ||
{ | ||
if (elements == null) | ||
if (elements is null) | ||
{ | ||
return false; | ||
|
||
if (predicate == null) | ||
predicate = x => true; | ||
} | ||
|
||
foreach (var element in elements) | ||
{ | ||
foreach (var item in element.GestureRecognizers) | ||
{ | ||
var gesture = item as T; | ||
if (gesture != null && predicate(gesture)) | ||
if (item is T gesture && (predicate is null || predicate(gesture))) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static IEnumerable<T> GetChildGesturesFor<T>(this IEnumerable<GestureElement> elements, Func<T, bool> predicate = null) where T : GestureRecognizer | ||
public static IEnumerable<T> GetChildGesturesFor<T>(this IEnumerable<GestureElement>? elements, Func<T, bool>? predicate = null) where T : GestureRecognizer | ||
{ | ||
if (elements == null) | ||
if (elements is null) | ||
{ | ||
yield break; | ||
|
||
if (predicate == null) | ||
predicate = x => true; | ||
} | ||
|
||
foreach (var element in elements) | ||
{ | ||
foreach (var item in element.GestureRecognizers) | ||
{ | ||
var gesture = item as T; | ||
if (gesture != null && predicate(gesture)) | ||
if (item is T gesture && (predicate is null || predicate(gesture))) | ||
{ | ||
yield return gesture; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static IEnumerable<T> GetGesturesFor<T>(this IEnumerable<IGestureRecognizer> gestures, Func<T, bool> predicate = null) where T : GestureRecognizer | ||
/// <remarks>The method makes a defensive copy of the gestures.</remarks> | ||
public static IEnumerable<T> GetGesturesFor<T>(this IEnumerable<IGestureRecognizer>? gestures, Func<T, bool>? predicate = null) where T : GestureRecognizer | ||
{ | ||
if (gestures == null) | ||
if (gestures is null) | ||
{ | ||
yield break; | ||
|
||
if (predicate == null) | ||
predicate = x => true; | ||
} | ||
|
||
foreach (IGestureRecognizer item in new List<IGestureRecognizer>(gestures)) | ||
{ | ||
var gesture = item as T; | ||
if (gesture != null && predicate(gesture)) | ||
if (item is T gesture && (predicate is null || predicate(gesture))) | ||
{ | ||
yield return gesture; | ||
} | ||
} | ||
} | ||
|
||
internal static bool HasAnyGesturesFor<T>(this IEnumerable<IGestureRecognizer>? gestures, Func<T, bool>? predicate = null) where T : GestureRecognizer | ||
=> FirstGestureOrDefault(gestures, predicate) is not null; | ||
|
||
internal static T? FirstGestureOrDefault<T>(this IEnumerable<IGestureRecognizer>? gestures, Func<T, bool>? predicate = null) where T : GestureRecognizer | ||
{ | ||
if (gestures is null) | ||
{ | ||
return null; | ||
} | ||
|
||
foreach (IGestureRecognizer item in gestures) | ||
{ | ||
if (item is T gesture && (predicate is null || predicate(gesture))) | ||
{ | ||
return gesture; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters