[NUI] Update recyclerView item and styles.add missing patches files.
[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         }
47
48         /// <summary>
49         /// Creates a new instance of DefaultGridItem with style
50         /// </summary>
51         /// <param name="style=">Create DefaultGridItem by special style defined in UX.</param>
52         [EditorBrowsable(EditorBrowsableState.Never)]
53         public DefaultGridItem(string style) : base(style)
54         {
55         }
56
57         /// <summary>
58         /// Creates a new instance of DefaultGridItem with style
59         /// </summary>
60         /// <param name="itemStyle=">Create DefaultGridItem by style customized by user.</param>
61         [EditorBrowsable(EditorBrowsableState.Never)]
62         public DefaultGridItem(DefaultGridItemStyle itemStyle) : base(itemStyle)
63         {
64         }
65
66         /// <summary>
67         /// Caption orientation.
68         /// </summary>
69         [EditorBrowsable(EditorBrowsableState.Never)]
70         public enum CaptionOrientation
71         {
72             /// <summary>
73             /// Outside of image bottom edge.
74             /// </summary>
75             [EditorBrowsable(EditorBrowsableState.Never)]
76             OutsideBottom,
77             /// <summary>
78             /// Outside of image top edge.
79             /// </summary>
80             [EditorBrowsable(EditorBrowsableState.Never)]
81             OutsideTop,
82             /// <summary>
83             /// inside of image bottom edge.
84             /// </summary>
85             [EditorBrowsable(EditorBrowsableState.Never)]
86             InsideBottom,
87             /// <summary>
88             /// inside of image top edge.
89             /// </summary>
90             [EditorBrowsable(EditorBrowsableState.Never)]
91             InsideTop,
92         }
93
94         /// <summary>
95         /// DefaultGridItem's icon part.
96         /// </summary>
97         [EditorBrowsable(EditorBrowsableState.Never)]
98         public ImageView Image
99         {
100             get
101             {
102                 if (itemImage == null)
103                 {
104                     itemImage = CreateImage(ItemStyle.Image);
105                     if (itemImage != null)
106                     {
107                         Add(itemImage);
108                         itemImage.Relayout += OnImageRelayout;
109                         layoutChanged = true;
110                     }
111                 }
112                 return itemImage;
113             }
114             internal set
115             {
116                 itemImage = value;
117                 layoutChanged = true;
118             }
119         }
120
121
122         /// <summary>
123         /// DefaultGridItem's badge object. will be placed in right-top edge.
124         /// </summary>
125         [EditorBrowsable(EditorBrowsableState.Never)]
126         public View Badge
127         {
128             get
129             {
130                 return itemBadge;
131
132             }
133             set
134             {
135                 if (value == null)
136                 {
137                     Remove(itemBadge);
138                 }
139                 itemBadge = value;
140                 if (itemBadge != null)
141                 {
142                     itemBadge.ApplyStyle(ItemStyle.Badge);
143                     Add(itemBadge);
144                 }
145                 layoutChanged = true;
146             }
147         }
148
149         /* open when ImageView using Uri not string
150                 /// <summary>
151                 /// Image image's resource url in DefaultGridItem.
152                 /// </summary>
153                 [EditorBrowsable(EditorBrowsableState.Never)]
154                 public string ImageUrl
155                 {
156                     get
157                     {
158                         return Image.ResourceUrl;
159                     }
160                     set
161                     {
162                         Image.ResourceUrl = value;
163                     }
164                 }
165         */
166
167         /// <summary>
168         /// DefaultGridItem's text part.
169         /// </summary>
170         [EditorBrowsable(EditorBrowsableState.Never)]
171         public TextLabel Caption
172         {
173             get
174             {
175                 if (itemCaption == null)
176                 {
177                     itemCaption = CreateLabel(ItemStyle.Caption);
178                     if (itemCaption != null)
179                     {
180                         Add(itemCaption);
181                         layoutChanged = true;
182                     }
183                 }
184                 return itemCaption;
185             }
186             internal set
187             {
188                 itemCaption = value;
189                 layoutChanged = true;
190                 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, itemCaption.Text);
191             }
192         }
193
194         /// <summary>
195         /// The text of DefaultGridItem.
196         /// </summary>
197         [EditorBrowsable(EditorBrowsableState.Never)]
198         public string Text
199         {
200             get
201             {
202                 return Caption.Text;
203             }
204             set
205             {
206                 Caption.Text = value;
207             }
208         }
209
210         /// <summary>
211         /// Caption relative orientation with image in DefaultGridItem.
212         /// </summary>
213         [EditorBrowsable(EditorBrowsableState.Never)]
214         public CaptionOrientation CaptionRelativeOrientation
215         {
216             get
217             {
218                 return captionOrientation;
219             }
220             set
221             {
222                 captionOrientation = value;
223                 layoutChanged = true;
224             }
225         }
226
227         /// <summary>
228         /// Apply style to DefaultLinearItemStyle.
229         /// </summary>
230         /// <param name="viewStyle">The style to apply.</param>
231         [EditorBrowsable(EditorBrowsableState.Never)]
232         public override void ApplyStyle(ViewStyle viewStyle)
233         {
234
235             base.ApplyStyle(viewStyle);
236             if (viewStyle != null && viewStyle is DefaultGridItemStyle defaultStyle)
237             {
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);
244             }
245         }
246
247         /// <summary>
248         /// Creates Item's text part.
249         /// </summary>
250         /// <return>The created Item's text part.</return>
251         [EditorBrowsable(EditorBrowsableState.Never)]
252         protected virtual TextLabel CreateLabel(TextLabelStyle textStyle)
253         {
254             return new TextLabel(textStyle)
255             {
256                 HorizontalAlignment = HorizontalAlignment.Center,
257                 VerticalAlignment = VerticalAlignment.Center
258             };
259         }
260
261         /// <summary>
262         /// Creates Item's icon part.
263         /// </summary>
264         /// <return>The created Item's icon part.</return>
265         [EditorBrowsable(EditorBrowsableState.Never)]
266         protected virtual ImageView CreateImage(ImageViewStyle imageStyle)
267         {
268             return new ImageView(imageStyle);
269         }
270
271         /// <inheritdoc/>
272         [EditorBrowsable(EditorBrowsableState.Never)]
273         protected override void MeasureChild()
274         {
275             //nothing to do.
276             if (itemCaption)
277             {
278                 var pad = Padding;
279                 var margin = itemCaption.Margin;
280                 itemCaption.SizeWidth = SizeWidth - pad.Start - pad.End - margin.Start - margin.End;
281             }
282         }
283
284         /// <inheritdoc/>
285         [EditorBrowsable(EditorBrowsableState.Never)]
286         protected override void LayoutChild()
287         {
288             if (!layoutChanged) return;
289             if (itemImage == null) return;
290
291             layoutChanged = false;
292
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);
298
299             if (itemCaption != null)
300             {
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);
308             }
309             else
310             {
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);
316             }
317
318             if (itemBadge)
319             {
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);
325             }
326
327             switch (captionOrientation)
328             {
329                 case CaptionOrientation.OutsideBottom:
330                     if (itemCaption != null)
331                     {
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);
337
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);
343                     }
344
345                     if (itemBadge)
346                     {
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);
352                     }
353                     break;
354
355                 case CaptionOrientation.OutsideTop:
356                     if (itemCaption != null)
357                     {
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);
363
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);
369                     }
370
371                     if (itemBadge)
372                     {
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);
378                     }
379                     break;
380
381                 case CaptionOrientation.InsideBottom:
382                     if (itemCaption != null)
383                     {
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);
389
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);
395                     }
396
397                     if (itemBadge)
398                     {
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);
404                     }
405                     break;
406
407                 case CaptionOrientation.InsideTop:
408                     if (itemCaption != null)
409                     {
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);
415
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);
421                     }
422
423                     if (itemBadge)
424                     {
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);
430                     }
431                     break;
432             }
433
434
435         }
436
437         /// <summary>
438         /// Initializes AT-SPI object.
439         /// </summary>
440         [EditorBrowsable(EditorBrowsableState.Never)]
441         public override void OnInitialize()
442         {
443             base.OnInitialize();
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 }