/
MauiScrollView.cs
438 lines (360 loc) · 13 KB
/
MauiScrollView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
using System;
using Android.Animation;
using Android.Content;
using Android.Graphics;
using Android.Hardware.Lights;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using AndroidX.Core.Widget;
namespace Microsoft.Maui.Platform
{
public class MauiScrollView : NestedScrollView, IScrollBarView, NestedScrollView.IOnScrollChangeListener
{
View? _content;
MauiHorizontalScrollView? _hScrollView;
bool _isBidirectional;
ScrollOrientation _scrollOrientation = ScrollOrientation.Vertical;
ScrollBarVisibility _defaultHorizontalScrollVisibility;
ScrollBarVisibility _defaultVerticalScrollVisibility;
ScrollBarVisibility _horizontalScrollVisibility;
internal float LastX { get; set; }
internal float LastY { get; set; }
internal bool ShouldSkipOnTouch;
public MauiScrollView(Context context) : base(context)
{
}
public MauiScrollView(Context context, Android.Util.IAttributeSet attrs) : base(context, attrs)
{
}
public MauiScrollView(Context context, Android.Util.IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
}
protected MauiScrollView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public void SetHorizontalScrollBarVisibility(ScrollBarVisibility scrollBarVisibility)
{
_horizontalScrollVisibility = scrollBarVisibility;
if (_hScrollView == null)
{
return;
}
if (_defaultHorizontalScrollVisibility == 0)
{
_defaultHorizontalScrollVisibility = _hScrollView.HorizontalScrollBarEnabled ? ScrollBarVisibility.Always : ScrollBarVisibility.Never;
}
if (scrollBarVisibility == ScrollBarVisibility.Default)
{
scrollBarVisibility = _defaultHorizontalScrollVisibility;
}
_hScrollView.HorizontalScrollBarEnabled = scrollBarVisibility == ScrollBarVisibility.Always;
}
public void SetVerticalScrollBarVisibility(ScrollBarVisibility scrollBarVisibility)
{
if (_defaultVerticalScrollVisibility == 0)
_defaultVerticalScrollVisibility = VerticalScrollBarEnabled ? ScrollBarVisibility.Always : ScrollBarVisibility.Never;
if (scrollBarVisibility == ScrollBarVisibility.Default)
scrollBarVisibility = _defaultVerticalScrollVisibility;
VerticalScrollBarEnabled = scrollBarVisibility == ScrollBarVisibility.Always;
this.HandleScrollBarVisibilityChange();
}
public void SetContent(View content)
{
_content = content;
SetOrientation(_scrollOrientation);
}
public void SetOrientation(ScrollOrientation orientation)
{
_scrollOrientation = orientation;
if (orientation == ScrollOrientation.Horizontal || orientation == ScrollOrientation.Both)
{
if (_hScrollView == null)
{
_hScrollView = new MauiHorizontalScrollView(Context, this);
_hScrollView.HorizontalFadingEdgeEnabled = HorizontalFadingEdgeEnabled;
_hScrollView.SetFadingEdgeLength(HorizontalFadingEdgeLength);
SetHorizontalScrollBarVisibility(_horizontalScrollVisibility);
}
_hScrollView.IsBidirectional = _isBidirectional = orientation == ScrollOrientation.Both;
if (_hScrollView.Parent != this)
{
if (_content != null)
{
_content.RemoveFromParent();
_hScrollView.AddView(_content);
}
AddView(_hScrollView);
}
}
else
{
if (_content != null && _content.Parent != this)
{
_content.RemoveFromParent();
if (_hScrollView != null)
_hScrollView.RemoveFromParent();
AddView(_content);
}
}
}
public override bool OnInterceptTouchEvent(MotionEvent? ev)
{
// See also MauiHorizontalScrollView notes in OnInterceptTouchEvent
/*if (ev == null)
return false;
// set the start point for the bidirectional scroll;
// Down is swallowed by other controls, so we'll just sneak this in here without actually preventing
// other controls from getting the event.
if (_isBidirectional && ev.Action == MotionEventActions.Down)
{
LastY = ev.RawY;
LastX = ev.RawX;
}*/
return base.OnInterceptTouchEvent(ev);
}
public override bool OnTouchEvent(MotionEvent? ev)
{
/*if (ev == null || !Enabled)
return false;
if (ShouldSkipOnTouch)
{
ShouldSkipOnTouch = false;
return false;
}
// The nested ScrollViews will allow us to scroll EITHER vertically OR horizontally in a single gesture.
// This will allow us to also scroll diagonally.
// We'll fall through to the base event so we still get the fling from the ScrollViews.
// We have to do this in both ScrollViews, since a single gesture will be owned by one or the other, depending
// on the initial direction of movement (i.e., horizontal/vertical).
if (_isBidirectional) // // See also MauiHorizontalScrollView notes in OnInterceptTouchEvent
{
float dX = LastX - ev.RawX;
LastY = ev.RawY;
LastX = ev.RawX;
if (ev.Action == MotionEventActions.Move)
{
foreach (MauiHorizontalScrollView child in this.GetChildrenOfType<MauiHorizontalScrollView>())
{
child.ScrollBy((int)dX, 0);
break;
}
// Fall through to base.OnTouchEvent, it'll take care of the Y scrolling
}
}*/
return base.OnTouchEvent(ev);
}
void IScrollBarView.AwakenScrollBars()
{
base.AwakenScrollBars();
}
bool IScrollBarView.ScrollBarsInitialized { get; set; }
protected override void OnLayout(bool changed, int left, int top, int right, int bottom)
{
base.OnLayout(changed, left, top, right, bottom);
/*if (_hScrollView?.Parent == this && _content is not null)
{
var scrollViewContentHeight = _content.Height;
var hScrollViewHeight = bottom - top;
var hScrollViewWidth = right - left;
//if we are scrolling both ways we need to lay out our MauiHorizontalScrollView with more than the available height
//so its parent the NestedScrollView can scroll vertically
hScrollViewHeight = _isBidirectional ? Math.Max(hScrollViewHeight, scrollViewContentHeight) : hScrollViewHeight;
// Ensure that the horizontal scrollview has been measured at the actual target size
// so that it updates its internal bookkeeping to use the full size
_hScrollView.Measure(MeasureSpec.MakeMeasureSpec(right - left, MeasureSpecMode.Exactly),
MeasureSpec.MakeMeasureSpec(hScrollViewHeight, MeasureSpecMode.Exactly));
_hScrollView.Layout(0, 0, hScrollViewWidth, hScrollViewHeight);
}*/
if (CrossPlatformArrange == null)
{
return;
}
var destination = Context!.ToCrossPlatformRectInReferenceFrame(left, top, right, bottom);
CrossPlatformArrange(destination);
}
public void ScrollTo(int x, int y, bool instant, Action finished)
{
if (instant)
{
JumpTo(x, y, finished);
}
else
{
SmoothScrollTo(x, y, finished);
}
}
void JumpTo(int x, int y, Action finished)
{
switch (_scrollOrientation)
{
case ScrollOrientation.Vertical:
ScrollTo(x, y);
break;
case ScrollOrientation.Horizontal:
_hScrollView?.ScrollTo(x, y);
break;
case ScrollOrientation.Both:
_hScrollView?.ScrollTo(x, y);
ScrollTo(x, y);
break;
case ScrollOrientation.Neither:
break;
}
finished();
}
static int GetDistance(double start, double position, double v)
{
return (int)(start + (position - start) * v);
}
void SmoothScrollTo(int x, int y, Action finished)
{
int currentX = _scrollOrientation == ScrollOrientation.Horizontal || _scrollOrientation == ScrollOrientation.Both ? _hScrollView!.ScrollX : ScrollX;
int currentY = _scrollOrientation == ScrollOrientation.Vertical || _scrollOrientation == ScrollOrientation.Both ? ScrollY : _hScrollView!.ScrollY;
ValueAnimator? animator = ValueAnimator.OfFloat(0f, 1f);
animator!.SetDuration(1000);
animator.Update += (o, animatorUpdateEventArgs) =>
{
var v = (double)(animatorUpdateEventArgs.Animation!.AnimatedValue!);
int distX = GetDistance(currentX, x, v);
int distY = GetDistance(currentY, y, v);
switch (_scrollOrientation)
{
case ScrollOrientation.Horizontal:
_hScrollView?.ScrollTo(distX, distY);
break;
case ScrollOrientation.Vertical:
ScrollTo(distX, distY);
break;
default:
_hScrollView?.ScrollTo(distX, distY);
ScrollTo(distX, distY);
break;
}
};
animator.AnimationEnd += delegate
{
finished();
};
animator.Start();
}
#pragma warning disable CA1822 // DO NOT REMOVE! Needed because dotnet format will else try to make this static and break things
void IOnScrollChangeListener.OnScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
#pragma warning restore CA1822
{
OnScrollChanged(scrollX, scrollY, oldScrollX, oldScrollY);
}
internal Func<Graphics.Rect, Graphics.Size>? CrossPlatformArrange { get; set; }
}
internal class MauiHorizontalScrollView : HorizontalScrollView, IScrollBarView
{
readonly MauiScrollView? _parentScrollView;
protected MauiHorizontalScrollView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public MauiHorizontalScrollView(Context? context, MauiScrollView parentScrollView) : base(context)
{
_parentScrollView = parentScrollView;
Tag = "Microsoft.Maui.Android.HorizontalScrollView";
}
public MauiHorizontalScrollView(Context? context, IAttributeSet? attrs) : base(context, attrs)
{
}
public MauiHorizontalScrollView(Context? context, IAttributeSet? attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
}
public MauiHorizontalScrollView(Context? context, IAttributeSet? attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
}
internal bool IsBidirectional { get; set; }
public override void Draw(Canvas? canvas)
{
try
{
//canvas?.ClipRect(canvas?.ClipBounds!);
base.Draw(canvas!);
}
catch (Java.Lang.NullPointerException)
{
// This will most likely never run since UpdateScrollBars is called
// when the scrollbars visibilities are updated but I left it here
// just in case there's an edge case that causes an exception
this.HandleScrollBarVisibilityChange();
}
}
public override bool OnInterceptTouchEvent(MotionEvent? ev)
{
/*if (ev == null || _parentScrollView == null)
return false;
// TODO ezhart 2021-07-12 The previous version of this checked _renderer.Element.InputTransparent; we don't have acces to that here,
// and I'm not sure it even applies. We need to determine whether touch events will get here at all if we've marked the ScrollView InputTransparent
// We _should_ be able to deal with it at the handler level by force-setting an OnTouchListener for the PlatformView that always returns false; then we
// can just stop worrying about it here because the touches _can't_ reach this.
// set the start point for the bidirectional scroll;
// Down is swallowed by other controls, so we'll just sneak this in here without actually preventing
// other controls from getting the event.
if (IsBidirectional && ev.Action == MotionEventActions.Down)
{
_parentScrollView.LastY = ev.RawY;
_parentScrollView.LastX = ev.RawX;
}*/
return base.OnInterceptTouchEvent(ev);
}
public override bool OnTouchEvent(MotionEvent? ev)
{
/*if (ev == null || _parentScrollView == null)
return false;
if (!_parentScrollView.Enabled)
return false;
// If the touch is caught by the horizontal scrollview, forward it to the parent
_parentScrollView.ShouldSkipOnTouch = true;
_parentScrollView.OnTouchEvent(ev);
// The nested ScrollViews will allow us to scroll EITHER vertically OR horizontally in a single gesture.
// This will allow us to also scroll diagonally.
// We'll fall through to the base event so we still get the fling from the ScrollViews.
// We have to do this in both ScrollViews, since a single gesture will be owned by one or the other, depending
// on the initial direction of movement (i.e., horizontal/vertical).
if (IsBidirectional)
{
float dY = _parentScrollView.LastY - ev.RawY;
_parentScrollView.LastY = ev.RawY;
_parentScrollView.LastX = ev.RawX;
if (ev.Action == MotionEventActions.Move)
{
_parentScrollView.ScrollBy(0, (int)dY);
// Fall through to base.OnTouchEvent, it'll take care of the X scrolling
}
}*/
return base.OnTouchEvent(ev);
}
public override bool HorizontalScrollBarEnabled
{
get { return base.HorizontalScrollBarEnabled; }
set
{
base.HorizontalScrollBarEnabled = value;
this.HandleScrollBarVisibilityChange();
}
}
void IScrollBarView.AwakenScrollBars()
{
base.AwakenScrollBars();
}
bool IScrollBarView.ScrollBarsInitialized { get; set; }
protected override void OnScrollChanged(int l, int t, int oldl, int oldt)
{
base.OnScrollChanged(l, t, oldl, oldt);
if (_parentScrollView is NestedScrollView.IOnScrollChangeListener scrollChangeListener)
{
scrollChangeListener.OnScrollChange(_parentScrollView, l, t, oldl, oldt);
}
}
}
internal interface IScrollBarView
{
bool ScrollBarsInitialized { get; set; }
bool ScrollbarFadingEnabled { get; set; }
void AwakenScrollBars();
}
}