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.BaseComponents;
22 namespace Tizen.NUI.CommonUI
25 /// The control component is base class of tv nui components. It's abstract class, so cann't instantiate and can only be inherited.
27 /// <since_tizen> 6 </since_tizen>
28 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
29 [EditorBrowsable(EditorBrowsableState.Never)]
30 public abstract class Control : VisualView
35 /// <since_tizen> 6 </since_tizen>
36 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
37 [EditorBrowsable(EditorBrowsableState.Never)]
38 protected string style;
40 /// Control attributes.
42 /// <since_tizen> 6 </since_tizen>
43 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
44 [EditorBrowsable(EditorBrowsableState.Never)]
45 protected Attributes attributes;
47 private TapGestureDetector tapGestureDetector = new TapGestureDetector();
48 private bool isFocused = false;
51 /// Construct an empty Control.
53 /// <since_tizen> 6 </since_tizen>
54 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
55 [EditorBrowsable(EditorBrowsableState.Never)]
56 public Control() : base()
62 /// Construct with attributes
64 /// <param name="attributes">Create attributes customized by user</param>
65 /// <since_tizen> 6 </since_tizen>
66 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
67 [EditorBrowsable(EditorBrowsableState.Never)]
68 public Control(Attributes attributes) : base()
71 this.attributes = attributes.Clone();
75 /// Construct with style
77 /// <param name="style">Style to be applied</param>
78 /// <since_tizen> 6 </since_tizen>
79 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
80 [EditorBrowsable(EditorBrowsableState.Never)]
81 public Control(string style) : base()
87 /// Get/Set the control state.
89 /// <since_tizen> 6 </since_tizen>
90 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
91 [EditorBrowsable(EditorBrowsableState.Never)]
92 public new ControlStates State
98 /// Whether focusable when touch
100 /// <since_tizen> 6 </since_tizen>
101 internal bool StateFocusableOnTouchMode
107 internal bool IsFocused
111 return isFocused || HasFocus();
115 /// Apply attributes for View, Image or TextLabel.
117 /// <param name="view">View which will be applied attrs</param>
118 /// <param name="attrs">Attributes for View, Image or TextLabel</param>
119 /// <since_tizen> 6 </since_tizen>
120 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
121 [EditorBrowsable(EditorBrowsableState.Never)]
122 protected void ApplyAttributes(View view, ViewAttributes attrs)
124 if (view == null || attrs == null)
129 if (attrs.Position2D != null)
131 view.Position2D = attrs.Position2D;
133 if (attrs.Size2D != null)
135 view.Size2D = attrs.Size2D;
137 if (attrs.MinimumSize != null)
139 view.MinimumSize = attrs.MinimumSize;
141 if (attrs.BackgroundColor?.GetValue(State) != null)
143 view.BackgroundColor = attrs.BackgroundColor.GetValue(State);
145 if (attrs.PositionUsesPivotPoint != null)
147 view.PositionUsesPivotPoint = attrs.PositionUsesPivotPoint.Value;
149 if (attrs.ParentOrigin != null)
151 view.ParentOrigin = attrs.ParentOrigin;
153 if (attrs.PivotPoint != null)
155 view.PivotPoint = attrs.PivotPoint;
157 if (attrs.WidthResizePolicy!= null)
159 view.WidthResizePolicy = attrs.WidthResizePolicy.Value;
161 if (attrs.HeightResizePolicy != null)
163 view.HeightResizePolicy = attrs.HeightResizePolicy.Value;
165 if (attrs.SizeModeFactor != null)
167 view.SizeModeFactor = attrs.SizeModeFactor;
169 if (attrs.Opacity?.GetValue(State) != null)
171 view.Opacity = attrs.Opacity.GetValue(State).Value;
174 ImageView image = view as ImageView;
175 ImageAttributes imageAttrs = attrs as ImageAttributes;
176 if (image != null && imageAttrs != null)
178 if (imageAttrs.ResourceURL?.GetValue(State) != null)
180 image.ResourceUrl = imageAttrs.ResourceURL.GetValue(State);
182 if (imageAttrs.Border?.GetValue(State) != null)
184 image.Border = imageAttrs.Border.GetValue(State);
189 TextLabel text = view as TextLabel;
190 TextAttributes textAttrs = attrs as TextAttributes;
191 if (text != null && textAttrs != null)
193 if (textAttrs.Text?.GetValue(State) != null )
195 text.Text = textAttrs.Text.GetValue(State);
197 if (textAttrs.TranslatableText?.GetValue(State) != null)
199 text.TranslatableText = textAttrs.TranslatableText.GetValue(State);
201 if (textAttrs.MultiLine != null)
203 text.MultiLine = textAttrs.MultiLine.Value;
205 if (textAttrs.HorizontalAlignment != null)
207 text.HorizontalAlignment = textAttrs.HorizontalAlignment.Value;
209 if (textAttrs.VerticalAlignment != null)
211 text.VerticalAlignment = textAttrs.VerticalAlignment.Value;
213 if (textAttrs.EnableMarkup != null)
215 text.EnableMarkup = textAttrs.EnableMarkup.Value;
217 if (textAttrs.AutoScrollLoopCount != null)
219 text.AutoScrollLoopCount = textAttrs.AutoScrollLoopCount.Value;
221 if (textAttrs.AutoScrollSpeed != null)
223 text.AutoScrollSpeed = textAttrs.AutoScrollSpeed.Value;
225 if (textAttrs.AutoScrollGap != null)
227 text.AutoScrollGap = textAttrs.AutoScrollGap.Value;
229 if (textAttrs.AutoScrollLoopDelay != null)
231 text.AutoScrollLoopDelay = textAttrs.AutoScrollLoopDelay.Value;
233 if (textAttrs.AutoScrollStopMode != null)
235 text.AutoScrollStopMode = textAttrs.AutoScrollStopMode.Value;
237 if (textAttrs.LineSpacing != null)
239 text.LineSpacing = textAttrs.LineSpacing.Value;
241 if (textAttrs.TextColor?.GetValue(State) != null)
243 text.TextColor = textAttrs.TextColor.GetValue(State);
245 if (textAttrs.FontFamily != null)
247 text.FontFamily = textAttrs.FontFamily;
249 if (textAttrs.PointSize?.GetValue(State) != null)
251 text.PointSize = textAttrs.PointSize.GetValue(State).Value;
256 if (textAttrs.OutstrokeThickness?.GetValue(State) != null)
258 thickness = textAttrs.OutstrokeThickness.GetValue(State).Value;
260 if (textAttrs.OutstrokeColor?.GetValue(State) != null)
262 Color outstrokeColor = textAttrs.OutstrokeColor.GetValue(State);
263 PropertyMap outlineMap = new PropertyMap();
264 outlineMap.Add("color", new PropertyValue(new Color(outstrokeColor.R, outstrokeColor.G, outstrokeColor.B, outstrokeColor.A)));
265 outlineMap.Add("width", new PropertyValue(thickness));
266 text.Outline = outlineMap;
270 text.Outline = new PropertyMap();
274 TextField textField = view as TextField;
275 TextFieldAttributes textFieldAttrs = attrs as TextFieldAttributes;
276 if (textField != null && textFieldAttrs != null)
278 if (textFieldAttrs.Text?.GetValue(State) != null)
280 textField.Text = textFieldAttrs.Text.GetValue(State);
282 if (textFieldAttrs.PlaceholderText?.GetValue(State) != null)
284 textField.PlaceholderText = textFieldAttrs.PlaceholderText.GetValue(State);
286 if (textFieldAttrs.TranslatablePlaceholderText?.GetValue(State) != null)
288 textField.TranslatablePlaceholderText = textFieldAttrs.TranslatablePlaceholderText.GetValue(State);
290 if (textFieldAttrs.HorizontalAlignment != null)
292 textField.HorizontalAlignment = textFieldAttrs.HorizontalAlignment.Value;
294 if (textFieldAttrs.VerticalAlignment != null)
296 textField.VerticalAlignment = textFieldAttrs.VerticalAlignment.Value;
298 if (textFieldAttrs.EnableMarkup != null)
300 textField.EnableMarkup = textFieldAttrs.EnableMarkup.Value;
302 if (textFieldAttrs.TextColor?.GetValue(State) != null)
304 textField.TextColor = textFieldAttrs.TextColor.GetValue(State);
306 if (textFieldAttrs.PlaceholderTextColor?.GetValue(State) != null)
308 textField.PlaceholderTextColor = textFieldAttrs.PlaceholderTextColor.GetValue(State);
310 if (textFieldAttrs.PrimaryCursorColor?.GetValue(State) != null)
312 textField.PrimaryCursorColor = textFieldAttrs.PrimaryCursorColor.GetValue(State);
314 if (textFieldAttrs.SecondaryCursorColor?.GetValue(State) != null)
316 textField.SecondaryCursorColor = textFieldAttrs.SecondaryCursorColor.GetValue(State);
318 if (textFieldAttrs.FontFamily != null)
320 textField.FontFamily = textFieldAttrs.FontFamily;
322 if (textFieldAttrs.PointSize?.GetValue(State) != null)
324 textField.PointSize = textFieldAttrs.PointSize.GetValue(State).Value;
326 if (textFieldAttrs.EnableCursorBlink != null)
328 textField.EnableCursorBlink = textFieldAttrs.EnableCursorBlink.Value;
330 if (textFieldAttrs.EnableSelection != null)
332 textField.EnableSelection = textFieldAttrs.EnableSelection.Value;
334 if (textFieldAttrs.CursorWidth != null)
336 textField.CursorWidth = textFieldAttrs.CursorWidth.Value;
338 if (textFieldAttrs.EnableEllipsis != null)
340 textField.Ellipsis = textFieldAttrs.EnableEllipsis.Value;
345 /// Dispose Control and all children on it.
347 /// <param name="type">Dispose type.</param>
348 /// <since_tizen> 6 </since_tizen>
349 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
350 [EditorBrowsable(EditorBrowsableState.Never)]
351 protected override void Dispose(DisposeTypes type)
358 if (type == DisposeTypes.Explicit)
360 StyleManager.Instance.ThemeChangedEvent -= OnThemeChangedEvent;
361 tapGestureDetector.Detected -= OnTapGestureDetected;
362 tapGestureDetector.Detach(this);
367 /// Get attribues, it is abstract function and must be override.
369 /// <since_tizen> 6 </since_tizen>
370 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
371 [EditorBrowsable(EditorBrowsableState.Never)]
372 protected abstract Attributes GetAttributes();
374 /// Called after a key event is received by the view that has had its focus set.
376 /// <param name="key">The key event.</param>
377 /// <returns>True if the key event should be consumed.</returns>
378 /// <since_tizen> 6 </since_tizen>
379 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
380 [EditorBrowsable(EditorBrowsableState.Never)]
381 public override bool OnKey(Key key)
387 /// Called after the size negotiation has been finished for this control.<br />
388 /// The control is expected to assign this given size to itself or its children.<br />
389 /// Should be overridden by derived classes if they need to layout views differently after certain operations like add or remove views, resize, or after changing specific properties.<br />
390 /// As this function is called from inside the size negotiation algorithm, you cannot call RequestRelayout (the call would just be ignored).<br />
392 /// <param name="size">The allocated size.</param>
393 /// <param name="container">The control should add views to this container that it is not able to allocate a size for.</param>
394 /// <since_tizen> 6 </since_tizen>
395 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
396 [EditorBrowsable(EditorBrowsableState.Never)]
397 public override void OnRelayout(Vector2 size, RelayoutContainer container)
403 /// Called when the control gain key input focus. Should be overridden by derived classes if they need to customize what happens when the focus is gained.
405 /// <since_tizen> 6 </since_tizen>
406 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
407 [EditorBrowsable(EditorBrowsableState.Never)]
408 public override void OnFocusGained()
414 /// Called when the control loses key input focus. Should be overridden by derived classes if they need to customize what happens when the focus is lost.
416 /// <since_tizen> 6 </since_tizen>
417 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
418 [EditorBrowsable(EditorBrowsableState.Never)]
419 public override void OnFocusLost()
425 /// Tap gesture callback.
427 /// <param name="source">The sender</param>
428 /// <param name="e">The tap gesture event data</param>
429 /// <since_tizen> 6 </since_tizen>
430 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
431 [EditorBrowsable(EditorBrowsableState.Never)]
432 protected virtual void OnTapGestureDetected(object source, TapGestureDetector.DetectedEventArgs e)
436 /// Called after a touch event is received by the owning view.<br />
437 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
439 /// <param name="touch">The touch event.</param>
440 /// <returns>True if the event should be consumed.</returns>
441 /// <since_tizen> 6 </since_tizen>
442 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
443 [EditorBrowsable(EditorBrowsableState.Never)]
444 public override bool OnTouch(Touch touch)
450 /// Update by attributes.
452 /// <since_tizen> 6 </since_tizen>
453 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
454 [EditorBrowsable(EditorBrowsableState.Never)]
455 protected virtual void OnUpdate()
459 /// Theme change callback when theme is changed, this callback will be trigger.
461 /// <param name="sender">The sender</param>
462 /// <param name="e">The event data</param>
463 /// <since_tizen> 6 </since_tizen>
464 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
465 [EditorBrowsable(EditorBrowsableState.Never)]
466 protected virtual void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
470 private void Initialize(string style)
472 attributes = (style == null) ? GetAttributes() : GetAttributes(style);
473 State = ControlStates.Normal;
475 LeaveRequired = true;
477 StateFocusableOnTouchMode = false;
479 tapGestureDetector.Attach(this);
480 tapGestureDetector.Detected += OnTapGestureDetected;
482 StyleManager.Instance.ThemeChangedEvent += OnThemeChangedEvent;
485 private Attributes GetAttributes(string style)
487 Attributes attributes = StyleManager.Instance.GetAttributes(style);
488 if(attributes == null)
490 throw new InvalidOperationException($"There is no style {style}");