Esempio n. 1
0
        public CocoaWebview(WindowConfiguration config, IContentProvider contentProvider, WebviewBridge bridge)
        {
            this.config          = config ?? throw new ArgumentNullException(nameof(config));
            this.contentProvider = contentProvider ?? throw new ArgumentNullException(nameof(contentProvider));
            this.bridge          = bridge ?? throw new ArgumentNullException(nameof(bridge));

            Interlocked.Increment(ref count);

            // need to keep the delegates around or they will get garbage collected
            loadDelegate                 = LoadCallback;
            loadFailedDelegate           = LoadFailedCallback;
            observedValueChangedDelegate = ObservedValueChanged;
            scriptDelegate               = ScriptCallback;
            uriSchemeStartDelegate       = UriSchemeStartCallback;
            uriSchemeStopDelegate        = UriSchemeStopCallback;


            IntPtr configuration = WebKit.Call("WKWebViewConfiguration", "new");
            IntPtr manager       = ObjC.Call(configuration, "userContentController");
            IntPtr callbackClass = CreateCallbackClass();

            customHost = CreateSchemeHandler(configuration);

            if (config.EnableScriptInterface)
            {
                ObjC.Call(manager, "addScriptMessageHandler:name:", callbackClass, NSString.Create("external"));
                IntPtr script = WebKit.Call("WKUserScript", "alloc");
                ObjC.Call(
                    script,
                    "initWithSource:injectionTime:forMainFrameOnly:",
                    NSString.Create(Resources.GetInitScript("Mac")),
                    IntPtr.Zero,
                    IntPtr.Zero);
                ObjC.Call(manager, "addUserScript:", script);
            }

            Handle = WebKit.Call("WKWebView", "alloc");
            ObjC.Call(Handle, "initWithFrame:configuration:", CGRect.Zero, configuration);
            ObjC.Call(Handle, "setNavigationDelegate:", callbackClass);

            IntPtr bgColor = NSColor.FromHex(config.BackgroundColor);

            ObjC.Call(Handle, "setBackgroundColor:", bgColor);

            IntPtr boolValue = Foundation.Call("NSNumber", "numberWithBool:", 0);

            ObjC.Call(Handle, "setValue:forKey:", boolValue, NSString.Create("drawsBackground"));

            if (config.UseBrowserTitle)
            {
                ObjC.Call(Handle, "addObserver:forKeyPath:options:context:", callbackClass, NSString.Create("title"), IntPtr.Zero, IntPtr.Zero);
            }

            if (enableDevTools)
            {
                var preferences = ObjC.Call(configuration, "preferences");
                ObjC.Call(preferences, "setValue:forKey:", new IntPtr(1), NSString.Create("developerExtrasEnabled"));
            }
        }
Esempio n. 2
0
        public CocoaWebview(WebviewBridge bridge)
        {
            this.bridge = bridge ?? throw new ArgumentNullException(nameof(bridge));

            IntPtr configuration = WebKit.Call("WKWebViewConfiguration", "new");
            IntPtr manager       = ObjC.Call(configuration, "userContentController");

            callbackClass = CallbackClassDefinition.CreateInstance(this);
            schemeHandler = SchemeHandlerDefinition.CreateInstance(this);

            const string scheme = "spidereye";

            customHost = new Uri(UriTools.GetRandomResourceUrl(scheme));
            ObjC.Call(configuration, "setURLSchemeHandler:forURLScheme:", schemeHandler.Handle, NSString.Create(scheme));

            ObjC.Call(manager, "addScriptMessageHandler:name:", callbackClass.Handle, NSString.Create("external"));
            IntPtr script = WebKit.Call("WKUserScript", "alloc");

            ObjC.Call(
                script,
                "initWithSource:injectionTime:forMainFrameOnly:",
                NSString.Create(Resources.GetInitScript("Mac")),
                IntPtr.Zero,
                IntPtr.Zero);
            ObjC.Call(manager, "addUserScript:", script);

            Handle = WebKit.Call("WKWebView", "alloc");
            ObjC.Call(Handle, "initWithFrame:configuration:", CGRect.Zero, configuration);
            ObjC.Call(Handle, "setNavigationDelegate:", callbackClass.Handle);

            IntPtr boolValue = Foundation.Call("NSNumber", "numberWithBool:", false);

            ObjC.Call(Handle, "setValue:forKey:", boolValue, NSString.Create("drawsBackground"));
            ObjC.Call(Handle, "addObserver:forKeyPath:options:context:", callbackClass.Handle, NSString.Create("title"), IntPtr.Zero, IntPtr.Zero);

            preferences = ObjC.Call(configuration, "preferences");
        }