Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 975577e

Browse files
[C] index on VTChanges (#11775)
* [C] index on VTChanges [C] send index on VTChange.Add [C] register LV cells as LogicalChildren [C] add index in VTChanges.Remove - fixes #11334 - fixes #11335 - fixes AB#1152749 - fixes AB#1152814 * Update Xamarin.Forms.Core/Element.cs change obsolescence message Co-authored-by: Samantha Houts <[email protected]> Co-authored-by: Samantha Houts <[email protected]>
1 parent 16b290a commit 975577e

File tree

21 files changed

+368
-250
lines changed

21 files changed

+368
-250
lines changed

Xamarin.Forms.Core/AbsoluteLayout.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,13 @@ protected override void OnChildAdded(Element child)
7676
child.PropertyChanged += ChildOnPropertyChanged;
7777
}
7878

79-
protected override void OnChildRemoved(Element child)
79+
[Obsolete("OnChildRemoved(Element) is obsolete as of version 4.8.0. Please use OnChildRemoved(Element, int) instead.")]
80+
protected override void OnChildRemoved(Element child) => OnChildRemoved(child, -1);
81+
82+
protected override void OnChildRemoved(Element child, int oldLogicalIndex)
8083
{
8184
child.PropertyChanged -= ChildOnPropertyChanged;
82-
base.OnChildRemoved(child);
85+
base.OnChildRemoved(child, oldLogicalIndex);
8386
}
8487

8588
[Obsolete("OnSizeRequest is obsolete as of version 2.2.0. Please use OnMeasure instead.")]

Xamarin.Forms.Core/Cells/ViewCell.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public View View
2222

2323
if (_view != null)
2424
{
25-
OnChildRemoved(_view);
25+
OnChildRemoved(_view, 0);
2626
_view.ComputedConstraint = LayoutConstraint.None;
2727
}
2828

Xamarin.Forms.Core/Element.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,6 @@ public Element Parent
201201
SetInheritedBindingContext(this, null);
202202
}
203203

204-
VisualDiagnostics.SendVisualTreeChanged(value, this);
205-
206204
OnParentSet();
207205

208206
OnPropertyChanged();
@@ -325,17 +323,24 @@ protected virtual void OnChildAdded(Element child)
325323

326324
ChildAdded?.Invoke(this, new ElementEventArgs(child));
327325

326+
VisualDiagnostics.OnChildAdded(this, child);
327+
328328
OnDescendantAdded(child);
329329
foreach (Element element in child.Descendants())
330330
OnDescendantAdded(element);
331331
}
332332

333-
protected virtual void OnChildRemoved(Element child)
333+
[Obsolete("OnChildRemoved(Element) is obsolete as of version 4.8.0. Please use OnChildRemoved(Element, int) instead.")]
334+
protected virtual void OnChildRemoved(Element child) => OnChildRemoved(child, -1);
335+
336+
protected virtual void OnChildRemoved(Element child, int oldLogicalIndex)
334337
{
335338
child.Parent = null;
336339

337340
ChildRemoved?.Invoke(child, new ElementEventArgs(child));
338341

342+
VisualDiagnostics.OnChildRemoved(this, child, oldLogicalIndex);
343+
339344
OnDescendantRemoved(child);
340345
foreach (Element element in child.Descendants())
341346
OnDescendantRemoved(element);

Xamarin.Forms.Core/ElementEventArgs.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ namespace Xamarin.Forms
44
{
55
public class ElementEventArgs : EventArgs
66
{
7-
public ElementEventArgs(Element element)
8-
{
9-
if (element == null)
10-
throw new ArgumentNullException("element");
11-
12-
Element = element;
13-
}
7+
public ElementEventArgs(Element element) => Element = element ?? throw new ArgumentNullException(nameof(element));
148

159
public Element Element { get; private set; }
1610
}

Xamarin.Forms.Core/Items/ItemsView.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.ObjectModel;
55
using System.Windows.Input;
66
using Xamarin.Forms.Internals;
7+
using Xamarin.Forms.Xaml.Diagnostics;
78

89
namespace Xamarin.Forms
910
{
@@ -98,6 +99,8 @@ public void AddLogicalChild(Element element)
9899
_logicalChildren.Add(element);
99100

100101
element.Parent = this;
102+
103+
VisualDiagnostics.OnChildAdded(this, element);
101104
}
102105

103106
public void RemoveLogicalChild(Element element)
@@ -108,7 +111,12 @@ public void RemoveLogicalChild(Element element)
108111
}
109112

110113
element.Parent = null;
114+
if (!_logicalChildren.Contains(element))
115+
return;
116+
117+
var oldLogicalIndex = _logicalChildren.IndexOf(element);
111118
_logicalChildren.Remove(element);
119+
VisualDiagnostics.OnChildRemoved(this, element, oldLogicalIndex);
112120
}
113121

114122
#if NETSTANDARD1_0

Xamarin.Forms.Core/Layout.cs

Lines changed: 39 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,50 @@
88

