This repository was 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 pathTwoPaneViewLayoutGuide.shared.cs
395 lines (337 loc) · 9.48 KB
/
TwoPaneViewLayoutGuide.shared.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.DualScreen
{
internal class TwoPaneViewLayoutGuide : INotifyPropertyChanged
{
public static TwoPaneViewLayoutGuide Instance => _twoPaneViewLayoutGuide.Value;
static Lazy<TwoPaneViewLayoutGuide> _twoPaneViewLayoutGuide = new Lazy<TwoPaneViewLayoutGuide>(() => new TwoPaneViewLayoutGuide());
IDualScreenService DualScreenService =>
_dualScreenService ?? DependencyService.Get<IDualScreenService>() ?? NoDualScreenServiceImpl.Instance;
Rectangle _hinge;
Rectangle _leftPage;
Rectangle _rightPane;
TwoPaneViewMode _mode;
VisualElement _layout;
readonly IDualScreenService _dualScreenService;
bool _isLandscape;
public event PropertyChangedEventHandler PropertyChanged;
List<string> _pendingPropertyChanges = new List<string>();
Rectangle _absoluteLayoutPosition;
object _watchHandle;
Action _layoutChangedReference;
TwoPaneViewLayoutGuide()
{
}
public TwoPaneViewLayoutGuide(VisualElement layout) : this(layout, null)
{
}
internal TwoPaneViewLayoutGuide(VisualElement layout, IDualScreenService dualScreenService)
{
_layout = layout;
_dualScreenService = dualScreenService;
if(_layout != null)
{
UpdateLayouts();
_layout.PropertyChanged += OnLayoutPropertyChanged;
_layout.PropertyChanging += OnLayoutPropertyChanging;
WatchForChanges();
}
}
void OnLayoutPropertyChanging(object sender, PropertyChangingEventArgs e)
{
if (e.PropertyName == "Renderer")
{
StopWatchingForChanges();
}
}
void OnLayoutPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Renderer")
{
WatchForChanges();
}
}
public void WatchForChanges()
{
if (_layout != null && _watchHandle == null)
{
_layoutChangedReference = OnLayoutChanged;
var layoutHandle = DualScreenService.WatchForChangesOnLayout(_layout, _layoutChangedReference);
if (layoutHandle == null)
{
_layoutChangedReference = null;
return;
}
_watchHandle = layoutHandle;
OnScreenChanged(DualScreenService, EventArgs.Empty);
DualScreenService.OnScreenChanged += OnScreenChanged;
}
else
{
DualScreenService.OnScreenChanged += OnScreenChanged;
}
}
public void StopWatchingForChanges()
{
DualScreenService.OnScreenChanged -= OnScreenChanged;
if (_layout != null)
{
DualScreenService.StopWatchingForChangesOnLayout(_layout, _watchHandle);
}
_layoutChangedReference = null;
_watchHandle = null;
}
void OnLayoutChanged()
{
if (_watchHandle == null)
{
StopWatchingForChanges();
return;
}
OnScreenChanged(DualScreenService, EventArgs.Empty);
}
void OnScreenChanged(object sender, EventArgs e)
{
if (_layout == null)
{
UpdateLayouts();
return;
}
if(_layout != null && _watchHandle == null)
{
StopWatchingForChanges();
return;
}
var screenPosition = DualScreenService.GetLocationOnScreen(_layout);
if (screenPosition == null)
{
UpdateLayouts();
return;
}
var newPosition = new Rectangle(screenPosition.Value, _layout.Bounds.Size);
if (newPosition != _absoluteLayoutPosition)
{
_absoluteLayoutPosition = newPosition;
UpdateLayouts();
}
}
public bool IsLandscape
{
get => DualScreenService.IsLandscape;
set => SetProperty(ref _isLandscape, value);
}
public TwoPaneViewMode Mode
{
get
{
return GetTwoPaneViewMode();
}
private set
{
SetProperty(ref _mode, value);
}
}
public Rectangle Pane1
{
get
{
return _leftPage;
}
private set
{
SetProperty(ref _leftPage, value);
}
}
public Rectangle Pane2
{
get
{
return _rightPane;
}
private set
{
SetProperty(ref _rightPane, value);
}
}
public Rectangle Hinge
{
get
{
return DualScreenService.GetHinge();
}
private set
{
SetProperty(ref _hinge, value);
}
}
Rectangle GetContainerArea()
{
Rectangle containerArea;
if (_layout == null)
{
containerArea = new Rectangle(Point.Zero, DualScreenService.ScaledScreenSize);
}
else
{
containerArea = _layout.Bounds;
}
return containerArea;
}
Rectangle GetScreenRelativeBounds()
{
Rectangle containerArea;
if (_layout == null)
{
containerArea = new Rectangle(Point.Zero, DualScreenService.ScaledScreenSize);
}
else
{
var locationOnScreen = DualScreenService.GetLocationOnScreen(_layout);
if (!locationOnScreen.HasValue)
return Rectangle.Zero;
containerArea = new Rectangle(locationOnScreen.Value, _layout.Bounds.Size);
}
return containerArea;
}
internal void UpdateLayouts()
{
Rectangle containerArea = GetContainerArea();
if (containerArea.Width <= 0)
{
return;
}
// Pane dimensions are calculated relative to the layout container
Rectangle _newPane1 = Pane1;
Rectangle _newPane2 = Pane2;
var locationOnScreen = GetScreenRelativeBounds();
if (locationOnScreen == Rectangle.Zero && Hinge == Rectangle.Zero)
locationOnScreen = containerArea;
bool isSpanned = IsInMultipleRegions(locationOnScreen);
if (!DualScreenService.IsLandscape)
{
if (isSpanned)
{
var pane2X = Hinge.X + Hinge.Width;
var containerRightX = locationOnScreen.X + locationOnScreen.Width;
var pane2Width = containerRightX - pane2X;
_newPane1 = new Rectangle(0, 0, Hinge.X - locationOnScreen.X, locationOnScreen.Height);
_newPane2 = new Rectangle(_newPane1.Width + Hinge.Width, 0, pane2Width, locationOnScreen.Height);
}
else
{
// Check if part of the layout is underneath the hinge
var containerRightX = locationOnScreen.X + locationOnScreen.Width;
var hingeRightX = Hinge.X + Hinge.Width;
// Right side under hinge
if (containerRightX > Hinge.X && containerRightX < hingeRightX)
{
_newPane1 = new Rectangle(0, 0, Hinge.X - locationOnScreen.X, locationOnScreen.Height);
}
// left side under hinge
else if (Hinge.X < locationOnScreen.X && hingeRightX > locationOnScreen.X)
{
var amountObscured = hingeRightX - locationOnScreen.X;
_newPane1 = new Rectangle(amountObscured, 0, locationOnScreen.Width - amountObscured, locationOnScreen.Height);
}
else
_newPane1 = new Rectangle(0, 0, locationOnScreen.Width, locationOnScreen.Height);
_newPane2 = Rectangle.Zero;
}
}
else
{
if (isSpanned)
{
var pane2Y = Hinge.Y + Hinge.Height;
var containerBottomY = locationOnScreen.Y + locationOnScreen.Height;
var pane2Height = containerBottomY - pane2Y;
_newPane1 = new Rectangle(0, 0, locationOnScreen.Width, Hinge.Y - locationOnScreen.Y);
_newPane2 = new Rectangle(0, _newPane1.Height + Hinge.Height, locationOnScreen.Width, pane2Height);
}
else
{
// Check if part of the layout is underneath the hinge
var containerBottomY = locationOnScreen.Y + locationOnScreen.Height;
var hingeBottomY = Hinge.Y + Hinge.Height;
// bottom under hinge
if (containerBottomY > Hinge.Y && containerBottomY < hingeBottomY)
{
_newPane1 = new Rectangle(0, 0, locationOnScreen.Width, Hinge.Y - locationOnScreen.Y);
}
// top under hinge
else if (Hinge.Y < locationOnScreen.Y && hingeBottomY > locationOnScreen.Y)
{
var amountObscured = hingeBottomY - locationOnScreen.Y;
_newPane1 = new Rectangle(0, amountObscured, locationOnScreen.Width, locationOnScreen.Height - amountObscured);
}
else
_newPane1 = new Rectangle(0, 0, locationOnScreen.Width, locationOnScreen.Height);
_newPane2 = Rectangle.Zero;
}
}
if (_newPane2.Height < 0 || _newPane2.Width < 0)
_newPane2 = Rectangle.Zero;
if (_newPane1.Height < 0 || _newPane1.Width < 0)
_newPane1 = Rectangle.Zero;
Pane1 = _newPane1;
Pane2 = _newPane2;
Mode = GetTwoPaneViewMode();
Hinge = DualScreenService.GetHinge();
IsLandscape = DualScreenService.IsLandscape;
var properties = _pendingPropertyChanges.ToList();
_pendingPropertyChanges.Clear();
foreach (var property in properties)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
bool IsInMultipleRegions(Rectangle layoutBounds)
{
bool isInMultipleRegions = false;
var hinge = DualScreenService.GetHinge();
// Portrait
if (!DualScreenService.IsLandscape)
{
// Check that the control is over the split
if (layoutBounds.X < hinge.X && layoutBounds.X + layoutBounds.Width > (hinge.X + hinge.Width))
{
isInMultipleRegions = true;
}
}
else
{
// Check that the control is over the split
if (layoutBounds.Y < hinge.Y && layoutBounds.Y + layoutBounds.Height > (hinge.Y + hinge.Height))
{
isInMultipleRegions = true;
}
}
return isInMultipleRegions;
}
TwoPaneViewMode GetTwoPaneViewMode()
{
if (!IsInMultipleRegions(GetScreenRelativeBounds()))
return TwoPaneViewMode.SinglePane;
if (DualScreenService.IsLandscape)
return TwoPaneViewMode.Tall;
return TwoPaneViewMode.Wide;
}
protected bool SetProperty<T>(ref T backingStore, T value,
[CallerMemberName]string propertyName = "",
Action onChanged = null)
{
if (EqualityComparer<T>.Default.Equals(backingStore, value))
return false;
backingStore = value;
onChanged?.Invoke();
_pendingPropertyChanges.Add(propertyName);
return true;
}
}
}