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 itemLabel;
31 private ImageView itemImage;
32 private View itemBadge;
33 private LabelOrientation labelOrientation;
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()
49 /// Creates a new instance of DefaultGridItem with style
51 /// <param name="style=">Create DefaultGridItem by special style defined in UX.</param>
52 [EditorBrowsable(EditorBrowsableState.Never)]
53 public DefaultGridItem(string style) : base(style)
58 /// Creates a new instance of DefaultGridItem with style
60 /// <param name="itemStyle=">Create DefaultGridItem by style customized by user.</param>
61 [EditorBrowsable(EditorBrowsableState.Never)]
62 public DefaultGridItem(DefaultGridItemStyle itemStyle) : base(itemStyle)
67 /// Label orientation.
69 [EditorBrowsable(EditorBrowsableState.Never)]
70 public enum LabelOrientation
73 /// Outside of image bottom edge.
75 [EditorBrowsable(EditorBrowsableState.Never)]
78 /// Outside of image top edge.
80 [EditorBrowsable(EditorBrowsableState.Never)]
83 /// inside of image bottom edge.
85 [EditorBrowsable(EditorBrowsableState.Never)]
88 /// inside of image top edge.
90 [EditorBrowsable(EditorBrowsableState.Never)]
95 /// DefaultGridItem's icon part.
97 [EditorBrowsable(EditorBrowsableState.Never)]
98 public ImageView Image
102 if (itemImage == null)
104 itemImage = CreateImage(ItemStyle.Image);
105 if (itemImage != null)
108 itemImage.Relayout += OnImageRelayout;
109 layoutChanged = true;
116 if (itemImage != null) Remove(itemImage);
118 if (itemImage != null)
120 //FIXME: User applied image's style can be overwritten!
121 if (ItemStyle != null) itemImage.ApplyStyle(ItemStyle.Image);
123 itemImage.Relayout += OnImageRelayout;
125 layoutChanged = true;
131 /// DefaultGridItem's badge object. will be placed in right-top edge.
133 [EditorBrowsable(EditorBrowsableState.Never)]
143 if (itemBadge != null) Remove(itemBadge);
145 if (itemBadge != null)
147 //FIXME: User applied badge's style can be overwritten!
148 if (ItemStyle != null) itemBadge.ApplyStyle(ItemStyle.Badge);
151 layoutChanged = true;
155 /* open when ImageView using Uri not string
157 /// Image image's resource url in DefaultGridItem.
159 [EditorBrowsable(EditorBrowsableState.Never)]
160 public string ImageUrl
164 return Image.ResourceUrl;
168 Image.ResourceUrl = value;
174 /// DefaultGridItem's text part.
176 [EditorBrowsable(EditorBrowsableState.Never)]
177 public TextLabel Label
181 if (itemLabel == null)
183 itemLabel = CreateLabel(ItemStyle.Label);
184 if (itemLabel != null)
187 layoutChanged = true;
195 layoutChanged = true;
196 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, itemLabel.Text);
201 /// The text of DefaultGridItem.
203 [EditorBrowsable(EditorBrowsableState.Never)]
217 /// Label relative orientation with image in DefaultGridItem.
219 [EditorBrowsable(EditorBrowsableState.Never)]
220 public LabelOrientation LabelOrientationType
224 return labelOrientation;
228 labelOrientation = value;
229 layoutChanged = true;
234 /// Apply style to DefaultLinearItemStyle.
236 /// <param name="viewStyle">The style to apply.</param>
237 [EditorBrowsable(EditorBrowsableState.Never)]
238 public override void ApplyStyle(ViewStyle viewStyle)
241 base.ApplyStyle(viewStyle);
242 if (viewStyle != null && viewStyle is DefaultGridItemStyle defaultStyle)
244 if (itemLabel != null)
245 itemLabel.ApplyStyle(defaultStyle.Label);
246 if (itemImage != null)
247 itemImage.ApplyStyle(defaultStyle.Image);
248 if (itemBadge != null)
249 itemBadge.ApplyStyle(defaultStyle.Badge);
254 /// Creates Item's text part.
256 /// <return>The created Item's text part.</return>
257 [EditorBrowsable(EditorBrowsableState.Never)]
258 protected virtual TextLabel CreateLabel(TextLabelStyle textStyle)
260 return new TextLabel(textStyle)
262 HorizontalAlignment = HorizontalAlignment.Center,
263 VerticalAlignment = VerticalAlignment.Center
268 /// Creates Item's icon part.
270 /// <return>The created Item's icon part.</return>
271 [EditorBrowsable(EditorBrowsableState.Never)]
272 protected virtual ImageView CreateImage(ImageViewStyle imageStyle)
274 return new ImageView(imageStyle);
278 [EditorBrowsable(EditorBrowsableState.Never)]
279 protected override void MeasureChild()
285 var margin = itemLabel.Margin;
286 itemLabel.SizeWidth = SizeWidth - pad.Start - pad.End - margin.Start - margin.End;
291 [EditorBrowsable(EditorBrowsableState.Never)]
292 protected override void LayoutChild()
294 if (!layoutChanged) return;
295 if (itemImage == null) return;
297 layoutChanged = false;
299 RelativeLayout.SetLeftTarget(itemImage, this);
300 RelativeLayout.SetLeftRelativeOffset(itemImage, 0.0F);
301 RelativeLayout.SetRightTarget(itemImage, this);
302 RelativeLayout.SetRightRelativeOffset(itemImage, 1.0F);
303 RelativeLayout.SetHorizontalAlignment(itemImage, RelativeLayout.Alignment.Center);
305 if (itemLabel != null)
307 itemLabel.RaiseAbove(itemImage);
308 RelativeLayout.SetLeftTarget(itemLabel, itemImage);
309 RelativeLayout.SetLeftRelativeOffset(itemLabel, 0.0F);
310 RelativeLayout.SetRightTarget(itemLabel, itemImage);
311 RelativeLayout.SetRightRelativeOffset(itemLabel, 1.0F);
312 RelativeLayout.SetHorizontalAlignment(itemLabel, RelativeLayout.Alignment.Center);
313 RelativeLayout.SetFillHorizontal(itemLabel, true);
317 RelativeLayout.SetTopTarget(itemImage, this);
318 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
319 RelativeLayout.SetBottomTarget(itemImage, this);
320 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
321 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
326 RelativeLayout.SetLeftTarget(itemBadge, itemImage);
327 RelativeLayout.SetLeftRelativeOffset(itemBadge, 1.0F);
328 RelativeLayout.SetRightTarget(itemBadge, itemImage);
329 RelativeLayout.SetRightRelativeOffset(itemBadge, 1.0F);
330 RelativeLayout.SetHorizontalAlignment(itemBadge, RelativeLayout.Alignment.End);
333 switch (labelOrientation)
335 case LabelOrientation.OutsideBottom:
336 if (itemLabel != null)
338 RelativeLayout.SetTopTarget(itemLabel, this);
339 RelativeLayout.SetTopRelativeOffset(itemLabel, 1.0F);
340 RelativeLayout.SetBottomTarget(itemLabel, this);
341 RelativeLayout.SetBottomRelativeOffset(itemLabel, 1.0F);
342 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.End);
344 RelativeLayout.SetTopTarget(itemImage, this);
345 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
346 RelativeLayout.SetBottomTarget(itemImage, itemLabel);
347 RelativeLayout.SetBottomRelativeOffset(itemImage, 0.0F);
348 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
353 RelativeLayout.SetTopTarget(itemBadge, itemImage);
354 RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
355 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
356 RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
357 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
361 case LabelOrientation.OutsideTop:
362 if (itemLabel != null)
364 RelativeLayout.SetTopTarget(itemLabel, this);
365 RelativeLayout.SetTopRelativeOffset(itemLabel, 0.0F);
366 RelativeLayout.SetBottomTarget(itemLabel, this);
367 RelativeLayout.SetBottomRelativeOffset(itemLabel, 0.0F);
368 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.Start);
370 RelativeLayout.SetTopTarget(itemImage, itemLabel);
371 RelativeLayout.SetTopRelativeOffset(itemImage, 1.0F);
372 RelativeLayout.SetBottomTarget(itemImage, this);
373 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
374 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
379 RelativeLayout.SetTopTarget(itemBadge, itemImage);
380 RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
381 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
382 RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
383 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
387 case LabelOrientation.InsideBottom:
388 if (itemLabel != null)
390 RelativeLayout.SetTopTarget(itemLabel, this);
391 RelativeLayout.SetTopRelativeOffset(itemLabel, 1.0F);
392 RelativeLayout.SetBottomTarget(itemLabel, this);
393 RelativeLayout.SetBottomRelativeOffset(itemLabel, 1.0F);
394 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.End);
396 RelativeLayout.SetTopTarget(itemImage, this);
397 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
398 RelativeLayout.SetBottomTarget(itemImage, this);
399 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
400 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
405 RelativeLayout.SetTopTarget(itemBadge, itemImage);
406 RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
407 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
408 RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
409 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
413 case LabelOrientation.InsideTop:
414 if (itemLabel != null)
416 RelativeLayout.SetTopTarget(itemLabel, this);
417 RelativeLayout.SetTopRelativeOffset(itemLabel, 0.0F);
418 RelativeLayout.SetBottomTarget(itemLabel, this);
419 RelativeLayout.SetBottomRelativeOffset(itemLabel, 0.0F);
420 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.Start);
422 RelativeLayout.SetTopTarget(itemImage, this);
423 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
424 RelativeLayout.SetBottomTarget(itemImage, this);
425 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
426 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
431 RelativeLayout.SetTopTarget(itemBadge, itemImage);
432 RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
433 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
434 RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
435 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
444 /// Initializes AT-SPI object.
446 [EditorBrowsable(EditorBrowsableState.Never)]
447 public override void OnInitialize()
450 Layout = new RelativeLayout();
451 layoutChanged = true;
452 LayoutDirectionChanged += OnLayoutDirectionChanged;
456 /// Dispose Item and all children on it.
458 /// <param name="type">Dispose type.</param>
459 [EditorBrowsable(EditorBrowsableState.Never)]
460 protected override void Dispose(DisposeTypes type)
467 if (type == DisposeTypes.Explicit)
469 //Extension : Extension?.OnDispose(this);
471 // Arugable to disposing user-created members.
473 if (itemBadge != null)
475 Utility.Dispose(itemBadge);
479 if (itemImage != null)
481 Utility.Dispose(itemImage);
484 if (itemLabel != null)
486 Utility.Dispose(itemLabel);
495 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
500 private void OnImageRelayout(object sender, EventArgs e)