5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2011 Xamarin Inc
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.Reflection;
31
using System.Threading.Tasks;
35
public static class Application
37
static readonly TaskScheduler taskScheduler = new XwtTaskScheduler ();
39
public static TaskScheduler UITaskScheduler {
40
get { return taskScheduler; }
43
internal static System.Threading.Thread UIThread {
48
static EngineBackend engine;
50
internal static EngineBackend EngineBackend {
51
get { return engine; }
54
public static void Initialize ()
61
public static void Initialize (ToolkitType type)
63
Initialize (GetBackendType (type));
66
public static void Initialize (string backendType)
68
InitBackend (backendType);
69
engine.InitializeApplication ();
70
UIThread = System.Threading.Thread.CurrentThread;
73
public static void Run ()
75
Toolkit.InvokePlatformCode (delegate {
76
engine.RunApplication ();
80
public static void Exit ()
82
Toolkit.InvokePlatformCode (delegate {
83
engine.ExitApplication ();
87
/// Invokes an action in the GUI thread
89
/// <param name='action'>
90
/// The action to execute.
92
public static void Invoke (Action action)
94
engine.InvokeAsync (delegate {
96
Toolkit.EnterUserCode ();
98
Toolkit.ExitUserCode (null);
99
} catch (Exception ex) {
100
Toolkit.ExitUserCode (ex);
106
/// Invokes an action in the GUI thread after the provided time span
111
/// <param name='action'>
112
/// The action to execute.
115
/// This method schedules the execution of the provided function. The function
116
/// must return 'true' if it has to be executed again after the time span, or 'false'
117
/// if the timer can be discarded.
118
/// The execution of the funciton can be canceled by disposing the returned object.
120
public static IDisposable TimeoutInvoke (int ms, Func<bool> action)
122
return TimeoutInvoke (TimeSpan.FromMilliseconds (ms), action);
126
/// Invokes an action in the GUI thread after the provided time span
131
/// <param name='action'>
132
/// The action to execute.
135
/// This method schedules the execution of the provided function. The function
136
/// must return 'true' if it has to be executed again after the time span, or 'false'
137
/// if the timer can be discarded.
138
/// The execution of the funciton can be canceled by disposing the returned object.
140
public static IDisposable TimeoutInvoke (TimeSpan timeSpan, Func<bool> action)
142
Timer t = new Timer ();
143
t.Id = engine.TimerInvoke (delegate {
146
Toolkit.EnterUserCode ();
148
Toolkit.ExitUserCode (null);
149
} catch (Exception ex) {
150
Toolkit.ExitUserCode (ex);
157
public static void DispatchPendingEvents ()
160
Toolkit.ExitUserCode (null);
161
engine.DispatchPendingEvents ();
163
Toolkit.EnterUserCode ();
167
public static StatusIcon CreateStatusIcon ()
169
return new StatusIcon ();
172
public static event EventHandler<ExceptionEventArgs> UnhandledException;
174
class Timer: IDisposable
177
public void Dispose ()
179
Application.engine.CancelTimerInvoke (Id);
183
static bool LoadBackend (string type)
185
int i = type.IndexOf (',');
186
string assembly = type.Substring (i+1).Trim ();
187
type = type.Substring (0, i).Trim ();
189
Assembly asm = Assembly.Load (assembly);
191
Type t = asm.GetType (type);
193
engine = (EngineBackend) Activator.CreateInstance (t);
198
catch (Exception ex) {
199
Console.WriteLine (ex);
204
static void InitBackend (string type)
206
Toolkit.EnterUserCode ();
207
if (type != null && LoadBackend (type))
210
throw new InvalidOperationException ("Xwt engine not found");
213
internal static string GetBackendType (ToolkitType type)
215
string version = typeof(Application).Assembly.GetName ().Version.ToString ();
218
case ToolkitType.Gtk:
219
return "Xwt.GtkBackend.GtkEngine, Xwt.Gtk, Version=" + version;
220
case ToolkitType.Cocoa:
221
return "Xwt.Mac.MacEngine, Xwt.Mac, Version=" + version;
222
case ToolkitType.Wpf:
223
return "Xwt.WPFBackend.WPFEngine, Xwt.WPF, Version=" + version;
225
throw new ArgumentException ("Invalid toolkit type");
229
internal static void NotifyException (Exception ex)
231
var unhandledException = UnhandledException;
232
if (unhandledException != null)
234
unhandledException (null, new ExceptionEventArgs (ex));
238
Console.WriteLine (ex);
243
public enum ToolkitType