[NUI] Modify NUI.Components private variable (#2764)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / RecyclerView / Item / DefaultGridItem.cs
1 /* Copyright (c) 2021 Samsung Electronics Co., Ltd.
2  *
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
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  *
15  */
16 using System;
17 using System.ComponentModel;
18 using Tizen.NUI.BaseComponents;
19 using Tizen.NUI.Accessibility;
20
21 namespace Tizen.NUI.Components
22 {
23     /// <summary>
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.
26     /// </summary>
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     public class DefaultGridItem : RecyclerViewItem
29     {
30         private TextLabel itemCaption;
31         private ImageView itemImage;
32         private View itemBadge;
33         private CaptionOrientation captionOrientation;
34         private bool layoutChanged;
35
36         private DefaultGridItemStyle ItemStyle => ViewStyle as DefaultGridItemStyle;
37
38         static DefaultGridItem() { }
39
40         /// <summary>
41         /// Creates a new instance of DefaultGridItem.
42         /// </summary>
43         [EditorBrowsable(EditorBrowsableState.Never)]
44         public DefaultGridItem() : base()
45         {
46             Initialize();
47         }
48
49         /// <summary>
50         /// Creates a new instance of DefaultGridItem with style
51         /// </summary>
52         /// <param name="style=">Create DefaultGridItem by special style defined in UX.</param>
53         [EditorBrowsable(EditorBrowsableState.Never)]
54         public DefaultGridItem(string style) : base(style)
55         {
56             Initialize();
57         }
58
59         /// <summary>
60         /// Creates a new instance of DefaultGridItem with style
61         /// </summary>
62         /// <param name="itemStyle=">Create DefaultGridItem by style customized by user.</param>
63         [EditorBrowsable(EditorBrowsableState.Never)]
64         public DefaultGridItem(DefaultGridItemStyle itemStyle) : base(itemStyle)
65         {
66             Initialize();
67         }
68
69         /// <summary>
70         /// Caption orientation.
71         /// </summary>
72         [EditorBrowsable(EditorBrowsableState.Never)]
73         public enum CaptionOrientation
74         {
75             /// <summary>
76             /// Outside of image bottom edge.
77             /// </summary>
78             [EditorBrowsable(EditorBrowsableState.Never)]
79             OutsideBottom,
80             /// <summary>
81             /// Outside of image top edge.
82             /// </summary>
83             [EditorBrowsable(EditorBrowsableState.Never)]
84             OutsideTop,
85             /// <summary>
86             /// inside of image bottom edge.
87             /// </summary>
88             [EditorBrowsable(EditorBrowsableState.Never)]
89             InsideBottom,
90             /// <summary>
91             /// inside of image top edge.
92             /// </summary>
93             [EditorBrowsable(EditorBrowsableState.Never)]
94             InsideTop,
95         }
96
97         /// <summary>
98         /// DefaultGridItem's icon part.
99         /// </summary>
100         [EditorBrowsable(EditorBrowsableState.Never)]
101         public ImageView Image
102         {
103             get
104             {
105                 if (itemImage == null)
106                 {
107                     itemImage = CreateImage(ItemStyle.Image);
108                     if (itemImage != null)
109                     {
110                         Add(itemImage);
111                         itemImage.Relayout += OnImageRelayout;
112                         layoutChanged = true;
113                     }
114                 }
115                 return itemImage;
116             }
117             internal set
118             {
119                 itemImage = value;
120                 layoutChanged = true;
121             }
122         }
123
124
125         /// <summary>
126         /// DefaultGridItem's badge object. will be placed in right-top edge.
127         /// </summary>
128         [EditorBrowsable(EditorBrowsableState.Never)]
129         public View Badge
130         {
131             get
132             {
133                 return itemBadge;
134
135             }
136             set
137             {
138                 if (value == null)
139                 {
140                     Remove(itemBadge);
141                 }
142                 itemBadge = value;
143                 if (itemBadge != null)
144                 {
145                     itemBadge.ApplyStyle(ItemStyle.Badge);
146                     Add(itemBadge);
147                 }
148                 layoutChanged = true;
149             }
150         }
151
152         /* open when ImageView using Uri not string
153                 /// <summary>
154                 /// Image image's resource url in DefaultGridItem.
155                 /// </summary>
156                 [EditorBrowsable(EditorBrowsableState.Never)]
157                 public string ImageUrl
158                 {
159                     get
160                     {
161                         return Image.ResourceUrl;
162                     }
163                     set
164                     {
165                         Image.ResourceUrl = value;
166                     }
167                 }
168         */
169
170         /// <summary>
171         /// DefaultGridItem's text part.
172         /// </summary>
173         [EditorBrowsable(EditorBrowsableState.Never)]
174         public TextLabel Caption
175         {
176             get
177             {
178                 if (itemCaption == null)
179                 {
180                     itemCaption = CreateLabel(ItemStyle.Caption);
181                     if (itemCaption != null)
182                     {
183                         Add(itemCaption);
184                         layoutChanged = true;
185                     }
186                 }
187                 return itemCaption;
188             }
189             internal set
190             {
191                 itemCaption = value;
192                 layoutChanged = true;
193                 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, itemCaption.Text);
194             }
195         }
196
197         /// <summary>
198         /// The text of DefaultGridItem.
199         /// </summary>
200         [EditorBrowsable(EditorBrowsableState.Never)]
201         public string Text
202         {
203             get
204             {
205                 return Caption.Text;
206             }
207             set
208             {
209                 Caption.Text = value;
210             }
211         }
212
213         /// <summary>
214         /// Caption relative orientation with image in DefaultGridItem.
215         /// </summary>
216         [EditorBrowsable(EditorBrowsableState.Never)]
217         public CaptionOrientation CaptionRelativeOrientation
218         {
219             get
220             {
221                 return captionOrientation;
222             }
223             set
224             {
225                 captionOrientation = value;
226                 layoutChanged = true;
227             }
228         }
229
230         /// <summary>
231         /// Apply style to DefaultLinearItemStyle.
232         /// </summary>
233         /// <param name="viewStyle">The style to apply.</param>
234         [EditorBrowsable(EditorBrowsableState.Never)]
235         public override void ApplyStyle(ViewStyle viewStyle)
236         {
237
238             base.ApplyStyle(viewStyle);
239             if (viewStyle != null && viewStyle is DefaultGridItemStyle defaultStyle)
240             {
241                 if (itemCaption != null)
242                     itemCaption.ApplyStyle(defaultStyle.Caption);
243                 if (itemImage != null)
244                     itemImage.ApplyStyle(defaultStyle.Image);
245                 if (itemBadge != null)
246                     itemBadge.ApplyStyle(defaultStyle.Badge);
247             }
248         }
249
250         /// <summary>
251         /// Creates Item's text part.
252         /// </summary>
253         /// <return>The created Item's text part.</return>
254         [EditorBrowsable(EditorBrowsableState.Never)]
255         protected virtual TextLabel CreateLabel(TextLabelStyle textStyle)
256         {
257             return new TextLabel(textStyle)
258             {
259                 HorizontalAlignment = HorizontalAlignment.Center,
260                 VerticalAlignment = VerticalAlignment.Center
261             };
262         }
263
264         /// <summary>
265         /// Creates Item's icon part.
266         /// </summary>
267         /// <return>The created Item's icon part.</return>
268         [EditorBrowsable(EditorBrowsableState.Never)]
269         protected virtual ImageView CreateImage(ImageViewStyle imageStyle)
270         {
271             return new ImageView(imageStyle);
272         }
273
274         /// <inheritdoc/>
275         [EditorBrowsable(EditorBrowsableState.Never)]
276         protected override void MeasureChild()
277         {
278             //nothing to do.
279             if (itemCaption)
280             {
281                 var pad = Padding;
282                 var margin = itemCaption.Margin;
283                 itemCaption.SizeWidth = SizeWidth - pad.Start - pad.End - margin.Start - margin.End;
284             }
285         }
286
287         /// <inheritdoc/>
288         [EditorBrowsable(EditorBrowsableState.Never)]
289         protected override void LayoutChild()
290         {
291             if (!layoutChanged) return;
292             if (itemImage == null) return;
293
294             layoutChanged = false;
295
296             RelativeLayout.SetLeftTarget(itemImage, this);
297             RelativeLayout.SetLeftRelativeOffset(itemImage, 0.0F);
298             RelativeLayout.SetRightTarget(itemImage, this);
299             RelativeLayout.SetRightRelativeOffset(itemImage, 1.0F);
300             RelativeLayout.SetHorizontalAlignment(itemImage, RelativeLayout.Alignment.Center);
301
302             if (itemCaption != null)
303             {
304                 itemCaption.RaiseAbove(itemImage);
305                 RelativeLayout.SetLeftTarget(itemCaption, itemImage);
306                 RelativeLayout.SetLeftRelativeOffset(itemCaption, 0.0F);
307                 RelativeLayout.SetRightTarget(itemCaption, itemImage);
308                 RelativeLayout.SetRightRelativeOffset(itemCaption, 1.0F);
309                 RelativeLayout.SetHorizontalAlignment(itemCaption, RelativeLayout.Alignment.Center);
310                 RelativeLayout.SetFillHorizontal(itemCaption, true);
311             }
312             else
313             {
314                 RelativeLayout.SetTopTarget(itemImage, this);
315                 RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
316                 RelativeLayout.SetBottomTarget(itemImage, this);
317                 RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
318                 RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
319             }
320
321             if (itemBadge)
322             {
323                 RelativeLayout.SetLeftTarget(itemBadge, itemImage);
324                 RelativeLayout.SetLeftRelativeOffset(itemBadge, 1.0F);
325                 RelativeLayout.SetRightTarget(itemBadge, itemImage);
326                 RelativeLayout.SetRightRelativeOffset(itemBadge, 1.0F);
327                 RelativeLayout.SetHorizontalAlignment(itemBadge, RelativeLayout.Alignment.End);
328             }
329
330             switch (captionOrientation)
331             {
332                 case CaptionOrientation.OutsideBottom:
333                     if (itemCaption != null)
334                     {
335                         RelativeLayout.SetTopTarget(itemCaption, this);
336                         RelativeLayout.SetTopRelativeOffset(itemCaption, 1.0F);
337                         RelativeLayout.SetBottomTarget(itemCaption, this);
338                         RelativeLayout.SetBottomRelativeOffset(itemCaption, 1.0F);
339                         RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.End);
340
341                         RelativeLayout.SetTopTarget(itemImage, this);
342                         RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
343                         RelativeLayout.SetBottomTarget(itemImage, itemCaption);
344                         RelativeLayout.SetBottomRelativeOffset(itemImage, 0.0F);
345                         RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
346                     }
347
348                     if (itemBadge)
349                     {
350                         RelativeLayout.SetTopTarget(itemBadge, itemImage);
351                         RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
352                         RelativeLayout.SetBottomTarget(itemBadge, itemImage);
353                         RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
354                         RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
355                     }
356                     break;
357
358                 case CaptionOrientation.OutsideTop:
359                     if (itemCaption != null)
360                     {
361                         RelativeLayout.SetTopTarget(itemCaption, this);
362                         RelativeLayout.SetTopRelativeOffset(itemCaption, 0.0F);
363                         RelativeLayout.SetBottomTarget(itemCaption, this);
364                         RelativeLayout.SetBottomRelativeOffset(itemCaption, 0.0F);
365                         RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.Start);
366
367                         RelativeLayout.SetTopTarget(itemImage, itemCaption);
368                         RelativeLayout.SetTopRelativeOffset(itemImage, 1.0F);
369                         RelativeLayout.SetBottomTarget(itemImage, this);
370                         RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
371                         RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
372                     }
373
374                     if (itemBadge)
375                     {
376                         RelativeLayout.SetTopTarget(itemBadge, itemImage);
377                         RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
378                         RelativeLayout.SetBottomTarget(itemBadge, itemImage);
379                         RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
380                         RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
381                     }
382                     break;
383
384                 case CaptionOrientation.InsideBottom:
385                     if (itemCaption != null)
386                     {
387                         RelativeLayout.SetTopTarget(itemCaption, this);
388                         RelativeLayout.SetTopRelativeOffset(itemCaption, 1.0F);
389                         RelativeLayout.SetBottomTarget(itemCaption, this);
390                         RelativeLayout.SetBottomRelativeOffset(itemCaption, 1.0F);
391                         RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.End);
392
393                         RelativeLayout.SetTopTarget(itemImage, this);
394                         RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
395                         RelativeLayout.SetBottomTarget(itemImage, this);
396                         RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
397                         RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
398                     }
399
400                     if (itemBadge)
401                     {
402                         RelativeLayout.SetTopTarget(itemBadge, itemImage);
403                         RelativeLayout.SetTopRelativeOffset(itemBadge, 0.0F);
404                         RelativeLayout.SetBottomTarget(itemBadge, itemImage);
405                         RelativeLayout.SetBottomRelativeOffset(itemBadge, 0.0F);
406                         RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.Start);
407                     }
408                     break;
409
410                 case CaptionOrientation.InsideTop:
411                     if (itemCaption != null)
412                     {
413                         RelativeLayout.SetTopTarget(itemCaption, this);
414                         RelativeLayout.SetTopRelativeOffset(itemCaption, 0.0F);
415                         RelativeLayout.SetBottomTarget(itemCaption, this);
416                         RelativeLayout.SetBottomRelativeOffset(itemCaption, 0.0F);
417                         RelativeLayout.SetVerticalAlignment(itemCaption, RelativeLayout.Alignment.Start);
418
419                         RelativeLayout.SetTopTarget(itemImage, this);
420                         RelativeLayout.SetTopRelativeOffset(itemImage, 0.0F);
421                         RelativeLayout.SetBottomTarget(itemImage, this);
422                         RelativeLayout.SetBottomRelativeOffset(itemImage, 1.0F);
423                         RelativeLayout.SetVerticalAlignment(itemImage, RelativeLayout.Alignment.Center);
424                     }
425
426                     if (itemBadge)
427                     {
428                         RelativeLayout.SetTopTarget(itemBadge, itemImage);
429                         RelativeLayout.SetTopRelativeOffset(itemBadge, 1.0F);
430                         RelativeLayout.SetBottomTarget(itemBadge, itemImage);
431                         RelativeLayout.SetBottomRelativeOffset(itemBadge, 1.0F);
432                         RelativeLayout.SetVerticalAlignment(itemBadge, RelativeLayout.Alignment.End);
433                     }
434                     break;
435             }
436
437
438         }
439
440         /// <inheritdoc/>
441         [EditorBrowsable(EditorBrowsableState.Never)]
442         private void Initialize()
443         {
444             Layout = new RelativeLayout();
445             layoutChanged = true;
446             LayoutDirectionChanged += OnLayoutDirectionChanged;
447         }
448
449         /// <summary>
450         /// Dispose Item and all children on it.
451         /// </summary>
452         /// <param name="type">Dispose type.</param>
453         [EditorBrowsable(EditorBrowsableState.Never)]
454         protected override void Dispose(DisposeTypes type)
455         {
456             if (disposed)
457             {
458                 return;
459             }
460
461             if (type == DisposeTypes.Explicit)
462             {
463                 //Extension : Extension?.OnDispose(this);
464
465                 if (itemImage != null)
466                 {
467                     Utility.Dispose(itemImage);
468                 }
469                 if (itemCaption != null)
470                 {
471                     Utility.Dispose(itemCaption);
472                 }
473                 if (itemBadge != null)
474                 {
475                     Utility.Dispose(itemBadge);
476                 }
477             }
478
479             base.Dispose(type);
480         }
481
482         private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
483         {
484             MeasureChild();
485             LayoutChild();
486         }
487         private void OnImageRelayout(object sender, EventArgs e)
488         {
489             MeasureChild();
490             LayoutChild();
491         }
492     }
493 }