0b44afec50599da7eef43050dfa7c4c90665a3bc
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Wearable / src / public / WearableList.cs
1 /* Copyright (c) 2020 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 Tizen.NUI.BaseComponents;
18 using Tizen.NUI.Components;
19 using System.ComponentModel;
20
21 namespace Tizen.NUI.Wearable
22 {
23     /// <summary>
24     /// [Draft] This class provides a list view styled by wearable ux.
25     /// List will lay out all items with Fish-Eye layout manager.
26     /// </summary>
27     /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
28     [EditorBrowsable(EditorBrowsableState.Never)]
29     public class WearableList : RecyclerView
30     {
31         private RecycleItem FocusedItem = null;
32
33         /// <summary>
34         /// Default constructor.
35         /// </summary>
36         /// <param name="adapter">Recycle adapter of List.</param>
37         /// <since_tizen> 8 </since_tizen>
38         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
39         [EditorBrowsable(EditorBrowsableState.Never)]
40         public WearableList(RecycleAdapter adapter) : base(adapter, new FishEyeLayoutManager())
41         {
42             ScrollingDirection = ScrollableBase.Direction.Vertical;
43
44             ScrollDragStartEvent += OnScrollDragStart;
45             ScrollAnimationEndEvent += OnAnimationEnd;
46
47             foreach (View child in mContainer.Children)
48             {
49                 child.PositionUsesPivotPoint = true;
50                 child.ParentOrigin = Tizen.NUI.ParentOrigin.TopCenter;
51             }
52
53             mContainer.PositionUsesPivotPoint = true;
54             mContainer.ParentOrigin = Tizen.NUI.ParentOrigin.Center;
55             mContainer.PivotPoint = ScrollingDirection == Direction.Vertical ? Tizen.NUI.PivotPoint.TopCenter : Tizen.NUI.PivotPoint.CenterLeft;
56             ScrollAvailableArea = new Vector2( 0,ListLayoutManager.StepSize * (mAdapter.Data.Count - 1) );
57
58             noticeAnimationEndBeforePosition = 50;
59
60             SetFocus(0, false);
61         }
62
63         /// <summary>
64         /// Set focus to item which has specific data index.
65         /// </summary>
66         /// <param name="dataIndex">Data index of item.</param>
67         /// <param name="animated">If set true, scroll to item using animation.</param>
68         /// <since_tizen> 8 </since_tizen>
69         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
70         public void SetFocus(int dataIndex, bool animated)
71         {
72             foreach (RecycleItem item in mContainer.Children)
73             {
74                 if (item.DataIndex == dataIndex)
75                 {
76                     RecycleItem prevFocusedItem = FocusedItem;
77                     prevFocusedItem?.OnFocusLost();
78                     FocusedItem = item;
79                     FocusedItem.OnFocusGained();
80
81                     ScrollTo(item.DataIndex * mLayoutManager.StepSize, animated);
82                 }
83             }
84         }
85
86         private void OnAnimationEnd(object source, ScrollableBase.ScrollEventArgs args)
87         {
88         }
89
90         /// <summary>
91         /// This helps developer who wants to know before scroll is reaching target position.
92         /// </summary>
93         /// <param name="targetPosition">Index of item.</param>
94         /// <since_tizen> 8 </since_tizen>
95         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
96         protected override void OnPreReachedTargetPosition(float targetPosition)
97         {
98             int targetDataIndex = (int)Math.Round(Math.Abs(targetPosition) / mLayoutManager.StepSize);
99
100             for (int i = 0; i < mContainer.Children.Count; i++)
101             {
102                 RecycleItem item = mContainer.Children[i] as RecycleItem;
103
104                 if (targetDataIndex == item.DataIndex)
105                 {
106                     FocusedItem = item;
107                     item.OnFocusGained();
108                     break;
109                 }
110             }
111         }
112
113         private void OnScrollDragStart(object source, ScrollableBase.ScrollEventArgs args)
114         {
115             RecycleItem prevFocusedItem = FocusedItem;
116             prevFocusedItem?.OnFocusLost();
117         }
118     }
119 }