1 /* Copyright (c) 2021 Samsung Electronics Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 using System.ComponentModel;
18 using Tizen.NUI.BaseComponents;
19 using Tizen.NUI.Binding;
20 using Tizen.NUI.Components.Extension;
21 using Tizen.NUI.Accessibility;
23 namespace Tizen.NUI.Components
26 /// DefaultLinearItem is one kind of common component, a DefaultLinearItem clearly describes what action will occur when the user selects it.
27 /// DefaultLinearItem may contain text or an icon.
29 [EditorBrowsable(EditorBrowsableState.Never)]
30 public class DefaultLinearItem : RecyclerViewItem
32 private View itemIcon;
33 private TextLabel itemLabel;
34 private TextLabel itemSubLabel;
35 private View itemExtra;
36 private View itemSeperator;
37 private bool layoutChanged;
38 private Size prevSize;
39 private DefaultLinearItemStyle ItemStyle => ViewStyle as DefaultLinearItemStyle;
42 /// Return a copied Style instance of DefaultLinearItem
45 /// It returns copied Style instance and changing it does not effect to the DefaultLinearItem.
46 /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
48 [EditorBrowsable(EditorBrowsableState.Never)]
49 public new DefaultLinearItemStyle Style
53 var result = new DefaultLinearItemStyle(ItemStyle);
54 result.CopyPropertiesFromView(this);
55 if (itemLabel) result.Label.CopyPropertiesFromView(itemLabel);
56 if (itemSubLabel) result.SubLabel.CopyPropertiesFromView(itemSubLabel);
57 if (itemIcon) result.Icon.CopyPropertiesFromView(itemIcon);
58 if (itemExtra) result.Extra.CopyPropertiesFromView(itemExtra);
59 if (itemSeperator) result.Seperator.CopyPropertiesFromView(itemSeperator);
65 static DefaultLinearItem() {}
68 /// Creates a new instance of DefaultLinearItem.
70 [EditorBrowsable(EditorBrowsableState.Never)]
71 public DefaultLinearItem() : base()
77 /// Creates a new instance of a DefaultLinearItem with style.
79 /// <param name="style">Create DefaultLinearItem by style defined in UX.</param>
80 [EditorBrowsable(EditorBrowsableState.Never)]
81 public DefaultLinearItem(string style) : base(style)
87 /// Creates a new instance of a DefaultLinearItem with style.
89 /// <param name="itemStyle">Create DefaultLinearItem by style customized by user.</param>
90 [EditorBrowsable(EditorBrowsableState.Never)]
91 public DefaultLinearItem(DefaultLinearItemStyle itemStyle) : base(itemStyle)
97 /// Icon part of DefaultLinearItem.
99 [EditorBrowsable(EditorBrowsableState.Never)]
104 if (itemIcon == null)
106 itemIcon = CreateIcon(ItemStyle?.Icon);
107 if (itemIcon != null)
109 layoutChanged = true;
111 itemIcon.Relayout += OnIconRelayout;
122 /* open when imageView using Uri not string.
124 /// Icon image's resource url. Only activatable for icon as ImageView.
126 [EditorBrowsable(EditorBrowsableState.Never)]
127 public string IconUrl
131 return (Icon as ImageView)?.ResourceUrl;
135 if (itemIcon != null && !(itemIcon is ImageView))
137 // Tizen.Log.Error("IconUrl only can set Icon is ImageView");
140 (Icon as ImageView).ResourceUrl = value;
146 /// DefaultLinearItem's text part of DefaultLinearItem
148 [EditorBrowsable(EditorBrowsableState.Never)]
149 public TextLabel Label
153 if (itemLabel == null)
155 itemLabel = CreateLabel(ItemStyle?.Label);
156 if (itemLabel != null)
158 layoutChanged = true;
167 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, itemLabel.Text);
172 /// The text of DefaultLinearItem.
174 [EditorBrowsable(EditorBrowsableState.Never)]
188 /// DefaultLinearItem's secondary text part of DefaultLinearItem
190 [EditorBrowsable(EditorBrowsableState.Never)]
191 public TextLabel SubLabel
195 if (itemSubLabel == null)
197 itemSubLabel = CreateLabel(ItemStyle?.SubLabel);
198 if (itemLabel != null)
200 layoutChanged = true;
208 itemSubLabel = value;
209 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, itemSubLabel.Text);
214 /// The text of DefaultLinearItem.
216 [EditorBrowsable(EditorBrowsableState.Never)]
217 public string SubText
221 return SubLabel.Text;
225 SubLabel.Text = value;
230 /// Extra icon part of DefaultLinearItem. it will place next of label.
232 [EditorBrowsable(EditorBrowsableState.Never)]
237 if (itemExtra == null)
239 itemExtra = CreateIcon(ItemStyle?.Extra);
240 if (itemExtra != null)
242 layoutChanged = true;
244 itemExtra.Relayout += OnExtraRelayout;
251 if (itemExtra != null) Remove(itemExtra);
258 /// Seperator devider of DefaultLinearItem. it will place at the end of item.
260 [EditorBrowsable(EditorBrowsableState.Never)]
261 public View Seperator
265 if (itemSeperator == null)
267 itemSeperator = new View(ItemStyle?.Seperator)
269 //need to consider horizontal/vertical!
270 WidthSpecification = LayoutParamPolicies.MatchParent,
271 HeightSpecification = 2,
272 ExcludeLayouting = true
274 layoutChanged = true;
277 return itemSeperator;
282 /// Apply style to DefaultLinearItemStyle.
284 /// <param name="viewStyle">The style to apply.</param>
285 [EditorBrowsable(EditorBrowsableState.Never)]
286 public override void ApplyStyle(ViewStyle viewStyle)
289 base.ApplyStyle(viewStyle);
290 if (viewStyle != null && viewStyle is DefaultLinearItemStyle defaultStyle)
292 if (itemLabel != null)
293 itemLabel.ApplyStyle(defaultStyle.Label);
294 if (itemSubLabel != null)
295 itemSubLabel.ApplyStyle(defaultStyle.SubLabel);
296 if (itemIcon != null)
297 itemIcon.ApplyStyle(defaultStyle.Icon);
298 if (itemExtra != null)
299 itemExtra.ApplyStyle(defaultStyle.Extra);
300 if (itemSeperator != null)
301 itemSeperator.ApplyStyle(defaultStyle.Seperator);
306 [EditorBrowsable(EditorBrowsableState.Never)]
307 public override void OnRelayout(Vector2 size, RelayoutContainer container)
309 base.OnRelayout(size, container);
311 if (prevSize != Size)
316 var margin = itemSeperator.Margin;
317 itemSeperator.SizeWidth = SizeWidth - margin.Start - margin.End;
318 itemSeperator.SizeHeight = itemSeperator.HeightSpecification;
319 itemSeperator.Position = new Position(margin.Start, SizeHeight - margin.Bottom -itemSeperator.SizeHeight);
325 /// Creates Item's text part.
327 /// <return>The created Item's text part.</return>
328 [EditorBrowsable(EditorBrowsableState.Never)]
329 protected virtual TextLabel CreateLabel(TextLabelStyle style)
331 return new TextLabel(style)
333 HorizontalAlignment = HorizontalAlignment.Begin,
334 VerticalAlignment = VerticalAlignment.Center
339 /// Creates Item's icon part.
341 /// <return>The created Item's icon part.</return>
342 [EditorBrowsable(EditorBrowsableState.Never)]
343 protected virtual ImageView CreateIcon(ViewStyle style)
345 return new ImageView(style);
349 [EditorBrowsable(EditorBrowsableState.Never)]
350 protected override void MeasureChild()
355 var margin = itemLabel.Margin;
356 itemLabel.SizeWidth = SizeWidth - pad.Start - pad.End - margin.Start - margin.End;
361 var margin = itemSubLabel.Margin;
362 itemSubLabel.SizeWidth = SizeWidth - pad.Start - pad.End - margin.Start - margin.End;
367 [EditorBrowsable(EditorBrowsableState.Never)]
368 protected override void LayoutChild()
370 if (!layoutChanged) return;
371 if (itemLabel == null) return;
373 layoutChanged = false;
375 if (itemIcon != null)
377 RelativeLayout.SetLeftTarget(itemIcon, this);
378 RelativeLayout.SetLeftRelativeOffset(itemIcon, 0.0F);
379 RelativeLayout.SetRightTarget(itemIcon, this);
380 RelativeLayout.SetRightRelativeOffset(itemIcon, 0.0F);
381 RelativeLayout.SetTopTarget(itemIcon, this);
382 RelativeLayout.SetTopRelativeOffset(itemIcon, 0.0F);
383 RelativeLayout.SetBottomTarget(itemIcon, this);
384 RelativeLayout.SetBottomRelativeOffset(itemIcon, 1.0F);
385 RelativeLayout.SetVerticalAlignment(itemIcon, RelativeLayout.Alignment.Center);
386 RelativeLayout.SetHorizontalAlignment(itemIcon, RelativeLayout.Alignment.Start);
389 if (itemExtra != null)
391 RelativeLayout.SetLeftTarget(itemExtra, this);
392 RelativeLayout.SetLeftRelativeOffset(itemExtra, 1.0F);
393 RelativeLayout.SetRightTarget(itemExtra, this);
394 RelativeLayout.SetRightRelativeOffset(itemIcon, 1.0F);
395 RelativeLayout.SetTopTarget(itemExtra, this);
396 RelativeLayout.SetTopRelativeOffset(itemExtra, 0.0F);
397 RelativeLayout.SetBottomTarget(itemExtra, this);
398 RelativeLayout.SetBottomRelativeOffset(itemExtra, 1.0F);
399 RelativeLayout.SetVerticalAlignment(itemExtra, RelativeLayout.Alignment.Center);
400 RelativeLayout.SetHorizontalAlignment(itemExtra, RelativeLayout.Alignment.End);
403 if (itemSubLabel != null)
407 RelativeLayout.SetLeftTarget(itemSubLabel, itemIcon);
408 RelativeLayout.SetLeftRelativeOffset(itemSubLabel, 1.0F);
412 RelativeLayout.SetLeftTarget(itemSubLabel, this);
413 RelativeLayout.SetLeftRelativeOffset(itemSubLabel, 0.0F);
417 RelativeLayout.SetRightTarget(itemSubLabel, itemExtra);
418 RelativeLayout.SetRightRelativeOffset(itemSubLabel, 0.0F);
422 RelativeLayout.SetRightTarget(itemSubLabel, this);
423 RelativeLayout.SetRightRelativeOffset(itemSubLabel, 1.0F);
426 RelativeLayout.SetTopTarget(itemSubLabel, this);
427 RelativeLayout.SetTopRelativeOffset(itemSubLabel, 1.0F);
428 RelativeLayout.SetBottomTarget(itemSubLabel, this);
429 RelativeLayout.SetBottomRelativeOffset(itemSubLabel, 1.0F);
430 RelativeLayout.SetVerticalAlignment(itemSubLabel, RelativeLayout.Alignment.End);
432 RelativeLayout.SetHorizontalAlignment(itemSubLabel, RelativeLayout.Alignment.Center);
433 RelativeLayout.SetFillHorizontal(itemSubLabel, true);
438 RelativeLayout.SetLeftTarget(itemLabel, itemIcon);
439 RelativeLayout.SetLeftRelativeOffset(itemLabel, 1.0F);
443 RelativeLayout.SetLeftTarget(itemLabel, this);
444 RelativeLayout.SetLeftRelativeOffset(itemLabel, 0.0F);
448 RelativeLayout.SetRightTarget(itemLabel, itemExtra);
449 RelativeLayout.SetRightRelativeOffset(itemLabel, 0.0F);
453 RelativeLayout.SetRightTarget(itemLabel, this);
454 RelativeLayout.SetRightRelativeOffset(itemLabel, 1.0F);
457 RelativeLayout.SetTopTarget(itemLabel, this);
458 RelativeLayout.SetTopRelativeOffset(itemLabel, 0.0F);
462 RelativeLayout.SetBottomTarget(itemLabel, itemSubLabel);
463 RelativeLayout.SetBottomRelativeOffset(itemLabel, 0.0F);
467 RelativeLayout.SetBottomTarget(itemLabel, this);
468 RelativeLayout.SetBottomRelativeOffset(itemLabel, 1.0F);
470 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.Center);
471 RelativeLayout.SetHorizontalAlignment(itemLabel, RelativeLayout.Alignment.Center);
472 RelativeLayout.SetFillHorizontal(itemLabel, true);
474 if (prevSize != Size)
479 var margin = itemSeperator.Margin;
480 itemSeperator.SizeWidth = SizeWidth - margin.Start - margin.End;
481 itemSeperator.SizeHeight = itemSeperator.HeightSpecification;
482 itemSeperator.Position = new Position(margin.Start, SizeHeight - margin.Bottom -itemSeperator.SizeHeight);
488 /// Dispose Item and all children on it.
490 /// <param name="type">Dispose type.</param>
491 [EditorBrowsable(EditorBrowsableState.Never)]
492 protected override void Dispose(DisposeTypes type)
499 if (type == DisposeTypes.Explicit)
501 //Extension : Extension?.OnDispose(this);
503 if (itemIcon != null)
505 Utility.Dispose(itemIcon);
507 if (itemExtra != null)
509 Utility.Dispose(itemExtra);
511 if (itemLabel != null)
513 Utility.Dispose(itemLabel);
515 if (itemSubLabel != null)
517 Utility.Dispose(itemSubLabel);
519 if (itemSeperator != null)
521 Utility.Dispose(itemSeperator);
529 /// Get DefaultLinearItem style.
531 /// <returns>The default DefaultLinearItem style.</returns>
532 [EditorBrowsable(EditorBrowsableState.Never)]
533 protected override ViewStyle CreateViewStyle()
535 return new DefaultLinearItemStyle();
538 private void Initialize()
541 Layout = new RelativeLayout();
542 var seperator = Seperator;
543 layoutChanged = true;
544 LayoutDirectionChanged += OnLayoutDirectionChanged;
547 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
553 private void OnIconRelayout(object sender, EventArgs e)
559 private void OnExtraRelayout(object sender, EventArgs e)