Beispiel #1
0
 public NSRunLoop()
 {
     loop   = Foundation.Call("NSRunLoop", "currentRunLoop");
     mode   = ObjC.Call(loop, "currentMode");
     date   = Foundation.Call("NSDate", "distantFuture");
     method = ObjC.RegisterName("runMode:beforeDate:");
 }
Beispiel #2
0
        private static void FinishUriSchemeCallback(IntPtr url, IntPtr schemeTask, IntPtr data, long contentLength, Uri uri)
        {
            IntPtr response = Foundation.Call("NSURLResponse", "alloc");

            ObjC.Call(
                response,
                "initWithURL:MIMEType:expectedContentLength:textEncodingName:",
                url,
                NSString.Create(MimeTypes.FindForUri(uri)),
                new IntPtr(contentLength),
                IntPtr.Zero);

            ObjC.Call(schemeTask, "didReceiveResponse:", response);

            IntPtr nsData = Foundation.Call(
                "NSData",
                "dataWithBytesNoCopy:length:freeWhenDone:",
                data,
                new IntPtr(contentLength),
                IntPtr.Zero);

            ObjC.Call(schemeTask, "didReceiveData:", nsData);

            ObjC.Call(schemeTask, "didFinish");
        }
        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"));
            }
        }
Beispiel #4
0
        private static void FinishUriSchemeCallbackWithError(IntPtr schemeTask, int errorCode)
        {
            var error = Foundation.Call(
                "NSError",
                "errorWithDomain:code:userInfo:",
                NSString.Create("com.bildstein.spidereye"),
                new IntPtr(errorCode),
                IntPtr.Zero);

            ObjC.Call(schemeTask, "didFailWithError:", error);
        }
Beispiel #5
0
        private void SetFileFilters(IntPtr dialog, IEnumerable <FileFilter> filters)
        {
            var fileTypes = filters
                            .SelectMany(t => t.Filters.Select(u => u.TrimStart('*', '.')))
                            .Distinct()
                            .Select(t => NSString.Create(t));

            if (fileTypes.Any())
            {
                var data  = fileTypes.ToArray();
                var array = Foundation.Call("NSArray", "arrayWithObjects:count:", data, new IntPtr(data.Length));
                ObjC.Call(dialog, "setAllowedFileTypes:", array);
            }
        }
Beispiel #6
0
        protected override void BeforeShow(NSDialog dialog)
        {
            if (!string.IsNullOrWhiteSpace(InitialDirectory))
            {
                var url = Foundation.Call("NSURL", "fileURLWithPath:", NSString.Create(InitialDirectory));
                ObjC.Call(dialog.Handle, "setDirectoryURL:", url);
            }

            if (!string.IsNullOrWhiteSpace(FileName))
            {
                ObjC.Call(dialog.Handle, "setNameFieldStringValue:", NSString.Create(FileName));
            }

            SetFileFilters(dialog.Handle, FileFilters);
        }
        protected override NSDialog CreateDialog()
        {
            var panel = NSDialog.CreateOpenPanel();

            ObjC.Call(panel.Handle, "setCanChooseFiles:", false);
            ObjC.Call(panel.Handle, "setCanChooseDirectories:", true);
            ObjC.Call(panel.Handle, "setAllowsMultipleSelection:", false);

            if (!string.IsNullOrWhiteSpace(SelectedPath))
            {
                var url = Foundation.Call("NSURL", "fileURLWithPath:", NSString.Create(SelectedPath));
                ObjC.Call(panel.Handle, "setDirectoryURL:", url);
            }

            return(panel);
        }
Beispiel #8
0
        public void LoadUri(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }

            if (!uri.IsAbsoluteUri)
            {
                uri = new Uri(customHost, uri);
            }

            IntPtr nsUrl   = Foundation.Call("NSURL", "URLWithString:", NSString.Create(uri.ToString()));
            IntPtr request = Foundation.Call("NSURLRequest", "requestWithURL:", nsUrl);

            ObjC.Call(Handle, "loadRequest:", request);
        }
Beispiel #9
0
        static partial void InvokeImpl(Action action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            bool isMainThread = Foundation.Call("NSThread", "isMainThread") != IntPtr.Zero;

            if (isMainThread)
            {
                action();
            }
            else
            {
                Dispatch.SyncFunction(Dispatch.MainQueue, IntPtr.Zero, ctx => action());
            }
        }
Beispiel #10
0
        public void NavigateToFile(string url)
        {
            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }

            if (customHost != null)
            {
                url = UriTools.Combine(customHost, url).ToString();
            }
            else
            {
                url = UriTools.Combine(config.ExternalHost, url).ToString();
            }

            IntPtr nsUrl   = Foundation.Call("NSURL", "URLWithString:", NSString.Create(url));
            IntPtr request = Foundation.Call("NSURLRequest", "requestWithURL:", nsUrl);

            ObjC.Call(Handle, "loadRequest:", request);
        }
Beispiel #11
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");
        }
Beispiel #12
0
        public DialogResult Show(IWindow parent)
        {
            var window = parent as CocoaWindow;

            if (parent != null && window == null)
            {
                throw new ArgumentException("Invalid window type.", nameof(parent));
            }

            var dialog = CreateDialog();

            if (!string.IsNullOrWhiteSpace(InitialDirectory))
            {
                var url = Foundation.Call("NSURL", "fileURLWithPath:", NSString.Create(InitialDirectory));
                ObjC.Call(dialog.Handle, "setDirectoryURL:", url);
            }

            if (!string.IsNullOrWhiteSpace(FileName))
            {
                ObjC.Call(dialog.Handle, "setNameFieldStringValue:", NSString.Create(FileName));
            }

            ObjC.Call(dialog.Handle, "setTitle:", NSString.Create(Title));
            ObjC.Call(dialog.Handle, "setCanCreateDirectories:", true);
            SetFileFilters(dialog.Handle, FileFilters);

            int result = dialog.Run(window);

            var selection = ObjC.Call(dialog.Handle, "URL");

            FileName = NSString.GetString(ObjC.Call(selection, "path"));

            BeforeReturn(dialog);

            return(MapResult(result));
        }
Beispiel #13
0
        private unsafe void UpdateIcon(AppIcon?icon)
        {
            var image = IntPtr.Zero;

            if (icon != null && icon.Icons.Length > 0)
            {
                byte[] data = icon.GetIconData(icon.DefaultIcon);
                fixed(byte *dataPtr = data)
                {
                    IntPtr nsData = Foundation.Call(
                        "NSData",
                        "dataWithBytesNoCopy:length:freeWhenDone:",
                        (IntPtr)dataPtr,
                        new IntPtr(data.Length),
                        IntPtr.Zero);

                    image = AppKit.Call("NSImage", "alloc");
                    ObjC.Call(image, "initWithData:", nsData);
                    ObjC.Call(statusBarButton, "setImage:", image);
                }
            }

            ObjC.Call(statusBarButton, "setImage:", image);
        }