-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix NSApplication blocking main thread
- Loading branch information
1 parent
44fee38
commit 3a5c05f
Showing
4 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.Versioning; | ||
using SharpMetal.ObjectiveCCore; | ||
|
||
namespace SharpMetal.Examples.Common | ||
{ | ||
[SupportedOSPlatform("macos")] | ||
public class NSApplicationDelegate | ||
{ | ||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] | ||
public delegate void OnDidFinishLaunchingDelegate(IntPtr id, IntPtr cmd, IntPtr notification); | ||
|
||
private OnDidFinishLaunchingDelegate _onDidFinishLaunching; | ||
private NSApplication _application; | ||
|
||
public Action<NSApplication> OnDidFinishLaunching; | ||
public IntPtr NativePtr; | ||
|
||
public unsafe NSApplicationDelegate(NSApplication application) | ||
{ | ||
_application = application; | ||
char[] name = "AppDelegate".ToCharArray(); | ||
char[] types = "v@:#".ToCharArray(); | ||
|
||
fixed (char* pName = name) | ||
{ | ||
fixed (char* pTypes = types) | ||
{ | ||
_onDidFinishLaunching = (_, _, _) => OnDidFinishLaunching?.Invoke(_application); | ||
var delegatePtr = Marshal.GetFunctionPointerForDelegate(_onDidFinishLaunching); | ||
|
||
var appDelegateClass = ObjectiveC.objc_allocateClassPair(new ObjectiveCClass("NSObject"), pName, 0); | ||
var test = ObjectiveC.class_addMethod(appDelegateClass, "applicationDidFinishLaunching:", delegatePtr, pTypes); | ||
ObjectiveC.objc_registerClassPair(appDelegateClass); | ||
|
||
NativePtr = new ObjectiveCClass(appDelegateClass).AllocInit(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters