Updated zoom level

This commit is contained in:
Florian Rappl
2026-09-04 16:03:43 +02:00
parent 703cf8f880
commit 98c72af3d6
8 changed files with 109 additions and 6 deletions

View File

@@ -0,0 +1,18 @@
namespace ElectronNET.API.Entities
{
/// <summary>
/// The direction of a zoom change triggered by the user.
/// </summary>
public enum ZoomDirection
{
/// <summary>
/// The user zoomed in.
/// </summary>
In,
/// <summary>
/// The user zoomed out.
/// </summary>
Out
}
}

View File

@@ -113,6 +113,15 @@ public class WebContents : ApiBase
remove => RemoveEvent(value, Id);
}
/// <summary>
/// Emitted when the user is requesting to change the zoom level using the mouse wheel or the keyboard.
/// </summary>
public event Action<ZoomDirection> OnZoomChanged
{
add => AddEvent(value, Id);
remove => RemoveEvent(value, Id);
}
internal WebContents(int id)
{
Id = id;
@@ -337,14 +346,14 @@ public class WebContents : ApiBase
/// Returns number - The current zoom level.
/// </summary>
/// <returns></returns>
public Task<int> GetZoomLevelAsync() => InvokeAsync<int>();
public Task<double> GetZoomLevelAsync() => InvokeAsync<double>();
/// <summary>
/// Changes the zoom level to the specified level.
/// The original size is 0 and each increment above or below represents zooming 20% larger or smaller to default limits of 300% and 50% of original size, respectively.
/// </summary>
/// <param name="level"></param>
public void SetZoomLevel(int level)
public void SetZoomLevel(double level)
{
BridgeConnector.Socket.Emit("webContents-setZoomLevel", Id, level);
}
@@ -354,7 +363,7 @@ public class WebContents : ApiBase
/// </summary>
/// <param name="minimumLevel"></param>
/// <param name="maximumLevel"></param>
public Task SetVisualZoomLevelLimitsAsync(int minimumLevel, int maximumLevel)
public Task SetVisualZoomLevelLimitsAsync(double minimumLevel, double maximumLevel)
{
var tcs = new TaskCompletionSource();

View File

@@ -111,6 +111,13 @@ module.exports = (socket) => {
electronSocket.emit("webContents-domReady" + id);
});
});
socket.on("register-webContents-zoomChanged", (id) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.removeAllListeners("zoom-changed");
browserWindow.webContents.on("zoom-changed", (_, zoomDirection) => {
electronSocket.emit("webContents-zoomChanged" + id, zoomDirection);
});
});
socket.on("webContents-openDevTools", (id, options) => {
if (options) {
getWindowById(id).webContents.openDevTools(options);

File diff suppressed because one or more lines are too long

View File

@@ -103,6 +103,15 @@ export = (socket: Socket) => {
});
});
socket.on("register-webContents-zoomChanged", (id) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.removeAllListeners("zoom-changed");
browserWindow.webContents.on("zoom-changed", (_, zoomDirection) => {
electronSocket.emit("webContents-zoomChanged" + id, zoomDirection);
});
});
socket.on("webContents-openDevTools", (id, options) => {
if (options) {
getWindowById(id).webContents.openDevTools(options);

View File

@@ -108,13 +108,19 @@ namespace ElectronNET.IntegrationTests.Tests
await Task.Delay(500.ms());
var ok = await window.WebContents.GetZoomLevelAsync();
ok.Should().Be(0);
ok.Should().Be(0.0);
window.WebContents.SetZoomLevel(2);
await Task.Delay(500.ms());
ok = await window.WebContents.GetZoomLevelAsync();
ok.Should().Be(2);
ok.Should().Be(2.0);
window.WebContents.SetZoomLevel(0.5);
await Task.Delay(500.ms());
ok = await window.WebContents.GetZoomLevelAsync();
ok.Should().BeApproximately(0.5, 0.001);
}
finally
{