forked from xamarin/Xamarin.Forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormsApplication.cs
250 lines (216 loc) · 5.53 KB
/
FormsApplication.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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading.Tasks;
using ElmSharp;
using Tizen.Applications;
using Xamarin.Forms.Internals;
using ELayout = ElmSharp.Layout;
using DeviceOrientation = Xamarin.Forms.Internals.DeviceOrientation;
namespace Xamarin.Forms.Platform.Tizen
{
public class FormsApplication : CoreUIApplication
{
ITizenPlatform _platform;
Application _application;
bool _isInitialStart;
Window _window;
protected FormsApplication()
{
_isInitialStart = true;
}
/// <summary>
/// Gets the main window or <c>null</c> if it's not set.
/// </summary>
/// <value>The main window or <c>null</c>.</value>
public Window MainWindow
{
get
{
return _window;
}
protected set
{
_window = value;
InitializeWindow();
}
}
public ELayout BaseLayout
{
get; protected set;
}
protected override void OnPreCreate()
{
base.OnPreCreate();
Application.ClearCurrent();
MainWindow = PreloadedWindow.GetInstance() ?? new Window("FormsWindow");
}
protected override void OnTerminate()
{
base.OnTerminate();
if (_platform != null)
{
_platform.Dispose();
}
}
protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
{
base.OnAppControlReceived(e);
if (!_isInitialStart && _application != null)
{
_application.SendResume();
}
_isInitialStart = false;
}
protected override void OnPause()
{
base.OnPause();
if (_application != null)
{
_application.SendSleep();
}
}
protected override void OnResume()
{
base.OnResume();
if (_application != null)
{
_application.SendResume();
}
}
[EditorBrowsable(EditorBrowsableState.Never)]
public static Func<Task> RequestingUserConsentFunc { get; set; } = null;
public async void LoadApplication(Application application)
{
if (RequestingUserConsentFunc != null)
{
await RequestingUserConsentFunc();
}
if (null == MainWindow)
{
throw new InvalidOperationException("MainWindow is not prepared. This method should be called in OnCreated().");
}
if (null == application)
{
throw new ArgumentNullException(nameof(application));
}
_application = application;
Application.Current = application;
application.SendStart();
application.PropertyChanged += new PropertyChangedEventHandler(this.AppOnPropertyChanged);
SetPage(_application.MainPage);
}
void AppOnPropertyChanged(object sender, PropertyChangedEventArgs args)
{
if ("MainPage" == args.PropertyName)
{
SetPage(_application.MainPage);
}
}
void SetPage(Page page)
{
if (!Forms.IsInitialized)
{
throw new InvalidOperationException("Call Forms.Init (UIApplication) before this");
}
#pragma warning disable CS0618 // Type or member is obsolete
// The Platform property is no longer necessary, but we have to set it because some third-party
// library might still be retrieving it and using it
if (_application != null)
{
_application.Platform = _platform;
}
#pragma warning restore CS0618 // Type or member is obsolete
_platform.HasAlpha = MainWindow.Alpha;
_platform.SetPage(page);
}
void InitializeWindow()
{
Debug.Assert(MainWindow != null, "Window cannot be null");
MainWindow.Active();
MainWindow.Show();
if (MainWindow is PreloadedWindow precreated)
{
BaseLayout = precreated.BaseLayout;
}
else
{
var conformant = new Conformant(MainWindow);
conformant.Show();
var layout = new ELayout(conformant);
layout.SetTheme("layout", "application", "default");
layout.Show();
BaseLayout = layout;
conformant.SetContent(BaseLayout);
}
MainWindow.AvailableRotations = DisplayRotation.Degree_0 | DisplayRotation.Degree_90 | DisplayRotation.Degree_180 | DisplayRotation.Degree_270;
MainWindow.Deleted += (s, e) =>
{
Exit();
};
Device.Info.CurrentOrientation = MainWindow.GetDeviceOrientation();
MainWindow.RotationChanged += (sender, e) =>
{
Device.Info.CurrentOrientation = MainWindow.GetDeviceOrientation();
};
MainWindow.BackButtonPressed += (sender, e) =>
{
if (_platform != null)
{
if (!_platform.SendBackButtonPressed())
{
Exit();
}
}
};
_platform = Platform.CreatePlatform(BaseLayout);
BaseLayout.SetContent(_platform.GetRootNativeView());
_platform.RootNativeViewChanged += (s, e) => BaseLayout.SetContent(e.RootNativeView);
}
public void Run()
{
Run(System.Environment.GetCommandLineArgs());
}
/// <summary>
/// Exits the application's main loop, which initiates the process of its termination
/// </summary>
public override void Exit()
{
if (_platform == null)
{
Log.Warn("Exit was already called or FormsApplication is not initialized yet.");
return;
}
try
{
_platform.Dispose();
_platform = null;
}
catch (Exception e)
{
Log.Error("Exception thrown from Dispose: {0}", e.Message);
}
base.Exit();
}
}
static class WindowExtension
{
public static DeviceOrientation GetDeviceOrientation(this Window window)
{
DeviceOrientation orientation = DeviceOrientation.Other;
var isPortraitDevice = Forms.NaturalOrientation.IsPortrait();
switch (window.Rotation)
{
case 0:
case 180:
orientation = isPortraitDevice ? DeviceOrientation.Portrait : DeviceOrientation.Landscape;
break;
case 90:
case 270:
orientation = isPortraitDevice ? DeviceOrientation.Landscape : DeviceOrientation.Portrait;
break;
}
return orientation;
}
}
}