Follow formatting NUI
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / FlexibleView / OrientationHelper.cs
1 /*
2  * Copyright(c) 2019 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 using System;
18
19 namespace Tizen.NUI.Components
20 {
21     // Helper class for LayoutManagers to abstract measurements depending on the View's orientation.
22     // It is developed to easily support vertical and horizontal orientations in a LayoutManager but
23     // can also be used to abstract calls around view bounds and child measurements with margins and
24     // decorations.
25     //
26     // @see #createHorizontalHelper(FlexibleViewRecyclerView.LayoutManager)
27     // @see #createVerticalHelper(FlexibleViewRecyclerView.LayoutManager)
28     internal abstract class OrientationHelper
29     {
30         public const int HORIZONTAL = 0;
31         public const int VERTICAL = 1;
32
33         private const int INVALID_SIZE = -1;
34
35         protected FlexibleViewLayoutManager mLayoutManager;
36
37         private float mLastTotalSpace = INVALID_SIZE;
38
39         public OrientationHelper(FlexibleViewLayoutManager layoutManager)
40         {
41             mLayoutManager = layoutManager;
42         }
43
44         // Call this method after onLayout method is complete if state is NOT pre-layout.
45         // This method records information like layout bounds that might be useful in the next layout
46         // calculations.
47         public void OnLayoutComplete()
48         {
49             mLastTotalSpace = GetTotalSpace();
50         }
51
52         // Returns the layout space change between the previous layout pass and current layout pass.
53         // Make sure you call {@link #onLayoutComplete()} at the end of your LayoutManager's
54         // {@link FlexibleViewRecyclerView.LayoutManager#onLayoutChildren(FlexibleViewRecyclerView.Recycler,
55         // FlexibleViewRecyclerView.State)} method.
56         //
57         // @return The difference between the current total space and previous layout's total space.
58         // @see #onLayoutComplete()
59         public float GetTotalSpaceChange()
60         {
61             return INVALID_SIZE == mLastTotalSpace ? 0 : GetTotalSpace() - mLastTotalSpace;
62         }
63
64         // Returns the start of the view including its decoration and margin.
65         // For example, for the horizontal helper, if a View's left is at pixel 20, has 2px left
66         // decoration and 3px left margin, returned value will be 15px.
67         //
68         // @param view The view element to check
69         // @return The first pixel of the element
70         // @see #getDecoratedEnd(android.view.View)
71         public abstract float GetViewHolderStart(FlexibleViewViewHolder holder);
72
73         // Returns the end of the view including its decoration and margin.
74         // For example, for the horizontal helper, if a View's right is at pixel 200, has 2px right
75         // decoration and 3px right margin, returned value will be 205.
76         //
77         // @param view The view element to check
78         // @return The last pixel of the element
79         // @see #getDecoratedStart(android.view.View)
80         public abstract float GetViewHolderEnd(FlexibleViewViewHolder holder);
81
82         // Returns the space occupied by this View in the current orientation including decorations and
83         // margins.
84         //
85         // @param view The view element to check
86         // @return Total space occupied by this view
87         // @see #getDecoratedMeasurementInOther(View)
88
89         public abstract float GetViewHolderMeasurement(FlexibleViewViewHolder holder);
90
91         // Returns the space occupied by this View in the perpendicular orientation including
92         // decorations and margins.
93         //
94         // @param view The view element to check
95         // @return Total space occupied by this view in the perpendicular orientation to current one
96         // @see #getDecoratedMeasurement(View)
97         public abstract float GetViewHolderMeasurementInOther(FlexibleViewViewHolder holder);
98
99         // Returns the start position of the layout after the start padding is added.
100         //
101         // @return The very first pixel we can draw.
102         public abstract float GetStartAfterPadding();
103
104         // Returns the end position of the layout after the end padding is removed.
105         //
106         // @return The end boundary for this layout.
107         public abstract float GetEndAfterPadding();
108
109         // Returns the end position of the layout without taking padding into account.
110         //
111         // @return The end boundary for this layout without considering padding.
112         public abstract float GetEnd();
113
114         // Offsets all children's positions by the given amount.
115         //
116         // @param amount Value to add to each child's layout parameters
117         public abstract void OffsetChildren(float amount, bool immediate);
118
119         // Returns the total space to layout. This number is the difference between
120         // {@link #getEndAfterPadding()} and {@link #getStartAfterPadding()}.
121         //
122         // @return Total space to layout children
123         public abstract float GetTotalSpace();
124
125         // Offsets the child in this orientation.
126         //
127         // @param view   View to offset
128         // @param offset offset amount
129         internal abstract void OffsetChild(FlexibleViewViewHolder holder, int offset);
130
131         // Returns the padding at the end of the layout. For horizontal helper, this is the right
132         // padding and for vertical helper, this is the bottom padding. This method does not check
133         // whether the layout is RTL or not.
134         //
135         // @return The padding at the end of the layout.
136         public abstract float GetEndPadding();
137
138         // Creates an OrientationHelper for the given LayoutManager and orientation.
139         //
140         // @param layoutManager LayoutManager to attach to
141         // @param orientation   Desired orientation. Should be {@link #HORIZONTAL} or {@link #VERTICAL}
142         // @return A new OrientationHelper
143         public static OrientationHelper CreateOrientationHelper(
144                 FlexibleViewLayoutManager layoutManager, int orientation)
145         {
146             if (orientation == HORIZONTAL)
147             {
148                 return CreateHorizontalHelper(layoutManager);
149             }
150             else if (orientation == VERTICAL)
151             {
152                 return CreateVerticalHelper(layoutManager);
153             }
154
155             throw new ArgumentException("invalid orientation");
156         }
157
158
159         // Creates a horizontal OrientationHelper for the given LayoutManager.
160         //
161         // @param layoutManager The LayoutManager to attach to.
162         // @return A new OrientationHelper
163         public static OrientationHelper CreateHorizontalHelper(FlexibleViewLayoutManager layoutManager)
164         {
165             return new HorizontalHelper(layoutManager);
166
167         }
168
169         // Creates a vertical OrientationHelper for the given LayoutManager.
170         //
171         // @param layoutManager The LayoutManager to attach to.
172         // @return A new OrientationHelper
173         public static OrientationHelper CreateVerticalHelper(FlexibleViewLayoutManager layoutManager)
174         {
175             return new VerticalHelper(layoutManager);
176         }
177     }
178
179     internal class HorizontalHelper : OrientationHelper
180     {
181         public HorizontalHelper(FlexibleViewLayoutManager layoutManager) : base(layoutManager)
182         {
183
184         }
185
186         public override float GetEndAfterPadding()
187         {
188             return mLayoutManager.Width - mLayoutManager.PaddingRight;
189         }
190
191         public override float GetEnd()
192         {
193             return mLayoutManager.Width;
194         }
195
196         public override void OffsetChildren(float amount, bool immediate)
197         {
198             mLayoutManager.OffsetChildrenHorizontal(amount, immediate);
199         }
200
201
202         public override float GetStartAfterPadding()
203         {
204             return mLayoutManager.PaddingLeft;
205         }
206
207         public override float GetViewHolderMeasurement(FlexibleViewViewHolder holder)
208         {
209             return holder.Right - holder.Left;
210         }
211
212         public override float GetViewHolderMeasurementInOther(FlexibleViewViewHolder holder)
213         {
214             return holder.Bottom - holder.Top;
215         }
216
217         public override float GetViewHolderEnd(FlexibleViewViewHolder holder)
218         {
219             return holder.Right;
220         }
221
222         public override float GetViewHolderStart(FlexibleViewViewHolder holder)
223         {
224             return holder.Left;
225         }
226
227         public override float GetTotalSpace()
228         {
229             return mLayoutManager.Width - mLayoutManager.PaddingLeft
230                     - mLayoutManager.PaddingRight;
231         }
232
233         internal override void OffsetChild(FlexibleViewViewHolder holder, int offset)
234         {
235             //holder.offsetLeftAndRight(offset);
236         }
237
238         public override float GetEndPadding()
239         {
240             return mLayoutManager.PaddingRight;
241         }
242
243     }
244
245     internal class VerticalHelper : OrientationHelper
246     {
247         public VerticalHelper(FlexibleViewLayoutManager layoutManager) : base(layoutManager)
248         {
249
250         }
251
252         public override float GetEndAfterPadding()
253         {
254             return mLayoutManager.Height - mLayoutManager.PaddingBottom;
255         }
256
257         public override float GetEnd()
258         {
259             return mLayoutManager.Height;
260         }
261
262         public override void OffsetChildren(float amount, bool immediate)
263         {
264             mLayoutManager.OffsetChildrenVertical(amount, immediate);
265         }
266
267         public override float GetStartAfterPadding()
268         {
269             return mLayoutManager.PaddingTop;
270         }
271
272         public override float GetViewHolderMeasurement(FlexibleViewViewHolder holder)
273         {
274             return holder.Bottom - holder.Top;
275         }
276
277         public override float GetViewHolderMeasurementInOther(FlexibleViewViewHolder holder)
278         {
279             return holder.Right - holder.Left;
280         }
281
282         public override float GetViewHolderEnd(FlexibleViewViewHolder holder)
283         {
284             return holder.Bottom;
285         }
286
287         public override float GetViewHolderStart(FlexibleViewViewHolder holder)
288         {
289             return holder.Top;
290         }
291
292         public override float GetTotalSpace()
293         {
294             return mLayoutManager.Height - mLayoutManager.PaddingTop
295                     - mLayoutManager.PaddingBottom;
296         }
297
298         internal override void OffsetChild(FlexibleViewViewHolder holder, int offset)
299         {
300             //holder.offsetTopAndBottom(offset);
301         }
302
303         public override float GetEndPadding()
304         {
305             return mLayoutManager.PaddingBottom;
306         }
307
308     }
309
310 }