forked from microsoft/react-native-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactFlipViewManager.cs
166 lines (144 loc) · 4.73 KB
/
ReactFlipViewManager.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Newtonsoft.Json.Linq;
using ReactNative.UIManager;
using ReactNative.UIManager.Annotations;
using ReactNative.UIManager.Events;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
namespace ReactNative.Views.Flip
{
class ReactFlipViewManager : ViewParentManager<FlipView>
{
private const int SetPage = 1;
public override string Name
{
get
{
return "WindowsFlipView";
}
}
public override JObject ViewCommandsMap
{
get
{
return new JObject
{
{ "setPage", SetPage },
};
}
}
public override JObject CustomDirectEventTypeConstants
{
get
{
return new JObject
{
{
"topSelectionChange",
new JObject
{
{ "registrationName", "onSelectionChange" },
}
},
};
}
}
[ReactProp("alwaysAnimate", DefaultBoolean = true)]
public void SetAlwaysAnimate(FlipView view, bool alwaysAnimate)
{
view.UseTouchAnimationsForAllNavigation = alwaysAnimate;
}
[ReactProp("backgroundColor")]
public void SetBackgroundColor(FlipView view, uint? color)
{
view.Background = color.HasValue
? new SolidColorBrush(ColorHelpers.Parse(color.Value))
: null;
}
public override void AddView(FlipView parent, DependencyObject child, int index)
{
parent.Items.Insert(index, child);
}
public override DependencyObject GetChildAt(FlipView parent, int index)
{
return (DependencyObject)parent.Items[index];
}
public override int GetChildCount(FlipView parent)
{
return parent.Items.Count;
}
public override void RemoveAllChildren(FlipView parent)
{
parent.Items.Clear();
}
public override void RemoveChildAt(FlipView parent, int index)
{
parent.Items.RemoveAt(index);
}
public override void OnDropViewInstance(ThemedReactContext reactContext, FlipView view)
{
base.OnDropViewInstance(reactContext, view);
view.SelectionChanged -= OnSelectionChanged;
}
public override async void ReceiveCommand(FlipView view, int commandId, JArray args)
{
switch (commandId)
{
case SetPage:
// TODO: (#328) Fix issue with `setPage` on mount
await Task.Yield();
view.SelectionChanged -= OnSelectionChanged;
view.SelectedIndex = args.First.Value<int>();
view.SelectionChanged += OnSelectionChanged;
break;
}
}
protected override FlipView CreateViewInstance(ThemedReactContext reactContext)
{
return new FlipView();
}
protected override void AddEventEmitters(ThemedReactContext reactContext, FlipView view)
{
base.AddEventEmitters(reactContext, view);
view.SelectionChanged += OnSelectionChanged;
}
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var flipView = (FlipView)sender;
flipView.GetReactContext()
.GetNativeModule<UIManagerModule>()
.EventDispatcher
.DispatchEvent(
new SelectionChangedEvent(
flipView.GetTag(),
flipView.SelectedIndex));
}
class SelectionChangedEvent : Event
{
private readonly int _position;
public SelectionChangedEvent(int viewTag, int position)
: base(viewTag)
{
_position = position;
}
public override string EventName
{
get
{
return "topSelectionChange";
}
}
public override void Dispatch(RCTEventEmitter eventEmitter)
{
var eventData = new JObject
{
{ "position", _position },
};
eventEmitter.receiveEvent(ViewTag, EventName, eventData);
}
}
}
}