Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/xwt.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriain holmes <iain@xamarin.com>2020-03-13 18:23:57 +0300
committeriain holmes <iain@xamarin.com>2021-03-22 13:23:36 +0300
commitba1edff47720b5246e176e91cec72078bb20bfff (patch)
treec03f5e2841ce1340f067dcffa0ccdcbdc169f2d7
parentd89687fd4d3885b42f3aeca2e82e0c54dc4fb7d9 (diff)
Allow application to specify whether to initialize parent toolkit or not
If Xwt.Mac is initialized inside a Cocoa application then NSApplication.Initialize cannot be called twice.
-rw-r--r--Xwt.XamMac/Xwt.Mac/MacEngine.cs37
-rw-r--r--[-rwxr-xr-x]Xwt/Xwt.Backends/ToolkitEngineBackend.cs10
-rw-r--r--[-rwxr-xr-x]Xwt/Xwt/Application.cs14
-rw-r--r--Xwt/Xwt/Toolkit.cs21
4 files changed, 55 insertions, 27 deletions
diff --git a/Xwt.XamMac/Xwt.Mac/MacEngine.cs b/Xwt.XamMac/Xwt.Mac/MacEngine.cs
index 4d8b45c0..f487e79d 100644
--- a/Xwt.XamMac/Xwt.Mac/MacEngine.cs
+++ b/Xwt.XamMac/Xwt.Mac/MacEngine.cs
@@ -47,20 +47,29 @@ namespace Xwt.Mac
public override void InitializeApplication ()
{
- NSApplicationInitializer.Initialize ();
-
- //Hijack ();
- if (pool != null)
- pool.Dispose ();
- pool = new NSAutoreleasePool ();
- appDelegate = AppDelegateFactory?.Invoke(IsGuest) ?? new AppDelegate (IsGuest);
- NSApplication.SharedApplication.Delegate = appDelegate;
-
- // If NSPrincipalClass is not set, set it now. This allows running
- // the application without a bundle
- var info = NSBundle.MainBundle.InfoDictionary;
- if (info.ValueForKey ((NSString)"NSPrincipalClass") == null)
- info.SetValueForKey ((NSString)"NSApplication", (NSString)"NSPrincipalClass");
+ if (InitializeToolkit)
+ {
+ NSApplicationInitializer.Initialize();
+
+ //Hijack ();
+ if (pool != null)
+ pool.Dispose();
+ pool = new NSAutoreleasePool();
+ appDelegate = AppDelegateFactory?.Invoke(IsGuest) ?? new AppDelegate(IsGuest);
+ NSApplication.SharedApplication.Delegate = appDelegate;
+
+ // If NSPrincipalClass is not set, set it now. This allows running
+ // the application without a bundle
+ var info = NSBundle.MainBundle.InfoDictionary;
+ if (info.ValueForKey((NSString)"NSPrincipalClass") == null)
+ info.SetValueForKey((NSString)"NSApplication", (NSString)"NSPrincipalClass");
+ }
+ else
+ {
+ // Although the AppDelegate does not need to be hijacked, keep this allocated
+ // as there are some non-NSApplicationDelegate methods still used by Xwt
+ appDelegate = new AppDelegate(IsGuest);
+ }
}
public override void InitializeBackends ()
diff --git a/Xwt/Xwt.Backends/ToolkitEngineBackend.cs b/Xwt/Xwt.Backends/ToolkitEngineBackend.cs
index 7c215550..e36a5f74 100755..100644
--- a/Xwt/Xwt.Backends/ToolkitEngineBackend.cs
+++ b/Xwt/Xwt.Backends/ToolkitEngineBackend.cs
@@ -40,16 +40,19 @@ namespace Xwt.Backends
Dictionary<Type,Type> backendTypesByFrontend;
Toolkit toolkit;
bool isGuest;
+ bool initializeToolkit;
/// <summary>
/// Initialize the specified toolkit.
/// </summary>
/// <param name="toolkit">Toolkit to initialize.</param>
/// <param name="isGuest">If set to <c>true</c> the toolkit will be initialized as guest of another toolkit.</param>
- internal void Initialize (Toolkit toolkit, bool isGuest)
+ internal void Initialize (Toolkit toolkit, bool isGuest, bool initializeToolkit)
{
this.toolkit = toolkit;
this.isGuest = isGuest;
+ this.initializeToolkit = initializeToolkit;
+
if (backendTypes == null) {
backendTypes = new Dictionary<Type, Type> ();
backendTypesByFrontend = new Dictionary<Type, Type> ();
@@ -86,6 +89,11 @@ namespace Xwt.Backends
get { return isGuest; }
}
+ public bool InitializeToolkit
+ {
+ get { return initializeToolkit; }
+ }
+
/// <summary>
/// Initializes the application.
/// </summary>
diff --git a/Xwt/Xwt/Application.cs b/Xwt/Xwt/Application.cs
index cc9f545c..50e66313 100755..100644
--- a/Xwt/Xwt/Application.cs
+++ b/Xwt/Xwt/Application.cs
@@ -90,7 +90,12 @@ namespace Xwt
/// <param name="type">The toolkit type.</param>
public static void Initialize (ToolkitType type)
{
- Initialize (Toolkit.GetBackendType (type));
+ Initialize (Toolkit.GetBackendType (type), true);
+ }
+
+ public static void Initialize(ToolkitType type, bool initializeToolkit)
+ {
+ Initialize(Toolkit.GetBackendType(type), initializeToolkit);
toolkit.Type = type;
}
@@ -99,11 +104,16 @@ namespace Xwt
/// </summary>
/// <param name="backendType">The <see cref="Type.FullName"/> of the backend type.</param>
public static void Initialize (string backendType)
+ {
+ Initialize(backendType, true);
+ }
+
+ public static void Initialize (string backendType, bool initializeToolkit)
{
if (engine != null)
return;
- toolkit = Toolkit.Load (backendType, false);
+ toolkit = Toolkit.Load (backendType, false, initializeToolkit);
toolkit.SetActive ();
engine = toolkit.Backend;
mainLoop = new UILoop (toolkit);
diff --git a/Xwt/Xwt/Toolkit.cs b/Xwt/Xwt/Toolkit.cs
index 4ecd1f44..7a536876 100644
--- a/Xwt/Xwt/Toolkit.cs
+++ b/Xwt/Xwt/Toolkit.cs
@@ -190,7 +190,7 @@ namespace Xwt
/// <param name="fullTypeName">The <see cref="Type.FullName"/> of the toolkit type.</param>
public static Toolkit Load (string fullTypeName)
{
- return Load (fullTypeName, true);
+ return Load (fullTypeName, true, true);
}
/// <summary>
@@ -198,12 +198,13 @@ namespace Xwt
/// </summary>
/// <param name="fullTypeName">The <see cref="Type.FullName"/> of the toolkit type.</param>
/// <param name="isGuest">If set to <c>true</c> the toolkit is loaded as guest of another toolkit.</param>
- internal static Toolkit Load (string fullTypeName, bool isGuest)
+ /// <param name="initializeToolkit">If set to <c>true</c> the parent toolkit is initialized.</param>
+ internal static Toolkit Load (string fullTypeName, bool isGuest, bool initializeToolkit)
{
Toolkit t = new Toolkit ();
if (!string.IsNullOrEmpty (fullTypeName)) {
- t.LoadBackend (fullTypeName, isGuest, true);
+ t.LoadBackend (fullTypeName, isGuest, initializeToolkit, true);
var bk = knownBackends.FirstOrDefault (tk => fullTypeName.StartsWith (tk.TypeName));
if (bk != null)
t.Type = bk.Type;
@@ -211,7 +212,7 @@ namespace Xwt
}
foreach (var bk in knownBackends) {
- if (t.LoadBackend (bk.FullTypeName, isGuest, false)) {
+ if (t.LoadBackend (bk.FullTypeName, isGuest, initializeToolkit, false)) {
t.Type = bk.Type;
return t;
}
@@ -232,7 +233,7 @@ namespace Xwt
Toolkit t = new Toolkit ();
t.toolkitType = type;
- t.LoadBackend (GetBackendType (type), true, true);
+ t.LoadBackend (GetBackendType (type), true, true, true);
return t;
}
@@ -252,7 +253,7 @@ namespace Xwt
Toolkit t = new Toolkit ();
t.toolkitType = type;
- if (t.LoadBackend (GetBackendType (type), true, false)) {
+ if (t.LoadBackend (GetBackendType (type), true, true, false)) {
toolkit = t;
return true;
}
@@ -274,7 +275,7 @@ namespace Xwt
throw new ArgumentException ("Invalid toolkit type");
}
- bool LoadBackend (string type, bool isGuest, bool throwIfFails)
+ bool LoadBackend (string type, bool isGuest, bool initializeToolkit, bool throwIfFails)
{
int i = type.IndexOf (',');
string assembly = type.Substring (i+1).Trim ();
@@ -285,7 +286,7 @@ namespace Xwt
Type t = asm.GetType (type);
if (t != null) {
backend = (ToolkitEngineBackend) Activator.CreateInstance (t);
- Initialize (isGuest);
+ Initialize (isGuest, initializeToolkit);
return true;
}
}
@@ -299,10 +300,10 @@ namespace Xwt
return false;
}
- void Initialize (bool isGuest)
+ void Initialize (bool isGuest, bool initializeToolkit)
{
toolkits[Backend.GetType ()] = this;
- backend.Initialize (this, isGuest);
+ backend.Initialize (this, isGuest, initializeToolkit);
ContextBackendHandler = Backend.CreateBackend<ContextBackendHandler> ();
GradientBackendHandler = Backend.CreateBackend<GradientBackendHandler> ();
TextLayoutBackendHandler = Backend.CreateBackend<TextLayoutBackendHandler> ();