Пример #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"));
            }
        }
Пример #2
0
        public CocoaWindow(WindowConfiguration config, IUiFactory windowFactory)
        {
            if (windowFactory == null)
            {
                throw new ArgumentNullException(nameof(windowFactory));
            }

            this.config = config ?? throw new ArgumentNullException(nameof(config));

            Interlocked.Increment(ref count);

            // need to keep the delegates around or they will get garbage collected
            windowShouldCloseDelegate = WindowShouldCloseCallback;
            windowWillCloseDelegate   = WindowWillCloseCallback;

            Handle = AppKit.Call("NSWindow", "alloc");

            var style = NSWindowStyleMask.Titled | NSWindowStyleMask.Closable | NSWindowStyleMask.Miniaturizable;

            if (config.CanResize)
            {
                style |= NSWindowStyleMask.Resizable;
            }

            ObjC.SendMessage(
                Handle,
                ObjC.RegisterName("initWithContentRect:styleMask:backing:defer:"),
                new CGRect(0, 0, config.Width, config.Height),
                (int)style,
                2,
                0);

            Title = config.Title;

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

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

            var contentProvider = new EmbeddedFileProvider(config.ContentAssembly, config.ContentFolder);

            bridge  = new WebviewBridge();
            webview = new CocoaWebview(config, contentProvider, bridge);
            ObjC.Call(Handle, "setContentView:", webview.Handle);

            if (config.EnableScriptInterface)
            {
                bridge.Init(this, webview, windowFactory);
            }

            if (config.UseBrowserTitle)
            {
                webview.TitleChanged += Webview_TitleChanged;
                bridge.TitleChanged  += Webview_TitleChanged;
            }

            SetWindowDelegate(Handle);
        }