-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathReactViewManager.cs
255 lines (236 loc) · 9.57 KB
/
ReactViewManager.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Portions derived from React Native:
// Copyright (c) 2015-present, Facebook, Inc.
// Licensed under the MIT License.
using ReactNative.Reflection;
using ReactNative.UIManager;
using ReactNative.UIManager.Annotations;
using Newtonsoft.Json.Linq;
#if WINDOWS_UWP
using ReactNative.Accessibility;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
#else
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
#endif
namespace ReactNative.Views.View
{
/// <summary>
/// View manager for React view instances.
/// </summary>
public class ReactViewManager : ViewParentManager<BorderedCanvas>
{
/// <summary>
/// The name of this view manager. This will be the name used to
/// reference this view manager from JavaScript.
/// </summary>
public override string Name => ViewProps.ViewClassName;
/// <summary>
/// Creates a new view instance of type <see cref="Canvas"/>.
/// </summary>
/// <param name="reactContext">The React context.</param>
/// <returns>The view instance.</returns>
protected override BorderedCanvas CreateViewInstance(ThemedReactContext reactContext)
{
return new BorderedCanvas();
}
/// <summary>
/// Sets whether the view is collapsible.
/// </summary>
/// <param name="view">The view instance.</param>
/// <param name="collapsible">The flag.</param>
[ReactProp(ViewProps.Collapsible)]
public void SetCollapsible(BorderedCanvas view, bool collapsible)
{
// no-op: it's here only so that "collapsable" prop is exported to JS. The value is actually
// handled in NativeViewHierarchyOptimizer
}
/// <summary>
/// Sets whether or not the view is an accessibility element.
/// </summary>
/// <param name="view">The view.</param>
/// <param name="accessible">A flag indicating whether or not the view is an accessibility element.</param>
[ReactProp("accessible")]
public void SetAccessible(BorderedCanvas view, bool accessible)
{
// TODO: #557 Provide implementation for View's accessible prop
// We need to have this stub for this prop so that Views which
// specify the accessible prop aren't considered to be layout-only.
// The proper implementation is still to be determined.
}
/// <summary>
/// Set the pointer events handling mode for the view.
/// </summary>
/// <param name="view">The view.</param>
/// <param name="pointerEventsValue">The pointerEvents mode.</param>
[ReactProp("pointerEvents")]
public void SetPointerEvents(BorderedCanvas view, string pointerEventsValue)
{
var pointerEvents = EnumHelpers.ParseNullable<PointerEvents>(pointerEventsValue) ?? PointerEvents.Auto;
view.SetPointerEvents(pointerEvents);
}
#if WINDOWS_UWP
/// <summary>
/// Set accessibility traits for the view.
/// </summary>
/// <param name="view">The view.</param>
/// <param name="accessibilityTraitsValue">Can be <see cref="JArray"/> of objects or a single object.
/// String representation of the object(s) is parsed as <see cref="AccessibilityTrait"/>.</param>
[ReactProp("accessibilityTraits")]
public void SetAccessibilityTraits(BorderedCanvas view, object accessibilityTraitsValue)
{
AccessibilityHelper.SetAccessibilityTraits(view, accessibilityTraitsValue);
}
/// <summary>
/// Sets <see cref="ImportantForAccessibility"/> for the BorderedCanvas.
/// </summary>
/// <param name="view">The view.</param>
/// <param name="importantForAccessibilityValue">The string to be parsed as <see cref="ImportantForAccessibility"/>.</param>
[ReactProp("importantForAccessibility")]
public void SetImportantForAccessibility(BorderedCanvas view, string importantForAccessibilityValue)
{
var importantForAccessibility = EnumHelpers.ParseNullable<ImportantForAccessibility>(importantForAccessibilityValue)
?? ImportantForAccessibility.Auto;
AccessibilityHelper.SetImportantForAccessibility(view, importantForAccessibility);
}
#endif
/// <summary>
/// Enum values correspond to positions of prop names in ReactPropGroup attribute
/// applied to <see cref="SetBorderRadius(BorderedCanvas, int, double)"/>
/// </summary>
private enum Radius
{
All,
TopLeft,
TopRight,
BottomLeft,
BottomRight,
}
/// <summary>
/// Sets the border radius of the view.
/// </summary>
/// <param name="view">The view panel.</param>
/// <param name="index">The prop index.</param>
/// <param name="radius">The border radius value.</param>
[ReactPropGroup(
ViewProps.BorderRadius,
ViewProps.BorderTopLeftRadius,
ViewProps.BorderTopRightRadius,
ViewProps.BorderBottomLeftRadius,
ViewProps.BorderBottomRightRadius)]
public void SetBorderRadius(BorderedCanvas view, int index, double radius)
{
var cornerRadius = view.CornerRadius;
switch ((Radius)index)
{
case Radius.All:
cornerRadius = new CornerRadius(radius);
break;
case Radius.TopLeft:
cornerRadius.TopLeft = radius;
break;
case Radius.TopRight:
cornerRadius.TopRight = radius;
break;
case Radius.BottomLeft:
cornerRadius.BottomLeft = radius;
break;
case Radius.BottomRight:
cornerRadius.BottomRight = radius;
break;
}
view.CornerRadius = cornerRadius;
}
/// <summary>
/// Sets the background color of the view.
/// </summary>
/// <param name="view">The view panel.</param>
/// <param name="color">The masked color value.</param>
[ReactProp(
ViewProps.BackgroundColor,
CustomType = "Color",
DefaultUInt32 = ColorHelpers.Transparent)]
public void SetBackgroundColor(BorderedCanvas view, uint color)
{
view.Background = new SolidColorBrush(ColorHelpers.Parse(color));
}
/// <summary>
/// Set the border color of the view.
/// </summary>
/// <param name="view">The view panel.</param>
/// <param name="color">The color hex code.</param>
[ReactProp("borderColor", CustomType = "Color")]
public void SetBorderColor(BorderedCanvas view, uint? color)
{
view.BorderBrush = color.HasValue
? new SolidColorBrush(ColorHelpers.Parse(color.Value))
: null;
}
/// <summary>
/// Sets the border thickness of the view.
/// </summary>
/// <param name="view">The view panel.</param>
/// <param name="index">The prop index.</param>
/// <param name="width">The border width in pixels.</param>
[ReactPropGroup(
ViewProps.BorderWidth,
ViewProps.BorderLeftWidth,
ViewProps.BorderRightWidth,
ViewProps.BorderTopWidth,
ViewProps.BorderBottomWidth)]
public void SetBorderWidth(BorderedCanvas view, int index, double width)
{
view.SetBorderWidth(ViewProps.BorderSpacingTypes[index], width);
}
/// <summary>
/// Adds a child at the given index.
/// </summary>
/// <param name="parent">The parent view.</param>
/// <param name="child">The child view.</param>
/// <param name="index">The index.</param>
public override void AddView(BorderedCanvas parent, DependencyObject child, int index)
{
parent.Children.Insert(index, child.As<UIElement>());
}
/// <summary>
/// Gets the child at the given index.
/// </summary>
/// <param name="parent">The parent view.</param>
/// <param name="index">The index.</param>
/// <returns>The child view.</returns>
public override DependencyObject GetChildAt(BorderedCanvas parent, int index)
{
return parent.Children[index];
}
/// <summary>
/// Gets the number of children in the view parent.
/// </summary>
/// <param name="parent">The view parent.</param>
/// <returns>The number of children.</returns>
public override int GetChildCount(BorderedCanvas parent)
{
return parent.Children.Count;
}
/// <summary>
/// Removes all children from the view parent.
/// </summary>
/// <param name="parent">The view parent.</param>
public override void RemoveAllChildren(BorderedCanvas parent)
{
parent.Children.Clear();
}
/// <summary>
/// Removes the child at the given index.
/// </summary>
/// <param name="parent">The view parent.</param>
/// <param name="index">The index.</param>
public override void RemoveChildAt(BorderedCanvas parent, int index)
{
parent.Children.RemoveAt(index);
}
}
}