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 partial 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)]
138 return GetValue(BadgeProperty) as View;
142 SetValue(BadgeProperty, value);
143 NotifyPropertyChanged();
146 private View InternalBadge
155 if (itemBadge != null) Remove(itemBadge);
157 if (itemBadge != null)
159 //FIXME: User applied badge's style can be overwritten!
160 if (ItemStyle != null) itemBadge.ApplyStyle(ItemStyle.Badge);
163 layoutChanged = true;
168 /// Image resource url in DefaultGridItem.
170 [EditorBrowsable(EditorBrowsableState.Never)]
171 public string ResourceUrl
173 get => GetValue(ResourceUrlProperty) as string;
176 SetValue(ResourceUrlProperty, value);
177 NotifyPropertyChanged();
181 internal string InternalResourceUrl
185 return Image.ResourceUrl;
189 Image.ResourceUrl = value;
195 /// DefaultGridItem's text part.
197 [EditorBrowsable(EditorBrowsableState.Never)]
198 public TextLabel Label
202 if (itemLabel == null)
204 itemLabel = CreateLabel(ItemStyle.Label);
205 if (itemLabel != null)
208 layoutChanged = true;
216 layoutChanged = true;
221 /// The text of DefaultGridItem.
223 [EditorBrowsable(EditorBrowsableState.Never)]
228 return GetValue(TextProperty) as string;
232 SetValue(TextProperty, value);
233 NotifyPropertyChanged();
236 private string InternalText
249 /// Label relative orientation with image in DefaultGridItem.
251 [EditorBrowsable(EditorBrowsableState.Never)]
252 public LabelOrientation LabelOrientationType
256 return (Tizen.NUI.Components.DefaultGridItem.LabelOrientation)GetValue(LabelOrientationTypeProperty);
260 SetValue(LabelOrientationTypeProperty, value);
261 NotifyPropertyChanged();
264 private Tizen.NUI.Components.DefaultGridItem.LabelOrientation InternalLabelOrientationType
268 return labelOrientation;
272 labelOrientation = value;
273 layoutChanged = true;
278 /// Apply style to DefaultLinearItemStyle.
280 /// <param name="viewStyle">The style to apply.</param>
281 [EditorBrowsable(EditorBrowsableState.Never)]
282 public override void ApplyStyle(ViewStyle viewStyle)
285 base.ApplyStyle(viewStyle);
286 if (viewStyle != null && viewStyle is DefaultGridItemStyle defaultStyle)
288 if (itemLabel != null)
289 itemLabel.ApplyStyle(defaultStyle.Label);
290 if (itemImage != null)
291 itemImage.ApplyStyle(defaultStyle.Image);
292 if (itemBadge != null)
293 itemBadge.ApplyStyle(defaultStyle.Badge);
298 /// Creates Item's text part.
300 /// <return>The created Item's text part.</return>
301 [EditorBrowsable(EditorBrowsableState.Never)]
302 protected virtual TextLabel CreateLabel(TextLabelStyle textStyle)
304 return new TextLabel(textStyle)
306 HorizontalAlignment = HorizontalAlignment.Center,
307 VerticalAlignment = VerticalAlignment.Center
312 /// Creates Item's icon part.
314 /// <return>The created Item's icon part.</return>
315 [EditorBrowsable(EditorBrowsableState.Never)]
316 protected virtual ImageView CreateImage(ImageViewStyle imageStyle)
318 return new ImageView(imageStyle);
322 [EditorBrowsable(EditorBrowsableState.Never)]
323 protected override void MeasureChild()
329 var margin = itemLabel.Margin;
330 itemLabel.SizeWidth = SizeWidth - pad.Start - pad.End - margin.Start - margin.End;
335 [EditorBrowsable(EditorBrowsableState.Never)]
336 protected override void LayoutChild()
338 if (!layoutChanged) return;
339 if (itemImage == null) return;
341 layoutChanged = false;
343 RelativeLayout.SetLeftTarget(itemImage, this);
344 RelativeLayout.SetLeftRelativeOffset(itemImage, 0.0F);
345 RelativeLayout.SetRightTarget(itemImage, this);
346 RelativeLayout.SetRightRelativeOffset(itemImage, 1.0F);
347 RelativeLayout.SetHorizontalAlignment(itemImage, RelativeLayout.Alignment.Center);
348 RelativeLayout.SetFillHorizontal(itemImage, true);
349 RelativeLayout.SetFillVertical(itemImage, true);
351 if (itemLabel != null)
353 itemLabel.RaiseAbove(itemImage);
354 RelativeLayout.SetLeftTarget(itemLabel, itemImage);
355 RelativeLayout.SetLeftRelativeOffset(itemLabel, 0.0F);
356 RelativeLayout.SetRightTarget(itemLabel, itemImage);
357 RelativeLayout.SetRightRelativeOffset(itemLabel, 1.0F);
358 RelativeLayout.SetHorizontalAlignment(itemLabel, RelativeLayout.Alignment.Center);
359 RelativeLayout.SetFillHorizontal(itemLabel, true);
363 RelativeLayout.SetTopTarget(itemImage, this);
364 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
365 RelativeLayout.SetBottomTarget(itemImage, this);
366 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
367 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
372 itemBadge.RaiseAbove(itemImage);
373 RelativeLayout.SetLeftTarget(itemBadge, itemImage);
374 RelativeLayout.SetLeftRelativeOffset(itemBadge, 1.0F);
375 RelativeLayout.SetRightTarget(itemBadge, itemImage);
376 RelativeLayout.SetRightRelativeOffset(itemBadge, 1.0F);
377 RelativeLayout.SetHorizontalAlignment(itemBadge, RelativeLayout.Alignment.End);
380 switch (labelOrientation)
382 case LabelOrientation.OutsideBottom:
383 if (itemLabel != null)
385 RelativeLayout.SetTopTarget(itemLabel, this);
386 RelativeLayout.SetTopRelativeOffset(itemLabel, 1.0F);
387 RelativeLayout.SetBottomTarget(itemLabel, this);
388 RelativeLayout.SetBottomRelativeOffset(itemLabel, 1.0F);
389 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.End);
391 RelativeLayout.SetTopTarget(itemImage, this);
392 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
393 RelativeLayout.SetBottomTarget(itemImage, itemLabel);
394 RelativeLayout.SetBottomRelativeOffset(itemImage, 0.0F);
395 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
400 RelativeLayout.SetTopTarget(itemBadge, itemImage);
401 RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
402 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
403 RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
404 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
408 case LabelOrientation.OutsideTop:
409 if (itemLabel != null)
411 RelativeLayout.SetTopTarget(itemLabel, this);
412 RelativeLayout.SetTopRelativeOffset(itemLabel, 0.0F);
413 RelativeLayout.SetBottomTarget(itemLabel, this);
414 RelativeLayout.SetBottomRelativeOffset(itemLabel, 0.0F);
415 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.Start);
417 RelativeLayout.SetTopTarget(itemImage, itemLabel);
418 RelativeLayout.SetTopRelativeOffset(itemImage, 1.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, 1.0F);
428 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
429 RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
430 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
434 case LabelOrientation.InsideBottom:
435 if (itemLabel != null)
437 RelativeLayout.SetTopTarget(itemLabel, this);
438 RelativeLayout.SetTopRelativeOffset(itemLabel, 1.0F);
439 RelativeLayout.SetBottomTarget(itemLabel, this);
440 RelativeLayout.SetBottomRelativeOffset(itemLabel, 1.0F);
441 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.End);
443 Console.WriteLine("Item Label Inside Bottom!");
445 RelativeLayout.SetTopTarget(itemImage, this);
446 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
447 RelativeLayout.SetBottomTarget(itemImage, this);
448 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
449 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
454 RelativeLayout.SetTopTarget(itemBadge, itemImage);
455 RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
456 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
457 RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
458 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
462 case LabelOrientation.InsideTop:
463 if (itemLabel != null)
465 RelativeLayout.SetTopTarget(itemLabel, this);
466 RelativeLayout.SetTopRelativeOffset(itemLabel, 0.0F);
467 RelativeLayout.SetBottomTarget(itemLabel, this);
468 RelativeLayout.SetBottomRelativeOffset(itemLabel, 0.0F);
469 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.Start);
471 RelativeLayout.SetTopTarget(itemImage, this);
472 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
473 RelativeLayout.SetBottomTarget(itemImage, this);
474 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
475 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
480 RelativeLayout.SetTopTarget(itemBadge, itemImage);
481 RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
482 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
483 RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
484 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
493 /// Gets accessibility name.
495 [EditorBrowsable(EditorBrowsableState.Never)]
496 protected override string AccessibilityGetName()
498 return itemLabel.Text;
502 /// Initializes AT-SPI object.
504 [EditorBrowsable(EditorBrowsableState.Never)]
505 public override void OnInitialize()
508 Layout = new RelativeLayout();
509 layoutChanged = true;
510 LayoutDirectionChanged += OnLayoutDirectionChanged;
511 EnableControlStatePropagation = true;
515 /// Dispose Item and all children on it.
517 /// <param name="type">Dispose type.</param>
518 [EditorBrowsable(EditorBrowsableState.Never)]
519 protected override void Dispose(DisposeTypes type)
526 if (type == DisposeTypes.Explicit)
528 //Extension : Extension?.OnDispose(this);
530 // Arugable to disposing user-created members.
532 if (itemBadge != null)
534 Utility.Dispose(itemBadge);
538 if (itemImage != null)
540 Utility.Dispose(itemImage);
543 if (itemLabel != null)
545 Utility.Dispose(itemLabel);
554 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
559 private void OnImageRelayout(object sender, EventArgs e)