Skip to content

Commit

Permalink
Add set/get zoom level
Browse files Browse the repository at this point in the history
  • Loading branch information
Voltstro committed Jan 24, 2024
1 parent f4a1cc0 commit 04a71ba
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 3 deletions.
40 changes: 40 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,46 @@ public void ExecuteJs(string js)
communicationsManager.ExecuteJs(js);
}

/// <summary>
/// Sets zoom level based off a percentage
/// </summary>
/// <param name="percent"></param>
/// <exception cref="ArgumentOutOfRangeException">Thrown if percent is 0 or less</exception>
public void SetZoomLevelPercent(double percent)
{
if (percent <= 0)
throw new ArgumentOutOfRangeException(nameof(percent),
"Percent must be larger then 0. To reset, use SetZoomLevel(0).");

//Logic from:
//https://magpcss.org/ceforum/viewtopic.php?t=11491
double scale = 1.2 * percent;
double zoomLevel = Math.Log(scale);
SetZoomLevel(zoomLevel);
}

/// <summary>
/// Set browser's zoom level. Use 0.0 to reset.
/// </summary>
/// <param name="zoomLevel"></param>
public void SetZoomLevel(double zoomLevel)
{
CheckIfIsReadyAndConnected();

communicationsManager.SetZoomLevel(zoomLevel);
}

/// <summary>
/// Get's browser's zoom level
/// </summary>
/// <returns></returns>
public double GetZoomLevel()
{
CheckIfIsReadyAndConnected();

return communicationsManager.GetZoomLevel();
}

/// <summary>
/// Shows dev tools
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ public void ExecuteJs(string js)
ExecuteTask(() => engineProxy.ExecuteJs(js));
}

public void SetZoomLevel(double zoomLevel)
{
ExecuteTask(() => engineProxy.SetZoomLevel(zoomLevel));
}

public double GetZoomLevel()
{
using (sendEventMarker.Auto())
{
lock (threadLock)
{
return engineProxy.GetZoomLevel();
}
}
}

public void OpenDevTools()
{
ExecuteTask(() => engineProxy.OpenDevTools());
Expand Down Expand Up @@ -263,7 +279,7 @@ public void InputFocusChange(bool focused)
{
ExecuteOnUnity(() => client.InvokeOnInputFocus(focused));
}

public void Ready()
{
client.EngineReady().Forget();
Expand Down
10 changes: 10 additions & 0 deletions src/UnityWebBrowser.Engine.Cef/Browser/UwbCefClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ public void ExecuteJs(string js)
browser.GetMainFrame()?.ExecuteJavaScript(js, "", 0);
}

public void SetZoomLevel(double zoomLevel)
{
browserHost.SetZoomLevel(zoomLevel);
}

public double GetZoomLevel()
{
return browserHost.GetZoomLevel();
}

public void OpenDevTools()
{
try
Expand Down
10 changes: 10 additions & 0 deletions src/UnityWebBrowser.Engine.Cef/Core/CefEngineControlsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ public void ExecuteJs(string js)
cefClient.ExecuteJs(js);
}

public void SetZoomLevel(double zoomLevel)
{
cefClient.SetZoomLevel(zoomLevel);
}

public double GetZoomLevel()
{
return cefClient.GetZoomLevel();
}

public void OpenDevTools()
{
cefClient.OpenDevTools();
Expand Down
28 changes: 26 additions & 2 deletions src/UnityWebBrowser.UnityProject/Assets/Scripts/UWBPrjDebugUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class UWBPrjDebugUI : MonoBehaviour
private List<string> unformattedConsoleItems;

private string inputUrl;
private double zoomLevel = double.MinValue;

private void Start()
{
Expand Down Expand Up @@ -131,15 +132,38 @@ private void OnImGuiLayout(UImGui.UImGui uImGui)
//Mouse position
webBrowserUIBasic.GetMousePosition(out Vector2 mousePos);
ImGui.Text($"Mouse Position: {mousePos}");
if (ImGui.Button("Get Scroll Position"))
webBrowserUIBasic.browserClient.logger?.Debug(webBrowserUIBasic.browserClient.GetScrollPosition());
ImGui.Spacing();
ImGui.Separator();

if (ImGui.Button("Get Scroll Position"))
webBrowserUIBasic.browserClient.logger?.Debug(webBrowserUIBasic.browserClient.GetScrollPosition());

ImGui.SameLine();

if(ImGui.Button("Open DevTools"))
webBrowserUIBasic.browserClient.OpenDevTools();

ImGui.SameLine();

ImGui.Text("Zoom Percent");
ImGui.SameLine();

//Get zoom level when ready
if (zoomLevel <= 0 && webBrowserUIBasic.browserClient.IsConnected)
zoomLevel = webBrowserUIBasic.browserClient.GetZoomLevel();

if (ImGui.InputDouble("Zoom", ref zoomLevel))
{
zoomLevel = Math.Clamp(zoomLevel, 0.1, double.MaxValue);
webBrowserUIBasic.browserClient.SetZoomLevelPercent(zoomLevel);
}

ImGui.Spacing();
ImGui.Separator();

//URL
ImGui.Text("URL");
ImGui.SameLine();
if (ImGui.InputText("URL", ref inputUrl, 1000))
inputUrl = inputUrl;
ImGui.SameLine();
Expand Down
12 changes: 12 additions & 0 deletions src/VoltstroStudios.UnityWebBrowser.Shared/Core/IEngineControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ internal interface IEngineControls
/// </summary>
/// <param name="js"></param>
public void ExecuteJs(string js);

/// <summary>
/// Sets the engine's zoom level
/// </summary>
/// <param name="zoomLevel"></param>
public void SetZoomLevel(double zoomLevel);

/// <summary>
/// Returns the engine's current Zoom level
/// </summary>
/// <returns></returns>
public double GetZoomLevel();

/// <summary>
/// Open chrome dev tools
Expand Down

0 comments on commit 04a71ba

Please sign in to comment.