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")); } }
/// <summary> /// Initializes the UWKWebView, registers default delagates, and creates textures for the page and icon /// to lack of constructor parameters /// </summary> void Awake() { // ensure core is up UWKCore.Init(); if (createMode) { URL = createURL; MaxWidth = createMaxWidth; MaxHeight = createMaxHeight; CurrentWidth = MaxWidth; CurrentHeight = MaxHeight; } if (MaxWidth < 64) MaxWidth = 64; if (MaxHeight < 64) MaxHeight = 64; if (CurrentWidth > MaxWidth) CurrentWidth = MaxWidth; if (CurrentHeight > MaxHeight) CurrentHeight = MaxHeight; maxWidth = MaxWidth; maxHeight = MaxHeight; // default delegate handlers LoadFinished += loadFinished; URLChanged += urlChanged; TitleChanged += titleChanged; JSConsole += jsConsole; JSMessageReceived += jsMessageReceived; LoadProgress += loadProgress; ContentSizeChanged += contentSizeChanged; NewViewRequested += newViewRequested; TextureFormat format = TextureFormat.ARGB32; if (SystemInfo.graphicsDeviceVersion.IndexOf("Direct3D 11") != -1) { format = TextureFormat.BGRA32; } // note that on Direct3D11 shared gpu textures, mipmapping is not allowed WebTexture = new Texture2D( MaxWidth, MaxHeight, format, false); Color32[] colors = new Color32[MaxWidth * MaxHeight]; for (int i = 0; i < MaxWidth * MaxHeight; i++) { colors[i]. r = colors[i].g = colors[i].b = colors[i]. a = 0; } WebTexture.SetPixels32(colors); WebTexture.Apply(); ID = UWKCore.CreateView(this, MaxWidth, MaxHeight, "", WebTexture.GetNativeTexturePtr()); }
/// <summary> /// Initializes the UWKWebView, registers default delagates, and creates textures for the page and icon /// to lack of constructor parameters /// </summary> void Awake() { // ensure core is up UWKCore.Init(); if (createMode) { URL = createURL; MaxWidth = createMaxWidth; MaxHeight = createMaxHeight; CurrentWidth = MaxWidth; CurrentHeight = MaxHeight; } if (MaxWidth < 64) { MaxWidth = 64; } if (MaxHeight < 64) { MaxHeight = 64; } if (CurrentWidth > MaxWidth) { CurrentWidth = MaxWidth; } if (CurrentHeight > MaxHeight) { CurrentHeight = MaxHeight; } maxWidth = MaxWidth; maxHeight = MaxHeight; // default delegate handlers LoadFinished += loadFinished; URLChanged += urlChanged; TitleChanged += titleChanged; JSConsole += jsConsole; JSMessageReceived += jsMessageReceived; LoadProgress += loadProgress; ContentSizeChanged += contentSizeChanged; NewViewRequested += newViewRequested; TextureFormat format = TextureFormat.ARGB32; if (SystemInfo.graphicsDeviceVersion.IndexOf("Direct3D 11") != -1) { format = TextureFormat.BGRA32; } // note that on Direct3D11 shared gpu textures, mipmapping is not allowed WebTexture = new Texture2D(MaxWidth, MaxHeight, format, false); Color32[] colors = new Color32[MaxWidth * MaxHeight]; for (int i = 0; i < MaxWidth * MaxHeight; i++) { colors[i].r = colors[i].g = colors[i].b = colors[i].a = 0; } WebTexture.SetPixels32(colors); WebTexture.Apply(); ID = UWKCore.CreateView(this, MaxWidth, MaxHeight, "", WebTexture.GetNativeTexturePtr()); }
void Awake() { // ensure core is up if (!UWKCore.Init()) { return; } if (createMode) { URL = createURL; MaxWidth = createMaxWidth; MaxHeight = createMaxHeight; InitialWidth = createInitialWidth; InitialHeight = createInitialHeight; } if (InitialWidth <= 0) { InitialWidth = MaxWidth; } if (InitialHeight <= 0) { InitialHeight = MaxHeight; } InitialWidth = Mathf.Clamp(InitialWidth, 64, 4096); InitialHeight = Mathf.Clamp(InitialHeight, 64, 4096); MaxWidth = Mathf.Clamp(MaxWidth, 64, 4096); MaxHeight = Mathf.Clamp(MaxHeight, 64, 4096); if (InitialWidth > MaxWidth) { MaxWidth = InitialWidth; } if (InitialHeight > MaxHeight) { MaxHeight = InitialHeight; } TextureFormat format = TextureFormat.ARGB32; if (SystemInfo.graphicsDeviceVersion.IndexOf("Direct3D 11") != -1) { format = TextureFormat.BGRA32; } // note that on Direct3D11 shared gpu textures, mipmapping is not allowed WebTexture = new Texture2D(MaxWidth, MaxHeight, format, false); Color32[] colors = new Color32[MaxWidth * MaxHeight]; for (int i = 0; i < MaxWidth * MaxHeight; i++) { colors[i].r = colors[i].g = colors[i].b = colors[i].a = 0; } WebTexture.SetPixels32(colors); WebTexture.Apply(); width = InitialWidth; height = InitialHeight; ID = UWKCore.CreateView(this, InitialWidth, InitialHeight, MaxWidth, MaxHeight, URL, WebTexture.GetNativeTexturePtr()); // default delegate handlers LoadFinished += loadFinished; URLChanged += urlChanged; TitleChanged += titleChanged; LoadStateChanged += loadStateChanged; PopupRequested += popupRequested; WebQuery += webQuery; }