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()
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 /// Caption orientation.
69 [EditorBrowsable(EditorBrowsableState.Never)]
70 public enum CaptionOrientation
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;
117 layoutChanged = true;
123 /// DefaultGridItem's badge object. will be placed in right-top edge.
125 [EditorBrowsable(EditorBrowsableState.Never)]
140 if (itemBadge != null)
142 itemBadge.ApplyStyle(ItemStyle.Badge);
145 layoutChanged = true;
149 /* open when ImageView using Uri not string
151 /// Image image's resource url in DefaultGridItem.
153 [EditorBrowsable(EditorBrowsableState.Never)]
154 public string ImageUrl
158 return Image.ResourceUrl;
162 Image.ResourceUrl = value;
168 /// DefaultGridItem's text part.
170 [EditorBrowsable(EditorBrowsableState.Never)]
171 public TextLabel Caption
175 if (itemCaption == null)
177 itemCaption = CreateLabel(ItemStyle.Caption);
178 if (itemCaption != null)
181 layoutChanged = true;
189 layoutChanged = true;
190 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, itemCaption.Text);
195 /// The text of DefaultGridItem.
197 [EditorBrowsable(EditorBrowsableState.Never)]
206 Caption.Text = value;
211 /// Caption relative orientation with image in DefaultGridItem.
213 [EditorBrowsable(EditorBrowsableState.Never)]
214 public CaptionOrientation CaptionRelativeOrientation
218 return captionOrientation;
222 captionOrientation = value;
223 layoutChanged = true;
228 /// Apply style to DefaultLinearItemStyle.
230 /// <param name="viewStyle">The style to apply.</param>
231 [EditorBrowsable(EditorBrowsableState.Never)]
232 public override void ApplyStyle(ViewStyle viewStyle)
235 base.ApplyStyle(viewStyle);
236 if (viewStyle != null && viewStyle is DefaultGridItemStyle defaultStyle)
238 if (itemCaption != null)
239 itemCaption.ApplyStyle(defaultStyle.Caption);
240 if (itemImage != null)
241 itemImage.ApplyStyle(defaultStyle.Image);
242 if (itemBadge != null)
243 itemBadge.ApplyStyle(defaultStyle.Badge);
248 /// Creates Item's text part.
250 /// <return>The created Item's text part.</return>
251 [EditorBrowsable(EditorBrowsableState.Never)]
252 protected virtual TextLabel CreateLabel(TextLabelStyle textStyle)
254 return new TextLabel(textStyle)
256 HorizontalAlignment = HorizontalAlignment.Center,
257 VerticalAlignment = VerticalAlignment.Center
262 /// Creates Item's icon part.
264 /// <return>The created Item's icon part.</return>
265 [EditorBrowsable(EditorBrowsableState.Never)]
266 protected virtual ImageView CreateImage(ImageViewStyle imageStyle)
268 return new ImageView(imageStyle);
272 [EditorBrowsable(EditorBrowsableState.Never)]
273 protected override void MeasureChild()
279 var margin = itemCaption.Margin;
280 itemCaption.SizeWidth = SizeWidth - pad.Start - pad.End - margin.Start - margin.End;
285 [EditorBrowsable(EditorBrowsableState.Never)]
286 protected override void LayoutChild()
288 if (!layoutChanged) return;
289 if (itemImage == null) return;
291 layoutChanged = false;
293 RelativeLayout.SetLeftTarget(itemImage, this);
294 RelativeLayout.SetLeftRelativeOffset(itemImage, 0.0F);
295 RelativeLayout.SetRightTarget(itemImage, this);
296 RelativeLayout.SetRightRelativeOffset(itemImage, 1.0F);
297 RelativeLayout.SetHorizontalAlignment(itemImage, RelativeLayout.Alignment.Center);
299 if (itemCaption != null)
301 itemCaption.RaiseAbove(itemImage);
302 RelativeLayout.SetLeftTarget(itemCaption, itemImage);
303 RelativeLayout.SetLeftRelativeOffset(itemCaption, 0.0F);
304 RelativeLayout.SetRightTarget(itemCaption, itemImage);
305 RelativeLayout.SetRightRelativeOffset(itemCaption, 1.0F);
306 RelativeLayout.SetHorizontalAlignment(itemCaption, RelativeLayout.Alignment.Center);
307 RelativeLayout.SetFillHorizontal(itemCaption, true);
311 RelativeLayout.SetTopTarget(itemImage, this);
312 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
313 RelativeLayout.SetBottomTarget(itemImage, this);
314 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
315 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
320 RelativeLayout.SetLeftTarget(itemBadge, itemImage);
321 RelativeLayout.SetLeftRelativeOffset(itemBadge, 1.0F);
322 RelativeLayout.SetRightTarget(itemBadge, itemImage);
323 RelativeLayout.SetRightRelativeOffset(itemBadge, 1.0F);
324 RelativeLayout.SetHorizontalAlignment(itemBadge, RelativeLayout.Alignment.End);
327 switch (captionOrientation)
329 case CaptionOrientation.OutsideBottom:
330 if (itemCaption != null)
332 RelativeLayout.SetTopTarget(itemCaption, this);
333 RelativeLayout.SetTopRelativeOffset(itemCaption, 1.0F);
334 RelativeLayout.SetBottomTarget(itemCaption, this);
335 RelativeLayout.SetBottomRelativeOffset(itemCaption, 1.0F);
336 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.End);
338 RelativeLayout.SetTopTarget(itemImage, this);
339 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
340 RelativeLayout.SetBottomTarget(itemImage, itemCaption);
341 RelativeLayout.SetBottomRelativeOffset(itemImage, 0.0F);
342 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
347 RelativeLayout.SetTopTarget(itemBadge, itemImage);
348 RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
349 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
350 RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
351 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
355 case CaptionOrientation.OutsideTop:
356 if (itemCaption != null)
358 RelativeLayout.SetTopTarget(itemCaption, this);
359 RelativeLayout.SetTopRelativeOffset(itemCaption, 0.0F);
360 RelativeLayout.SetBottomTarget(itemCaption, this);
361 RelativeLayout.SetBottomRelativeOffset(itemCaption, 0.0F);
362 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.Start);
364 RelativeLayout.SetTopTarget(itemImage, itemCaption);
365 RelativeLayout.SetTopRelativeOffset(itemImage, 1.0F);
366 RelativeLayout.SetBottomTarget(itemImage, this);
367 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
368 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
373 RelativeLayout.SetTopTarget(itemBadge, itemImage);
374 RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
375 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
376 RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
377 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
381 case CaptionOrientation.InsideBottom:
382 if (itemCaption != null)
384 RelativeLayout.SetTopTarget(itemCaption, this);
385 RelativeLayout.SetTopRelativeOffset(itemCaption, 1.0F);
386 RelativeLayout.SetBottomTarget(itemCaption, this);
387 RelativeLayout.SetBottomRelativeOffset(itemCaption, 1.0F);
388 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.End);
390 RelativeLayout.SetTopTarget(itemImage, this);
391 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
392 RelativeLayout.SetBottomTarget(itemImage, this);
393 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
394 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
399 RelativeLayout.SetTopTarget(itemBadge, itemImage);
400 RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
401 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
402 RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
403 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
407 case CaptionOrientation.InsideTop:
408 if (itemCaption != null)
410 RelativeLayout.SetTopTarget(itemCaption, this);
411 RelativeLayout.SetTopRelativeOffset(itemCaption, 0.0F);
412 RelativeLayout.SetBottomTarget(itemCaption, this);
413 RelativeLayout.SetBottomRelativeOffset(itemCaption, 0.0F);
414 RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.Start);
416 RelativeLayout.SetTopTarget(itemImage, this);
417 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
418 RelativeLayout.SetBottomTarget(itemImage, this);
419 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
420 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
425 RelativeLayout.SetTopTarget(itemBadge, itemImage);
426 RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
427 RelativeLayout.SetBottomTarget(itemBadge, itemImage);
428 RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
429 RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
438 /// Initializes AT-SPI object.
440 [EditorBrowsable(EditorBrowsableState.Never)]
441 public override void OnInitialize()
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)