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.Accessibility;
21 namespace Tizen.NUI.Components
24 /// DefaultTitleItem is one kind of common component, a DefaultTitleItem clearly describes what action will occur when the user selects it.
25 /// DefaultTitleItem may contain text or an icon.
27 [EditorBrowsable(EditorBrowsableState.Never)]
28 public class DefaultTitleItem : RecyclerViewItem
30 private TextLabel itemLabel;
31 private View itemIcon;
32 private View itemSeperator;
33 private bool layoutChanged;
34 private Size prevSize;
35 private DefaultTitleItemStyle ItemStyle => ViewStyle as DefaultTitleItemStyle;
37 static DefaultTitleItem() { }
40 /// Creates a new instance of DefaultTitleItem.
42 [EditorBrowsable(EditorBrowsableState.Never)]
43 public DefaultTitleItem() : base()
49 /// Creates a new instance of a DefaultTitleItem with style.
51 /// <param name="style">Create DefaultTitleItem by style defined in UX.</param>
52 [EditorBrowsable(EditorBrowsableState.Never)]
53 public DefaultTitleItem(string style) : base(style)
59 /// Creates a new instance of a DefaultTitleItem with style.
61 /// <param name="itemStyle">Create DefaultTitleItem by style customized by user.</param>
62 [EditorBrowsable(EditorBrowsableState.Never)]
63 public DefaultTitleItem(DefaultTitleItemStyle itemStyle) : base(itemStyle)
69 /// Icon part of DefaultTitleItem.
71 [EditorBrowsable(EditorBrowsableState.Never)]
78 itemIcon = CreateIcon(ItemStyle?.Icon);
83 itemIcon.Relayout += OnIconRelayout;
94 /* open when imageView using Uri not string.
96 /// Icon image's resource url. Only activatable for icon as ImageView.
98 [EditorBrowsable(EditorBrowsableState.Never)]
103 return (Icon as ImageView)?.ResourceUrl;
107 if (itemIcon != null && !(itemIcon is ImageView))
109 // Tizen.Log.Error("IconUrl only can set Icon is ImageView");
112 (Icon as ImageView).ResourceUrl = value;
118 /// DefaultTitleItem's text part of DefaultTitleItem
120 [EditorBrowsable(EditorBrowsableState.Never)]
121 public TextLabel Label
125 if (itemLabel == null)
127 itemLabel = CreateLabel(ItemStyle?.Label);
128 if (itemLabel != null)
130 layoutChanged = true;
139 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, itemLabel.Text);
144 /// The text of DefaultTitleItem.
146 [EditorBrowsable(EditorBrowsableState.Never)]
160 /// Seperator divider of DefaultTitleItem. it will place at the end of item.
162 [EditorBrowsable(EditorBrowsableState.Never)]
163 public View Seperator
167 if (itemSeperator == null)
169 itemSeperator = new View(ItemStyle?.Seperator)
171 //need to consider horizontal/vertical!
172 WidthSpecification = LayoutParamPolicies.MatchParent,
173 HeightSpecification = 2,
174 ExcludeLayouting = true
176 layoutChanged = true;
179 return itemSeperator;
184 /// Apply style to DefaultTitleItemStyle.
186 /// <param name="viewStyle">The style to apply.</param>
187 [EditorBrowsable(EditorBrowsableState.Never)]
188 public override void ApplyStyle(ViewStyle viewStyle)
191 base.ApplyStyle(viewStyle);
192 if (viewStyle != null && viewStyle is DefaultTitleItemStyle defaultStyle)
194 if (itemLabel != null)
195 itemLabel.ApplyStyle(defaultStyle.Label);
196 if (itemIcon != null)
197 itemIcon.ApplyStyle(defaultStyle.Icon);
198 if (itemSeperator != null)
199 itemSeperator.ApplyStyle(defaultStyle.Seperator);
204 [EditorBrowsable(EditorBrowsableState.Never)]
205 public override void OnRelayout(Vector2 size, RelayoutContainer container)
207 base.OnRelayout(size, container);
209 if (prevSize != Size)
214 var margin = itemSeperator.Margin;
215 itemSeperator.SizeWidth = SizeWidth - margin.Start - margin.End;
216 itemSeperator.SizeHeight = itemSeperator.HeightSpecification;
217 itemSeperator.Position = new Position(margin.Start, SizeHeight - margin.Bottom - itemSeperator.SizeHeight);
223 /// Creates Item's text part.
225 /// <return>The created Item's text part.</return>
226 [EditorBrowsable(EditorBrowsableState.Never)]
227 protected virtual TextLabel CreateLabel(TextLabelStyle style)
229 return new TextLabel(style)
231 HorizontalAlignment = HorizontalAlignment.Begin,
232 VerticalAlignment = VerticalAlignment.Center
237 /// Creates Item's icon part.
239 /// <return>The created Item's icon part.</return>
240 [EditorBrowsable(EditorBrowsableState.Never)]
241 protected virtual ImageView CreateIcon(ViewStyle style)
243 return new ImageView(style);
247 [EditorBrowsable(EditorBrowsableState.Never)]
248 protected override void MeasureChild()
253 var margin = itemLabel.Margin;
254 itemLabel.SizeWidth = SizeWidth - pad.Start - pad.End - margin.Start - margin.End;
259 [EditorBrowsable(EditorBrowsableState.Never)]
260 protected override void LayoutChild()
262 if (!layoutChanged) return;
263 if (itemLabel == null) return;
265 layoutChanged = false;
267 if (itemIcon != null)
269 RelativeLayout.SetLeftTarget(itemIcon, this);
270 RelativeLayout.SetLeftRelativeOffset(itemIcon, 1.0F);
271 RelativeLayout.SetRightTarget(itemIcon, this);
272 RelativeLayout.SetRightRelativeOffset(itemIcon, 1.0F);
273 RelativeLayout.SetTopTarget(itemIcon, this);
274 RelativeLayout.SetTopRelativeOffset(itemIcon, 0.0F);
275 RelativeLayout.SetBottomTarget(itemIcon, this);
276 RelativeLayout.SetBottomRelativeOffset(itemIcon, 1.0F);
277 RelativeLayout.SetVerticalAlignment(itemIcon, RelativeLayout.Alignment.Center);
278 RelativeLayout.SetHorizontalAlignment(itemIcon, RelativeLayout.Alignment.End);
281 RelativeLayout.SetLeftTarget(itemLabel, this);
282 RelativeLayout.SetLeftRelativeOffset(itemLabel, 0.0F);
285 RelativeLayout.SetRightTarget(itemLabel, itemIcon);
286 RelativeLayout.SetRightRelativeOffset(itemLabel, 0.0F);
290 RelativeLayout.SetRightTarget(itemLabel, this);
291 RelativeLayout.SetRightRelativeOffset(itemLabel, 1.0F);
294 RelativeLayout.SetTopTarget(itemLabel, this);
295 RelativeLayout.SetTopRelativeOffset(itemLabel, 0.0F);
296 RelativeLayout.SetBottomTarget(itemLabel, this);
297 RelativeLayout.SetBottomRelativeOffset(itemLabel, 1.0F);
298 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.Center);
299 RelativeLayout.SetHorizontalAlignment(itemLabel, RelativeLayout.Alignment.Center);
300 RelativeLayout.SetFillHorizontal(itemLabel, true);
302 if (prevSize != Size)
307 var margin = itemSeperator.Margin;
308 itemSeperator.SizeWidth = SizeWidth - margin.Start - margin.End;
309 itemSeperator.SizeHeight = itemSeperator.HeightSpecification;
310 itemSeperator.Position = new Position(margin.Start, SizeHeight - margin.Bottom - itemSeperator.SizeHeight);
316 /// Dispose Item and all children on it.
318 /// <param name="type">Dispose type.</param>
319 [EditorBrowsable(EditorBrowsableState.Never)]
320 protected override void Dispose(DisposeTypes type)
327 if (type == DisposeTypes.Explicit)
329 //Extension : Extension?.OnDispose(this);
331 if (itemIcon != null)
333 Utility.Dispose(itemIcon);
335 if (itemLabel != null)
337 Utility.Dispose(itemLabel);
339 if (itemSeperator != null)
341 Utility.Dispose(itemSeperator);
349 /// Get DefaultTitleItem style.
351 /// <returns>The default DefaultTitleItem style.</returns>
352 [EditorBrowsable(EditorBrowsableState.Never)]
353 protected override ViewStyle CreateViewStyle()
355 return new DefaultTitleItemStyle();
358 private void Initialize()
361 Layout = new RelativeLayout();
362 var seperator = Seperator;
363 layoutChanged = true;
364 LayoutDirectionChanged += OnLayoutDirectionChanged;
367 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
373 private void OnIconRelayout(object sender, EventArgs e)
379 private void OnExtraRelayout(object sender, EventArgs e)