/
Switch.cs
357 lines (318 loc) · 11.5 KB
/
Switch.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright(c) 2021 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.ComponentModel;
using System.Diagnostics;
using Tizen.NUI.BaseComponents;
using Tizen.NUI.Components.Extension;
namespace Tizen.NUI.Components
{
/// <summary>
/// Switch is a kind of <see cref="Button"/> component that uses icon part as a toggle shape.
/// The icon part consists of track and thumb.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public class Switch : Button
{
private ImageView thumb = null;
static Switch() { }
/// <summary>
/// Creates a new instance of a Switch.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public Switch() : base()
{
}
/// <summary>
/// Creates a new instance of a Switch with style.
/// </summary>
/// <param name="style">Create Switch by special style defined in UX.</param>
/// <since_tizen> 8 </since_tizen>
public Switch(string style) : base(style)
{
}
/// <summary>
/// Creates a new instance of a Switch with style.
/// </summary>
/// <param name="switchStyle">Create Switch by style customized by user.</param>
/// <since_tizen> 8 </since_tizen>
public Switch(SwitchStyle switchStyle) : base(switchStyle)
{
}
/// <summary>
/// Initialize AT-SPI object.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public override void OnInitialize()
{
base.OnInitialize();
SetAccessibilityConstructor(Role.ToggleButton);
IsSelectable = true;
#if PROFILE_MOBILE
Feedback = true;
#endif
}
/// <summary>
/// Informs AT-SPI bridge about the set of AT-SPI states associated with this object.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected override AccessibilityStates AccessibilityCalculateStates(ulong states)
{
var accessibilityStates = base.AccessibilityCalculateStates(states);
FlagSetter(ref accessibilityStates, AccessibilityStates.Checked, this.IsSelected);
return accessibilityStates;
}
/// <summary>
/// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
/// </summary>
/// <since_tizen> 6 </since_tizen>
[Obsolete("Deprecated in API8; Will be removed in API10. Please use SelectedChanged event instead.")]
public event EventHandler<SelectEventArgs> SelectedEvent;
/// <summary>
/// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public event EventHandler<SelectedChangedEventArgs> SelectedChanged;
/// <summary>
/// Return currently applied style.
/// </summary>
/// <remarks>
/// Modifying contents in style may cause unexpected behaviour.
/// </remarks>
/// <since_tizen> 8 </since_tizen>
public new SwitchStyle Style => (SwitchStyle)(ViewStyle as SwitchStyle)?.Clone();
/// <summary>
/// Apply style to switch.
/// </summary>
/// <param name="viewStyle">The style to apply.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public override void ApplyStyle(ViewStyle viewStyle)
{
if (viewStyle is SwitchStyle switchStyle)
{
if (Extension is SwitchExtension extension)
{
Icon.Unparent();
thumb.Unparent();
Icon = extension.OnCreateTrack(this, Icon);
thumb = extension.OnCreateThumb(this, thumb);
Icon.Add(thumb);
LayoutItems();
}
if (switchStyle.Track != null)
{
Track.ApplyStyle(switchStyle.Track);
}
if (switchStyle.Thumb != null)
{
Thumb.ApplyStyle(switchStyle.Thumb);
}
}
base.ApplyStyle(viewStyle);
}
/// <summary>
/// Switch's track part.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public ImageView Track
{
get => Icon;
internal set
{
Icon = value;
}
}
/// <summary>
/// Switch's thumb part.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public ImageView Thumb
{
get => thumb;
internal set
{
thumb = value;
}
}
/// <summary>
/// Switch's track part image url selector.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public StringSelector SwitchBackgroundImageURLSelector
{
get => new StringSelector(Icon.ResourceUrlSelector);
set
{
Debug.Assert(Icon != null);
Icon.ResourceUrlSelector = value;
}
}
/// <summary>
/// Handler image's resource url in Switch.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public string SwitchHandlerImageURL
{
get
{
return Thumb.ResourceUrl;
}
set
{
Thumb.ResourceUrl = value;
}
}
/// <summary>
/// Handler image's resource url selector in Switch.
/// Getter returns copied selector value if exist, null otherwise.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public StringSelector SwitchHandlerImageURLSelector
{
get => new StringSelector(thumb.ResourceUrlSelector);
set
{
Debug.Assert(thumb != null);
thumb.ResourceUrlSelector = value;
}
}
/// <summary>
/// Handler image's size in Switch.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public Size SwitchHandlerImageSize
{
get
{
return Thumb.Size;
}
set
{
Thumb.Size = value;
}
}
/// <summary>
/// Dispose Switch and all children on it.
/// </summary>
/// <param name="type">Dispose type.</param>
/// <since_tizen> 6 </since_tizen>
protected override void Dispose(DisposeTypes type)
{
if (disposed) return;
if (type == DisposeTypes.Explicit)
{
Utility.Dispose(thumb);
}
base.Dispose(type);
}
/// <summary>
/// Called after a key event is received by the view that has had its focus set.
/// </summary>
/// <param name="key">The key event.</param>
/// <returns>True if the key event should be consumed.</returns>
/// <since_tizen> 8 </since_tizen>
public override bool OnKey(Key key)
{
return base.OnKey(key);
}
/// <summary>
/// Called after a touch event is received by the owning view.<br />
/// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
/// </summary>
/// <param name="touch">The touch event.</param>
/// <returns>True if the event should be consumed.</returns>
/// <since_tizen> 8 </since_tizen>
[Obsolete("Deprecated in API8; Will be removed in API10. Please use OnClicked instead.")]
#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member, It will be removed in API10
public override bool OnTouch(Touch touch)
#pragma warning restore CS0809 // Obsolete member overrides non-obsolete member, It will be removed in API10
{
return base.OnTouch(touch);
}
/// <summary>
/// Get Switch style.
/// </summary>
/// <returns>The default switch style.</returns>
/// <since_tizen> 8 </since_tizen>
protected override ViewStyle CreateViewStyle()
{
return new SwitchStyle();
}
/// <inheritdoc/>
[EditorBrowsable(EditorBrowsableState.Never)]
protected override ImageView CreateIcon()
{
var icon = new ImageView()
{
AccessibilityHighlightable = false,
EnableControlStatePropagation = true
};
thumb = new ImageView();
icon.Add(thumb);
return icon;
}
/// <inheritdoc/>
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
{
base.OnControlStateChanged(controlStateChangedInfo);
if (!IsSelectable)
{
return;
}
bool previousSelected = controlStateChangedInfo.PreviousState.Contains(ControlState.Selected);
if (previousSelected != IsSelected)
{
OnSelect();
}
}
private void OnSelect()
{
if (Accessibility.Accessibility.Enabled && IsHighlighted)
{
EmitAccessibilityStatesChangedEvent(AccessibilityStates.Checked, IsSelected);
}
((SwitchExtension)Extension)?.OnSelectedChanged(this);
if (SelectedEvent != null)
{
SelectEventArgs eventArgs = new SelectEventArgs();
eventArgs.IsSelected = IsSelected;
SelectedEvent(this, eventArgs);
}
if (SelectedChanged != null)
{
SelectedChangedEventArgs eventArgs = new SelectedChangedEventArgs();
eventArgs.IsSelected = IsSelected;
SelectedChanged(this, eventArgs);
}
}
/// <summary>
/// SelectEventArgs is a class to record item selected arguments which will sent to user.
/// </summary>
/// <since_tizen> 6 </since_tizen>
/// It will be removed in API10
[Obsolete("Deprecated in API8; Will be removed in API10. Please use SelectedChangedEventArgs instead.")]
[global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:Do not declare visible instance fields")]
[global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
public class SelectEventArgs : EventArgs
{
/// <summary> Select state of Switch </summary>
/// <since_tizen> 6 </since_tizen>
public bool IsSelected;
}
}
}