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.
17 using System.ComponentModel;
18 using Tizen.NUI.Binding;
19 using Tizen.NUI.Components;
21 namespace Tizen.NUI.BaseComponents
24 /// Selector class, which is related by Control State, it is base class for other Selector.
26 /// <since_tizen> 6 </since_tizen>
27 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
28 [EditorBrowsable(EditorBrowsableState.Never)]
29 public class Selector<T> : BindableObject
31 /// <since_tizen> 6 </since_tizen>
32 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
33 public static implicit operator Selector<T>(T value)
35 Selector<T> selector = new Selector<T>();
40 /// Default Contructor
41 [EditorBrowsable(EditorBrowsableState.Never)]
47 [EditorBrowsable(EditorBrowsableState.Never)]
48 public Selector(T value) : this()
56 /// <since_tizen> 6 </since_tizen>
57 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
58 [EditorBrowsable(EditorBrowsableState.Never)]
67 /// <since_tizen> 6 </since_tizen>
68 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
69 [EditorBrowsable(EditorBrowsableState.Never)]
78 /// <since_tizen> 6 </since_tizen>
79 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
80 [EditorBrowsable(EditorBrowsableState.Never)]
89 /// <since_tizen> 6 </since_tizen>
90 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
91 [EditorBrowsable(EditorBrowsableState.Never)]
100 /// <since_tizen> 6 </since_tizen>
101 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
102 [EditorBrowsable(EditorBrowsableState.Never)]
111 /// <since_tizen> 6 </since_tizen>
112 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
113 [EditorBrowsable(EditorBrowsableState.Never)]
120 /// DisabledFocused State.
122 /// <since_tizen> 6 </since_tizen>
123 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
124 [EditorBrowsable(EditorBrowsableState.Never)]
125 public T DisabledFocused
131 /// SelectedFocused State.
133 /// <since_tizen> 6 </since_tizen>
134 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
135 public T SelectedFocused
141 /// DisabledSelected State.
143 /// <since_tizen> 6 </since_tizen>
144 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
145 [EditorBrowsable(EditorBrowsableState.Never)]
146 public T DisabledSelected
154 /// <since_tizen> 6 </since_tizen>
155 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
156 [EditorBrowsable(EditorBrowsableState.Never)]
163 /// Get value by State.
165 /// <since_tizen> 6 </since_tizen>
166 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
167 [EditorBrowsable(EditorBrowsableState.Never)]
168 public T GetValue(ControlStates state)
176 case ControlStates.Normal:
177 return Normal != null? Normal : Other;
178 case ControlStates.Focused:
179 return Focused != null? Focused : Other;
180 case ControlStates.Pressed:
181 return Pressed != null? Pressed : Other;
182 case ControlStates.Disabled:
183 return Disabled != null? Disabled : Other;
184 case ControlStates.Selected:
185 return Selected != null? Selected : Other;
186 case ControlStates.DisabledFocused:
187 return DisabledFocused != null? DisabledFocused : Other;
188 case ControlStates.DisabledSelected:
189 return DisabledSelected != null? DisabledSelected : Other;
190 case ControlStates.SelectedFocused:
191 return SelectedFocused != null ? SelectedFocused : Other;
199 /// <since_tizen> 6 </since_tizen>
200 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
201 [EditorBrowsable(EditorBrowsableState.Never)]
202 public void Clone(Selector<T> selector)
205 Normal = selector.Normal;
206 Focused = selector.Focused;
207 Pressed = selector.Pressed;
208 Disabled = selector.Disabled;
209 Selected = selector.Selected;
210 DisabledSelected = selector.DisabledSelected;
211 DisabledFocused = selector.DisabledFocused;
212 SelectedFocused = selector.SelectedFocused;
213 Other = selector.Other;
216 internal void Clone<U>(Selector<U> other) where U : T, Tizen.NUI.Internal.ICloneable
218 // TODO Apply constraint to the Selector (not to Clone method)
220 All = (T)(other.All)?.Clone();
221 Normal = (T)(other.Normal)?.Clone();
222 Focused = (T)(other.Focused)?.Clone();
223 Pressed = (T)(other.Pressed)?.Clone();
224 Disabled = (T)(other.Disabled)?.Clone();
225 Selected = (T)(other.Selected)?.Clone();
226 DisabledSelected = (T)(other.DisabledSelected)?.Clone();
227 DisabledFocused = (T)(other.DisabledFocused)?.Clone();
228 SelectedFocused = (T)(other.SelectedFocused)?.Clone();
229 Other = (T)(other.Other)?.Clone();
233 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
234 [EditorBrowsable(EditorBrowsableState.Never)]
235 public class TriggerableSelector<T> : Selector<T>
237 public TriggerableSelector(View view, BindableProperty bindableProperty)
240 targetBindableProperty = bindableProperty;
241 view.ControlStateChangeEvent += OnViewControlState;
247 /// <since_tizen> 6 </since_tizen>
248 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
249 [EditorBrowsable(EditorBrowsableState.Never)]
250 public new void Clone(Selector<T> selector)
252 base.Clone(selector);
254 if (null != targetView && null != GetValue(targetView.ControlState))
256 targetView.SetValue(targetBindableProperty, GetValue(targetView.ControlState));
260 private void OnViewControlState(View obj, ControlStates state)
262 if (null != obj && null != GetValue(state))
264 obj.SetValue(targetBindableProperty, GetValue(state));
268 private View targetView;
269 private BindableProperty targetBindableProperty;
273 /// A class that helps binding a non-selector property in View to selector property in ViewStyle.
275 internal class ViewSelector<T> where T : class, Tizen.NUI.Internal.ICloneable
277 private Selector<T> selector;
279 private View.ControlStateChangesDelegate controlStateChanged;
281 internal ViewSelector(View view, View.ControlStateChangesDelegate controlStateChanged)
283 if (view == null || controlStateChanged == null)
285 throw new global::System.ArgumentNullException();
288 this.controlStateChanged = controlStateChanged;
289 this.selector = null;
292 internal T GetValue()
294 return selector == null ? null : selector.GetValue(view.ControlState);
297 internal void Clone(object value)
299 bool hadMultiValue = HasMultiValue();
300 var type = value?.GetType();
302 if (type == typeof(T))
304 selector = new Selector<T>();
305 selector.All = (T)((T)value).Clone();
307 else if (type == typeof(Selector<T>))
309 selector = new Selector<T>();
310 selector.Clone<T>((Selector<T>)value);
317 if (hadMultiValue != HasMultiValue())
319 if (hadMultiValue) view.ControlStateChangeEvent -= controlStateChanged;
320 else view.ControlStateChangeEvent += controlStateChanged;
324 internal void Clear()
328 view.ControlStateChangeEvent -= controlStateChanged;
333 internal bool IsEmpty()
335 return selector == null;
338 private bool HasMultiValue()
340 return (selector != null && selector.All == null);
344 internal static class SelectorHelper<T> where T : class, Tizen.NUI.Internal.ICloneable
347 /// For the object type of T or Selector T, convert it to Selector T and return the cloned one.
348 /// Otherwise, return null. <br/>
350 static internal Selector<T> Clone(object value)
352 var type = value?.GetType();
354 if (type == typeof(Selector<T>))
356 var result = new Selector<T>();
357 result.Clone<T>((Selector<T>)value);
361 if (type == typeof(T))
363 return new Selector<T>((T)((T)value).Clone());
370 /// For the object type of T or Selector T, convert it to T and return the cloned one.
371 /// Otherwise, return null. <br/>
373 static internal T Clone(object value, View view)
375 var type = value?.GetType();
377 if (type == typeof(T))
379 return (T)((T)value).Clone();
382 if (type == typeof(Selector<T>) && view != null)
384 Selector<T> selector = (Selector<T>)value;
385 T valueInState = selector.GetValue(view.ControlState);
387 return (T)valueInState?.Clone();