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 /// DefaultGridItem is one kind of common component, a DefaultGridItem clearly describes what action will occur when the user selects it.
27 /// DefaultGridItem may contain text or an icon.
29 [EditorBrowsable(EditorBrowsableState.Never)]
30 public class DefaultGridItem : RecyclerViewItem
32 private TextLabel itemCaption;
33 private ImageView itemImage;
34 private View itemBadge;
35 private CaptionOrientation captionOrientation;
36 private bool layoutChanged;
38 private DefaultGridItemStyle ItemStyle => ViewStyle as DefaultGridItemStyle;
40 static DefaultGridItem() { }
43 /// Creates a new instance of DefaultGridItem.
45 [EditorBrowsable(EditorBrowsableState.Never)]
46 public DefaultGridItem() : base()
52 /// Creates a new instance of DefaultGridItem with style
54 /// <param name="style=">Create DefaultGridItem by special style defined in UX.</param>
55 [EditorBrowsable(EditorBrowsableState.Never)]
56 public DefaultGridItem(string style) : base(style)
62 /// Creates a new instance of DefaultGridItem with style
64 /// <param name="itemStyle=">Create DefaultGridItem by style customized by user.</param>
65 [EditorBrowsable(EditorBrowsableState.Never)]
66 public DefaultGridItem(DefaultGridItemStyle itemStyle) : base(itemStyle)
72 /// Caption orientation.
74 [EditorBrowsable(EditorBrowsableState.Never)]
75 public enum CaptionOrientation
78 /// Outside of image bottom edge.
80 [EditorBrowsable(EditorBrowsableState.Never)]
83 /// Outside of image top edge.
85 [EditorBrowsable(EditorBrowsableState.Never)]
88 /// inside of image bottom edge.
90 [EditorBrowsable(EditorBrowsableState.Never)]
93 /// inside of image top edge.
95 [EditorBrowsable(EditorBrowsableState.Never)]
100 /// DefaultGridItem's icon part.
102 [EditorBrowsable(EditorBrowsableState.Never)]
103 public ImageView Image
107 if (itemImage == null)
109 itemImage = CreateImage(ItemStyle.Image);
110 if (itemImage != null)
113 itemImage.Relayout += OnImageRelayout;
114 layoutChanged = true;
122 layoutChanged = true;
128 /// DefaultGridItem's badge object. will be placed in right-top edge.
130 [EditorBrowsable(EditorBrowsableState.Never)]
145 if (itemBadge != null)
147 itemBadge.ApplyStyle(ItemStyle.Badge);
150 layoutChanged = true;
154 /* open when ImageView using Uri not string
156 /// Image image's resource url in DefaultGridItem.
158 [EditorBrowsable(EditorBrowsableState.Never)]
159 public string ImageUrl
163 return Image.ResourceUrl;
167 Image.ResourceUrl = value;
173 /// DefaultGridItem's text part.
175 [EditorBrowsable(EditorBrowsableState.Never)]
176 public TextLabel Caption
180 if (itemCaption == null)
182 itemCaption = CreateLabel(ItemStyle.Caption);
183 if (itemCaption != null)
186 layoutChanged = true;
194 layoutChanged = true;
195 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, itemCaption.Text);
200 /// The text of DefaultGridItem.
202 [EditorBrowsable(EditorBrowsableState.Never)]
211 Caption.Text = value;
216 /// Caption relative orientation with image in DefaultGridItem.
218 [EditorBrowsable(EditorBrowsableState.Never)]
219 public CaptionOrientation CaptionRelativeOrientation
223 return captionOrientation;
227 captionOrientation = value;
228 layoutChanged = true;
233 /// Apply style to DefaultLinearItemStyle.
235 /// <param name="viewStyle">The style to apply.</param>
236 [EditorBrowsable(EditorBrowsableState.Never)]
237 public override void ApplyStyle(ViewStyle viewStyle)
240 base.ApplyStyle(viewStyle);
241 if (viewStyle != null && viewStyle is DefaultGridItemStyle defaultStyle)
243 if (itemCaption != null)
244 itemCaption.ApplyStyle(defaultStyle.Caption);
245 if (itemImage != null)
246 itemImage.ApplyStyle(defaultStyle.Image);
247 if (itemBadge != null)
248 itemBadge.ApplyStyle(defaultStyle.Badge);
253 /// Creates Item's text part.
255 /// <return>The created Item's text part.</return>
256 [EditorBrowsable(EditorBrowsableState.Never)]
257 protected virtual TextLabel CreateLabel(TextLabelStyle textStyle)
259 return new TextLabel(textStyle)
261 HorizontalAlignment = HorizontalAlignment.Center,
262 VerticalAlignment = VerticalAlignment.Center
267 /// Creates Item's icon part.
269 /// <return>The created Item's icon part.</return>
270 [EditorBrowsable(EditorBrowsableState.Never)]
271 protected virtual ImageView CreateImage(ImageViewStyle imageStyle)
273 return new ImageView(imageStyle);
277 [EditorBrowsable(EditorBrowsableState.Never)]
278 protected override void MeasureChild()
284 var margin = itemCaption.Margin;
285 itemCaption.SizeWidth = SizeWidth - pad.Start - pad.End - margin.Start - margin.End;
290 [EditorBrowsable(EditorBrowsableState.Never)]
291 protected override void LayoutChild()
293 if (!layoutChanged) return;
294 if (itemImage == null) return;
296 layoutChanged = false;
298 RelativeLayout.SetLeftTarget(itemImage, this);
299 RelativeLayout.SetLeftRelativeOffset(itemImage, 0.0F);
300 RelativeLayout.SetRightTarget(itemImage, this);
301 RelativeLayout.SetRightRelativeOffset(itemImage, 1.0F);
302 RelativeLayout.SetHorizontalAlignment(itemImage, RelativeLayout.Alignment.Center);
304 if (itemCaption != null)
306 itemCaption.RaiseAbove(itemImage);
307 RelativeLayout.SetLeftTarget(itemCaption, itemImage);
308 RelativeLayout.SetLeftRelativeOffset(itemCaption, 0.0F);
309 RelativeLayout.SetRightTarget(itemCaption, itemImage);
310 RelativeLayout.SetRightRelativeOffset(itemCaption, 1.0F);
311 RelativeLayout.SetHorizontalAlignment(itemCaption, RelativeLayout.Alignment.Center);
312 RelativeLayout.SetFillHorizontal(itemCaption, true);
316 RelativeLayout.SetTopTarget(itemImage, this);
317 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
318 RelativeLayout.SetBottomTarget(itemImage, this);
319 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
320 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
325 RelativeLayout.SetLeftTarget(itemBadge, itemImage);
326 RelativeLayout.SetLeftRelativeOffset(itemBadge, 1.0F);
327 RelativeLayout.SetRightTarget(itemBadge, itemImage);
328 RelativeLayout.SetRightRelativeOffset(itemBadge, 1.0F);
329 RelativeLayout.SetHorizontalAlignment(itemBadge, RelativeLayout.Alignment.End);
332 switch (captionOrientation)
334 case CaptionOrientation.OutsideBottom:
335 if (itemCaption != null)
337 RelativeLayout.SetTopTarget(itemCaption, this);
338 RelativeLayout.SetTopRelativeOffset(itemCaption, 1.0F);
339 RelativeLayout.SetBottomTarget(itemCaption, this);
340 RelativeLayout.SetBottomRelativeOffset(itemCaption, 1.0F);
341 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.End);
343 RelativeLayout.SetTopTarget(itemImage, this);
344 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
345 RelativeLayout.SetBottomTarget(itemImage, itemCaption);
346 RelativeLayout.SetBottomRelativeOffset(itemImage, 0.0F);
347 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
352 RelativeLayout.SetTopTarget(itemBadge, itemImage);
353 RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
354 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
355 RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
356 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
360 case CaptionOrientation.OutsideTop:
361 if (itemCaption != null)
363 RelativeLayout.SetTopTarget(itemCaption, this);
364 RelativeLayout.SetTopRelativeOffset(itemCaption, 0.0F);
365 RelativeLayout.SetBottomTarget(itemCaption, this);
366 RelativeLayout.SetBottomRelativeOffset(itemCaption, 0.0F);
367 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.Start);
369 RelativeLayout.SetTopTarget(itemImage, itemCaption);
370 RelativeLayout.SetTopRelativeOffset(itemImage, 1.0F);
371 RelativeLayout.SetBottomTarget(itemImage, this);
372 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
373 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
378 RelativeLayout.SetTopTarget(itemBadge, itemImage);
379 RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
380 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
381 RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
382 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
386 case CaptionOrientation.InsideBottom:
387 if (itemCaption != null)
389 RelativeLayout.SetTopTarget(itemCaption, this);
390 RelativeLayout.SetTopRelativeOffset(itemCaption, 1.0F);
391 RelativeLayout.SetBottomTarget(itemCaption, this);
392 RelativeLayout.SetBottomRelativeOffset(itemCaption, 1.0F);
393 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.End);
395 RelativeLayout.SetTopTarget(itemImage, this);
396 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
397 RelativeLayout.SetBottomTarget(itemImage, this);
398 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
399 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
404 RelativeLayout.SetTopTarget(itemBadge, itemImage);
405 RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
406 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
407 RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
408 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
412 case CaptionOrientation.InsideTop:
413 if (itemCaption != null)
415 RelativeLayout.SetTopTarget(itemCaption, this);
416 RelativeLayout.SetTopRelativeOffset(itemCaption, 0.0F);
417 RelativeLayout.SetBottomTarget(itemCaption, this);
418 RelativeLayout.SetBottomRelativeOffset(itemCaption, 0.0F);
419 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.Start);
421 RelativeLayout.SetTopTarget(itemImage, this);
422 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
423 RelativeLayout.SetBottomTarget(itemImage, this);
424 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
425 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
430 RelativeLayout.SetTopTarget(itemBadge, itemImage);
431 RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
432 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
433 RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
434 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
443 [EditorBrowsable(EditorBrowsableState.Never)]
444 private void Initialize()
446 Layout = new RelativeLayout();
447 layoutChanged = true;
448 LayoutDirectionChanged += OnLayoutDirectionChanged;
452 /// Dispose Item and all children on it.
454 /// <param name="type">Dispose type.</param>
455 [EditorBrowsable(EditorBrowsableState.Never)]
456 protected override void Dispose(DisposeTypes type)
463 if (type == DisposeTypes.Explicit)
465 //Extension : Extension?.OnDispose(this);
467 if (itemImage != null)
469 Utility.Dispose(itemImage);
471 if (itemCaption != null)
473 Utility.Dispose(itemCaption);
475 if (itemBadge != null)
477 Utility.Dispose(itemBadge);
484 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
489 private void OnImageRelayout(object sender, EventArgs e)