2 using System.ComponentModel;
3 using System.Diagnostics;
4 using System.Threading.Tasks;
5 using System.Reflection;
7 using Tizen.Applications;
8 using Xamarin.Forms.Internals;
9 using ELayout = ElmSharp.Layout;
10 using DeviceOrientation = Xamarin.Forms.Internals.DeviceOrientation;
11 using Xamarin.Forms.Platform.Tizen.Native;
12 using ElmSharp.Wearable;
13 using Specific = Xamarin.Forms.PlatformConfiguration.TizenSpecific.Application;
14 using CircularUI = Tizen.Wearable.CircularUI.Forms;
16 namespace Xamarin.Forms.Platform.Tizen
19 public class FormsApplication : CoreUIApplication
21 ITizenPlatform _platform;
22 Application _application;
25 bool _useBezelInteration;
27 protected FormsApplication()
29 _isInitialStart = true;
33 /// Gets the main window or <c>null</c> if it's not set.
35 /// <value>The main window or <c>null</c>.</value>
36 public Window MainWindow
49 public ELayout BaseLayout
54 public CircleSurface BaseCircleSurface
59 public bool UseBezelInteration => _useBezelInteration;
61 protected override void OnPreCreate()
64 Application.ClearCurrent();
65 PreloadedWindow window = PreloadedWindow.GetInstance() ?? new PreloadedWindow();
67 MainWindow = window.Window;
69 if (window.BaseConformant != null)
71 window.BaseConformant.Show();
74 BaseLayout = window.BaseLayout;
77 BaseCircleSurface = window.BaseCircleSurface;
78 Forms.CircleSurface = BaseCircleSurface;
81 protected override void OnTerminate()
84 if (_platform != null)
90 protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
92 base.OnAppControlReceived(e);
94 if (!_isInitialStart && _application != null)
96 _application.SendResume();
98 _isInitialStart = false;
101 protected override void OnPause()
104 if (_application != null)
106 _application.SendSleep();
110 protected override void OnResume()
113 if (_application != null)
115 _application.SendResume();
119 [EditorBrowsable(EditorBrowsableState.Never)]
120 public static Func<Task> RequestingUserConsentFunc { get; set; } = null;
122 public async void LoadApplication(Application application)
124 if (RequestingUserConsentFunc != null)
126 await RequestingUserConsentFunc();
129 if (null == MainWindow)
131 throw new InvalidOperationException("MainWindow is not prepared. This method should be called in OnCreated().");
134 if (null == application)
136 throw new ArgumentNullException(nameof(application));
138 _application = application;
139 Application.Current = application;
140 application.SendStart();
141 application.PropertyChanged += new PropertyChangedEventHandler(this.AppOnPropertyChanged);
142 SetPage(_application.MainPage);
143 if (Device.Idiom == TargetIdiom.Watch)
145 _useBezelInteration = Specific.GetUseBezelInteraction(_application);
146 UpdateOverlayContent();
150 void AppOnPropertyChanged(object sender, PropertyChangedEventArgs args)
152 if ("MainPage" == args.PropertyName)
154 SetPage(_application.MainPage);
156 else if (Device.Idiom == TargetIdiom.Watch)
158 if (Specific.UseBezelInteractionProperty.PropertyName == args.PropertyName)
160 _useBezelInteration = Specific.GetUseBezelInteraction(_application);
162 else if (Specific.OverlayContentProperty.PropertyName == args.PropertyName)
164 UpdateOverlayContent();
169 void UpdateOverlayContent()
171 EvasObject nativeView = null;
172 var content = Specific.GetOverlayContent(_application);
175 var renderer = Platform.GetOrCreateRenderer(content);
176 (renderer as LayoutRenderer)?.RegisterOnLayoutUpdated();
177 nativeView = renderer?.NativeView;
179 Forms.BaseLayout.SetPartContent("elm.swallow.overlay", nativeView);
182 void SetPage(Page page)
184 if (!Forms.IsInitialized)
186 throw new InvalidOperationException("Call Forms.Init (UIApplication) before this");
189 #pragma warning disable CS0618 // Type or member is obsolete
190 // The Platform property is no longer necessary, but we have to set it because some third-party
191 // library might still be retrieving it and using it
192 if (_application != null)
194 _application.Platform = _platform;
196 #pragma warning restore CS0618 // Type or member is obsolete
198 _platform.HasAlpha = MainWindow.Alpha;
199 _platform.SetPage(page);
201 Device.BeginInvokeOnMainThread(() => Native.NativeFactory.DeleteUnusedNative());
202 Device.BeginInvokeOnMainThread(() => CircularUI.Renderer.NativeFactory.DeleteUnusedNative());
205 void InitializeWindow()
207 Debug.Assert(MainWindow != null, "Window cannot be null");
212 // in case of no use of preloaded window
213 if (BaseLayout == null)
215 var conformant = new Conformant(MainWindow);
218 var layout = new ELayout(conformant);
219 layout.SetTheme("layout", "application", "default");
224 if (Device.Idiom == TargetIdiom.Watch)
226 BaseCircleSurface = new CircleSurface(conformant);
227 Forms.CircleSurface = BaseCircleSurface;
229 conformant.SetContent(BaseLayout);
232 MainWindow.AvailableRotations = DisplayRotation.Degree_0 | DisplayRotation.Degree_90 | DisplayRotation.Degree_180 | DisplayRotation.Degree_270;
234 MainWindow.Deleted += (s, e) =>
239 Device.Info.CurrentOrientation = MainWindow.GetDeviceOrientation();
241 MainWindow.RotationChanged += (sender, e) =>
243 Device.Info.CurrentOrientation = MainWindow.GetDeviceOrientation();
246 MainWindow.BackButtonPressed += (sender, e) =>
248 if (_platform != null)
250 if (!_platform.SendBackButtonPressed())
257 _platform = Platform.CreatePlatform(BaseLayout);
258 BaseLayout.SetContent(_platform.GetRootNativeView());
259 _platform.RootNativeViewChanged += (s, e) => BaseLayout.SetContent(e.RootNativeView);
264 Run(System.Environment.GetCommandLineArgs());
268 /// Exits the application's main loop, which initiates the process of its termination
270 public override void Exit()
272 if (_platform == null)
274 Log.Warn("Exit was already called or FormsApplication is not initialized yet.");
284 Log.Error("Exception thrown from Dispose: {0}", e.Message);
290 static class WindowExtension
292 public static DeviceOrientation GetDeviceOrientation(this Window window)
294 DeviceOrientation orientation = DeviceOrientation.Other;
295 var isPortraitDevice = Forms.NaturalOrientation.IsPortrait();
296 switch (window.Rotation)
300 orientation = isPortraitDevice ? DeviceOrientation.Portrait : DeviceOrientation.Landscape;
305 orientation = isPortraitDevice ? DeviceOrientation.Landscape : DeviceOrientation.Portrait;