2 using System.ComponentModel;
3 using System.Diagnostics;
4 using Tizen.Applications;
6 using EProgressBar = ElmSharp.ProgressBar;
7 using EColor = ElmSharp.Color;
8 using ELabel = ElmSharp.Label;
10 namespace Xamarin.Forms.Platform.Tizen
12 public class FormsApplication : CoreUIApplication
15 Application _application;
18 Native.Dialog _pageBusyDialog;
19 Native.Window _window;
21 protected FormsApplication()
23 _isInitialStart = true;
28 /// Gets the main window or <c>null</c> if it's not set.
30 /// <value>The main window or <c>null</c>.</value>
31 public Native.Window MainWindow
44 protected override void OnPreCreate()
47 Application.ClearCurrent();
51 protected override void OnTerminate()
54 MessagingCenter.Unsubscribe<Page, AlertArguments>(this, "Xamarin.SendAlert");
55 MessagingCenter.Unsubscribe<Page, bool>(this, "Xamarin.BusySet");
56 MessagingCenter.Unsubscribe<Page, ActionSheetArguments>(this, "Xamarin.ShowActionSheet");
57 if (_platform != null)
63 protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
65 base.OnAppControlReceived(e);
67 if (!_isInitialStart && _application != null)
69 _application.SendResume();
71 _isInitialStart = false;
74 protected override void OnPause()
77 if (_application != null)
79 _application.SendSleepAsync();
83 protected override void OnResume()
86 if (_application != null)
88 _application.SendResume();
92 public void LoadApplication(Application application)
94 if (null == MainWindow)
96 throw new NullReferenceException("MainWindow is not prepared. This method should be called in OnCreated().");
98 if (null == application)
100 throw new ArgumentNullException("application");
102 _application = application;
103 Application.Current = application;
104 application.SendStart();
105 application.PropertyChanged += new PropertyChangedEventHandler(this.AppOnPropertyChanged);
106 SetPage(_application.MainPage);
109 void AppOnPropertyChanged(object sender, PropertyChangedEventArgs args)
111 if ("MainPage" == args.PropertyName)
113 SetPage(_application.MainPage);
117 void ShowActivityIndicatorDialog(bool enabled)
119 if (null == _pageBusyDialog)
121 _pageBusyDialog = new Native.Dialog(Forms.Context.MainWindow)
123 Orientation = PopupOrientation.Top,
126 var activity = new EProgressBar(_pageBusyDialog)
128 Style = "process_large",
131 activity.PlayPulse();
134 _pageBusyDialog.Content = activity;
137 _pageBusyCount = Math.Max(0, enabled ? _pageBusyCount + 1 : _pageBusyCount - 1);
138 if (_pageBusyCount > 0)
140 _pageBusyDialog.Show();
144 _pageBusyDialog.Dismiss();
145 _pageBusyDialog = null;
149 void SetPage(Page page)
151 if (!Forms.IsInitialized)
153 throw new InvalidOperationException("Call Forms.Init (UIApplication) before this");
155 if (_platform != null)
157 _platform.SetPage(page);
160 MessagingCenter.Subscribe<Page, bool>(this, Page.BusySetSignalName, delegate (Page sender, bool enabled)
162 ShowActivityIndicatorDialog(enabled);
165 MessagingCenter.Subscribe<Page, AlertArguments>(this, Page.AlertSignalName, delegate (Page sender, AlertArguments arguments)
167 Native.Dialog alert = new Native.Dialog(Forms.Context.MainWindow);
168 alert.Title = arguments.Title;
169 var message = arguments.Message.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace(Environment.NewLine, "<br>");
170 var label = new ELabel(alert)
172 Text = "<span font_size=30 color=#000000>" + message + "<\\span>",
176 var box = new Box(alert);
179 bool labelAdded = false;
180 box.Resized += (s, e) =>
182 label.LineWrapType = WrapType.Word;
183 //set 2% padding for alert text message width
184 label.LineWrapWidth = (int)Math.Round(box.Geometry.Width * 0.98);
187 /*Adding label to the box (box.PackEnd(label)) has been placed in box.Resized()
188 event due to get better performance. For some reason (probably EFL bug) when
189 it's placed outside of it, box.Resized() event is called far too many times.*/
197 Native.Button cancel = new Native.Button(alert) { Text = arguments.Cancel };
198 alert.NegativeButton = cancel;
199 cancel.Clicked += (s, evt) =>
201 arguments.SetResult(false);
205 if (arguments.Accept != null)
207 Native.Button ok = new Native.Button(alert) { Text = arguments.Accept };
208 alert.PositiveButton = ok;
209 ok.Clicked += (s, evt) =>
211 arguments.SetResult(true);
216 alert.BackButtonPressed += (s, evt) =>
218 arguments.SetResult(false);
225 MessagingCenter.Subscribe<Page, ActionSheetArguments>(this, Page.ActionSheetSignalName, delegate (Page sender, ActionSheetArguments arguments)
227 Native.Dialog alert = new Native.Dialog(Forms.Context.MainWindow);
229 alert.Title = arguments.Title;
230 Box box = new Box(alert);
232 if (null != arguments.Destruction)
234 Native.Button destruction = new Native.Button(alert)
236 Text = arguments.Destruction,
237 TextColor = EColor.Red,
240 destruction.Clicked += (s, evt) =>
242 arguments.SetResult(arguments.Destruction);
246 box.PackEnd(destruction);
249 foreach (string buttonName in arguments.Buttons)
251 Native.Button button = new Native.Button(alert)
256 button.Clicked += (s, evt) =>
258 arguments.SetResult(buttonName);
268 if (null != arguments.Cancel)
270 Native.Button cancel = new Native.Button(Forms.Context.MainWindow) { Text = arguments.Cancel };
271 alert.NegativeButton = cancel;
272 cancel.Clicked += (s, evt) =>
278 alert.BackButtonPressed += (s, evt) =>
286 _platform = new Platform(this);
287 if (_application != null)
289 _application.Platform = _platform;
291 _platform.SetPage(page);
296 Debug.Assert(null == MainWindow);
298 var window = new Native.Window();
299 window.Closed += (s, e) =>
303 window.RotationChanged += (sender, e) =>
305 switch (_window.CurrentOrientation)
307 case Native.DisplayOrientations.None:
308 Device.Info.CurrentOrientation = DeviceOrientation.Other;
311 case Native.DisplayOrientations.Portrait:
312 Device.Info.CurrentOrientation = DeviceOrientation.PortraitUp;
315 case Native.DisplayOrientations.Landscape:
316 Device.Info.CurrentOrientation = DeviceOrientation.LandscapeLeft;
319 case Native.DisplayOrientations.PortraitFlipped:
320 Device.Info.CurrentOrientation = DeviceOrientation.PortraitDown;
323 case Native.DisplayOrientations.LandscapeFlipped:
324 Device.Info.CurrentOrientation = DeviceOrientation.LandscapeRight;
333 Run(System.Environment.GetCommandLineArgs());
337 /// Exits the application's main loop, which initiates the process of its termination
339 public override void Exit()
341 if (_platform == null)
343 Log.Warn("Exit was already called or FormsApplication is not initialized yet.");
346 // before everything is closed, inform the MainPage that it is disappearing
349 (_platform?.Page as IPageController)?.SendDisappearing();
354 Log.Error("Exception thrown from SendDisappearing: {0}", e.Message);