mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-09-21 06:25:20 +00:00
Updated zoom level
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
## ElectronNET.Core
|
||||
|
||||
- Updated dependencies
|
||||
- Updated `WebContents` zoom level APIs to use `double` instead of `int` (#956)
|
||||
- Improved migration checks to honor custom output paths and to detect `ProjectGuid` in publish profiles (#946)
|
||||
- Fixed single instance handling on macOS (#1040)
|
||||
- Fixed slow socket bridge startup by binding to an explicit loopback address (#1103)
|
||||
@@ -11,6 +12,7 @@
|
||||
- Fixed cross-compilation behavior on same platform (#1098) @epsnm
|
||||
- Fixed resolution of unrelated target (#1099) @epsnm
|
||||
- Fixed false alarm for `ELECTRON001` on a root `package-lock.json` (#946)
|
||||
- Added `WebContents.OnZoomChanged` event (#956)
|
||||
- Added target framework customization (#1095) @epsnm
|
||||
- Added configurable Electron root directory for custom packaging layouts (#1106) @DYH1319
|
||||
- Added ability for `custom_main.js` to modify command line switches (#1029) @AeonSake
|
||||
|
||||
@@ -113,6 +113,31 @@ Prints window's web page as PDF with Chromium's preview printing custom settings
|
||||
|
||||
Whether the PDF generation succeeded.
|
||||
|
||||
#### 🧊 `Task<double> GetZoomFactorAsync()`
|
||||
Returns the current zoom factor. A factor of `1.0` means 100%.
|
||||
|
||||
#### 🧊 `void SetZoomFactor(double factor)`
|
||||
Changes the zoom factor to the specified factor. The zoom factor is the zoom percent divided by 100, so 300% = `3.0`. The factor must be greater than `0.0`.
|
||||
|
||||
**Parameters:**
|
||||
- `factor` - The zoom factor
|
||||
|
||||
#### 🧊 `Task<double> GetZoomLevelAsync()`
|
||||
Returns the current zoom level.
|
||||
|
||||
#### 🧊 `void SetZoomLevel(double level)`
|
||||
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 the original size, respectively.
|
||||
|
||||
**Parameters:**
|
||||
- `level` - The zoom level
|
||||
|
||||
#### 🧊 `Task SetVisualZoomLevelLimitsAsync(double minimumLevel, double maximumLevel)`
|
||||
Sets the maximum and minimum pinch-to-zoom level.
|
||||
|
||||
**Parameters:**
|
||||
- `minimumLevel` - The minimum pinch-to-zoom level
|
||||
- `maximumLevel` - The maximum pinch-to-zoom level
|
||||
|
||||
## Events
|
||||
|
||||
#### ⚡ `InputEvent`
|
||||
@@ -142,6 +167,9 @@ Emitted when the document in the top-level frame is loaded.
|
||||
#### ⚡ `OnWillRedirect`
|
||||
Emitted when a server side redirect occurs during navigation.
|
||||
|
||||
#### ⚡ `OnZoomChanged`
|
||||
Emitted when the user changes the zoom level using the mouse wheel or the keyboard. The handler receives the `ZoomDirection` (`In` or `Out`).
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### Page Loading
|
||||
@@ -279,6 +307,30 @@ webContents.OnCrashed += (killed) =>
|
||||
};
|
||||
```
|
||||
|
||||
### Zoom Control
|
||||
|
||||
```csharp
|
||||
// Set the zoom to 150%
|
||||
webContents.SetZoomFactor(1.5);
|
||||
|
||||
var factor = await webContents.GetZoomFactorAsync();
|
||||
Console.WriteLine($"Current zoom: {factor * 100}%");
|
||||
|
||||
// Zoom levels are relative: each step is 20% larger/smaller, 0 is the original size
|
||||
webContents.SetZoomLevel(2);
|
||||
|
||||
// Restrict pinch-to-zoom
|
||||
await webContents.SetVisualZoomLevelLimitsAsync(1, 3);
|
||||
|
||||
// React to zoom changes triggered by the user
|
||||
webContents.OnZoomChanged += (direction) =>
|
||||
{
|
||||
Console.WriteLine($"User zoomed {direction}");
|
||||
};
|
||||
```
|
||||
|
||||
> **Note:** The zoom factor is shared by all windows using the same session partition. Assign a unique `WebPreferences.Partition` per window if each window should keep its own zoom.
|
||||
|
||||
## Related APIs
|
||||
|
||||
- [Electron.WindowManager](WindowManager.md) - Windows containing web contents
|
||||
|
||||
18
src/ElectronNET.API/API/Entities/ZoomDirection.cs
Normal file
18
src/ElectronNET.API/API/Entities/ZoomDirection.cs
Normal 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
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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
@@ -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);
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user