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;
41 /// Return a copied Style instance of DefaultLinearItem
44 /// It returns copied Style instance and changing it does not effect to the DefaultLinearItem.
45 /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
47 [EditorBrowsable(EditorBrowsableState.Never)]
48 public new DefaultGridItemStyle Style
52 var result = new DefaultGridItemStyle(ItemStyle);
53 result.CopyPropertiesFromView(this);
54 if (itemCaption) result.Caption.CopyPropertiesFromView(itemCaption);
55 if (itemImage) result.Image.CopyPropertiesFromView(itemImage);
56 if (itemBadge) result.Badge.CopyPropertiesFromView(itemBadge);
62 static DefaultGridItem() { }
65 /// Creates a new instance of DefaultGridItem.
67 [EditorBrowsable(EditorBrowsableState.Never)]
68 public DefaultGridItem() : base()
74 /// Creates a new instance of DefaultGridItem with style
76 /// <param name="style=">Create DefaultGridItem by special style defined in UX.</param>
77 [EditorBrowsable(EditorBrowsableState.Never)]
78 public DefaultGridItem(string style) : base(style)
84 /// Creates a new instance of DefaultGridItem with style
86 /// <param name="itemStyle=">Create DefaultGridItem by style customized by user.</param>
87 [EditorBrowsable(EditorBrowsableState.Never)]
88 public DefaultGridItem(DefaultGridItemStyle itemStyle) : base(itemStyle)
94 /// Caption orientation.
96 [EditorBrowsable(EditorBrowsableState.Never)]
97 public enum CaptionOrientation
100 /// Outside of image bottom edge.
102 [EditorBrowsable(EditorBrowsableState.Never)]
105 /// Outside of image top edge.
107 [EditorBrowsable(EditorBrowsableState.Never)]
110 /// inside of image bottom edge.
112 [EditorBrowsable(EditorBrowsableState.Never)]
115 /// inside of image top edge.
117 [EditorBrowsable(EditorBrowsableState.Never)]
122 /// DefaultGridItem's icon part.
124 [EditorBrowsable(EditorBrowsableState.Never)]
125 public ImageView Image
129 if (itemImage == null)
131 itemImage = CreateImage(ItemStyle.Image);
132 if (itemImage != null)
135 itemImage.Relayout += OnImageRelayout;
136 layoutChanged = true;
144 layoutChanged = true;
150 /// DefaultGridItem's badge object. will be placed in right-top edge.
152 [EditorBrowsable(EditorBrowsableState.Never)]
167 if (itemBadge != null)
169 itemBadge.ApplyStyle(ItemStyle.Badge);
172 layoutChanged = true;
176 /* open when ImageView using Uri not string
178 /// Image image's resource url in DefaultGridItem.
180 [EditorBrowsable(EditorBrowsableState.Never)]
181 public string ImageUrl
185 return Image.ResourceUrl;
189 Image.ResourceUrl = value;
195 /// DefaultGridItem's text part.
197 [EditorBrowsable(EditorBrowsableState.Never)]
198 public TextLabel Caption
202 if (itemCaption == null)
204 itemCaption = CreateLabel(ItemStyle.Caption);
205 if (itemCaption != null)
208 layoutChanged = true;
216 layoutChanged = true;
217 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, itemCaption.Text);
222 /// The text of DefaultGridItem.
224 [EditorBrowsable(EditorBrowsableState.Never)]
233 Caption.Text = value;
238 /// Caption relative orientation with image in DefaultGridItem.
240 [EditorBrowsable(EditorBrowsableState.Never)]
241 public CaptionOrientation CaptionRelativeOrientation
245 return captionOrientation;
249 captionOrientation = value;
250 layoutChanged = true;
255 /// Apply style to DefaultLinearItemStyle.
257 /// <param name="viewStyle">The style to apply.</param>
258 [EditorBrowsable(EditorBrowsableState.Never)]
259 public override void ApplyStyle(ViewStyle viewStyle)
262 base.ApplyStyle(viewStyle);
263 if (viewStyle != null && viewStyle is DefaultGridItemStyle defaultStyle)
265 if (itemCaption != null)
266 itemCaption.ApplyStyle(defaultStyle.Caption);
267 if (itemImage != null)
268 itemImage.ApplyStyle(defaultStyle.Image);
269 if (itemBadge != null)
270 itemBadge.ApplyStyle(defaultStyle.Badge);
275 /// Creates Item's text part.
277 /// <return>The created Item's text part.</return>
278 [EditorBrowsable(EditorBrowsableState.Never)]
279 protected virtual TextLabel CreateLabel(TextLabelStyle textStyle)
281 return new TextLabel(textStyle)
283 HorizontalAlignment = HorizontalAlignment.Center,
284 VerticalAlignment = VerticalAlignment.Center
289 /// Creates Item's icon part.
291 /// <return>The created Item's icon part.</return>
292 [EditorBrowsable(EditorBrowsableState.Never)]
293 protected virtual ImageView CreateImage(ImageViewStyle imageStyle)
295 return new ImageView(imageStyle);
299 [EditorBrowsable(EditorBrowsableState.Never)]
300 protected override void MeasureChild()
306 var margin = itemCaption.Margin;
307 itemCaption.SizeWidth = SizeWidth - pad.Start - pad.End - margin.Start - margin.End;
312 [EditorBrowsable(EditorBrowsableState.Never)]
313 protected override void LayoutChild()
315 if (!layoutChanged) return;
316 if (itemImage == null) return;
318 layoutChanged = false;
320 RelativeLayout.SetLeftTarget(itemImage, this);
321 RelativeLayout.SetLeftRelativeOffset(itemImage, 0.0F);
322 RelativeLayout.SetRightTarget(itemImage, this);
323 RelativeLayout.SetRightRelativeOffset(itemImage, 1.0F);
324 RelativeLayout.SetHorizontalAlignment(itemImage, RelativeLayout.Alignment.Center);
326 if (itemCaption != null)
328 itemCaption.RaiseAbove(itemImage);
329 RelativeLayout.SetLeftTarget(itemCaption, itemImage);
330 RelativeLayout.SetLeftRelativeOffset(itemCaption, 0.0F);
331 RelativeLayout.SetRightTarget(itemCaption, itemImage);
332 RelativeLayout.SetRightRelativeOffset(itemCaption, 1.0F);
333 RelativeLayout.SetHorizontalAlignment(itemCaption, RelativeLayout.Alignment.Center);
334 RelativeLayout.SetFillHorizontal(itemCaption, true);
338 RelativeLayout.SetTopTarget(itemImage, this);
339 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
340 RelativeLayout.SetBottomTarget(itemImage, this);
341 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
342 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
347 RelativeLayout.SetLeftTarget(itemBadge, itemImage);
348 RelativeLayout.SetLeftRelativeOffset(itemBadge, 1.0F);
349 RelativeLayout.SetRightTarget(itemBadge, itemImage);
350 RelativeLayout.SetRightRelativeOffset(itemBadge, 1.0F);
351 RelativeLayout.SetHorizontalAlignment(itemBadge, RelativeLayout.Alignment.End);
354 switch (captionOrientation)
356 case CaptionOrientation.OutsideBottom:
357 if (itemCaption != null)
359 RelativeLayout.SetTopTarget(itemCaption, this);
360 RelativeLayout.SetTopRelativeOffset(itemCaption, 1.0F);
361 RelativeLayout.SetBottomTarget(itemCaption, this);
362 RelativeLayout.SetBottomRelativeOffset(itemCaption, 1.0F);
363 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.End);
365 RelativeLayout.SetTopTarget(itemImage, this);
366 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
367 RelativeLayout.SetBottomTarget(itemImage, itemCaption);
368 RelativeLayout.SetBottomRelativeOffset(itemImage, 0.0F);
369 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
374 RelativeLayout.SetTopTarget(itemBadge, itemImage);
375 RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
376 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
377 RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
378 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
382 case CaptionOrientation.OutsideTop:
383 if (itemCaption != null)
385 RelativeLayout.SetTopTarget(itemCaption, this);
386 RelativeLayout.SetTopRelativeOffset(itemCaption, 0.0F);
387 RelativeLayout.SetBottomTarget(itemCaption, this);
388 RelativeLayout.SetBottomRelativeOffset(itemCaption, 0.0F);
389 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.Start);
391 RelativeLayout.SetTopTarget(itemImage, itemCaption);
392 RelativeLayout.SetTopRelativeOffset(itemImage, 1.0F);
393 RelativeLayout.SetBottomTarget(itemImage, this);
394 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
395 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
400 RelativeLayout.SetTopTarget(itemBadge, itemImage);
401 RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
402 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
403 RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
404 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
408 case CaptionOrientation.InsideBottom:
409 if (itemCaption != null)
411 RelativeLayout.SetTopTarget(itemCaption, this);
412 RelativeLayout.SetTopRelativeOffset(itemCaption, 1.0F);
413 RelativeLayout.SetBottomTarget(itemCaption, this);
414 RelativeLayout.SetBottomRelativeOffset(itemCaption, 1.0F);
415 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.End);
417 RelativeLayout.SetTopTarget(itemImage, this);
418 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
419 RelativeLayout.SetBottomTarget(itemImage, this);
420 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
421 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
426 RelativeLayout.SetTopTarget(itemBadge, itemImage);
427 RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
428 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
429 RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
430 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
434 case CaptionOrientation.InsideTop:
435 if (itemCaption != null)
437 RelativeLayout.SetTopTarget(itemCaption, this);
438 RelativeLayout.SetTopRelativeOffset(itemCaption, 0.0F);
439 RelativeLayout.SetBottomTarget(itemCaption, this);
440 RelativeLayout.SetBottomRelativeOffset(itemCaption, 0.0F);
441 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.Start);
443 RelativeLayout.SetTopTarget(itemImage, this);
444 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
445 RelativeLayout.SetBottomTarget(itemImage, this);
446 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
447 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
452 RelativeLayout.SetTopTarget(itemBadge, itemImage);
453 RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
454 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
455 RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
456 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
465 [EditorBrowsable(EditorBrowsableState.Never)]
466 private void Initialize()
468 Layout = new RelativeLayout();
469 layoutChanged = true;
470 LayoutDirectionChanged += OnLayoutDirectionChanged;
474 /// Dispose Item and all children on it.
476 /// <param name="type">Dispose type.</param>
477 [EditorBrowsable(EditorBrowsableState.Never)]
478 protected override void Dispose(DisposeTypes type)
485 if (type == DisposeTypes.Explicit)
487 //Extension : Extension?.OnDispose(this);
489 if (itemImage != null)
491 Utility.Dispose(itemImage);
493 if (itemCaption != null)
495 Utility.Dispose(itemCaption);
497 if (itemBadge != null)
499 Utility.Dispose(itemBadge);
506 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
511 private void OnImageRelayout(object sender, EventArgs e)