Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/ElectronNET.API/API/BrowserWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,4 +1221,24 @@ public void SetBrowserView(BrowserView browserView)
// This message name does not match the default ApiBase naming convention.
BridgeConnector.Socket.Emit("browserWindow-setBrowserView", Id, browserView.Id);
}

/// <summary>
/// Adds a WebContentsView to the content view of this window.
/// If the same view is added again, it is moved to the top (or to the given index).
/// </summary>
/// <param name="view">The view to add.</param>
/// <param name="index">Index at which to insert the child view. Defaults to adding the child at the end of the child list.</param>
public void AddChildView(WebContentsView view, int? index = null)
{
BridgeConnector.Socket.Emit("browserWindow-addChildView", Id, view.Id, index);
}

/// <summary>
/// Removes a WebContentsView from the content view of this window.
/// </summary>
/// <param name="view">The view to remove.</param>
public void RemoveChildView(WebContentsView view)
{
BridgeConnector.Socket.Emit("browserWindow-removeChildView", Id, view.Id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace ElectronNET.API.Entities
{
/// <summary>
/// Options for creating a <see cref="WebContentsView"/>.
/// </summary>
/// <remarks>Up-to-date with Electron API 41</remarks>
public class WebContentsViewConstructorOptions
{
/// <summary>
/// Gets or sets the web preferences for the view (see WebPreferences).
/// </summary>
public WebPreferences WebPreferences { get; set; }

/// <summary>
/// Gets or sets a proxy to use on creation in the format host:port.
/// The proxy can be alternatively set using the WebContentsView.WebContents.SetProxyAsync function.
/// </summary>
/// <remarks>This is custom shortcut. Not part of the Electron API.</remarks>
public string Proxy { get; set; }

/// <summary>
/// Gets or sets the credentials of the proxy in the format username:password.
/// These will only be used if the Proxy field is also set.
/// </summary>
/// <remarks>This is custom shortcut. Not part of the Electron API.</remarks>
public string ProxyCredentials { get; set; }
}
}
14 changes: 14 additions & 0 deletions src/ElectronNET.API/API/IpcMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ public void Send(BrowserView browserView, string channel, params object[] data)
BridgeConnector.Socket.Emit("sendToIpcRendererBrowserView", browserView.Id, channel, data);
}

/// <summary>
/// Send a message to the WebContentsView renderer process asynchronously via channel, you can also send
/// arbitrary arguments. Arguments will be serialized in JSON internally and hence
/// no functions or prototype chain will be included. The renderer process handles it by
/// listening for channel with ipcRenderer module.
/// </summary>
/// <param name="webContentsView">WebContentsView with channel.</param>
/// <param name="channel">Channelname.</param>
/// <param name="data">Arguments data.</param>
public void Send(WebContentsView webContentsView, string channel, params object[] data)
{
BridgeConnector.Socket.Emit("sendToIpcRendererWebContentsView", webContentsView.Id, channel, data);
}

/// <summary>
/// Adds a handler for an invokeable IPC. This handler will be called
/// whenever a renderer calls ipcRenderer.invoke(channel, ...args).
Expand Down
81 changes: 81 additions & 0 deletions src/ElectronNET.API/API/WebContentsView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using ElectronNET.API.Entities;
using System.Threading.Tasks;

namespace ElectronNET.API
{
/// <summary>
/// A WebContentsView can be used to embed additional web content into a BrowserWindow.
/// It is the successor of <see cref="BrowserView"/>. Add it to a window via
/// <see cref="BrowserWindow.AddChildView(WebContentsView, int?)"/>.
/// </summary>
/// <remarks>Up-to-date with Electron API 41</remarks>
public class WebContentsView : ApiBase
{
/// <summary>
/// Offset applied to the id of the associated <see cref="API.WebContents"/> so the
/// host can distinguish it from BrowserWindow (id) and BrowserView (id + 1000) contents.
/// </summary>
internal const int WebContentsIdOffset = 1000000;

protected override SocketTaskEventNameTypes SocketTaskEventNameType => SocketTaskEventNameTypes.DashesLowerFirst;
protected override SocketTaskMessageNameTypes SocketTaskMessageNameType => SocketTaskMessageNameTypes.DashesLowerFirst;

/// <summary>
/// Gets the identifier.
/// </summary>
public override int Id { get; protected set; }

/// <summary>
/// Render and control web pages.
/// </summary>
public WebContents WebContents { get; internal set; }

internal WebContentsView(int id)
{
Id = id;
WebContents = new WebContents(id + WebContentsIdOffset);
}

/// <summary>
/// The bounds of this view, relative to its parent.
/// </summary>
public Task<Rectangle> GetBoundsAsync() => this.InvokeAsync<Rectangle>();

/// <summary>
/// Resizes and moves the view to the supplied bounds relative to its parent.
/// </summary>
/// <param name="bounds">The new bounds.</param>
public void SetBounds(Rectangle bounds)
{
BridgeConnector.Socket.Emit("webContentsView-setBounds", Id, bounds);
}

/// <summary>
/// Sets the background color of the view.
/// </summary>
/// <param name="color">Color in Hex, RGB, ARGB, HSL, HSLA or named CSS color format.</param>
public void SetBackgroundColor(string color)
{
BridgeConnector.Socket.Emit("webContentsView-setBackgroundColor", Id, color);
}

/// <summary>
/// Shows or hides the view.
/// </summary>
/// <param name="visible">If false, the view will be hidden from display.</param>
public void SetVisible(bool visible)
{
BridgeConnector.Socket.Emit("webContentsView-setVisible", Id, visible);
}

/// <summary>
/// Sets the border radius of the view in pixels.
/// Ignored on Electron versions that do not support it.
/// </summary>
/// <param name="radius">Border radius size in pixels.</param>
public void SetBorderRadius(int radius)
{
BridgeConnector.Socket.Emit("webContentsView-setBorderRadius", Id, radius);
}
}
}
44 changes: 44 additions & 0 deletions src/ElectronNET.API/API/WindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ public bool IsQuitOnWindowAllClosed

private readonly List<BrowserView> _browserViews = new();

/// <summary>
/// Gets the web contents views.
/// </summary>
/// <value>
/// The web contents views.
/// </value>
public IReadOnlyCollection<WebContentsView> WebContentsViews => _webContentsViews.AsReadOnly();

private readonly List<WebContentsView> _webContentsViews = new();

/// <summary>
/// Creates the window asynchronous.
/// </summary>
Expand Down Expand Up @@ -222,5 +232,39 @@ public async Task<BrowserView> CreateBrowserViewAsync(BrowserViewConstructorOpti

return await tcs.Task.ConfigureAwait(false);
}

/// <summary>
/// A WebContentsView can be used to embed additional web content into a BrowserWindow.
/// It is the successor of BrowserView.
/// </summary>
/// <returns></returns>
public Task<WebContentsView> CreateWebContentsViewAsync()
{
return CreateWebContentsViewAsync(new WebContentsViewConstructorOptions());
}

/// <summary>
/// A WebContentsView can be used to embed additional web content into a BrowserWindow.
/// It is the successor of BrowserView.
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
public async Task<WebContentsView> CreateWebContentsViewAsync(WebContentsViewConstructorOptions options)
{
var tcs = new TaskCompletionSource<WebContentsView>();

BridgeConnector.Socket.Once<int>("WebContentsViewCreated", (id) =>
{
WebContentsView view = new(id);

_webContentsViews.Add(view);

tcs.SetResult(view);
});

await BridgeConnector.Socket.Emit("createWebContentsView", options).ConfigureAwait(false);

return await tcs.Task.ConfigureAwait(false);
}
}
}
13 changes: 13 additions & 0 deletions src/ElectronNET.Host/api/browserWindows.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/ElectronNET.Host/api/browserWindows.js.map

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions src/ElectronNET.Host/api/browserWindows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Socket } from "net";
import { BrowserWindow, Menu } from "electron";

import { browserViewMediateService } from "./browserView";
import { webContentsViewMediateService } from "./webContentsView";

const windows: Electron.BrowserWindow[] = (global["browserWindows"] =
global["browserWindows"] || []) as Electron.BrowserWindow[];
Expand Down Expand Up @@ -911,6 +912,20 @@ export = (socket: Socket, app: Electron.App) => {
getWindowById(id).setBrowserView(browserViewMediateService(browserViewId));
});

socket.on("browserWindow-addChildView", (id, viewId, index) => {
const view = webContentsViewMediateService(viewId);
if (view) {
getWindowById(id).contentView.addChildView(view, index ?? undefined);
}
});

socket.on("browserWindow-removeChildView", (id, viewId) => {
const view = webContentsViewMediateService(viewId);
if (view) {
getWindowById(id).contentView.removeChildView(view);
}
});

function getWindowById(id: number): Electron.BrowserWindow {
const runtimeWindow = BrowserWindow.fromId(id);
if (runtimeWindow) {
Expand Down
7 changes: 7 additions & 0 deletions src/ElectronNET.Host/api/ipc.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading