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.ComponentModel;
19 using Tizen.NUI.Binding;
20 using Tizen.NUI.Components;
22 namespace Tizen.NUI.BaseComponents
25 /// Selector class, which is related by Control State, it is base class for other Selector.
27 /// <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>
28 /// <since_tizen> 6 </since_tizen>
29 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
30 [EditorBrowsable(EditorBrowsableState.Never)]
31 public class Selector<T> : StateValueCollection<T>
33 private readonly bool isCloneable = typeof(ICloneable).IsAssignableFrom(typeof(T));
34 /// <since_tizen> 6 </since_tizen>
35 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
36 public static implicit operator Selector<T>(T value)
38 return new Selector<T>(value);
41 /// Default Contructor
42 [EditorBrowsable(EditorBrowsableState.Never)]
48 [EditorBrowsable(EditorBrowsableState.Never)]
49 public Selector(T value) : this()
51 All = isCloneable ? (T)((ICloneable)value)?.Clone() : value;
55 [EditorBrowsable(EditorBrowsableState.Never)]
56 public Selector(Selector<T> value) : this()
64 /// <since_tizen> 6 </since_tizen>
65 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
66 [EditorBrowsable(EditorBrowsableState.Never)]
75 /// <since_tizen> 6 </since_tizen>
76 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
77 [EditorBrowsable(EditorBrowsableState.Never)]
80 get => Find(x => x.State == ControlState.Normal).Value;
81 set => Add(ControlState.Normal, value);
86 /// <since_tizen> 6 </since_tizen>
87 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
88 [EditorBrowsable(EditorBrowsableState.Never)]
91 get => Find(x => x.State == ControlState.Pressed).Value;
92 set => Add(ControlState.Pressed, value);
97 /// <since_tizen> 6 </since_tizen>
98 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
99 [EditorBrowsable(EditorBrowsableState.Never)]
102 get => Find(x => x.State == ControlState.Focused).Value;
103 set => Add(ControlState.Focused, value);
108 /// <since_tizen> 6 </since_tizen>
109 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
110 [EditorBrowsable(EditorBrowsableState.Never)]
113 get => Find(x => x.State == ControlState.Selected).Value;
114 set => Add(ControlState.Selected, value);
119 /// <since_tizen> 6 </since_tizen>
120 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
121 [EditorBrowsable(EditorBrowsableState.Never)]
124 get => Find(x => x.State == ControlState.Disabled).Value;
125 set => Add(ControlState.Disabled, value);
128 /// DisabledFocused State.
130 /// <since_tizen> 6 </since_tizen>
131 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
132 [EditorBrowsable(EditorBrowsableState.Never)]
133 public T DisabledFocused
135 get => Find(x => x.State == ControlState.DisabledFocused).Value;
136 set => Add(ControlState.DisabledFocused, value);
139 /// SelectedFocused State.
141 /// <since_tizen> 6 </since_tizen>
142 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
143 public T SelectedFocused
145 get => Find(x => x.State == ControlState.SelectedFocused).Value;
146 set => Add(ControlState.SelectedFocused, value);
149 /// DisabledSelected State.
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)]
154 public T DisabledSelected
156 get => Find(x => x.State == ControlState.DisabledSelected).Value;
157 set => Add(ControlState.DisabledSelected, value);
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)]
168 get => Find(x => x.State == ControlState.Other).Value;
169 set => Add(ControlState.Other, value);
172 /// Get value by 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 /// <returns>True if the selector has a given state value, false otherwise.</returns>
177 [EditorBrowsable(EditorBrowsableState.Never)]
178 public bool GetValue(ControlState state, out T result)
189 int index = StateValueList.FindIndex(x => x.State == state);
192 result = StateValueList[index].Value;
196 if (state.IsCombined)
198 index = StateValueList.FindIndex(x => state.Contains(x.State));
201 result = StateValueList[index].Value;
206 index = StateValueList.FindIndex(x => x.State == ControlState.Other);
209 result = StateValueList[index].Value;
217 [EditorBrowsable(EditorBrowsableState.Never)]
218 public override void Clear()
225 [EditorBrowsable(EditorBrowsableState.Never)]
226 public override string ToString()
228 string result = $"[All, {All}]";
230 foreach (var item in StateValueList)
232 result += $", {item}";
240 /// If type T implements ISelectorItem, it calls Clone() method to clone values, otherwise use operator=.
242 /// <since_tizen> 6 </since_tizen>
243 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
244 [EditorBrowsable(EditorBrowsableState.Never)]
245 public Selector<T> Clone()
247 var cloned = new Selector<T>();
253 /// Copy values from other selector.
255 [EditorBrowsable(EditorBrowsableState.Never)]
256 public void Clone(Selector<T> other)
262 All = (T)((ICloneable)other.All)?.Clone();
263 foreach (var item in other.StateValueList)
265 AddWithoutCheck(new StateValuePair<T>(item.State, (T)((ICloneable)item.Value)?.Clone()));
271 foreach (var item in other.StateValueList)
273 AddWithoutCheck(item);
278 internal bool HasMultiValue()
280 return StateValueList.Count > 1;
285 /// This will be attached to a View to detect ControlState change.
287 [EditorBrowsable(EditorBrowsableState.Never)]
288 public class TriggerableSelector<T>
291 /// Create an TriggerableSelector.
293 [EditorBrowsable(EditorBrowsableState.Never)]
294 public delegate T ValueGetter(View view);
296 private readonly BindableProperty targetBindableProperty;
297 private readonly ValueGetter propertyGetter;
298 private bool dirty = true;
299 private Selector<T> selector;
302 /// Create an TriggerableSelector.
304 /// <param name="targetBindableProperty">The TriggerableSelector will change this bindable property value when the view's ControlState has changed.</param>
305 /// <param name="propertyGetter">It is optional value in case the target bindable property getter is not proper to use.</param>
306 [EditorBrowsable(EditorBrowsableState.Never)]
307 public TriggerableSelector(BindableProperty targetBindableProperty, ValueGetter propertyGetter = null)
309 this.targetBindableProperty = targetBindableProperty;
310 this.propertyGetter = propertyGetter;
314 /// Return the containing selector. It can be null.
316 [EditorBrowsable(EditorBrowsableState.Never)]
317 public Selector<T> Get(View view)
319 if (!dirty) return selector;
323 if (propertyGetter != null)
325 value = propertyGetter(view);
329 value = (T)view.GetValue(targetBindableProperty);
332 Selector<T> converted = value == null ? null : new Selector<T>(value);
333 Update(view, converted);
339 /// Update containing selector from the other selector.
341 /// <param name="view">The View that is affected by this TriggerableSelector.</param>
342 /// <param name="otherSelector">The copy target.</param>
343 /// <param name="updateView">Whether it updates the target view after update the selector or not.</param>
344 [EditorBrowsable(EditorBrowsableState.Never)]
345 public void Update(View view, Selector<T> otherSelector, bool updateView = false)
349 if (otherSelector == null)
354 selector = otherSelector.Clone();
356 if (otherSelector.HasMultiValue())
358 view.ControlStateChangeEventInternal += OnViewControlState;
361 if (updateView && otherSelector.GetValue(view.ControlState, out var value))
363 view.SetValue(targetBindableProperty, value);
368 /// Update containing selector value from a single value.
369 /// Note that, it updates lazily if possible.
370 /// If you need to udpate directly, please use <seealso cref="Update" />.
372 /// <param name="view">The View that is affected by this TriggerableSelector.</param>
373 /// <param name="value">The copy target.</param>
374 [EditorBrowsable(EditorBrowsableState.Never)]
375 public void UpdateIfNeeds(View view, T value)
377 if (selector != null && selector.HasMultiValue())
379 Selector<T> converted = value == null ? null : new Selector<T>(value);
380 Update(view, converted);
388 /// Reset selector and listeners.
390 /// <param name="view">The View that is affected by this TriggerableSelector.</param>
391 [EditorBrowsable(EditorBrowsableState.Never)]
392 public void Reset(View view)
394 view.ControlStateChangeEventInternal -= OnViewControlState;
400 private void OnViewControlState(object obj, View.ControlStateChangedEventArgs controlStateChangedInfo)
402 View view = obj as View;
403 if (null != view && selector.GetValue(controlStateChangedInfo.CurrentState, out var value))
405 view.SetValue(targetBindableProperty, value);