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()
48 /// Creates a new instance of a DefaultTitleItem with style.
50 /// <param name="style">Create DefaultTitleItem by style defined in UX.</param>
51 [EditorBrowsable(EditorBrowsableState.Never)]
52 public DefaultTitleItem(string style) : base(style)
57 /// Creates a new instance of a DefaultTitleItem with style.
59 /// <param name="itemStyle">Create DefaultTitleItem by style customized by user.</param>
60 [EditorBrowsable(EditorBrowsableState.Never)]
61 public DefaultTitleItem(DefaultTitleItemStyle itemStyle) : base(itemStyle)
66 /// Icon part of DefaultTitleItem.
68 [EditorBrowsable(EditorBrowsableState.Never)]
75 itemIcon = CreateIcon(ItemStyle?.Icon);
80 itemIcon.Relayout += OnIconRelayout;
91 /* open when imageView using Uri not string.
93 /// Icon image's resource url. Only activatable for icon as ImageView.
95 [EditorBrowsable(EditorBrowsableState.Never)]
100 return (Icon as ImageView)?.ResourceUrl;
104 if (itemIcon != null && !(itemIcon is ImageView))
106 // Tizen.Log.Error("IconUrl only can set Icon is ImageView");
109 (Icon as ImageView).ResourceUrl = value;
115 /// DefaultTitleItem's text part of DefaultTitleItem
117 [EditorBrowsable(EditorBrowsableState.Never)]
118 public TextLabel Label
122 if (itemLabel == null)
124 itemLabel = CreateLabel(ItemStyle?.Label);
125 if (itemLabel != null)
127 layoutChanged = true;
136 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, itemLabel.Text);
141 /// The text of DefaultTitleItem.
143 [EditorBrowsable(EditorBrowsableState.Never)]
157 /// Seperator divider of DefaultTitleItem. it will place at the end of item.
159 [EditorBrowsable(EditorBrowsableState.Never)]
160 public View Seperator
164 if (itemSeperator == null)
166 itemSeperator = new View(ItemStyle?.Seperator)
168 //need to consider horizontal/vertical!
169 WidthSpecification = LayoutParamPolicies.MatchParent,
170 HeightSpecification = 2,
171 ExcludeLayouting = true
173 layoutChanged = true;
176 return itemSeperator;
181 /// Apply style to DefaultTitleItemStyle.
183 /// <param name="viewStyle">The style to apply.</param>
184 [EditorBrowsable(EditorBrowsableState.Never)]
185 public override void ApplyStyle(ViewStyle viewStyle)
188 base.ApplyStyle(viewStyle);
189 if (viewStyle != null && viewStyle is DefaultTitleItemStyle defaultStyle)
191 if (itemLabel != null)
192 itemLabel.ApplyStyle(defaultStyle.Label);
193 if (itemIcon != null)
194 itemIcon.ApplyStyle(defaultStyle.Icon);
195 if (itemSeperator != null)
197 itemSeperator.ApplyStyle(defaultStyle.Seperator);
198 //FIXME : currently padding and margin are not applied by ApplyStyle automatically as missing binding features.
199 itemSeperator.Margin = new Extents(defaultStyle.Seperator.Margin);
205 [EditorBrowsable(EditorBrowsableState.Never)]
206 public override void OnRelayout(Vector2 size, RelayoutContainer container)
208 base.OnRelayout(size, container);
210 if (prevSize != Size)
215 var margin = itemSeperator.Margin;
216 itemSeperator.SizeWidth = SizeWidth - margin.Start - margin.End;
217 itemSeperator.SizeHeight = itemSeperator.HeightSpecification;
218 itemSeperator.Position = new Position(margin.Start, SizeHeight - margin.Bottom - itemSeperator.SizeHeight);
224 /// Creates Item's text part.
226 /// <return>The created Item's text part.</return>
227 [EditorBrowsable(EditorBrowsableState.Never)]
228 protected virtual TextLabel CreateLabel(TextLabelStyle style)
230 return new TextLabel(style)
232 HorizontalAlignment = HorizontalAlignment.Begin,
233 VerticalAlignment = VerticalAlignment.Center
238 /// Creates Item's icon part.
240 /// <return>The created Item's icon part.</return>
241 [EditorBrowsable(EditorBrowsableState.Never)]
242 protected virtual ImageView CreateIcon(ViewStyle style)
244 return new ImageView(style);
248 [EditorBrowsable(EditorBrowsableState.Never)]
249 protected override void MeasureChild()
251 // Do measure in here if necessary.
255 [EditorBrowsable(EditorBrowsableState.Never)]
256 protected override void LayoutChild()
258 if (!layoutChanged) return;
259 if (itemLabel == null) return;
261 layoutChanged = false;
263 if (itemIcon != null)
265 RelativeLayout.SetLeftTarget(itemIcon, this);
266 RelativeLayout.SetLeftRelativeOffset(itemIcon, 1.0F);
267 RelativeLayout.SetRightTarget(itemIcon, this);
268 RelativeLayout.SetRightRelativeOffset(itemIcon, 1.0F);
269 RelativeLayout.SetTopTarget(itemIcon, this);
270 RelativeLayout.SetTopRelativeOffset(itemIcon, 0.0F);
271 RelativeLayout.SetBottomTarget(itemIcon, this);
272 RelativeLayout.SetBottomRelativeOffset(itemIcon, 1.0F);
273 RelativeLayout.SetVerticalAlignment(itemIcon, RelativeLayout.Alignment.Center);
274 RelativeLayout.SetHorizontalAlignment(itemIcon, RelativeLayout.Alignment.End);
277 RelativeLayout.SetLeftTarget(itemLabel, this);
278 RelativeLayout.SetLeftRelativeOffset(itemLabel, 0.0F);
281 RelativeLayout.SetRightTarget(itemLabel, itemIcon);
282 RelativeLayout.SetRightRelativeOffset(itemLabel, 0.0F);
286 RelativeLayout.SetRightTarget(itemLabel, this);
287 RelativeLayout.SetRightRelativeOffset(itemLabel, 1.0F);
290 RelativeLayout.SetTopTarget(itemLabel, this);
291 RelativeLayout.SetTopRelativeOffset(itemLabel, 0.0F);
292 RelativeLayout.SetBottomTarget(itemLabel, this);
293 RelativeLayout.SetBottomRelativeOffset(itemLabel, 1.0F);
294 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.Center);
295 RelativeLayout.SetHorizontalAlignment(itemLabel, RelativeLayout.Alignment.Center);
296 RelativeLayout.SetFillHorizontal(itemLabel, true);
298 if (prevSize != Size)
303 var margin = itemSeperator.Margin;
304 itemSeperator.SizeWidth = SizeWidth - margin.Start - margin.End;
305 itemSeperator.SizeHeight = itemSeperator.HeightSpecification;
306 itemSeperator.Position = new Position(margin.Start, SizeHeight - margin.Bottom - itemSeperator.SizeHeight);
312 /// Dispose Item and all children on it.
314 /// <param name="type">Dispose type.</param>
315 [EditorBrowsable(EditorBrowsableState.Never)]
316 protected override void Dispose(DisposeTypes type)
323 if (type == DisposeTypes.Explicit)
325 //Extension : Extension?.OnDispose(this);
327 if (itemIcon != null)
329 Utility.Dispose(itemIcon);
331 if (itemLabel != null)
333 Utility.Dispose(itemLabel);
335 if (itemSeperator != null)
337 Utility.Dispose(itemSeperator);
345 /// Get DefaultTitleItem style.
347 /// <returns>The default DefaultTitleItem style.</returns>
348 [EditorBrowsable(EditorBrowsableState.Never)]
349 protected override ViewStyle CreateViewStyle()
351 return new DefaultTitleItemStyle();
355 /// Initializes AT-SPI object.
357 [EditorBrowsable(EditorBrowsableState.Never)]
358 public override void OnInitialize()
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)