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;
167 /* open when ImageView using Uri not string
169 /// Image image's resource url in DefaultGridItem.
171 [EditorBrowsable(EditorBrowsableState.Never)]
172 public string ImageUrl
176 return Image.ResourceUrl;
180 Image.ResourceUrl = value;
186 /// DefaultGridItem's text part.
188 [EditorBrowsable(EditorBrowsableState.Never)]
189 public TextLabel Label
193 if (itemLabel == null)
195 itemLabel = CreateLabel(ItemStyle.Label);
196 if (itemLabel != null)
199 layoutChanged = true;
207 layoutChanged = true;
208 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, itemLabel.Text);
213 /// The text of DefaultGridItem.
215 [EditorBrowsable(EditorBrowsableState.Never)]
220 return GetValue(TextProperty) as string;
224 SetValue(TextProperty, value);
225 NotifyPropertyChanged();
228 private string InternalText
241 /// Label relative orientation with image in DefaultGridItem.
243 [EditorBrowsable(EditorBrowsableState.Never)]
244 public LabelOrientation LabelOrientationType
248 return (Tizen.NUI.Components.DefaultGridItem.LabelOrientation)GetValue(LabelOrientationTypeProperty);
252 SetValue(LabelOrientationTypeProperty, value);
253 NotifyPropertyChanged();
256 private Tizen.NUI.Components.DefaultGridItem.LabelOrientation InternalLabelOrientationType
260 return labelOrientation;
264 labelOrientation = value;
265 layoutChanged = true;
270 /// Apply style to DefaultLinearItemStyle.
272 /// <param name="viewStyle">The style to apply.</param>
273 [EditorBrowsable(EditorBrowsableState.Never)]
274 public override void ApplyStyle(ViewStyle viewStyle)
277 base.ApplyStyle(viewStyle);
278 if (viewStyle != null && viewStyle is DefaultGridItemStyle defaultStyle)
280 if (itemLabel != null)
281 itemLabel.ApplyStyle(defaultStyle.Label);
282 if (itemImage != null)
283 itemImage.ApplyStyle(defaultStyle.Image);
284 if (itemBadge != null)
285 itemBadge.ApplyStyle(defaultStyle.Badge);
290 /// Creates Item's text part.
292 /// <return>The created Item's text part.</return>
293 [EditorBrowsable(EditorBrowsableState.Never)]
294 protected virtual TextLabel CreateLabel(TextLabelStyle textStyle)
296 return new TextLabel(textStyle)
298 HorizontalAlignment = HorizontalAlignment.Center,
299 VerticalAlignment = VerticalAlignment.Center
304 /// Creates Item's icon part.
306 /// <return>The created Item's icon part.</return>
307 [EditorBrowsable(EditorBrowsableState.Never)]
308 protected virtual ImageView CreateImage(ImageViewStyle imageStyle)
310 return new ImageView(imageStyle);
314 [EditorBrowsable(EditorBrowsableState.Never)]
315 protected override void MeasureChild()
321 var margin = itemLabel.Margin;
322 itemLabel.SizeWidth = SizeWidth - pad.Start - pad.End - margin.Start - margin.End;
327 [EditorBrowsable(EditorBrowsableState.Never)]
328 protected override void LayoutChild()
330 if (!layoutChanged) return;
331 if (itemImage == null) return;
333 layoutChanged = false;
335 RelativeLayout.SetLeftTarget(itemImage, this);
336 RelativeLayout.SetLeftRelativeOffset(itemImage, 0.0F);
337 RelativeLayout.SetRightTarget(itemImage, this);
338 RelativeLayout.SetRightRelativeOffset(itemImage, 1.0F);
339 RelativeLayout.SetHorizontalAlignment(itemImage, RelativeLayout.Alignment.Center);
341 if (itemLabel != null)
343 itemLabel.RaiseAbove(itemImage);
344 RelativeLayout.SetLeftTarget(itemLabel, itemImage);
345 RelativeLayout.SetLeftRelativeOffset(itemLabel, 0.0F);
346 RelativeLayout.SetRightTarget(itemLabel, itemImage);
347 RelativeLayout.SetRightRelativeOffset(itemLabel, 1.0F);
348 RelativeLayout.SetHorizontalAlignment(itemLabel, RelativeLayout.Alignment.Center);
349 RelativeLayout.SetFillHorizontal(itemLabel, true);
353 RelativeLayout.SetTopTarget(itemImage, this);
354 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
355 RelativeLayout.SetBottomTarget(itemImage, this);
356 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
357 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
362 RelativeLayout.SetLeftTarget(itemBadge, itemImage);
363 RelativeLayout.SetLeftRelativeOffset(itemBadge, 1.0F);
364 RelativeLayout.SetRightTarget(itemBadge, itemImage);
365 RelativeLayout.SetRightRelativeOffset(itemBadge, 1.0F);
366 RelativeLayout.SetHorizontalAlignment(itemBadge, RelativeLayout.Alignment.End);
369 switch (labelOrientation)
371 case LabelOrientation.OutsideBottom:
372 if (itemLabel != null)
374 RelativeLayout.SetTopTarget(itemLabel, this);
375 RelativeLayout.SetTopRelativeOffset(itemLabel, 1.0F);
376 RelativeLayout.SetBottomTarget(itemLabel, this);
377 RelativeLayout.SetBottomRelativeOffset(itemLabel, 1.0F);
378 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.End);
380 RelativeLayout.SetTopTarget(itemImage, this);
381 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
382 RelativeLayout.SetBottomTarget(itemImage, itemLabel);
383 RelativeLayout.SetBottomRelativeOffset(itemImage, 0.0F);
384 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
389 RelativeLayout.SetTopTarget(itemBadge, itemImage);
390 RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
391 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
392 RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
393 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
397 case LabelOrientation.OutsideTop:
398 if (itemLabel != null)
400 RelativeLayout.SetTopTarget(itemLabel, this);
401 RelativeLayout.SetTopRelativeOffset(itemLabel, 0.0F);
402 RelativeLayout.SetBottomTarget(itemLabel, this);
403 RelativeLayout.SetBottomRelativeOffset(itemLabel, 0.0F);
404 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.Start);
406 RelativeLayout.SetTopTarget(itemImage, itemLabel);
407 RelativeLayout.SetTopRelativeOffset(itemImage, 1.0F);
408 RelativeLayout.SetBottomTarget(itemImage, this);
409 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
410 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
415 RelativeLayout.SetTopTarget(itemBadge, itemImage);
416 RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
417 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
418 RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
419 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
423 case LabelOrientation.InsideBottom:
424 if (itemLabel != null)
426 RelativeLayout.SetTopTarget(itemLabel, this);
427 RelativeLayout.SetTopRelativeOffset(itemLabel, 1.0F);
428 RelativeLayout.SetBottomTarget(itemLabel, this);
429 RelativeLayout.SetBottomRelativeOffset(itemLabel, 1.0F);
430 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.End);
432 RelativeLayout.SetTopTarget(itemImage, this);
433 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
434 RelativeLayout.SetBottomTarget(itemImage, this);
435 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
436 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
441 RelativeLayout.SetTopTarget(itemBadge, itemImage);
442 RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
443 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
444 RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
445 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
449 case LabelOrientation.InsideTop:
450 if (itemLabel != null)
452 RelativeLayout.SetTopTarget(itemLabel, this);
453 RelativeLayout.SetTopRelativeOffset(itemLabel, 0.0F);
454 RelativeLayout.SetBottomTarget(itemLabel, this);
455 RelativeLayout.SetBottomRelativeOffset(itemLabel, 0.0F);
456 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.Start);
458 RelativeLayout.SetTopTarget(itemImage, this);
459 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
460 RelativeLayout.SetBottomTarget(itemImage, this);
461 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
462 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
467 RelativeLayout.SetTopTarget(itemBadge, itemImage);
468 RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
469 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
470 RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
471 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
480 /// Initializes AT-SPI object.
482 [EditorBrowsable(EditorBrowsableState.Never)]
483 public override void OnInitialize()
486 Layout = new RelativeLayout();
487 layoutChanged = true;
488 LayoutDirectionChanged += OnLayoutDirectionChanged;
492 /// Dispose Item and all children on it.
494 /// <param name="type">Dispose type.</param>
495 [EditorBrowsable(EditorBrowsableState.Never)]
496 protected override void Dispose(DisposeTypes type)
503 if (type == DisposeTypes.Explicit)
505 //Extension : Extension?.OnDispose(this);
507 // Arugable to disposing user-created members.
509 if (itemBadge != null)
511 Utility.Dispose(itemBadge);
515 if (itemImage != null)
517 Utility.Dispose(itemImage);
520 if (itemLabel != null)
522 Utility.Dispose(itemLabel);
531 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
536 private void OnImageRelayout(object sender, EventArgs e)