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 /// DefaultGridItem is one kind of common component, a DefaultGridItem clearly describes what action will occur when the user selects it.
25 /// DefaultGridItem may contain text or an icon.
27 [EditorBrowsable(EditorBrowsableState.Never)]
28 public class DefaultGridItem : RecyclerViewItem
30 private TextLabel itemCaption;
31 private ImageView itemImage;
32 private View itemBadge;
33 private CaptionOrientation captionOrientation;
34 private bool layoutChanged;
36 private DefaultGridItemStyle ItemStyle => ViewStyle as DefaultGridItemStyle;
38 static DefaultGridItem() { }
41 /// Creates a new instance of DefaultGridItem.
43 [EditorBrowsable(EditorBrowsableState.Never)]
44 public DefaultGridItem() : base()
50 /// Creates a new instance of DefaultGridItem with style
52 /// <param name="style=">Create DefaultGridItem by special style defined in UX.</param>
53 [EditorBrowsable(EditorBrowsableState.Never)]
54 public DefaultGridItem(string style) : base(style)
60 /// Creates a new instance of DefaultGridItem with style
62 /// <param name="itemStyle=">Create DefaultGridItem by style customized by user.</param>
63 [EditorBrowsable(EditorBrowsableState.Never)]
64 public DefaultGridItem(DefaultGridItemStyle itemStyle) : base(itemStyle)
70 /// Caption orientation.
72 [EditorBrowsable(EditorBrowsableState.Never)]
73 public enum CaptionOrientation
76 /// Outside of image bottom edge.
78 [EditorBrowsable(EditorBrowsableState.Never)]
81 /// Outside of image top edge.
83 [EditorBrowsable(EditorBrowsableState.Never)]
86 /// inside of image bottom edge.
88 [EditorBrowsable(EditorBrowsableState.Never)]
91 /// inside of image top edge.
93 [EditorBrowsable(EditorBrowsableState.Never)]
98 /// DefaultGridItem's icon part.
100 [EditorBrowsable(EditorBrowsableState.Never)]
101 public ImageView Image
105 if (itemImage == null)
107 itemImage = CreateImage(ItemStyle.Image);
108 if (itemImage != null)
111 itemImage.Relayout += OnImageRelayout;
112 layoutChanged = true;
120 layoutChanged = true;
126 /// DefaultGridItem's badge object. will be placed in right-top edge.
128 [EditorBrowsable(EditorBrowsableState.Never)]
143 if (itemBadge != null)
145 itemBadge.ApplyStyle(ItemStyle.Badge);
148 layoutChanged = true;
152 /* open when ImageView using Uri not string
154 /// Image image's resource url in DefaultGridItem.
156 [EditorBrowsable(EditorBrowsableState.Never)]
157 public string ImageUrl
161 return Image.ResourceUrl;
165 Image.ResourceUrl = value;
171 /// DefaultGridItem's text part.
173 [EditorBrowsable(EditorBrowsableState.Never)]
174 public TextLabel Caption
178 if (itemCaption == null)
180 itemCaption = CreateLabel(ItemStyle.Caption);
181 if (itemCaption != null)
184 layoutChanged = true;
192 layoutChanged = true;
193 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, itemCaption.Text);
198 /// The text of DefaultGridItem.
200 [EditorBrowsable(EditorBrowsableState.Never)]
209 Caption.Text = value;
214 /// Caption relative orientation with image in DefaultGridItem.
216 [EditorBrowsable(EditorBrowsableState.Never)]
217 public CaptionOrientation CaptionRelativeOrientation
221 return captionOrientation;
225 captionOrientation = value;
226 layoutChanged = true;
231 /// Apply style to DefaultLinearItemStyle.
233 /// <param name="viewStyle">The style to apply.</param>
234 [EditorBrowsable(EditorBrowsableState.Never)]
235 public override void ApplyStyle(ViewStyle viewStyle)
238 base.ApplyStyle(viewStyle);
239 if (viewStyle != null && viewStyle is DefaultGridItemStyle defaultStyle)
241 if (itemCaption != null)
242 itemCaption.ApplyStyle(defaultStyle.Caption);
243 if (itemImage != null)
244 itemImage.ApplyStyle(defaultStyle.Image);
245 if (itemBadge != null)
246 itemBadge.ApplyStyle(defaultStyle.Badge);
251 /// Creates Item's text part.
253 /// <return>The created Item's text part.</return>
254 [EditorBrowsable(EditorBrowsableState.Never)]
255 protected virtual TextLabel CreateLabel(TextLabelStyle textStyle)
257 return new TextLabel(textStyle)
259 HorizontalAlignment = HorizontalAlignment.Center,
260 VerticalAlignment = VerticalAlignment.Center
265 /// Creates Item's icon part.
267 /// <return>The created Item's icon part.</return>
268 [EditorBrowsable(EditorBrowsableState.Never)]
269 protected virtual ImageView CreateImage(ImageViewStyle imageStyle)
271 return new ImageView(imageStyle);
275 [EditorBrowsable(EditorBrowsableState.Never)]
276 protected override void MeasureChild()
282 var margin = itemCaption.Margin;
283 itemCaption.SizeWidth = SizeWidth - pad.Start - pad.End - margin.Start - margin.End;
288 [EditorBrowsable(EditorBrowsableState.Never)]
289 protected override void LayoutChild()
291 if (!layoutChanged) return;
292 if (itemImage == null) return;
294 layoutChanged = false;
296 RelativeLayout.SetLeftTarget(itemImage, this);
297 RelativeLayout.SetLeftRelativeOffset(itemImage, 0.0F);
298 RelativeLayout.SetRightTarget(itemImage, this);
299 RelativeLayout.SetRightRelativeOffset(itemImage, 1.0F);
300 RelativeLayout.SetHorizontalAlignment(itemImage, RelativeLayout.Alignment.Center);
302 if (itemCaption != null)
304 itemCaption.RaiseAbove(itemImage);
305 RelativeLayout.SetLeftTarget(itemCaption, itemImage);
306 RelativeLayout.SetLeftRelativeOffset(itemCaption, 0.0F);
307 RelativeLayout.SetRightTarget(itemCaption, itemImage);
308 RelativeLayout.SetRightRelativeOffset(itemCaption, 1.0F);
309 RelativeLayout.SetHorizontalAlignment(itemCaption, RelativeLayout.Alignment.Center);
310 RelativeLayout.SetFillHorizontal(itemCaption, true);
314 RelativeLayout.SetTopTarget(itemImage, this);
315 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
316 RelativeLayout.SetBottomTarget(itemImage, this);
317 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
318 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
323 RelativeLayout.SetLeftTarget(itemBadge, itemImage);
324 RelativeLayout.SetLeftRelativeOffset(itemBadge, 1.0F);
325 RelativeLayout.SetRightTarget(itemBadge, itemImage);
326 RelativeLayout.SetRightRelativeOffset(itemBadge, 1.0F);
327 RelativeLayout.SetHorizontalAlignment(itemBadge, RelativeLayout.Alignment.End);
330 switch (captionOrientation)
332 case CaptionOrientation.OutsideBottom:
333 if (itemCaption != null)
335 RelativeLayout.SetTopTarget(itemCaption, this);
336 RelativeLayout.SetTopRelativeOffset(itemCaption, 1.0F);
337 RelativeLayout.SetBottomTarget(itemCaption, this);
338 RelativeLayout.SetBottomRelativeOffset(itemCaption, 1.0F);
339 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.End);
341 RelativeLayout.SetTopTarget(itemImage, this);
342 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
343 RelativeLayout.SetBottomTarget(itemImage, itemCaption);
344 RelativeLayout.SetBottomRelativeOffset(itemImage, 0.0F);
345 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
350 RelativeLayout.SetTopTarget(itemBadge, itemImage);
351 RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
352 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
353 RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
354 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
358 case CaptionOrientation.OutsideTop:
359 if (itemCaption != null)
361 RelativeLayout.SetTopTarget(itemCaption, this);
362 RelativeLayout.SetTopRelativeOffset(itemCaption, 0.0F);
363 RelativeLayout.SetBottomTarget(itemCaption, this);
364 RelativeLayout.SetBottomRelativeOffset(itemCaption, 0.0F);
365 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.Start);
367 RelativeLayout.SetTopTarget(itemImage, itemCaption);
368 RelativeLayout.SetTopRelativeOffset(itemImage, 1.0F);
369 RelativeLayout.SetBottomTarget(itemImage, this);
370 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
371 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
376 RelativeLayout.SetTopTarget(itemBadge, itemImage);
377 RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
378 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
379 RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
380 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
384 case CaptionOrientation.InsideBottom:
385 if (itemCaption != null)
387 RelativeLayout.SetTopTarget(itemCaption, this);
388 RelativeLayout.SetTopRelativeOffset(itemCaption, 1.0F);
389 RelativeLayout.SetBottomTarget(itemCaption, this);
390 RelativeLayout.SetBottomRelativeOffset(itemCaption, 1.0F);
391 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.End);
393 RelativeLayout.SetTopTarget(itemImage, this);
394 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
395 RelativeLayout.SetBottomTarget(itemImage, this);
396 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
397 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
402 RelativeLayout.SetTopTarget(itemBadge, itemImage);
403 RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
404 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
405 RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
406 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
410 case CaptionOrientation.InsideTop:
411 if (itemCaption != null)
413 RelativeLayout.SetTopTarget(itemCaption, this);
414 RelativeLayout.SetTopRelativeOffset(itemCaption, 0.0F);
415 RelativeLayout.SetBottomTarget(itemCaption, this);
416 RelativeLayout.SetBottomRelativeOffset(itemCaption, 0.0F);
417 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.Start);
419 RelativeLayout.SetTopTarget(itemImage, this);
420 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
421 RelativeLayout.SetBottomTarget(itemImage, this);
422 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
423 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
428 RelativeLayout.SetTopTarget(itemBadge, itemImage);
429 RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
430 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
431 RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
432 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
441 [EditorBrowsable(EditorBrowsableState.Never)]
442 private void Initialize()
444 Layout = new RelativeLayout();
445 layoutChanged = true;
446 LayoutDirectionChanged += OnLayoutDirectionChanged;
450 /// Dispose Item and all children on it.
452 /// <param name="type">Dispose type.</param>
453 [EditorBrowsable(EditorBrowsableState.Never)]
454 protected override void Dispose(DisposeTypes type)
461 if (type == DisposeTypes.Explicit)
463 //Extension : Extension?.OnDispose(this);
465 if (itemImage != null)
467 Utility.Dispose(itemImage);
469 if (itemCaption != null)
471 Utility.Dispose(itemCaption);
473 if (itemBadge != null)
475 Utility.Dispose(itemBadge);
482 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
487 private void OnImageRelayout(object sender, EventArgs e)