This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathShellFlyoutRenderer.cs
359 lines (288 loc) · 9.82 KB
/
ShellFlyoutRenderer.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
using System;
using System.ComponentModel;
using Android.Content;
using Android.Graphics;
using Android.Util;
using Android.Views;
using AndroidX.DrawerLayout.Widget;
using Xamarin.Forms.Internals;
using AView = Android.Views.View;
using LP = Android.Views.ViewGroup.LayoutParams;
namespace Xamarin.Forms.Platform.Android
{
public class ShellFlyoutRenderer : DrawerLayout, IShellFlyoutRenderer, DrawerLayout.IDrawerListener, IFlyoutBehaviorObserver, IAppearanceObserver
{
#region IAppearanceObserver
void IAppearanceObserver.OnAppearanceChanged(ShellAppearance appearance)
{
var previousFlyoutWidth = FlyoutWidth;
var previousFlyoutHeight = FlyoutHeight;
if (appearance == null)
{
UpdateScrim(Brush.Transparent);
_flyoutWidth = -1;
_flyoutHeight = LP.MatchParent;
}
else
{
UpdateScrim(appearance.FlyoutBackdrop);
if (appearance.FlyoutHeight != -1)
_flyoutHeight = Context.ToPixels(appearance.FlyoutHeight);
else
_flyoutHeight = LP.MatchParent;
if (appearance.FlyoutWidth != -1)
_flyoutWidth = Context.ToPixels(appearance.FlyoutWidth);
else
_flyoutWidth = -1;
}
if (previousFlyoutWidth != FlyoutWidth || previousFlyoutHeight != FlyoutHeight)
{
UpdateFlyoutSize();
if (_content != null)
UpdateDrawerLockMode(_behavior);
}
}
#endregion IAppearanceObserver
#region IShellFlyoutRenderer
AView IShellFlyoutRenderer.AndroidView => this;
void IShellFlyoutRenderer.AttachFlyout(IShellContext context, AView content)
{
AttachFlyout(context, content);
}
#endregion IShellFlyoutRenderer
#region IDrawerListener
void IDrawerListener.OnDrawerClosed(AView drawerView)
{
Shell.SetValueFromRenderer(Shell.FlyoutIsPresentedProperty, false);
}
void IDrawerListener.OnDrawerOpened(AView drawerView)
{
Shell.SetValueFromRenderer(Shell.FlyoutIsPresentedProperty, true);
}
void IDrawerListener.OnDrawerSlide(AView drawerView, float slideOffset)
{
_scrimOpacity = (int)(slideOffset * 255);
}
void IDrawerListener.OnDrawerStateChanged(int newState)
{
if (DrawerLayout.StateIdle == newState)
{
Shell.SetValueFromRenderer(Shell.FlyoutIsPresentedProperty, IsDrawerOpen(_flyoutContent.AndroidView));
}
}
#endregion IDrawerListener
#region IFlyoutBehaviorObserver
void IFlyoutBehaviorObserver.OnFlyoutBehaviorChanged(FlyoutBehavior behavior)
{
bool closeAfterUpdate = (behavior == FlyoutBehavior.Flyout && _behavior == FlyoutBehavior.Locked);
_behavior = behavior;
UpdateDrawerLockMode(behavior);
if (closeAfterUpdate)
CloseDrawer(_flyoutContent.AndroidView, false);
}
#endregion IFlyoutBehaviorObserver
const uint DefaultScrimColor = 0x99000000;
readonly IShellContext _shellContext;
AView _content;
IShellFlyoutContentRenderer _flyoutContent;
int _flyoutWidthDefault;
double _flyoutWidth = -1;
double _flyoutHeight;
int _currentLockMode;
bool _disposed;
Brush _scrimBrush;
Paint _scrimPaint;
int _previousHeight;
int _previousWidth;
int _scrimOpacity;
FlyoutBehavior _behavior;
public ShellFlyoutRenderer(IShellContext shellContext, Context context) : base(context)
{
_scrimBrush = Brush.Default;
_shellContext = shellContext;
_flyoutHeight = LP.MatchParent;
Shell.PropertyChanged += OnShellPropertyChanged;
ShellController.AddAppearanceObserver(this, Shell);
}
double FlyoutWidth => (_flyoutWidth == -1) ? _flyoutWidthDefault : _flyoutWidth;
int FlyoutHeight => (_flyoutHeight == -1) ? LP.MatchParent : (int)_flyoutHeight;
Shell Shell => _shellContext.Shell;
IShellController ShellController => _shellContext.Shell;
public override bool OnInterceptTouchEvent(MotionEvent ev)
{
bool result = base.OnInterceptTouchEvent(ev);
if (GetDrawerLockMode(_flyoutContent.AndroidView) == LockModeLockedOpen)
return false;
return result;
}
protected override bool DrawChild(Canvas canvas, AView child, long drawingTime)
{
bool returnValue = base.DrawChild(canvas, child, drawingTime);
if (_scrimPaint != null && ((LayoutParams)child.LayoutParameters).Gravity == (int)GravityFlags.NoGravity)
{
if (_previousHeight != Height || _previousWidth != Width)
{
_scrimPaint.UpdateBackground(_scrimBrush, Height, Width);
_previousHeight = Height;
_previousWidth = Width;
}
_scrimPaint.Alpha = _scrimOpacity;
canvas.DrawRect(0, 0, Width, Height, _scrimPaint);
}
return returnValue;
}
protected virtual void AttachFlyout(IShellContext context, AView content)
{
Profile.FrameBegin();
_content = content;
Profile.FramePartition("Create ContentRenderer");
_flyoutContent = context.CreateShellFlyoutContentRenderer();
// Depending on what you read the right edge of the drawer should be Max(56dp, actionBarSize)
// from the right edge of the screen. Fine. Well except that doesn't account
// for landscape devices, in which case its still, according to design
// documents from google 56dp, except google doesn't do that with their own apps.
// So we are just going to go ahead and do what google does here even though
// this isn't what DrawerLayout does by default.
// Oh then there is this rule about how wide it should be at most. It should not
// at least according to docs be more than 6 * actionBarSize wide. Again non of
// this is about landscape devices and google does not perfectly follow these
// rules... so we'll kind of just... do our best.
Profile.FramePartition("Fudge Width");
var metrics = Context.Resources.DisplayMetrics;
var width = Math.Min(metrics.WidthPixels, metrics.HeightPixels);
var actionBarHeight = (int)Context.ToPixels(56);
using (var tv = new TypedValue())
{
if (Context.Theme.ResolveAttribute(global::Android.Resource.Attribute.ActionBarSize, tv, true))
{
actionBarHeight = TypedValue.ComplexToDimensionPixelSize(tv.Data, metrics);
}
}
width -= actionBarHeight;
var maxWidth = actionBarHeight * 6;
width = Math.Min(width, maxWidth);
_flyoutWidthDefault = width;
UpdateFlyoutSize();
Profile.FramePartition("AddView Content");
AddView(content);
Profile.FramePartition("AddView Flyout");
AddView(_flyoutContent.AndroidView);
Profile.FramePartition("Add DrawerListener");
AddDrawerListener(this);
Profile.FramePartition("Add BehaviorObserver");
((IShellController)context.Shell).AddFlyoutBehaviorObserver(this);
Profile.FrameEnd();
if (Shell.FlyoutIsPresented)
OpenDrawer(_flyoutContent.AndroidView, false);
}
protected virtual void OnShellPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == Shell.FlyoutIsPresentedProperty.PropertyName)
{
var presented = Shell.FlyoutIsPresented;
if (presented)
OpenDrawer(_flyoutContent.AndroidView, true);
else
CloseDrawers();
}
}
void OnDualScreenServiceScreenChanged(object sender, EventArgs e)
{
UpdateFlyoutSize();
if (_content != null)
UpdateDrawerLockMode(_behavior);
}
protected virtual void UpdateDrawerLockMode(FlyoutBehavior behavior)
{
switch (behavior)
{
case FlyoutBehavior.Disabled:
CloseDrawers();
Shell.SetValueFromRenderer(Shell.FlyoutIsPresentedProperty, false);
_currentLockMode = LockModeLockedClosed;
SetDrawerLockMode(_currentLockMode);
_content.SetPadding(0, _content.PaddingTop, _content.PaddingRight, _content.PaddingBottom);
break;
case FlyoutBehavior.Flyout:
_currentLockMode = LockModeUnlocked;
SetDrawerLockMode(_currentLockMode);
_content.SetPadding(0, _content.PaddingTop, _content.PaddingRight, _content.PaddingBottom);
break;
case FlyoutBehavior.Locked:
Shell.SetValueFromRenderer(Shell.FlyoutIsPresentedProperty, true);
_currentLockMode = LockModeLockedOpen;
SetDrawerLockMode(_currentLockMode);
_content.SetPadding((int)FlyoutWidth, _content.PaddingTop, _content.PaddingRight, _content.PaddingBottom);
break;
}
UpdateScrim(_scrimBrush);
}
double _previouslyMeasuredFlyoutWidth;
int _previouslyMeasuredFlyoutHeight;
void UpdateFlyoutSize()
{
if (_flyoutContent?.AndroidView != null &&
(_previouslyMeasuredFlyoutWidth != FlyoutWidth || _previouslyMeasuredFlyoutHeight != FlyoutHeight))
{
_previouslyMeasuredFlyoutWidth = FlyoutWidth;
_previouslyMeasuredFlyoutHeight = FlyoutHeight;
_flyoutContent.AndroidView.LayoutParameters =
new LayoutParams((int)FlyoutWidth, FlyoutHeight) { Gravity = (int)GravityFlags.Start };
// This forces a redraw of the flyout
// without this the flyout will just be empty once you change
// the width
if (Shell.FlyoutIsPresented)
OpenDrawer(_flyoutContent.AndroidView, false);
}
}
void UpdateScrim(Brush backdrop)
{
_scrimBrush = backdrop;
if (_behavior == FlyoutBehavior.Locked)
{
SetScrimColor(Color.Transparent.ToAndroid());
_scrimPaint = null;
}
else
{
if (backdrop is SolidColorBrush solidColor)
{
_scrimPaint = null;
var backdropColor = solidColor.Color;
if (backdropColor == Color.Default)
{
unchecked
{
SetScrimColor((int)DefaultScrimColor);
}
}
else
SetScrimColor(backdropColor.ToAndroid());
}
else
{
_scrimPaint = _scrimPaint ?? new Paint();
_scrimPaint.UpdateBackground(_scrimBrush, Height, Width);
SetScrimColor(Color.Transparent.ToAndroid());
}
}
}
protected override void Dispose(bool disposing)
{
if (_disposed)
return;
_disposed = true;
if (disposing)
{
ShellController.RemoveAppearanceObserver(this);
Shell.PropertyChanged -= OnShellPropertyChanged;
RemoveDrawerListener(this);
((IShellController)_shellContext.Shell).RemoveFlyoutBehaviorObserver(this);
RemoveView(_content);
RemoveView(_flyoutContent.AndroidView);
_flyoutContent.Dispose();
}
base.Dispose(disposing);
}
}
}