From f4a1cc0272725a77ed91fefecce448a1e36abf9e Mon Sep 17 00:00:00 2001 From: Voltstro Date: Wed, 20 Dec 2023 21:24:53 +1000 Subject: [PATCH] Add open dev tools --- .../Runtime/Core/WebBrowserClient.cs | 10 ++++++ .../Core/WebBrowserCommunicationsManager.cs | 5 +++ .../Browser/UwbCefClient.cs | 36 ++++++++++++++++++- .../Core/CefEngineControlsManager.cs | 5 +++ .../Assets/Scripts/UWBPrjDebugUI.cs | 3 ++ .../Core/IEngineControls.cs | 5 +++ 6 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs b/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs index 38342a55..0a9330ad 100644 --- a/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs +++ b/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs @@ -788,6 +788,16 @@ public void ExecuteJs(string js) communicationsManager.ExecuteJs(js); } + /// + /// Shows dev tools + /// + public void OpenDevTools() + { + CheckIfIsReadyAndConnected(); + + communicationsManager.OpenDevTools(); + } + /// /// Resizes the screen. /// diff --git a/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserCommunicationsManager.cs b/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserCommunicationsManager.cs index b6522612..1c334f8d 100644 --- a/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserCommunicationsManager.cs +++ b/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserCommunicationsManager.cs @@ -167,6 +167,11 @@ public void ExecuteJs(string js) ExecuteTask(() => engineProxy.ExecuteJs(js)); } + public void OpenDevTools() + { + ExecuteTask(() => engineProxy.OpenDevTools()); + } + public void Resize(Resolution resolution) { ExecuteTask(() => engineProxy.Resize(resolution)); diff --git a/src/UnityWebBrowser.Engine.Cef/Browser/UwbCefClient.cs b/src/UnityWebBrowser.Engine.Cef/Browser/UwbCefClient.cs index d241661c..612a0a52 100644 --- a/src/UnityWebBrowser.Engine.Cef/Browser/UwbCefClient.cs +++ b/src/UnityWebBrowser.Engine.Cef/Browser/UwbCefClient.cs @@ -5,7 +5,9 @@ using System; using System.Numerics; +using UnityWebBrowser.Engine.Cef.Browser.Popups; using VoltstroStudios.UnityWebBrowser.Engine.Shared.Core; +using VoltstroStudios.UnityWebBrowser.Engine.Shared.Core.Logging; using VoltstroStudios.UnityWebBrowser.Engine.Shared.Popups; using VoltstroStudios.UnityWebBrowser.Shared; using VoltstroStudios.UnityWebBrowser.Shared.Events; @@ -29,9 +31,16 @@ public class UwbCefClient : CefClient, IDisposable private readonly UwbCefRenderHandler renderHandler; private readonly UwbCefRequestHandler requestHandler; + private readonly ProxySettings proxySettings; + private CefBrowser browser; private CefBrowserHost browserHost; + //Dev Tools + private CefWindowInfo devToolsWindowInfo; + private UwbCefPopupClient devToolsClient; + private CefBrowserSettings devToolsBrowserSettings; + /// /// Creates a new instance /// @@ -39,6 +48,8 @@ public UwbCefClient(CefSize size, PopupAction popupAction, EnginePopupManager po { ClientControls = clientControlsActions; + this.proxySettings = proxySettings; + //Setup our handlers loadHandler = new UwbCefLoadHandler(this); renderHandler = new UwbCefRenderHandler(this, size); @@ -206,7 +217,6 @@ private void MouseScrollEvent(CefMouseEvent mouseEvent, int scroll) public void LoadUrl(string url) { browser.GetMainFrame()?.LoadUrl(url); - //mainFrame.LoadUrl(url); } public Vector2 GetMouseScrollPosition() @@ -224,6 +234,30 @@ public void ExecuteJs(string js) browser.GetMainFrame()?.ExecuteJavaScript(js, "", 0); } + public void OpenDevTools() + { + try + { + if (devToolsWindowInfo == null) + { + devToolsWindowInfo = CefWindowInfo.Create(); + devToolsClient = new UwbCefPopupClient(proxySettings, () => + { + devToolsWindowInfo = null; + devToolsClient = null; + devToolsBrowserSettings = null; + }); + devToolsBrowserSettings = new CefBrowserSettings(); + } + + browserHost.ShowDevTools(devToolsWindowInfo, devToolsClient, devToolsBrowserSettings, new CefPoint()); + } + catch (Exception ex) + { + Logger.Error(ex, "An error occured while trying to open the dev tools!"); + } + } + public void GoBack() { if (browser.CanGoBack) diff --git a/src/UnityWebBrowser.Engine.Cef/Core/CefEngineControlsManager.cs b/src/UnityWebBrowser.Engine.Cef/Core/CefEngineControlsManager.cs index 3b69f2b1..10db6079 100644 --- a/src/UnityWebBrowser.Engine.Cef/Core/CefEngineControlsManager.cs +++ b/src/UnityWebBrowser.Engine.Cef/Core/CefEngineControlsManager.cs @@ -247,6 +247,11 @@ public void ExecuteJs(string js) cefClient.ExecuteJs(js); } + public void OpenDevTools() + { + cefClient.OpenDevTools(); + } + public void Resize(Resolution resolution) { cefClient.Resize(resolution); diff --git a/src/UnityWebBrowser.UnityProject/Assets/Scripts/UWBPrjDebugUI.cs b/src/UnityWebBrowser.UnityProject/Assets/Scripts/UWBPrjDebugUI.cs index 33fa3bd5..8a6a1774 100644 --- a/src/UnityWebBrowser.UnityProject/Assets/Scripts/UWBPrjDebugUI.cs +++ b/src/UnityWebBrowser.UnityProject/Assets/Scripts/UWBPrjDebugUI.cs @@ -136,6 +136,9 @@ private void OnImGuiLayout(UImGui.UImGui uImGui) ImGui.Spacing(); ImGui.Separator(); + if(ImGui.Button("Open DevTools")) + webBrowserUIBasic.browserClient.OpenDevTools(); + //URL if (ImGui.InputText("URL", ref inputUrl, 1000)) inputUrl = inputUrl; diff --git a/src/VoltstroStudios.UnityWebBrowser.Shared/Core/IEngineControls.cs b/src/VoltstroStudios.UnityWebBrowser.Shared/Core/IEngineControls.cs index 99226f4b..ad604a65 100644 --- a/src/VoltstroStudios.UnityWebBrowser.Shared/Core/IEngineControls.cs +++ b/src/VoltstroStudios.UnityWebBrowser.Shared/Core/IEngineControls.cs @@ -92,6 +92,11 @@ internal interface IEngineControls /// public void ExecuteJs(string js); + /// + /// Open chrome dev tools + /// + public void OpenDevTools(); + /// /// Tells the UWB engine to resize ///