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 /// DefaultTitleItem is one kind of common component, a DefaultTitleItem clearly describes what action will occur when the user selects it.
27 /// DefaultTitleItem may contain text or an icon.
29 [EditorBrowsable(EditorBrowsableState.Never)]
30 public class DefaultTitleItem : RecyclerViewItem
32 private TextLabel itemLabel;
33 private View itemIcon;
34 private View itemSeperator;
35 private bool layoutChanged;
36 private Size prevSize;
37 private DefaultTitleItemStyle ItemStyle => ViewStyle as DefaultTitleItemStyle;
40 /// Return a copied Style instance of DefaultTitleItem
43 /// It returns copied Style instance and changing it does not effect to the DefaultTitleItem.
44 /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
46 [EditorBrowsable(EditorBrowsableState.Never)]
47 public new DefaultTitleItemStyle Style
51 var result = new DefaultTitleItemStyle(ItemStyle);
52 result.CopyPropertiesFromView(this);
53 if (itemLabel) result.Label.CopyPropertiesFromView(itemLabel);
54 if (itemIcon) result.Icon.CopyPropertiesFromView(itemIcon);
55 if (itemSeperator) result.Seperator.CopyPropertiesFromView(itemSeperator);
61 static DefaultTitleItem() {}
64 /// Creates a new instance of DefaultTitleItem.
66 [EditorBrowsable(EditorBrowsableState.Never)]
67 public DefaultTitleItem() : base()
73 /// Creates a new instance of a DefaultTitleItem with style.
75 /// <param name="style">Create DefaultTitleItem by style defined in UX.</param>
76 [EditorBrowsable(EditorBrowsableState.Never)]
77 public DefaultTitleItem(string style) : base(style)
83 /// Creates a new instance of a DefaultTitleItem with style.
85 /// <param name="itemStyle">Create DefaultTitleItem by style customized by user.</param>
86 [EditorBrowsable(EditorBrowsableState.Never)]
87 public DefaultTitleItem(DefaultTitleItemStyle itemStyle) : base(itemStyle)
93 /// Icon part of DefaultTitleItem.
95 [EditorBrowsable(EditorBrowsableState.Never)]
100 if (itemIcon == null)
102 itemIcon = CreateIcon(ItemStyle?.Icon);
103 if (itemIcon != null)
105 layoutChanged = true;
107 itemIcon.Relayout += OnIconRelayout;
118 /* open when imageView using Uri not string.
120 /// Icon image's resource url. Only activatable for icon as ImageView.
122 [EditorBrowsable(EditorBrowsableState.Never)]
123 public string IconUrl
127 return (Icon as ImageView)?.ResourceUrl;
131 if (itemIcon != null && !(itemIcon is ImageView))
133 // Tizen.Log.Error("IconUrl only can set Icon is ImageView");
136 (Icon as ImageView).ResourceUrl = value;
142 /// DefaultTitleItem's text part of DefaultTitleItem
144 [EditorBrowsable(EditorBrowsableState.Never)]
145 public TextLabel Label
149 if (itemLabel == null)
151 itemLabel = CreateLabel(ItemStyle?.Label);
152 if (itemLabel != null)
154 layoutChanged = true;
163 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, itemLabel.Text);
168 /// The text of DefaultTitleItem.
170 [EditorBrowsable(EditorBrowsableState.Never)]
184 /// Seperator devider of DefaultTitleItem. it will place at the end of item.
186 [EditorBrowsable(EditorBrowsableState.Never)]
187 public View Seperator
191 if (itemSeperator == null)
193 itemSeperator = new View(ItemStyle?.Seperator)
195 //need to consider horizontal/vertical!
196 WidthSpecification = LayoutParamPolicies.MatchParent,
197 HeightSpecification = 2,
198 ExcludeLayouting = true
200 layoutChanged = true;
203 return itemSeperator;
208 /// Apply style to DefaultTitleItemStyle.
210 /// <param name="viewStyle">The style to apply.</param>
211 [EditorBrowsable(EditorBrowsableState.Never)]
212 public override void ApplyStyle(ViewStyle viewStyle)
215 base.ApplyStyle(viewStyle);
216 if (viewStyle != null && viewStyle is DefaultTitleItemStyle defaultStyle)
218 if (itemLabel != null)
219 itemLabel.ApplyStyle(defaultStyle.Label);
220 if (itemIcon != null)
221 itemIcon.ApplyStyle(defaultStyle.Icon);
222 if (itemSeperator != null)
223 itemSeperator.ApplyStyle(defaultStyle.Seperator);
228 [EditorBrowsable(EditorBrowsableState.Never)]
229 public override void OnRelayout(Vector2 size, RelayoutContainer container)
231 base.OnRelayout(size, container);
233 if (prevSize != Size)
238 var margin = itemSeperator.Margin;
239 itemSeperator.SizeWidth = SizeWidth - margin.Start - margin.End;
240 itemSeperator.SizeHeight = itemSeperator.HeightSpecification;
241 itemSeperator.Position = new Position(margin.Start, SizeHeight - margin.Bottom -itemSeperator.SizeHeight);
247 /// Creates Item's text part.
249 /// <return>The created Item's text part.</return>
250 [EditorBrowsable(EditorBrowsableState.Never)]
251 protected virtual TextLabel CreateLabel(TextLabelStyle style)
253 return new TextLabel(style)
255 HorizontalAlignment = HorizontalAlignment.Begin,
256 VerticalAlignment = VerticalAlignment.Center
261 /// Creates Item's icon part.
263 /// <return>The created Item's icon part.</return>
264 [EditorBrowsable(EditorBrowsableState.Never)]
265 protected virtual ImageView CreateIcon(ViewStyle style)
267 return new ImageView(style);
271 [EditorBrowsable(EditorBrowsableState.Never)]
272 protected override void MeasureChild()
277 var margin = itemLabel.Margin;
278 itemLabel.SizeWidth = SizeWidth - pad.Start - pad.End - margin.Start - margin.End;
283 [EditorBrowsable(EditorBrowsableState.Never)]
284 protected override void LayoutChild()
286 if (!layoutChanged) return;
287 if (itemLabel == null) return;
289 layoutChanged = false;
291 if (itemIcon != null)
293 RelativeLayout.SetLeftTarget(itemIcon, this);
294 RelativeLayout.SetLeftRelativeOffset(itemIcon, 1.0F);
295 RelativeLayout.SetRightTarget(itemIcon, this);
296 RelativeLayout.SetRightRelativeOffset(itemIcon, 1.0F);
297 RelativeLayout.SetTopTarget(itemIcon, this);
298 RelativeLayout.SetTopRelativeOffset(itemIcon, 0.0F);
299 RelativeLayout.SetBottomTarget(itemIcon, this);
300 RelativeLayout.SetBottomRelativeOffset(itemIcon, 1.0F);
301 RelativeLayout.SetVerticalAlignment(itemIcon, RelativeLayout.Alignment.Center);
302 RelativeLayout.SetHorizontalAlignment(itemIcon, RelativeLayout.Alignment.End);
305 RelativeLayout.SetLeftTarget(itemLabel, this);
306 RelativeLayout.SetLeftRelativeOffset(itemLabel, 0.0F);
309 RelativeLayout.SetRightTarget(itemLabel, itemIcon);
310 RelativeLayout.SetRightRelativeOffset(itemLabel, 0.0F);
314 RelativeLayout.SetRightTarget(itemLabel, this);
315 RelativeLayout.SetRightRelativeOffset(itemLabel, 1.0F);
318 RelativeLayout.SetTopTarget(itemLabel, this);
319 RelativeLayout.SetTopRelativeOffset(itemLabel, 0.0F);
320 RelativeLayout.SetBottomTarget(itemLabel, this);
321 RelativeLayout.SetBottomRelativeOffset(itemLabel, 1.0F);
322 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.Center);
323 RelativeLayout.SetHorizontalAlignment(itemLabel, RelativeLayout.Alignment.Center);
324 RelativeLayout.SetFillHorizontal(itemLabel, true);
326 if (prevSize != Size)
331 var margin = itemSeperator.Margin;
332 itemSeperator.SizeWidth = SizeWidth - margin.Start - margin.End;
333 itemSeperator.SizeHeight = itemSeperator.HeightSpecification;
334 itemSeperator.Position = new Position(margin.Start, SizeHeight - margin.Bottom -itemSeperator.SizeHeight);
340 /// Dispose Item and all children on it.
342 /// <param name="type">Dispose type.</param>
343 [EditorBrowsable(EditorBrowsableState.Never)]
344 protected override void Dispose(DisposeTypes type)
351 if (type == DisposeTypes.Explicit)
353 //Extension : Extension?.OnDispose(this);
355 if (itemIcon != null)
357 Utility.Dispose(itemIcon);
359 if (itemLabel != null)
361 Utility.Dispose(itemLabel);
363 if (itemSeperator != null)
365 Utility.Dispose(itemSeperator);
373 /// Get DefaultTitleItem style.
375 /// <returns>The default DefaultTitleItem style.</returns>
376 [EditorBrowsable(EditorBrowsableState.Never)]
377 protected override ViewStyle CreateViewStyle()
379 return new DefaultTitleItemStyle();
382 private void Initialize()
385 Layout = new RelativeLayout();
386 var seperator = Seperator;
387 layoutChanged = true;
388 LayoutDirectionChanged += OnLayoutDirectionChanged;
391 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
397 private void OnIconRelayout(object sender, EventArgs e)
403 private void OnExtraRelayout(object sender, EventArgs e)