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 /// <since_tizen> 6 </since_tizen>
28 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
29 [EditorBrowsable(EditorBrowsableState.Never)]
30 public class Selector<T> : BindableObject
32 /// <since_tizen> 6 </since_tizen>
33 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
34 public static implicit operator Selector<T>(T value)
36 Selector<T> selector = new Selector<T>();
41 /// Default Contructor
42 [EditorBrowsable(EditorBrowsableState.Never)]
48 [EditorBrowsable(EditorBrowsableState.Never)]
49 public Selector(T value) : this()
57 /// <since_tizen> 6 </since_tizen>
58 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
59 [EditorBrowsable(EditorBrowsableState.Never)]
68 /// <since_tizen> 6 </since_tizen>
69 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
70 [EditorBrowsable(EditorBrowsableState.Never)]
79 /// <since_tizen> 6 </since_tizen>
80 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
81 [EditorBrowsable(EditorBrowsableState.Never)]
90 /// <since_tizen> 6 </since_tizen>
91 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
92 [EditorBrowsable(EditorBrowsableState.Never)]
101 /// <since_tizen> 6 </since_tizen>
102 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
103 [EditorBrowsable(EditorBrowsableState.Never)]
112 /// <since_tizen> 6 </since_tizen>
113 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
114 [EditorBrowsable(EditorBrowsableState.Never)]
121 /// DisabledFocused State.
123 /// <since_tizen> 6 </since_tizen>
124 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
125 [EditorBrowsable(EditorBrowsableState.Never)]
126 public T DisabledFocused
132 /// SelectedFocused State.
134 /// <since_tizen> 6 </since_tizen>
135 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
136 public T SelectedFocused
142 /// DisabledSelected State.
144 /// <since_tizen> 6 </since_tizen>
145 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
146 [EditorBrowsable(EditorBrowsableState.Never)]
147 public T DisabledSelected
155 /// <since_tizen> 6 </since_tizen>
156 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
157 [EditorBrowsable(EditorBrowsableState.Never)]
164 /// Get value by State.
166 /// <since_tizen> 6 </since_tizen>
167 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
168 [EditorBrowsable(EditorBrowsableState.Never)]
169 public T GetValue(ControlStates state)
177 case ControlStates.Normal:
178 return Normal != null? Normal : Other;
179 case ControlStates.Focused:
180 return Focused != null? Focused : Other;
181 case ControlStates.Pressed:
182 return Pressed != null? Pressed : Other;
183 case ControlStates.Disabled:
184 return Disabled != null? Disabled : Other;
185 case ControlStates.Selected:
186 return Selected != null? Selected : Other;
187 case ControlStates.DisabledFocused:
188 return DisabledFocused != null? DisabledFocused : Other;
189 case ControlStates.DisabledSelected:
190 return DisabledSelected != null? DisabledSelected : Other;
191 case ControlStates.SelectedFocused:
192 return SelectedFocused != null ? SelectedFocused : Other;
200 /// <since_tizen> 6 </since_tizen>
201 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
202 [EditorBrowsable(EditorBrowsableState.Never)]
203 public void Clone(Selector<T> selector)
206 Normal = selector.Normal;
207 Focused = selector.Focused;
208 Pressed = selector.Pressed;
209 Disabled = selector.Disabled;
210 Selected = selector.Selected;
211 DisabledSelected = selector.DisabledSelected;
212 DisabledFocused = selector.DisabledFocused;
213 SelectedFocused = selector.SelectedFocused;
214 Other = selector.Other;
217 internal void Clone<U>(Selector<U> other) where U : T, Tizen.NUI.Internal.ICloneable
219 // TODO Apply constraint to the Selector (not to Clone method)
221 All = (T)(other.All)?.Clone();
222 Normal = (T)(other.Normal)?.Clone();
223 Focused = (T)(other.Focused)?.Clone();
224 Pressed = (T)(other.Pressed)?.Clone();
225 Disabled = (T)(other.Disabled)?.Clone();
226 Selected = (T)(other.Selected)?.Clone();
227 DisabledSelected = (T)(other.DisabledSelected)?.Clone();
228 DisabledFocused = (T)(other.DisabledFocused)?.Clone();
229 SelectedFocused = (T)(other.SelectedFocused)?.Clone();
230 Other = (T)(other.Other)?.Clone();
234 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
235 [EditorBrowsable(EditorBrowsableState.Never)]
236 public class TriggerableSelector<T> : Selector<T>
238 public TriggerableSelector(View view, BindableProperty bindableProperty)
241 targetBindableProperty = bindableProperty;
242 view.ControlStateChangeEvent += OnViewControlState;
248 /// <since_tizen> 6 </since_tizen>
249 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
250 [EditorBrowsable(EditorBrowsableState.Never)]
251 public new void Clone(Selector<T> selector)
253 base.Clone(selector);
255 if (null != targetView && null != GetValue(targetView.ControlState))
257 targetView.SetValue(targetBindableProperty, GetValue(targetView.ControlState));
261 private void OnViewControlState(View obj, ControlStates state)
263 if (null != obj && null != GetValue(state))
265 obj.SetValue(targetBindableProperty, GetValue(state));
269 private View targetView;
270 private BindableProperty targetBindableProperty;
274 /// A class that helps binding a non-selector property in View to selector property in ViewStyle.
276 internal class ViewSelector<T>
278 protected Selector<T> selector;
280 protected View.ControlStateChangesDelegate controlStateChanged;
282 internal ViewSelector(View view, View.ControlStateChangesDelegate controlStateChanged)
284 if (view == null || controlStateChanged == null)
286 throw new global::System.ArgumentNullException();
289 this.controlStateChanged = controlStateChanged;
290 this.selector = null;
293 internal T GetValue()
295 return selector == null ? default(T) : selector.GetValue(view.ControlState);
298 internal void Set(object value)
300 bool hadMultiValue = HasMultiValue();
301 var type = value?.GetType();
303 if (type == typeof(T))
305 CopyValueToSelector((T)value);
307 else if (type == typeof(Selector<T>))
309 CopySelectorToSelector((Selector<T>)value);
311 else if (type == Nullable.GetUnderlyingType(typeof(T)))
313 CopyValueToSelector((T)value);
320 if (hadMultiValue != HasMultiValue())
322 if (hadMultiValue) view.ControlStateChangeEvent -= controlStateChanged;
323 else view.ControlStateChangeEvent += controlStateChanged;
327 protected virtual void CopyValueToSelector(T value)
329 selector = new Selector<T>();
330 selector.All = value;
333 protected virtual void CopySelectorToSelector(Selector<T> value)
335 selector = new Selector<T>();
336 selector.Clone(value);
339 internal void Clear()
343 view.ControlStateChangeEvent -= controlStateChanged;
348 internal bool IsEmpty()
350 return selector == null;
353 protected bool HasMultiValue()
355 return (selector != null && selector.All == null);
360 /// ViewSelector class for ICloneable type
362 internal class CloneableViewSelector<T> : ViewSelector<T> where T : Tizen.NUI.Internal.ICloneable
364 internal CloneableViewSelector(View view, View.ControlStateChangesDelegate controlStateChanged) : base(view, controlStateChanged)
368 protected override void CopyValueToSelector(T value)
370 selector = new Selector<T>();
371 selector.All = (T)((T)value).Clone();
374 protected override void CopySelectorToSelector(Selector<T> value)
376 selector = new Selector<T>();
377 selector.Clone<T>((Selector<T>)value);
381 internal static class SelectorHelper
384 /// For the object type of T or Selector T, convert it to Selector T and return the cloned one.
385 /// Otherwise, return null. <br/>
387 static internal Selector<T> CopyCloneable<T>(object value) where T : class, Tizen.NUI.Internal.ICloneable
389 var type = value?.GetType();
391 if (type == typeof(Selector<T>))
393 var result = new Selector<T>();
394 result.Clone<T>((Selector<T>)value);
398 if (type == typeof(T))
400 return new Selector<T>((T)((T)value).Clone());
407 /// For the value type of T or Selector T, convert it to Selector T and return the cloned one.
408 /// Otherwise, return null. <br/>
410 static internal Selector<T> CopyValue<T>(object value)
412 var type = value?.GetType();
414 if (type == typeof(Selector<T>))
416 var result = new Selector<T>();
417 result.Clone((Selector<T>)value);
421 if (type == typeof(T))
423 return new Selector<T>((T)value);