/
ScrollViewHandler.Windows.cs
205 lines (166 loc) · 6.38 KB
/
ScrollViewHandler.Windows.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
#nullable enable
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.Maui.Graphics;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Windows.UI.Core;
using static Microsoft.Maui.Layouts.LayoutExtensions;
namespace Microsoft.Maui.Handlers
{
public partial class ScrollViewHandler : ViewHandler<IScrollView, ScrollViewer>, ICrossPlatformLayout
{
const string ContentPanelTag = "MAUIScrollViewContentPanel";
protected override ScrollViewer CreatePlatformView()
{
return new ScrollViewer();
}
internal static void MapInvalidateMeasure(IScrollViewHandler handler, IView view, object? args)
{
handler.PlatformView.InvalidateMeasure(view);
if (handler.PlatformView.Content is FrameworkElement content)
{
content.InvalidateMeasure();
}
}
protected override void ConnectHandler(ScrollViewer platformView)
{
base.ConnectHandler(platformView);
platformView.ViewChanged += ViewChanged;
}
protected override void DisconnectHandler(ScrollViewer platformView)
{
base.DisconnectHandler(platformView);
platformView.ViewChanged -= ViewChanged;
}
public static void MapContent(IScrollViewHandler handler, IScrollView scrollView)
{
if (handler.PlatformView == null || handler.MauiContext == null)
return;
if (handler is not ICrossPlatformLayout crossPlatformLayout)
{
return;
}
UpdateContentPanel(scrollView, handler, crossPlatformLayout);
}
public static void MapHorizontalScrollBarVisibility(IScrollViewHandler handler, IScrollView scrollView)
{
handler.PlatformView?.UpdateScrollBarVisibility(scrollView.Orientation, scrollView.HorizontalScrollBarVisibility);
}
public static void MapVerticalScrollBarVisibility(IScrollViewHandler handler, IScrollView scrollView)
{
handler.PlatformView.VerticalScrollBarVisibility = scrollView.VerticalScrollBarVisibility.ToWindowsScrollBarVisibility();
}
public static void MapOrientation(IScrollViewHandler handler, IScrollView scrollView)
{
handler.PlatformView?.UpdateScrollBarVisibility(scrollView.Orientation, scrollView.HorizontalScrollBarVisibility);
}
public static void MapRequestScrollTo(IScrollViewHandler handler, IScrollView scrollView, object? args)
{
if (args is ScrollToRequest request)
{
handler.PlatformView.ChangeView(request.HorizontalOffset, request.VerticalOffset, null, request.Instant);
}
}
void ViewChanged(object? sender, ScrollViewerViewChangedEventArgs e)
{
VirtualView.VerticalOffset = PlatformView.VerticalOffset;
VirtualView.HorizontalOffset = PlatformView.HorizontalOffset;
if (e.IsIntermediate == false)
{
VirtualView.ScrollFinished();
}
}
/*
Problem 1: Windows treats Padding differently than what we want for MAUI; Padding creates space
_around_ the scrollable area, rather than padding the content inside of it.
Problem 2: The ScrollViewer will force any content to start at the origin (0,0), even if we ask
to arrange it at an offset. This defeats our content's Margin properties.
To handle this, we insert a container ContentPanel which always lays out at the origin but provides both
the Padding and the Margin for the content. The extra layer uses the native ContentPanel control we already
provide as the backing control for ContentView, Page, etc.
The extra layer also provides a place to call CrossPlatformArrange for the content, since we
can't subclass ScrollViewer.
The methods below exist to support inserting/updating the padding/margin panel.
*/
static ContentPanel? GetContentPanel(ScrollViewer scrollViewer)
{
if (scrollViewer.Content is ContentPanel contentPanel)
{
if (contentPanel.Tag is string tag && tag == ContentPanelTag)
{
return contentPanel;
}
}
return null;
}
static void UpdateContentPanel(IScrollView scrollView, IScrollViewHandler handler, ICrossPlatformLayout crossPlatformLayout)
{
if (scrollView.PresentedContent == null || handler.MauiContext == null)
{
return;
}
var scrollViewer = handler.PlatformView;
var nativeContent = scrollView.PresentedContent.ToPlatform(handler.MauiContext);
if (GetContentPanel(scrollViewer) is ContentPanel currentPaddingLayer)
{
if (currentPaddingLayer.Children.Count == 0 || currentPaddingLayer.Children[0] != nativeContent)
{
currentPaddingLayer.Children.Clear();
currentPaddingLayer.Children.Add(nativeContent);
}
}
else
{
InsertContentPanel(scrollViewer, scrollView, nativeContent, crossPlatformLayout);
}
}
static void InsertContentPanel(ScrollViewer scrollViewer, IScrollView scrollView, FrameworkElement nativeContent, ICrossPlatformLayout crossPlatformLayout)
{
if (scrollView.PresentedContent == null)
{
return;
}
var paddingShim = new ContentPanel()
{
CrossPlatformLayout = crossPlatformLayout,
Tag = ContentPanelTag
};
scrollViewer.Content = null;
paddingShim.Children.Add(nativeContent);
scrollViewer.Content = paddingShim;
}
Size ICrossPlatformLayout.CrossPlatformMeasure(double widthConstraint, double heightConstraint)
{
var scrollView = VirtualView;
var padding = scrollView.Padding;
var presentedContent = scrollView.PresentedContent;
if (presentedContent == null)
{
return new Size(padding.HorizontalThickness, padding.VerticalThickness);
}
// Exclude the padding while measuring the internal content ...
var measurementWidth = widthConstraint - padding.HorizontalThickness;
var measurementHeight = heightConstraint - padding.VerticalThickness;
var result = (scrollView as ICrossPlatformLayout).CrossPlatformMeasure(measurementWidth, measurementHeight);
// ... and add the padding back in to the final result
var fullSize = new Size(result.Width + padding.HorizontalThickness, result.Height + padding.VerticalThickness);
if (double.IsInfinity(widthConstraint))
{
widthConstraint = result.Width;
}
if (double.IsInfinity(heightConstraint))
{
heightConstraint = result.Height;
}
// If the presented content has LayoutAlignment Fill, we'll need to adjust the measurement to account for that
return fullSize.AdjustForFill(new Rect(0, 0, widthConstraint, heightConstraint), presentedContent);
}
Size ICrossPlatformLayout.CrossPlatformArrange(Rect bounds)
{
return (VirtualView as ICrossPlatformLayout).CrossPlatformArrange(bounds);
}
}
}