99
namespace Xamarin.Forms
1010
{
11-
[ContentProperty("Children")]
11+
[ContentProperty(nameof(Children))]
1212
public abstract class Layout<T> : Layout, IViewContainer<T> where T : View
1313
{
1414
readonly ElementCollection<T> _children;
1515

16-
protected Layout()
17-
{
18-
_children = new ElementCollection<T>(InternalChildren);
19-
}
16+
protected Layout() => _children = new ElementCollection<T>(InternalChildren);
2017

21-
public new IList<T> Children
22-
{
23-
get { return _children; }
24-
}
25-
26-
protected virtual void OnAdded(T view)
27-
{
28-
}
18+
public new IList<T> Children => _children;
2919

3020
protected override void OnChildAdded(Element child)
3121
{
3222
base.OnChildAdded(child);
3323

34-
var typedChild = child as T;
35-
if (typedChild != null)
24+
if (child is T typedChild)
3625
OnAdded(typedChild);
3726
}
3827

39-
protected override void OnChildRemoved(Element child)
28+
[Obsolete("OnChildRemoved(Element) is obsolete as of version 4.8.0. Please use OnChildRemoved(Element, int) instead.")]
29+
protected override void OnChildRemoved(Element child) => OnChildRemoved(child, -1);
30+
31+
protected override void OnChildRemoved(Element child, int oldLogicalIndex)
4032
{
41-
base.OnChildRemoved(child);
33+
base.OnChildRemoved(child, oldLogicalIndex);
4234

43-
var typedChild = child as T;
44-
if (typedChild != null)
35+
if (child is T typedChild)
4536
OnRemoved(typedChild);
4637
}
4738

39+
protected virtual void OnAdded(T view)
40+
{
41+
}
42+
4843
protected virtual void OnRemoved(T view)
4944
{
5045
}
5146
}
5247

5348
public abstract class Layout : View, ILayout, ILayoutController, IPaddingElement
5449
{
55-
public static readonly BindableProperty IsClippedToBoundsProperty = BindableProperty.Create("IsClippedToBounds", typeof(bool), typeof(Layout), false);
50+
public static readonly BindableProperty IsClippedToBoundsProperty =
51+
BindableProperty.Create(nameof(IsClippedToBounds), typeof(bool), typeof(Layout), false);
5652

57-
public static readonly BindableProperty CascadeInputTransparentProperty = BindableProperty.Create(
58-
nameof(CascadeInputTransparent), typeof(bool), typeof(Layout), true);
53+
public static readonly BindableProperty CascadeInputTransparentProperty =
54+
BindableProperty.Create(nameof(CascadeInputTransparent), typeof(bool), typeof(Layout), true);
5955

6056
public static readonly BindableProperty PaddingProperty = PaddingElement.PaddingProperty;
6157

@@ -79,51 +75,36 @@ protected Layout()
7975

8076
public bool IsClippedToBounds
8177
{
82-
get { return (bool)GetValue(IsClippedToBoundsProperty); }
83-
set { SetValue(IsClippedToBoundsProperty, value); }
78+
get => (bool)GetValue(IsClippedToBoundsProperty);
79+
set => SetValue(IsClippedToBoundsProperty, value);
8480
}
8581

8682
public Thickness Padding
8783
{
88-
get { return (Thickness)GetValue(PaddingElement.PaddingProperty); }
89-
set { SetValue(PaddingElement.PaddingProperty, value); }
84+
get => (Thickness)GetValue(PaddingElement.PaddingProperty);
85+
set => SetValue(PaddingElement.PaddingProperty, value);
9086
}
9187

9288
public bool CascadeInputTransparent
9389
{
94-
get { return (bool)GetValue(CascadeInputTransparentProperty); }
95-
set { SetValue(CascadeInputTransparentProperty, value); }
90+
get => (bool)GetValue(CascadeInputTransparentProperty);
91+
set => SetValue(CascadeInputTransparentProperty, value);
9692
}
9793

98-
Thickness IPaddingElement.PaddingDefaultValueCreator()
99-
{
100-
return default(Thickness);
101-
}
94+
Thickness IPaddingElement.PaddingDefaultValueCreator() => default(Thickness);
10295

103-
void IPaddingElement.OnPaddingPropertyChanged(Thickness oldValue, Thickness newValue)
104-
{
105-
InvalidateLayout();
106-
}
96+
void IPaddingElement.OnPaddingPropertyChanged(Thickness oldValue, Thickness newValue) => InvalidateLayout();
10797

10898
internal ObservableCollection<Element> InternalChildren { get; } = new ObservableCollection<Element>();
10999

110-
internal override ReadOnlyCollection<Element> LogicalChildrenInternal
111-
{
112-
get { return _logicalChildren ?? (_logicalChildren = new ReadOnlyCollection<Element>(InternalChildren)); }
113-
}
100+
internal override ReadOnlyCollection<Element> LogicalChildrenInternal => _logicalChildren ?? (_logicalChildren = new ReadOnlyCollection<Element>(InternalChildren));
114101

115102
public event EventHandler LayoutChanged;
116103

117104
[EditorBrowsable(EditorBrowsableState.Never)]
118-
public IReadOnlyList<Element> Children
119-
{
120-
get { return InternalChildren; }
121-
}
105+
public IReadOnlyList<Element> Children => InternalChildren;
122106

123-
public void ForceLayout()
124-
{
125-
SizeAllocated(Width, Height);
126-
}
107+
public void ForceLayout() => SizeAllocated(Width, Height);
127108

128109
[Obsolete("OnSizeRequest is obsolete as of version 2.2.0. Please use OnMeasure instead.")]
129110
[EditorBrowsable(EditorBrowsableState.Never)]
@@ -136,13 +117,11 @@ public sealed override SizeRequest GetSizeRequest(double widthConstraint, double
136117

137118
public static void LayoutChildIntoBoundingRegion(VisualElement child, Rectangle region)
138119
{
139-
var parent = child.Parent as IFlowDirectionController;
140120
bool isRightToLeft = false;
141-
if (parent != null && (isRightToLeft = parent.ApplyEffectiveFlowDirectionToChildContainer && parent.EffectiveFlowDirection.IsRightToLeft()))
121+
if (child.Parent is IFlowDirectionController parent && (isRightToLeft = parent.ApplyEffectiveFlowDirectionToChildContainer && parent.EffectiveFlowDirection.IsRightToLeft()))
142122
region = new Rectangle(parent.Width - region.Right, region.Y, region.Width, region.Height);
143123

144-
var view = child as View;
145-
if (view == null)
124+
if (!(child is View view))
146125
{
147126
child.Layout(region);
148127
return;
@@ -224,15 +203,9 @@ protected override void OnSizeAllocated(double width, double height)
224203
UpdateChildrenLayout();
225204
}
226205

227-
protected virtual bool ShouldInvalidateOnChildAdded(View child)
228-
{
229-
return true;
230-
}
206+
protected virtual bool ShouldInvalidateOnChildAdded(View child) => true;
231207

232-
protected virtual bool ShouldInvalidateOnChildRemoved(View child)
233-
{
234-
return true;
235-
}
208+
protected virtual bool ShouldInvalidateOnChildRemoved(View child) => true;
236209

237210
protected void UpdateChildrenLayout()
238211
{
@@ -279,9 +252,8 @@ protected void UpdateChildrenLayout()
279252

280253
internal static void LayoutChildIntoBoundingRegion(View child, Rectangle region, SizeRequest childSizeRequest)
281254
{
282-
var parent = child.Parent as IFlowDirectionController;
283255
bool isRightToLeft = false;
284-
if (parent != null && (isRightToLeft = parent.ApplyEffectiveFlowDirectionToChildContainer && parent.EffectiveFlowDirection.IsRightToLeft()))
256+
if (child.Parent is IFlowDirectionController parent && (isRightToLeft = parent.ApplyEffectiveFlowDirectionToChildContainer && parent.EffectiveFlowDirection.IsRightToLeft()))
285257
region = new Rectangle(parent.Width - region.Right, region.Y, region.Width, region.Height);
286258

287259
if (region.Size != childSizeRequest.Request)
@@ -325,13 +297,11 @@ internal virtual void OnChildMeasureInvalidated(VisualElement child, Invalidatio
325297
int count = children.Count;
326298
for (var index = 0; index < count; index++)
327299
{
328-
var v = LogicalChildrenInternal[index] as VisualElement;
329-
if (v != null && v.IsVisible && (!v.IsPlatformEnabled || !v.IsNativeStateConsistent))
300+
if (LogicalChildrenInternal[index] is VisualElement v && v.IsVisible && (!v.IsPlatformEnabled || !v.IsNativeStateConsistent))
330301
return;
331302
}
332303

333-
var view = child as View;
334-
if (view != null)
304+
if (child is View view)
335305
{
336306
// we can ignore the request if we are either fully constrained or when the size request changes and we were already fully constrainted
337307
if ((trigger == InvalidationTrigger.MeasureChanged && view.Constraint == LayoutConstraint.Fixed) ||
@@ -433,7 +403,7 @@ void InternalChildrenOnCollectionChanged(object sender, NotifyCollectionChangedE
433403
if (v == null)
434404
continue;
435405

436-
OnInternalRemoved(v);
406+
OnInternalRemoved(v, e.OldStartingIndex + i);
437407
}
438408
}
439409

@@ -466,11 +436,11 @@ void OnInternalAdded(View view)
466436
view.MeasureInvalidated += OnChildMeasureInvalidated;
467437
}
468438

469-
void OnInternalRemoved(View view)
439+
void OnInternalRemoved(View view, int oldIndex)
470440
{
471441
view.MeasureInvalidated -= OnChildMeasureInvalidated;
472442

473-
OnChildRemoved(view);
443+
OnChildRemoved(view, oldIndex);
474444
if (ShouldInvalidateOnChildRemoved(view))
475445
InvalidateLayout();
476446
}

0 commit comments

Comments
 (0)