This commit is contained in:
Florian Rappl
2024-02-15 01:04:11 +01:00
11 changed files with 422 additions and 21 deletions

View File

@@ -53,7 +53,7 @@ using ElectronNET.API.Entities;
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseElectron(args);
// Is optional, but you can use the Electron.NET API-Classes directly with DI (relevant if you wont more encoupled code)
// Is optional, but you can use the Electron.NET API-Classes directly with DI (relevant if you want more encoupled code)
builder.Services.AddElectron();
var app = builder.Build();

View File

@@ -5,19 +5,54 @@
/// </summary>
public class Display
{
/// <summary>
/// Can be available, unavailable, unknown.
/// </summary>
public string AccelerometerSupport { get; set; }
/// <summary>
/// Gets or sets the bounds.
/// </summary>
/// <value>
/// The bounds.
/// The bounds of the display in DIP points.
/// </value>
public Rectangle Bounds { get; set; }
/// <summary>
/// The number of bits per pixel.
/// </summary>
public int ColorDepth { get; set; }
/// <summary>
/// Represent a color space (three-dimensional object which contains all realizable color combinations) for the purpose of color conversions.
/// </summary>
public string ColorSpace { get; set; }
/// <summary>
/// The number of bits per color component.
/// </summary>
public int DepthPerComponent { get; set; }
/// <summary>
/// The display refresh rate.
/// </summary>
public int DisplayFrequency { get; set; }
/// <summary>
/// Unique identifier associated with the display.
/// </summary>
public string Id { get; set; }
/// <summary>
/// true for an internal display and false for an external display.
/// </summary>
public bool Internal { get; set; }
/// <summary>
/// User-friendly label, determined by the platform.
/// </summary>
public string Label { get; set; }
/// <summary>
/// Can be 0, 90, 180, 270, represents screen rotation in clock-wise degrees.
/// </summary>
@@ -28,6 +63,16 @@
/// </summary>
public int ScaleFactor { get; set; }
/// <summary>
/// Can be available, unavailable, unknown.
/// </summary>
public string TouchSupport { get; set; }
/// <summary>
/// Whether or not the display is a monochrome display.
/// </summary>
public bool Monochrome { get; set; }
/// <summary>
/// Gets or sets the size.
/// </summary>
@@ -36,11 +81,6 @@
/// </value>
public Size Size { get; set; }
/// <summary>
/// Can be available, unavailable, unknown.
/// </summary>
public string TouchSupport { get; set; }
/// <summary>
/// Gets or sets the work area.
/// </summary>

View File

@@ -0,0 +1,18 @@
namespace ElectronNET.API.Entities;
/// <summary>
/// 'OnDidFailLoad' event details.
/// </summary>
public class OnDidFailLoadInfo
{
/// <summary>
/// The full list of error codes and their meaning is available here
/// https://source.chromium.org/chromium/chromium/src/+/main:net/base/net_error_list.h
/// </summary>
public int ErrorCode { get; set; }
/// <summary>
/// Validated URL.
/// </summary>
public string ValidatedUrl { get; set; }
}

View File

@@ -0,0 +1,17 @@
namespace ElectronNET.API.Entities;
/// <summary>
/// 'OnDidNavigate' event details.
/// </summary>
public class OnDidNavigateInfo
{
/// <summary>
/// Navigated URL.
/// </summary>
public string Url { get; set; }
/// <summary>
/// HTTP response code.
/// </summary>
public int HttpResponseCode { get; set; }
}

View File

@@ -117,7 +117,7 @@ namespace ElectronNET.API
public void Once(string channel, Action<object> listener)
{
BridgeConnector.Socket.Emit("registerOnceIpcMainChannel", channel);
BridgeConnector.Socket.On(channel, (args) =>
BridgeConnector.Socket.Once<object>(channel, (args) =>
{
List<object> objectArray = FormatArguments(args);

View File

@@ -84,6 +84,154 @@ public class WebContents
private event Action _didFinishLoad;
/// <summary>
/// Emitted when any frame (including main) starts navigating.
/// </summary>
public event Action<string> OnDidStartNavigation
{
add
{
if (_didStartNavigation == null)
{
BridgeConnector.Socket.On<string>("webContents-didStartNavigation" + Id, (url) =>
{
_didStartNavigation(url);
});
BridgeConnector.Socket.Emit("register-webContents-didStartNavigation", Id);
}
_didStartNavigation += value;
}
remove
{
_didStartNavigation -= value;
if (_didStartNavigation == null)
BridgeConnector.Socket.Off("webContents-didStartNavigation" + Id);
}
}
private event Action<string> _didStartNavigation;
/// <summary>
/// Emitted when a main frame navigation is done.
/// This event is not emitted for in-page navigations, such as clicking anchor links or updating the window.location.hash. Use did-navigate-in-page event for this purpose.
/// </summary>
public event Action<OnDidNavigateInfo> OnDidNavigate
{
add
{
if (_didNavigate == null)
{
BridgeConnector.Socket.On<OnDidNavigateInfo>("webContents-didNavigate" + Id, (data) =>
{
_didNavigate(data);
});
BridgeConnector.Socket.Emit("register-webContents-didNavigate", Id);
}
_didNavigate += value;
}
remove
{
_didNavigate -= value;
if (_didNavigate == null)
BridgeConnector.Socket.Off("webContents-didNavigate" + Id);
}
}
private event Action<OnDidNavigateInfo> _didNavigate;
/// <summary>
/// Emitted when a server side redirect occurs during navigation. For example a 302 redirect.
/// This event will be emitted after OnDidStartNavigation and always before the OnDidRedirectNavigation event for the same navigation.
/// </summary>
public event Action<string> OnWillRedirect
{
add
{
if (_willRedirect == null)
{
BridgeConnector.Socket.On<string>("webContents-willRedirect" + Id, (url) =>
{
_willRedirect(url);
});
BridgeConnector.Socket.Emit("register-webContents-willRedirect", Id);
}
_willRedirect += value;
}
remove
{
_willRedirect -= value;
if (_willRedirect == null)
BridgeConnector.Socket.Off("webContents-willRedirect" + Id);
}
}
private event Action<string> _willRedirect;
/// <summary>
/// Emitted after a server side redirect occurs during navigation. For example a 302 redirect.
/// </summary>
public event Action<string> OnDidRedirectNavigation
{
add
{
if (_didRedirectNavigation == null)
{
BridgeConnector.Socket.On("webContents-didRedirectNavigation" + Id, (url) =>
{
_didRedirectNavigation(url?.ToString());
});
BridgeConnector.Socket.Emit("register-webContents-didRedirectNavigation", Id);
}
_didRedirectNavigation += value;
}
remove
{
_didRedirectNavigation -= value;
if (_didRedirectNavigation == null)
BridgeConnector.Socket.Off("webContents-didRedirectNavigation" + Id);
}
}
private event Action<string> _didRedirectNavigation;
/// <summary>
/// This event is like OnDidFinishLoad but emitted when the load failed.
/// </summary>
public event Action<OnDidFailLoadInfo> OnDidFailLoad
{
add
{
if (_didFailLoad == null)
{
BridgeConnector.Socket.On("webContents-willRedirect" + Id, (data) =>
{
_didFailLoad(((JObject) data).ToObject<OnDidFailLoadInfo>());
});
BridgeConnector.Socket.Emit("register-webContents-willRedirect", Id);
}
_didFailLoad += value;
}
remove
{
_didFailLoad -= value;
if (_didFailLoad == null)
BridgeConnector.Socket.Off("webContents-willRedirect" + Id);
}
}
private event Action<OnDidFailLoadInfo> _didFailLoad;
/// <summary>
/// Emitted when an input event is sent to the WebContents.
/// </summary>
@@ -114,6 +262,35 @@ public class WebContents
private event Action<InputEvent> _inputEvent;
/// <summary>
/// Emitted when the document in the top-level frame is loaded.
/// </summary>
public event Action OnDomReady
{
add
{
if (_domReady == null)
{
BridgeConnector.Socket.On("webContents-domReady" + Id, () =>
{
_domReady();
});
BridgeConnector.Socket.Emit("register-webContents-domReady", Id);
}
_domReady += value;
}
remove
{
_domReady -= value;
if (_domReady == null)
BridgeConnector.Socket.Off("webContents-domReady" + Id);
}
}
private event Action _domReady;
internal WebContents(int id)
{
Id = id;
@@ -215,6 +392,37 @@ public class WebContents
return taskCompletionSource.Task;
}
/// <summary>
/// Evaluates script code in page.
/// </summary>
/// <param name="code">The code to execute.</param>
/// <param name="userGesture">if set to <c>true</c> simulate a user gesture.</param>
/// <returns>The result of the executed code.</returns>
/// <remarks>
/// <para>
/// In the browser window some HTML APIs like `requestFullScreen` can only be
/// invoked by a gesture from the user. Setting `userGesture` to `true` will remove
/// this limitation.
/// </para>
/// <para>
/// Code execution will be suspended until web page stop loading.
/// </para>
/// </remarks>
public Task<object> ExecuteJavaScriptAsync(string code, bool userGesture = false)
{
var taskCompletionSource = new TaskCompletionSource<object>();
BridgeConnector.Socket.On("webContents-executeJavaScript-completed", (result) =>
{
BridgeConnector.Socket.Off("webContents-executeJavaScript-completed");
taskCompletionSource.SetResult(result);
});
BridgeConnector.Socket.Emit("webContents-executeJavaScript", Id, code, userGesture);
return taskCompletionSource.Task;
}
/// <summary>
/// Is used to get the Url of the loaded page.
/// It's usefull if a web-server redirects you and you need to know where it redirects. For instance, It's useful in case of Implicit Authorization.

View File

@@ -48,7 +48,7 @@ namespace ElectronNET.CLI.Commands.Actions
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
netCorePublishRid = "osx-x64";
netCorePublishRid = RuntimeInformation.ProcessArchitecture == Architecture.Arm64 ? "osx-arm64" : "osx-x64";
electronPackerPlatform = "mac";
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))

View File

@@ -19,6 +19,41 @@ module.exports = (socket) => {
electronSocket.emit('webContents-didFinishLoad' + id);
});
});
socket.on('register-webContents-didStartNavigation', (id) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.removeAllListeners('did-start-navigation');
browserWindow.webContents.on('did-start-navigation', (_, url) => {
electronSocket.emit('webContents-didStartNavigation' + id, url);
});
});
socket.on('register-webContents-didNavigate', (id) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.removeAllListeners('did-navigate');
browserWindow.webContents.on('did-navigate', (_, url, httpResponseCode) => {
electronSocket.emit('webContents-didNavigate' + id, { url, httpResponseCode });
});
});
socket.on('register-webContents-willRedirect', (id) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.removeAllListeners('will-redirect');
browserWindow.webContents.on('will-redirect', (_, url) => {
electronSocket.emit('webContents-willRedirect' + id, url);
});
});
socket.on('register-webContents-didFailLoad', (id) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.removeAllListeners('did-fail-load');
browserWindow.webContents.on('did-fail-load', (_, errorCode, validatedUrl) => {
electronSocket.emit('webContents-didFailLoad' + id, { errorCode, validatedUrl });
});
});
socket.on('register-webContents-didRedirectNavigation', (id) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.removeAllListeners('did-redirect-navigation');
browserWindow.webContents.on('did-redirect-navigation', (_, url) => {
electronSocket.emit('webContents-didRedirectNavigation' + id, url);
});
});
socket.on('register-webContents-input-event', (id) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.removeAllListeners('input-event');
@@ -28,6 +63,16 @@ module.exports = (socket) => {
}
});
});
socket.on('register-webContents-domReady', (id) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.removeAllListeners('dom-ready');
browserWindow.webContents.on('dom-ready', () => {
electronSocket.emit('webContents-domReady' + id);
});
});
socket.on('webContentsOpenDevTools', (id, options) => {
if (options) {
getWindowById(id).webContents.openDevTools(options);
@@ -55,6 +100,12 @@ module.exports = (socket) => {
}
});
});
socket.on('webContents-executeJavaScript', async (id, code, userGesture = false) => {
const result = await getWindowById(id).webContents.executeJavaScript(code, userGesture);
electronSocket.emit('webContents-executeJavaScript-completed', result);
});
socket.on('webContents-getUrl', function (id) {
const browserWindow = getWindowById(id);
electronSocket.emit('webContents-getUrl' + id, browserWindow.webContents.getURL());

File diff suppressed because one or more lines are too long

View File

@@ -24,6 +24,51 @@ export = (socket: Socket) => {
});
});
socket.on('register-webContents-didStartNavigation', (id) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.removeAllListeners('did-start-navigation');
browserWindow.webContents.on('did-start-navigation', (_, url) => {
electronSocket.emit('webContents-didStartNavigation' + id, url);
});
});
socket.on('register-webContents-didNavigate', (id) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.removeAllListeners('did-navigate');
browserWindow.webContents.on('did-navigate', (_, url, httpResponseCode) => {
electronSocket.emit('webContents-didNavigate' + id, {url, httpResponseCode});
});
});
socket.on('register-webContents-willRedirect', (id) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.removeAllListeners('will-redirect');
browserWindow.webContents.on('will-redirect', (_, url) => {
electronSocket.emit('webContents-willRedirect' + id, url);
});
});
socket.on('register-webContents-didFailLoad', (id) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.removeAllListeners('did-fail-load');
browserWindow.webContents.on('did-fail-load', (_, errorCode, validatedUrl) => {
electronSocket.emit('webContents-didFailLoad' + id, {errorCode, validatedUrl});
});
});
socket.on('register-webContents-didRedirectNavigation', (id) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.removeAllListeners('did-redirect-navigation');
browserWindow.webContents.on('did-redirect-navigation', (_, url) => {
electronSocket.emit('webContents-didRedirectNavigation' + id, url);
});
});
socket.on('register-webContents-input-event', (id) => {
const browserWindow = getWindowById(id);
@@ -35,6 +80,15 @@ export = (socket: Socket) => {
});
});
socket.on('register-webContents-domReady', (id) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.removeAllListeners('dom-ready');
browserWindow.webContents.on('dom-ready', () => {
electronSocket.emit('webContents-domReady' + id);
});
});
socket.on('webContentsOpenDevTools', (id, options) => {
if (options) {
getWindowById(id).webContents.openDevTools(options);
@@ -65,6 +119,11 @@ export = (socket: Socket) => {
});
});
socket.on('webContents-executeJavaScript', async (id, code, userGesture = false) => {
const result = await getWindowById(id).webContents.executeJavaScript(code, userGesture);
electronSocket.emit('webContents-executeJavaScript-completed', result);
});
socket.on('webContents-getUrl', function (id) {
const browserWindow = getWindowById(id);
electronSocket.emit('webContents-getUrl' + id, browserWindow.webContents.getURL());

View File

@@ -115,17 +115,10 @@ function isSplashScreenEnabled() {
function startSplashScreen() {
let imageFile = path.join(currentBinPath, manifestJsonFile.splashscreen.imageFile);
imageSize(imageFile, (error, dimensions) => {
if (error) {
console.log(`load splashscreen error:`);
console.error(error);
throw new Error(error.message);
}
const startWindow = (width, height) => {
splashScreen = new BrowserWindow({
width: dimensions.width,
height: dimensions.height,
width: width,
height: height,
transparent: true,
center: true,
frame: false,
@@ -143,10 +136,25 @@ function startSplashScreen() {
const loadSplashscreenUrl = path.join(__dirname, 'splashscreen', 'index.html') + '?imgPath=' + imageFile;
splashScreen.loadURL('file://' + loadSplashscreenUrl);
splashScreen.once('closed', () => {
splashScreen = null;
});
}
if (manifestJsonFile.splashscreen.width && manifestJsonFile.splashscreen.height) {
startWindow(manifestJsonFile.splashscreen.width, manifestJsonFile.splashscreen.height);
return;
}
imageSize(imageFile, (error, dimensions) => {
if (error) {
console.log(`load splashscreen error:`);
console.error(error);
throw new Error(error.message);
}
startWindow(dimensions.width, dimensions.height)
});
}