2 * Copyright(c) 2019 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.ComponentModel;
20 using Tizen.NUI.Binding;
21 using Tizen.NUI.Components;
23 namespace Tizen.NUI.BaseComponents
26 /// Selector class, which is related by Control State, it is base class for other Selector.
28 /// <typeparam name="T">The property type of the selector. if it's reference type, it should be of type <see cref="ICloneable"/> that implement deep copy in <see cref="ICloneable.Clone"/>.</typeparam>
29 /// <since_tizen> 6 </since_tizen>
30 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
31 [EditorBrowsable(EditorBrowsableState.Never)]
32 public class Selector<T>
34 private readonly bool cloneable = typeof(ICloneable).IsAssignableFrom(typeof(T));
37 /// The list for adding state-value pair.
39 [EditorBrowsable(EditorBrowsableState.Never)]
40 public IList<StateValuePair<T>> StateValueList { get; set; } = new List<StateValuePair<T>>();
43 /// Adds the specified state and value.
45 /// <param name="state">The state.</param>
46 /// <param name="value">The value associated with state.</param>
47 [EditorBrowsable(EditorBrowsableState.Never)]
48 public void Add(ControlState state, T value) => Add(new StateValuePair<T>(state, value));
51 /// Adds the specified state and value.
53 /// <param name="stateValuePair"></param>
54 [EditorBrowsable(EditorBrowsableState.Never)]
55 public void Add(StateValuePair<T> stateValuePair)
57 // To prevent a state from having multiple values, remove existing state-value pair.
58 int index = ((List<StateValuePair<T>>)StateValueList).FindIndex(x => x.State == stateValuePair.State);
60 StateValueList.RemoveAt(index);
62 StateValueList.Add(stateValuePair);
65 /// <since_tizen> 6 </since_tizen>
66 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
67 public static implicit operator Selector<T>(T value)
69 return new Selector<T>(value);
72 /// Default Contructor
73 [EditorBrowsable(EditorBrowsableState.Never)]
79 [EditorBrowsable(EditorBrowsableState.Never)]
80 public Selector(T value) : this()
82 All = cloneable ? (T)((ICloneable)value)?.Clone() : value;
86 [EditorBrowsable(EditorBrowsableState.Never)]
87 public Selector(Selector<T> value) : this()
95 /// <since_tizen> 6 </since_tizen>
96 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
97 [EditorBrowsable(EditorBrowsableState.Never)]
106 /// <since_tizen> 6 </since_tizen>
107 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
108 [EditorBrowsable(EditorBrowsableState.Never)]
111 get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.Normal).Value;
112 set => Add(ControlState.Normal, value);
117 /// <since_tizen> 6 </since_tizen>
118 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
119 [EditorBrowsable(EditorBrowsableState.Never)]
123 get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.Pressed).Value;
124 set => Add(ControlState.Pressed, value);
129 /// <since_tizen> 6 </since_tizen>
130 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
131 [EditorBrowsable(EditorBrowsableState.Never)]
134 get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.Focused).Value;
135 set => Add(ControlState.Focused, value);
140 /// <since_tizen> 6 </since_tizen>
141 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
142 [EditorBrowsable(EditorBrowsableState.Never)]
145 get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.Selected).Value;
146 set => Add(ControlState.Selected, value);
151 /// <since_tizen> 6 </since_tizen>
152 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
153 [EditorBrowsable(EditorBrowsableState.Never)]
157 get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.Disabled).Value;
158 set => Add(ControlState.Disabled, value);
161 /// DisabledFocused State.
163 /// <since_tizen> 6 </since_tizen>
164 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
165 [EditorBrowsable(EditorBrowsableState.Never)]
166 public T DisabledFocused
168 get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.DisabledFocused).Value;
169 set => Add(ControlState.DisabledFocused, value);
172 /// SelectedFocused State.
174 /// <since_tizen> 6 </since_tizen>
175 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
176 public T SelectedFocused
178 get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.SelectedFocused).Value;
179 set => Add(ControlState.SelectedFocused, value);
182 /// DisabledSelected State.
184 /// <since_tizen> 6 </since_tizen>
185 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
186 [EditorBrowsable(EditorBrowsableState.Never)]
187 public T DisabledSelected
190 get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.DisabledSelected).Value;
191 set => Add(ControlState.DisabledSelected, value);
197 /// <since_tizen> 6 </since_tizen>
198 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
199 [EditorBrowsable(EditorBrowsableState.Never)]
202 get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.Other).Value;
203 set => Add(ControlState.Other, value);
207 /// Gets the number of elements.
209 [EditorBrowsable(EditorBrowsableState.Never)]
210 public int Count => StateValueList.Count;
213 /// Get value by State.
215 /// <exception cref="ArgumentNullException"> Thrown when state is null. </exception>
216 /// <since_tizen> 6 </since_tizen>
217 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
218 /// <returns>True if the selector has a given state value, false otherwise.</returns>
219 [EditorBrowsable(EditorBrowsableState.Never)]
220 public bool GetValue(ControlState state, out T result)
231 int index = ((List<StateValuePair<T>>)StateValueList).FindIndex(x => x.State == state);
234 result = StateValueList[index].Value;
240 throw new ArgumentNullException(nameof(state));
242 if (state.IsCombined)
244 index = ((List<StateValuePair<T>>)StateValueList).FindIndex(x => state.Contains(x.State));
247 result = StateValueList[index].Value;
252 index = ((List<StateValuePair<T>>)StateValueList).FindIndex(x => x.State == ControlState.Other);
255 result = StateValueList[index].Value;
263 /// Removes all elements.
265 [EditorBrowsable(EditorBrowsableState.Never)]
269 StateValueList.Clear();
273 [EditorBrowsable(EditorBrowsableState.Never)]
274 public override string ToString()
276 string result = $"[All, {All}]";
278 foreach (var item in StateValueList)
280 result += $", {item}";
288 /// If type T implements ICloneable, it calls Clone() method to clone values, otherwise use operator=.
290 /// <since_tizen> 6 </since_tizen>
291 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
292 [EditorBrowsable(EditorBrowsableState.Never)]
293 public Selector<T> Clone()
295 var cloned = new Selector<T>();
301 /// Copy values from other selector.
303 /// <exception cref="ArgumentNullException"> Thrown when other is null. </exception>
304 [EditorBrowsable(EditorBrowsableState.Never)]
305 public void Clone(Selector<T> other)
309 throw new ArgumentNullException(nameof(other));
314 All = (T)((ICloneable)other.All)?.Clone();
315 StateValueList = ((List<StateValuePair<T>>)other.StateValueList).ConvertAll(m => new StateValuePair<T>(m.State, (T)((ICloneable)m.Value)?.Clone()));
320 StateValueList = ((List<StateValuePair<T>>)other.StateValueList).ConvertAll(m => m);
324 internal bool HasMultiValue()
326 return StateValueList.Count > 1;
332 /// This will be attached to a View to detect ControlState change.
334 [EditorBrowsable(EditorBrowsableState.Never)]
335 public class TriggerableSelector<T>
338 /// Create an TriggerableSelector.
340 [EditorBrowsable(EditorBrowsableState.Never)]
341 public delegate T ValueGetter(View view);
343 private readonly BindableProperty targetBindableProperty;
344 private readonly ValueGetter propertyGetter;
345 private bool dirty = true;
346 private Selector<T> selector;
349 /// Create an TriggerableSelector.
351 /// <param name="targetBindableProperty">The TriggerableSelector will change this bindable property value when the view's ControlState has changed.</param>
352 /// <param name="propertyGetter">It is optional value in case the target bindable property getter is not proper to use.</param>
353 [EditorBrowsable(EditorBrowsableState.Never)]
354 public TriggerableSelector(BindableProperty targetBindableProperty, ValueGetter propertyGetter = null)
356 this.targetBindableProperty = targetBindableProperty;
357 this.propertyGetter = propertyGetter;
361 /// Return the containing selector. It can be null.
363 /// <exception cref="ArgumentNullException"> Thrown when view is null. </exception>
364 [EditorBrowsable(EditorBrowsableState.Never)]
365 public Selector<T> Get(View view)
367 if (!dirty) return selector;
370 throw new ArgumentNullException(nameof(view));
375 if (propertyGetter != null)
377 value = propertyGetter(view);
381 value = (T)view.GetValue(targetBindableProperty);
384 Selector<T> converted = value == null ? null : new Selector<T>(value);
385 Update(view, converted);
391 /// Update containing selector from the other selector.
393 /// <param name="view">The View that is affected by this TriggerableSelector.</param>
394 /// <param name="otherSelector">The copy target.</param>
395 /// <param name="updateView">Whether it updates the target view after update the selector or not.</param>
396 /// <exception cref="ArgumentNullException"> Thrown when view is null. </exception>
397 [EditorBrowsable(EditorBrowsableState.Never)]
398 public void Update(View view, Selector<T> otherSelector, bool updateView = false)
404 throw new ArgumentNullException(nameof(view));
407 if (otherSelector == null)
412 selector = otherSelector.Clone();
414 if (otherSelector.HasMultiValue())
416 view.ControlStateChangeEventInternal += OnViewControlState;
419 if (updateView && otherSelector.GetValue(view.ControlState, out var value))
421 view.SetValue(targetBindableProperty, value);
426 /// Update containing selector value from a single value.
427 /// Note that, it updates lazily if possible.
428 /// If you need to udpate directly, please use <seealso cref="Update" />.
430 /// <param name="view">The View that is affected by this TriggerableSelector.</param>
431 /// <param name="value">The copy target.</param>
432 [EditorBrowsable(EditorBrowsableState.Never)]
433 public void UpdateIfNeeds(View view, T value)
435 if (selector != null && selector.HasMultiValue())
437 Selector<T> converted = value == null ? null : new Selector<T>(value);
438 Update(view, converted);
446 /// Reset selector and listeners.
448 /// <param name="view">The View that is affected by this TriggerableSelector.</param>
449 [EditorBrowsable(EditorBrowsableState.Never)]
450 public void Reset(View view)
454 view.ControlStateChangeEventInternal -= OnViewControlState;
461 private void OnViewControlState(object obj, View.ControlStateChangedEventArgs controlStateChangedInfo)
463 View view = obj as View;
464 if (null != view && selector.GetValue(controlStateChangedInfo.CurrentState, out var value))
466 view.SetValue(targetBindableProperty, value);