-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
DualScreenInfo.shared.cs
165 lines (140 loc) · 4.09 KB
/
DualScreenInfo.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Xamarin.Forms.DualScreen
{
public class HingeAngleChangedEventArgs : EventArgs
{
public HingeAngleChangedEventArgs(double hingeAngleInDegrees)
{
HingeAngleInDegrees = hingeAngleInDegrees;
}
public double HingeAngleInDegrees { get; }
}
public partial class DualScreenInfo : INotifyPropertyChanged
{
Rectangle[] _spanningBounds;
Rectangle _hingeBounds;
bool _isLandscape;
TwoPaneViewMode _spanMode;
TwoPaneViewLayoutGuide _twoPaneViewLayoutGuide;
IDualScreenService _dualScreenService;
IDualScreenService DualScreenService =>
_dualScreenService ?? DependencyService.Get<IDualScreenService>() ?? NoDualScreenServiceImpl.Instance;
static Lazy<DualScreenInfo> _dualScreenInfo { get; } = new Lazy<DualScreenInfo>(OnCreate);
public static DualScreenInfo Current => _dualScreenInfo.Value;
public event PropertyChangedEventHandler PropertyChanged;
public DualScreenInfo(VisualElement layout) : this(layout, null)
{
}
internal DualScreenInfo(VisualElement layout, IDualScreenService dualScreenService)
{
_dualScreenService = dualScreenService;
if (layout == null)
{
_twoPaneViewLayoutGuide = TwoPaneViewLayoutGuide.Instance;
}
else
{
_twoPaneViewLayoutGuide = new TwoPaneViewLayoutGuide(layout, dualScreenService);
_twoPaneViewLayoutGuide.PropertyChanged += OnTwoPaneViewLayoutGuideChanged;
}
}
EventHandler<HingeAngleChangedEventArgs> _hingeAngleChanged;
int subscriberCount = 0;
public event EventHandler<HingeAngleChangedEventArgs> HingeAngleChanged
{
add
{
ProcessHingeAngleSubscriberCount(Interlocked.Increment(ref subscriberCount));
_hingeAngleChanged += value;
}
remove
{
ProcessHingeAngleSubscriberCount(Interlocked.Decrement(ref subscriberCount));
_hingeAngleChanged -= value;
}
}
public Rectangle[] SpanningBounds
{
get => GetSpanningBounds();
set
{
SetProperty(ref _spanningBounds, value);
}
}
public Rectangle HingeBounds
{
get => GetHingeBounds();
set
{
SetProperty(ref _hingeBounds, value);
}
}
public bool IsLandscape
{
get => GetIsLandscape();
set
{
SetProperty(ref _isLandscape, value);
}
}
public TwoPaneViewMode SpanMode
{
get => GetSpanMode();
set
{
SetProperty(ref _spanMode, value);
}
}
Rectangle[] GetSpanningBounds()
{
var guide = _twoPaneViewLayoutGuide;
var hinge = guide.Hinge;
if (hinge == Rectangle.Zero)
return new Rectangle[0];
if (guide.Pane2 == Rectangle.Zero)
return new Rectangle[0];
if(IsLandscape)
return new[] { guide.Pane1, new Rectangle(0, hinge.Height + guide.Pane1.Height, guide.Pane2.Width, guide.Pane2.Height) };
else
return new[] { guide.Pane1, new Rectangle(hinge.Width + guide.Pane1.Width, 0, guide.Pane2.Width, guide.Pane2.Height) };
}
Rectangle GetHingeBounds()
{
var guide = _twoPaneViewLayoutGuide;
return guide.Hinge;
}
bool GetIsLandscape() => _twoPaneViewLayoutGuide.IsLandscape;
TwoPaneViewMode GetSpanMode() => _twoPaneViewLayoutGuide.Mode;
static DualScreenInfo OnCreate()
{
DualScreenInfo dualScreenInfo = new DualScreenInfo(null);
dualScreenInfo._twoPaneViewLayoutGuide.WatchForChanges();
dualScreenInfo._twoPaneViewLayoutGuide.PropertyChanged += dualScreenInfo.OnTwoPaneViewLayoutGuideChanged;
return dualScreenInfo;
}
void OnTwoPaneViewLayoutGuideChanged(object sender, PropertyChangedEventArgs e)
{
SpanningBounds = GetSpanningBounds();
IsLandscape = GetIsLandscape();
HingeBounds = GetHingeBounds();
SpanMode = GetSpanMode();
}
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();
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
return true;
}
}
}