public CocoaWindow(WindowConfiguration config, WebviewBridge bridge) { if (config == null) { throw new ArgumentNullException(nameof(config)); } if (bridge == null) { throw new ArgumentNullException(nameof(bridge)); } Handle = AppKit.Call("NSWindow", "alloc"); canResizeField = config.CanResize; var style = GetWantedStyleMask(); ObjC.SendMessage( Handle, ObjC.RegisterName("initWithContentRect:styleMask:backing:defer:"), new CGRect(0, 0, config.Size.Width, config.Size.Height), new UIntPtr((uint)style), new UIntPtr(2), false); webview = new CocoaWebview(bridge); ObjC.Call(Handle, "setContentView:", webview.Handle); webview.TitleChanged += Webview_TitleChanged; windowDelegate = WindowDelegateDefinition.CreateInstance(this); ObjC.Call(Handle, "setDelegate:", windowDelegate.Handle); }
private static void UriSchemeStartCallback(CocoaWebview instance, IntPtr schemeTask) { try { IntPtr request = ObjC.Call(schemeTask, "request"); IntPtr url = ObjC.Call(request, "URL"); var uri = URL.GetAsUri(url) !; 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); } }
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 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); } } }