Skip to content

Commit

Permalink
Add open dev tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Voltstro committed Dec 20, 2023
1 parent 54fa348 commit f4a1cc0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,16 @@ public void ExecuteJs(string js)
communicationsManager.ExecuteJs(js);
}

/// <summary>
/// Shows dev tools
/// </summary>
public void OpenDevTools()
{
CheckIfIsReadyAndConnected();

communicationsManager.OpenDevTools();
}

/// <summary>
/// Resizes the screen.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
36 changes: 35 additions & 1 deletion src/UnityWebBrowser.Engine.Cef/Browser/UwbCefClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,16 +31,25 @@ 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;

/// <summary>
/// Creates a new <see cref="UwbCefClient" /> instance
/// </summary>
public UwbCefClient(CefSize size, PopupAction popupAction, EnginePopupManager popupManager, ProxySettings proxySettings, ClientControlsActions clientControlsActions)
{
ClientControls = clientControlsActions;

this.proxySettings = proxySettings;

//Setup our handlers
loadHandler = new UwbCefLoadHandler(this);
renderHandler = new UwbCefRenderHandler(this, size);
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ internal interface IEngineControls
/// <param name="js"></param>
public void ExecuteJs(string js);

/// <summary>
/// Open chrome dev tools
/// </summary>
public void OpenDevTools();

/// <summary>
/// Tells the UWB engine to resize
/// </summary>
Expand Down

0 comments on commit f4a1cc0

Please sign in to comment.