public Task <string?> ExecuteScriptAsync(string script) { var taskResult = new TaskCompletionSource <string?>(); NSBlock?block = null; ScriptEvalCallbackDelegate callback = (IntPtr self, IntPtr result, IntPtr error) => { try { if (error != IntPtr.Zero) { string?message = NSString.GetString(ObjC.Call(error, "localizedDescription")); taskResult.TrySetException(new ScriptException($"Script execution failed with: \"{message}\"")); } else { string?content = NSString.GetString(result); taskResult.TrySetResult(content); } } catch (Exception ex) { taskResult.TrySetException(ex); } finally { block !.Dispose(); } }; block = new NSBlock(callback); ObjC.Call( Handle, "evaluateJavaScript:completionHandler:", NSString.Create(script), block.Handle); return(taskResult.Task); }
private static void ObservedValueChanged(CocoaWebview instance, IntPtr keyPath) { string?key = NSString.GetString(keyPath); if (key == "title" && instance.UseBrowserTitle) { string?title = NSString.GetString(ObjC.Call(instance.Handle, "title")); instance.TitleChanged?.Invoke(instance, title ?? string.Empty); } }
private void ObservedValueChanged(IntPtr self, IntPtr op, IntPtr keyPath, IntPtr obj, IntPtr change, IntPtr context) { string key = NSString.GetString(keyPath); if (key == "title") { string title = NSString.GetString(ObjC.Call(Handle, "title")); TitleChanged?.Invoke(this, title); } }
private async void ScriptCallback(IntPtr self, IntPtr op, IntPtr notification, IntPtr message) { IntPtr body = ObjC.Call(message, "body"); IntPtr isString = ObjC.Call(body, "isKindOfClass:", Foundation.GetClass("NSString")); if (isString != IntPtr.Zero) { string data = NSString.GetString(body); await bridge.HandleScriptCall(data); } }
protected override void BeforeReturn(NSDialog dialog, DialogResult result) { if (result == DialogResult.Ok) { var selection = ObjC.Call(dialog.Handle, "URL"); SelectedPath = NSString.GetString(ObjC.Call(selection, "path")); } else { SelectedPath = null; } }
private static async void ScriptCallback(CocoaWebview instance, IntPtr message) { if (instance.EnableScriptInterface) { IntPtr body = ObjC.Call(message, "body"); IntPtr isString = ObjC.Call(body, "isKindOfClass:", Foundation.GetClass("NSString")); if (isString != IntPtr.Zero) { string data = NSString.GetString(body) !; await instance.bridge.HandleScriptCall(data); } } }
protected override void BeforeReturn(NSDialog dialog) { var urls = ObjC.Call(dialog.Handle, "URLs"); int count = ObjC.Call(urls, "count").ToInt32(); string[] result = new string[count]; for (int i = 0; i < count; i++) { var url = ObjC.Call(urls, "objectAtIndex:", new IntPtr(i)); result[i] = NSString.GetString(ObjC.Call(url, "path")); } SelectedFiles = result; }
protected override void BeforeReturn(NSDialog dialog, DialogResult result) { base.BeforeReturn(dialog, result); var urls = ObjC.Call(dialog.Handle, "URLs"); int count = ObjC.Call(urls, "count").ToInt32(); string[] files = new string[count]; for (int i = 0; i < count; i++) { var url = ObjC.Call(urls, "objectAtIndex:", new IntPtr(i)); files[i] = NSString.GetString(ObjC.Call(url, "path")) !; } SelectedFiles = files; }
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)); }
private static void UriSchemeStartCallback(CocoaWebview instance, IntPtr schemeTask) { try { IntPtr request = ObjC.Call(schemeTask, "request"); IntPtr url = ObjC.Call(request, "URL"); var uri = new Uri(NSString.GetString(ObjC.Call(url, "absoluteString"))); var host = new Uri(uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped)); if (host == instance.customHost) { using (var contentStream = Application.ContentProvider.GetStreamAsync(uri).GetAwaiter().GetResult()) { if (contentStream != null) { if (contentStream is UnmanagedMemoryStream unmanagedMemoryStream) { unsafe { long length = unmanagedMemoryStream.Length - unmanagedMemoryStream.Position; var data = (IntPtr)unmanagedMemoryStream.PositionPointer; FinishUriSchemeCallback(url, schemeTask, data, length, uri); return; } } else { byte[] data; long length; if (contentStream is MemoryStream memoryStream) { data = memoryStream.GetBuffer(); length = memoryStream.Length; } else { using (var copyStream = new MemoryStream()) { contentStream.CopyTo(copyStream); data = copyStream.GetBuffer(); length = copyStream.Length; } } unsafe { fixed(byte *dataPtr = data) { FinishUriSchemeCallback(url, schemeTask, (IntPtr)dataPtr, length, uri); return; } } } } } } FinishUriSchemeCallbackWithError(schemeTask, 404); } catch { FinishUriSchemeCallbackWithError(schemeTask, 500); } }