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;
212 /// The text of DefaultGridItem.
214 [EditorBrowsable(EditorBrowsableState.Never)]
219 return GetValue(TextProperty) as string;
223 SetValue(TextProperty, value);
224 NotifyPropertyChanged();
227 private string InternalText
240 /// Label relative orientation with image in DefaultGridItem.
242 [EditorBrowsable(EditorBrowsableState.Never)]
243 public LabelOrientation LabelOrientationType
247 return (Tizen.NUI.Components.DefaultGridItem.LabelOrientation)GetValue(LabelOrientationTypeProperty);
251 SetValue(LabelOrientationTypeProperty, value);
252 NotifyPropertyChanged();
255 private Tizen.NUI.Components.DefaultGridItem.LabelOrientation InternalLabelOrientationType
259 return labelOrientation;
263 labelOrientation = value;
264 layoutChanged = true;
269 /// Apply style to DefaultLinearItemStyle.
271 /// <param name="viewStyle">The style to apply.</param>
272 [EditorBrowsable(EditorBrowsableState.Never)]
273 public override void ApplyStyle(ViewStyle viewStyle)
276 base.ApplyStyle(viewStyle);
277 if (viewStyle != null && viewStyle is DefaultGridItemStyle defaultStyle)
279 if (itemLabel != null)
280 itemLabel.ApplyStyle(defaultStyle.Label);
281 if (itemImage != null)
282 itemImage.ApplyStyle(defaultStyle.Image);
283 if (itemBadge != null)
284 itemBadge.ApplyStyle(defaultStyle.Badge);
289 /// Creates Item's text part.
291 /// <return>The created Item's text part.</return>
292 [EditorBrowsable(EditorBrowsableState.Never)]
293 protected virtual TextLabel CreateLabel(TextLabelStyle textStyle)
295 return new TextLabel(textStyle)
297 HorizontalAlignment = HorizontalAlignment.Center,
298 VerticalAlignment = VerticalAlignment.Center
303 /// Creates Item's icon part.
305 /// <return>The created Item's icon part.</return>
306 [EditorBrowsable(EditorBrowsableState.Never)]
307 protected virtual ImageView CreateImage(ImageViewStyle imageStyle)
309 return new ImageView(imageStyle);
313 [EditorBrowsable(EditorBrowsableState.Never)]
314 protected override void MeasureChild()
320 var margin = itemLabel.Margin;
321 itemLabel.SizeWidth = SizeWidth - pad.Start - pad.End - margin.Start - margin.End;
326 [EditorBrowsable(EditorBrowsableState.Never)]
327 protected override void LayoutChild()
329 if (!layoutChanged) return;
330 if (itemImage == null) return;
332 layoutChanged = false;
334 RelativeLayout.SetLeftTarget(itemImage, this);
335 RelativeLayout.SetLeftRelativeOffset(itemImage, 0.0F);
336 RelativeLayout.SetRightTarget(itemImage, this);
337 RelativeLayout.SetRightRelativeOffset(itemImage, 1.0F);
338 RelativeLayout.SetHorizontalAlignment(itemImage, RelativeLayout.Alignment.Center);
340 if (itemLabel != null)
342 itemLabel.RaiseAbove(itemImage);
343 RelativeLayout.SetLeftTarget(itemLabel, itemImage);
344 RelativeLayout.SetLeftRelativeOffset(itemLabel, 0.0F);
345 RelativeLayout.SetRightTarget(itemLabel, itemImage);
346 RelativeLayout.SetRightRelativeOffset(itemLabel, 1.0F);
347 RelativeLayout.SetHorizontalAlignment(itemLabel, RelativeLayout.Alignment.Center);
348 RelativeLayout.SetFillHorizontal(itemLabel, true);
352 RelativeLayout.SetTopTarget(itemImage, this);
353 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
354 RelativeLayout.SetBottomTarget(itemImage, this);
355 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
356 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
361 itemBadge.RaiseAbove(itemImage);
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 Console.WriteLine("Item Label Inside Bottom!");
434 RelativeLayout.SetTopTarget(itemImage, this);
435 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
436 RelativeLayout.SetBottomTarget(itemImage, this);
437 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
438 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
443 RelativeLayout.SetTopTarget(itemBadge, itemImage);
444 RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
445 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
446 RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
447 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
451 case LabelOrientation.InsideTop:
452 if (itemLabel != null)
454 RelativeLayout.SetTopTarget(itemLabel, this);
455 RelativeLayout.SetTopRelativeOffset(itemLabel, 0.0F);
456 RelativeLayout.SetBottomTarget(itemLabel, this);
457 RelativeLayout.SetBottomRelativeOffset(itemLabel, 0.0F);
458 RelativeLayout.SetVerticalAlignment(itemLabel, RelativeLayout.Alignment.Start);
460 RelativeLayout.SetTopTarget(itemImage, this);
461 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
462 RelativeLayout.SetBottomTarget(itemImage, this);
463 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
464 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
469 RelativeLayout.SetTopTarget(itemBadge, itemImage);
470 RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
471 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
472 RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
473 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
482 /// Gets accessibility name.
484 [EditorBrowsable(EditorBrowsableState.Never)]
485 protected override string AccessibilityGetName()
487 return itemLabel.Text;
491 /// Initializes AT-SPI object.
493 [EditorBrowsable(EditorBrowsableState.Never)]
494 public override void OnInitialize()
497 Layout = new RelativeLayout();
498 layoutChanged = true;
499 LayoutDirectionChanged += OnLayoutDirectionChanged;
500 EnableControlStatePropagation = true;
504 /// Dispose Item and all children on it.
506 /// <param name="type">Dispose type.</param>
507 [EditorBrowsable(EditorBrowsableState.Never)]
508 protected override void Dispose(DisposeTypes type)
515 if (type == DisposeTypes.Explicit)
517 //Extension : Extension?.OnDispose(this);
519 // Arugable to disposing user-created members.
521 if (itemBadge != null)
523 Utility.Dispose(itemBadge);
527 if (itemImage != null)
529 Utility.Dispose(itemImage);
532 if (itemLabel != null)
534 Utility.Dispose(itemLabel);
543 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
548 private void OnImageRelayout(object sender, EventArgs e)