private string CreateSchemeHandler(IntPtr configuration)
        {
            string host = null;

            if (string.IsNullOrWhiteSpace(config.ExternalHost))
            {
                const string scheme = "spidereye";
                host = UriTools.GetRandomResourceUrl(scheme);

                IntPtr handlerClass = ObjC.AllocateClassPair(ObjC.GetClass("NSObject"), "SchemeHandler" + count, IntPtr.Zero);
                ObjC.AddProtocol(handlerClass, ObjC.GetProtocol("WKURLSchemeHandler"));

                ObjC.AddMethod(
                    handlerClass,
                    ObjC.RegisterName("webView:startURLSchemeTask:"),
                    uriSchemeStartDelegate,
                    "v@:@@");

                ObjC.AddMethod(
                    handlerClass,
                    ObjC.RegisterName("webView:stopURLSchemeTask:"),
                    uriSchemeStopDelegate,
                    "v@:@@");

                ObjC.RegisterClassPair(handlerClass);

                IntPtr handler = ObjC.Call(handlerClass, "new");
                ObjC.Call(configuration, "setURLSchemeHandler:forURLScheme:", handler, NSString.Create(scheme));
            }

            return(host);
        }
        private IntPtr CreateCallbackClass()
        {
            IntPtr callbackClass = ObjC.AllocateClassPair(ObjC.GetClass("NSObject"), "CallbackClass" + count, IntPtr.Zero);

            ObjC.AddProtocol(callbackClass, ObjC.GetProtocol("WKNavigationDelegate"));
            ObjC.AddProtocol(callbackClass, ObjC.GetProtocol("WKScriptMessageHandler"));

            ObjC.AddMethod(
                callbackClass,
                ObjC.RegisterName("webView:didFinishNavigation:"),
                loadDelegate,
                "v@:@@");

            ObjC.AddMethod(
                callbackClass,
                ObjC.RegisterName("webView:didFailNavigation:withError:"),
                loadFailedDelegate,
                "v@:@@@");

            ObjC.AddMethod(
                callbackClass,
                ObjC.RegisterName("observeValueForKeyPath:ofObject:change:context:"),
                observedValueChangedDelegate,
                "v@:@@@@");

            ObjC.AddMethod(
                callbackClass,
                ObjC.RegisterName("userContentController:didReceiveScriptMessage:"),
                scriptDelegate,
                "v@:@@");

            ObjC.RegisterClassPair(callbackClass);

            return(ObjC.Call(callbackClass, "new"));
        }
        private void SetCallbackClass()
        {
            string name          = "MenuCallbackObject" + Interlocked.Increment(ref classCount);
            IntPtr callbackClass = ObjC.AllocateClassPair(ObjC.GetClass("NSObject"), name, IntPtr.Zero);

            ObjC.AddMethod(
                callbackClass,
                ObjC.RegisterName("menuCallback:"),
                menuDelegate,
                "v@:@");

            ObjC.RegisterClassPair(callbackClass);
            ObjC.Call(Handle, "setTarget:", ObjC.Call(callbackClass, "new"));
        }
Exemple #4
0
        public static unsafe IntPtr Create(string value)
        {
            if (value == null)
            {
                return(IntPtr.Zero);
            }

            fixed(char *ptr = value)
            {
                return(ObjC.SendMessage(
                           ObjC.GetClass("NSString"),
                           ObjC.RegisterName("stringWithCharacters:length:"),
                           (IntPtr)ptr,
                           new UIntPtr((uint)value.Length)));
            }
        }
        public unsafe NSBlock(Delegate callback)
        {
            this.callback = callback ?? throw new ArgumentNullException(nameof(callback));

            var blp = (BlockLiteral *)Marshal.AllocHGlobal(sizeof(BlockLiteral));
            var bdp = (BlockDescriptor *)Marshal.AllocHGlobal(sizeof(BlockDescriptor));

            blp->Isa        = ObjC.GetClass("__NSStackBlock");
            blp->Flags      = 0;
            blp->Reserved   = 0;
            blp->Invoke     = Marshal.GetFunctionPointerForDelegate(callback);
            blp->Descriptor = bdp;

            bdp->Reserved      = IntPtr.Zero;
            bdp->Size          = new IntPtr(sizeof(BlockLiteral));
            bdp->CopyHelper    = IntPtr.Zero;
            bdp->DisposeHelper = IntPtr.Zero;

            Handle = (IntPtr)blp;
        }
        private static IMenu appMenu; // needed to prevent garbage collection

        static Application()
        {
            OS = GetOS();
            CheckOs(OperatingSystem.MacOS);

            Factory = new CocoaUiFactory();

            // need to keep the delegates around or they will get garbage collected
            ShouldTerminateDelegateRef      = ShouldTerminateCallback;
            AppFinishedLaunchingDelegateRef = AppFinishedLaunching;

            AppHandle = GetApp();
            ObjC.Call(AppHandle, "setActivationPolicy:", IntPtr.Zero);

            IntPtr appDelegateClass = ObjC.AllocateClassPair(ObjC.GetClass("NSObject"), "AppDelegate", IntPtr.Zero);

            ObjC.AddProtocol(appDelegateClass, ObjC.GetProtocol("NSApplicationDelegate"));

            ObjC.AddMethod(
                appDelegateClass,
                ObjC.RegisterName("applicationShouldTerminateAfterLastWindowClosed:"),
                ShouldTerminateDelegateRef,
                "c@:@");

            ObjC.AddMethod(
                appDelegateClass,
                ObjC.RegisterName("applicationDidFinishLaunching:"),
                AppFinishedLaunchingDelegateRef,
                "v@:@");

            ObjC.RegisterClassPair(appDelegateClass);

            IntPtr appDelegate = ObjC.Call(appDelegateClass, "new");

            ObjC.Call(AppHandle, "setDelegate:", appDelegate);

            CreateDefaultAppMenu();
        }
        private void SetWindowDelegate(IntPtr window)
        {
            IntPtr windowDelegateClass = ObjC.AllocateClassPair(ObjC.GetClass("NSObject"), "WindowDelegate" + count, IntPtr.Zero);

            ObjC.AddProtocol(windowDelegateClass, ObjC.GetProtocol("NSWindowDelegate"));

            ObjC.AddMethod(
                windowDelegateClass,
                ObjC.RegisterName("windowShouldClose:"),
                windowShouldCloseDelegate,
                "c@:@");

            ObjC.AddMethod(
                windowDelegateClass,
                ObjC.RegisterName("windowWillClose:"),
                windowWillCloseDelegate,
                "v@:@");

            ObjC.RegisterClassPair(windowDelegateClass);

            IntPtr windowDelegate = ObjC.Call(windowDelegateClass, "new");

            ObjC.Call(window, "setDelegate:", windowDelegate);
        }
Exemple #8
0
 public static NativeClassDefinition FromObject(string name, params IntPtr[] protocols)
 {
     return(FromClass(name, ObjC.GetClass("NSObject"), protocols));
 }