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 pathFormsApplication.cs
308 lines (269 loc) · 7.51 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
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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
using ElmSharp;
using ElmSharp.Wearable;
using Tizen.Applications;
using Tizen.Common;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.Tizen.Native;
using DeviceOrientation = Xamarin.Forms.Internals.DeviceOrientation;
using ELayout = ElmSharp.Layout;
using Specific = Xamarin.Forms.PlatformConfiguration.TizenSpecific.Application;
namespace Xamarin.Forms.Platform.Tizen
{
public class FormsApplication : CoreUIApplication
{
ITizenPlatform _platform;
Application _application;
Window _window;
bool _useBezelInteration;
protected FormsApplication()
{
}
/// <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;
}
public CircleSurface BaseCircleSurface
{
get; protected set;
}
public bool UseBezelInteration => _useBezelInteration;
protected override void OnPreCreate()
{
base.OnPreCreate();
Application.ClearCurrent();
if (DotnetUtil.TizenAPIVersion < 5)
{
// We should set the env variable to support IsolatedStorageFile on tizen 4.0 or lower version.
Environment.SetEnvironmentVariable("XDG_DATA_HOME", Current.DirectoryInfo.Data);
}
var type = typeof(Window);
// Use reflection to avoid breaking compatibility. ElmSharp.Window.CreateWindow() is has been added since API6.
var methodInfo = type.GetMethod("CreateWindow", BindingFlags.NonPublic | BindingFlags.Static);
Window window = null;
if (methodInfo != null)
{
window = (Window)methodInfo.Invoke(null, new object[] { "FormsWindow" });
BaseLayout = (ELayout)window.GetType().GetProperty("BaseLayout")?.GetValue(window);
BaseCircleSurface = (CircleSurface)window.GetType().GetProperty("BaseCircleSurface")?.GetValue(window);
Forms.CircleSurface = BaseCircleSurface;
}
else // in case of Xamarin Preload
{
window = PreloadedWindow.GetInstance() ?? new Window("FormsWindow");
if (window is PreloadedWindow precreated)
{
BaseLayout = precreated.BaseLayout;
}
}
MainWindow = window;
}
protected override void OnTerminate()
{
base.OnTerminate();
if (_platform != null)
{
_platform.Dispose();
}
}
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);
if (Device.Idiom == TargetIdiom.Watch)
{
_useBezelInteration = Specific.GetUseBezelInteraction(_application);
UpdateOverlayContent();
}
}
void AppOnPropertyChanged(object sender, PropertyChangedEventArgs args)
{
if ("MainPage" == args.PropertyName)
{
SetPage(_application.MainPage);
}
else if (Device.Idiom == TargetIdiom.Watch)
{
if (Specific.UseBezelInteractionProperty.PropertyName == args.PropertyName)
{
_useBezelInteration = Specific.GetUseBezelInteraction(_application);
}
else if (Specific.OverlayContentProperty.PropertyName == args.PropertyName)
{
UpdateOverlayContent();
}
}
}
void UpdateOverlayContent()
{
EvasObject nativeView = null;
var content = Specific.GetOverlayContent(_application);
if (content != null)
{
var renderer = Platform.GetOrCreateRenderer(content);
(renderer as ILayoutRenderer)?.RegisterOnLayoutUpdated();
nativeView = renderer?.NativeView;
}
Forms.BaseLayout.SetOverlayPart(nativeView);
}
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();
// in case of no use of preloaded window
if (BaseLayout == null)
{
var conformant = new Conformant(MainWindow);
conformant.Show();
var layout = new ApplicationLayout(conformant);
layout.Show();
BaseLayout = layout;
if (Device.Idiom == TargetIdiom.Watch)
{
BaseCircleSurface = new CircleSurface(conformant);
Forms.CircleSurface = BaseCircleSurface;
}
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;
}
}
}