-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
View.cs
331 lines (278 loc) · 12 KB
/
View.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#nullable disable
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using Microsoft.Maui;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.HotReload;
namespace Microsoft.Maui.Controls
{
/// <summary>A visual element that is used to place layouts and controls on the screen.</summary>
/// <remarks>
/// This is the base class for <see cref="Layout"/> and most of the controls.
/// Because <see cref="View" /> ultimately inherits from <see cref="BindableObject" />, application developers can use the Model-View-ViewModel architecture, as well as XAML, to develop portable user interfaces.
/// </remarks>
public partial class View : VisualElement, IViewController, IGestureController, IGestureRecognizers, IView, IPropertyMapperView, IHotReloadableView, IControlsView
{
protected internal IGestureController GestureController => this;
/// <summary>Bindable property for <see cref="VerticalOptions"/>.</summary>
public static readonly BindableProperty VerticalOptionsProperty =
BindableProperty.Create(nameof(VerticalOptions), typeof(LayoutOptions), typeof(View), LayoutOptions.Fill,
propertyChanged: (bindable, oldvalue, newvalue) =>
((View)bindable).InvalidateMeasureInternal(InvalidationTrigger.VerticalOptionsChanged));
/// <summary>Bindable property for <see cref="HorizontalOptions"/>.</summary>
public static readonly BindableProperty HorizontalOptionsProperty =
BindableProperty.Create(nameof(HorizontalOptions), typeof(LayoutOptions), typeof(View), LayoutOptions.Fill,
propertyChanged: (bindable, oldvalue, newvalue) =>
((View)bindable).InvalidateMeasureInternal(InvalidationTrigger.HorizontalOptionsChanged));
/// <summary>Bindable property for <see cref="Margin"/>.</summary>
public static readonly BindableProperty MarginProperty =
BindableProperty.Create(nameof(Margin), typeof(Thickness), typeof(View), default(Thickness),
propertyChanged: MarginPropertyChanged);
internal static readonly BindableProperty MarginLeftProperty =
BindableProperty.Create("MarginLeft", typeof(double), typeof(View), default(double),
propertyChanged: OnMarginLeftPropertyChanged);
static void OnMarginLeftPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var margin = (Thickness)bindable.GetValue(MarginProperty);
margin.Left = (double)newValue;
bindable.SetValue(MarginProperty, margin);
}
internal static readonly BindableProperty MarginTopProperty =
BindableProperty.Create("MarginTop", typeof(double), typeof(View), default(double),
propertyChanged: OnMarginTopPropertyChanged);
static void OnMarginTopPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var margin = (Thickness)bindable.GetValue(MarginProperty);
margin.Top = (double)newValue;
bindable.SetValue(MarginProperty, margin);
}
internal static readonly BindableProperty MarginRightProperty =
BindableProperty.Create("MarginRight", typeof(double), typeof(View), default(double),
propertyChanged: OnMarginRightPropertyChanged);
static void OnMarginRightPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var margin = (Thickness)bindable.GetValue(MarginProperty);
margin.Right = (double)newValue;
bindable.SetValue(MarginProperty, margin);
}
internal static readonly BindableProperty MarginBottomProperty =
BindableProperty.Create("MarginBottom", typeof(double), typeof(View), default(double),
propertyChanged: OnMarginBottomPropertyChanged);
static void OnMarginBottomPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var margin = (Thickness)bindable.GetValue(MarginProperty);
margin.Bottom = (double)newValue;
bindable.SetValue(MarginProperty, margin);
}
readonly ObservableCollection<IGestureRecognizer> _gestureRecognizers = new ObservableCollection<IGestureRecognizer>();
PointerGestureRecognizer _recognizerForPointerOverState;
/// <summary>
/// Initializes a new instance of the <see cref="View"/> class
/// </summary>
/// <remarks>It is unlikely that an application developer would want to create an instance of <see cref="View"/> on their own.</remarks>
protected internal View()
{
_gestureManager = new GestureManager(this);
_gestureRecognizers.CollectionChanged += (sender, args) =>
{
void AddItems(IEnumerable<IElementDefinition> elements)
{
foreach (IElementDefinition item in elements)
{
ValidateGesture(item as IGestureRecognizer);
item.Parent = this;
GestureController.CompositeGestureRecognizers.Add(item as IGestureRecognizer);
}
}
void RemoveItems(IEnumerable<IElementDefinition> elements)
{
foreach (IElementDefinition item in elements)
{
item.Parent = null;
GestureController.CompositeGestureRecognizers.Remove(item as IGestureRecognizer);
}
}
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
AddItems(args.NewItems.OfType<IElementDefinition>());
break;
case NotifyCollectionChangedAction.Remove:
RemoveItems(args.OldItems.OfType<IElementDefinition>());
break;
case NotifyCollectionChangedAction.Replace:
AddItems(args.NewItems.OfType<IElementDefinition>());
RemoveItems(args.OldItems.OfType<IElementDefinition>());
break;
case NotifyCollectionChangedAction.Reset:
List<IElementDefinition> remove = new List<IElementDefinition>();
List<IElementDefinition> add = new List<IElementDefinition>();
foreach (IElementDefinition item in _gestureRecognizers.OfType<IElementDefinition>())
{
if (!_gestureRecognizers.Contains((IGestureRecognizer)item))
add.Add(item);
item.Parent = this;
}
foreach (IElementDefinition item in GestureController.CompositeGestureRecognizers.OfType<IElementDefinition>())
{
if (item == _recognizerForPointerOverState)
continue;
if (_gestureRecognizers.Contains((IGestureRecognizer)item))
item.Parent = this;
else
remove.Add(item);
}
AddItems(add);
RemoveItems(remove);
break;
}
};
}
/// <summary>The collection of gesture recognizers associated with this view.</summary>
/// <remarks>
/// Adding items to this collection will associate gesture events with this element.
/// It is not recommended to add gesture recognizers for gestures that elements already natively support.
/// <para>
/// For example, adding a <see cref="TapGestureRecognizer"/> to a <see cref="Button"/> may lead to unexpected results.
/// </para>
/// </remarks>
public IList<IGestureRecognizer> GestureRecognizers
{
get { return _gestureRecognizers; }
}
ObservableCollection<IGestureRecognizer> _compositeGestureRecognizers;
IList<IGestureRecognizer> IGestureController.CompositeGestureRecognizers
{
get
{
if (_compositeGestureRecognizers == null)
{
_compositeGestureRecognizers = new ObservableCollection<IGestureRecognizer>();
CheckPointerOver();
}
return _compositeGestureRecognizers;
}
}
protected internal override void ChangeVisualState()
{
CheckPointerOver();
if (_recognizerForPointerOverState == null && IsPointerOver)
SetPointerOver(false, false);
base.ChangeVisualState();
}
void CheckPointerOver() =>
PointerGestureRecognizer
.SetupForPointerOverVSM(this, (result) => SetPointerOver(result), ref _recognizerForPointerOverState);
/// <summary>
/// Gets the child elements that are visually beneath the specified <paramref name="point" />.
/// </summary>
/// <param name="point">The point under which to search for child elements.</param>
/// <returns>All child elements visually beneath <paramref name="point"/>.</returns>
public virtual IList<GestureElement> GetChildElements(Point point)
{
return null;
}
/// <summary>
/// Gets or sets the <see cref="LayoutOptions" /> that define how the element gets laid out in a layout cycle. This is a bindable property.
/// </summary>
/// <remarks>
/// Assigning <see cref="HorizontalOptions"/> modifies how the element is laid out when there is excess space available along the X axis from the parent layout.
/// If multiple elements inside a layout are set to expand, the extra space is distributed proportionally.
/// </remarks>
public LayoutOptions HorizontalOptions
{
get { return (LayoutOptions)GetValue(HorizontalOptionsProperty); }
set { SetValue(HorizontalOptionsProperty, value); }
}
/// <summary>
/// Gets or set the margin for the view.
/// </summary>
public Thickness Margin
{
get { return (Thickness)GetValue(MarginProperty); }
set { SetValue(MarginProperty, value); }
}
/// <summary>
/// Gets or sets the <see cref="LayoutOptions" /> that define how the element gets laid out in a layout cycle. This is a bindable property.
/// </summary>
/// <remarks>
/// Assigning <see cref="VerticalOptions"/> modifies how the element is laid out when there is excess space available along the Y axis from the parent layout.
/// If multiple elements inside a layout are set to expand, the extra space is distributed proportionally.
/// </remarks>
public LayoutOptions VerticalOptions
{
get { return (LayoutOptions)GetValue(VerticalOptionsProperty); }
set { SetValue(VerticalOptionsProperty, value); }
}
/// <summary>
/// Invoked whenever the binding context of the <see cref="View" /> changes.
/// </summary>
/// <remarks>This method can be overridden to add class handling for this event. Overrides must call the base method.</remarks>
protected override void OnBindingContextChanged()
{
this.PropagateBindingContext(GestureRecognizers);
base.OnBindingContextChanged();
}
static void MarginPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
((View)bindable).InvalidateMeasureInternal(InvalidationTrigger.MarginChanged);
}
void ValidateGesture(IGestureRecognizer gesture)
{
if (gesture == null)
return;
if (gesture is PinchGestureRecognizer && _gestureRecognizers.GetGesturesFor<PinchGestureRecognizer>().Count() > 1)
throw new InvalidOperationException($"Only one {nameof(PinchGestureRecognizer)} per view is allowed");
}
#nullable enable
/// <inheritdoc/>
Thickness IView.Margin => Margin;
partial void HandlerChangedPartial();
GestureManager _gestureManager;
private protected override void OnHandlerChangedCore()
{
base.OnHandlerChangedCore();
HandlerChangedPartial();
}
/// <summary>
/// Represents the view's internal <see cref="PropertyMapper"/>.
/// </summary>
/// <remarks>Contains the unique property overrides that are used by <see cref="IPropertyMapperView.GetPropertyMapperOverrides"/>.</remarks>
protected PropertyMapper propertyMapper;
internal protected PropertyMapper<T> GetRendererOverrides<T>() where T : IView =>
(PropertyMapper<T>)(propertyMapper as PropertyMapper<T> ?? (propertyMapper = new PropertyMapper<T>()));
PropertyMapper IPropertyMapperView.GetPropertyMapperOverrides() => propertyMapper;
/// <inheritdoc/>
Primitives.LayoutAlignment IView.HorizontalLayoutAlignment => HorizontalOptions.ToCore();
/// <inheritdoc/>
Primitives.LayoutAlignment IView.VerticalLayoutAlignment => VerticalOptions.ToCore();
#region HotReload
IView IReplaceableView.ReplacedView =>
MauiHotReloadHelper.GetReplacedView(this) ?? this;
IReloadHandler IHotReloadableView.ReloadHandler { get; set; }
void IHotReloadableView.TransferState(IView newView)
{
//TODO: LEt you hot reload the the ViewModel
if (newView is View v)
v.BindingContext = BindingContext;
}
void IHotReloadableView.Reload()
{
Dispatcher.Dispatch(() =>
{
this.CheckHandlers();
//Handler = null;
var reloadHandler = ((IHotReloadableView)this).ReloadHandler;
reloadHandler?.Reload();
//TODO: if reload handler is null, Do a manual reload?
});
}
#endregion
#nullable disable
}
}