Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/ElectronNET/Electron.NET in…
Browse files Browse the repository at this point in the history
…to develop
  • Loading branch information
FlorianRappl committed Feb 15, 2024
2 parents eabcc3a + df3bd12 commit 464eaca
Show file tree
Hide file tree
Showing 11 changed files with 422 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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();
Expand Down
52 changes: 46 additions & 6 deletions src/ElectronNET.API/Entities/Display.cs
Expand Up @@ -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>
Expand All @@ -28,6 +63,16 @@ public class Display
/// </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>
Expand All @@ -36,11 +81,6 @@ public class Display
/// </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>
Expand Down
18 changes: 18 additions & 0 deletions src/ElectronNET.API/Entities/OnDidFailLoadInfo.cs
@@ -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; }
}
17 changes: 17 additions & 0 deletions src/ElectronNET.API/Entities/OnDidNavigateInfo.cs
@@ -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; }
}
2 changes: 1 addition & 1 deletion src/ElectronNET.API/IpcMain.cs
Expand Up @@ -117,7 +117,7 @@ public void OnSync(string channel, Func<object, object> listener)
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);
Expand Down
208 changes: 208 additions & 0 deletions src/ElectronNET.API/WebContents.cs
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -215,6 +392,37 @@ public Task<bool> PrintToPDFAsync(string path, PrintToPDFOptions options = null)
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.
Expand Down
Expand Up @@ -48,7 +48,7 @@ public static GetTargetPlatformInformationResult Do(string desiredPlatform, stri
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
netCorePublishRid = "osx-x64";
netCorePublishRid = RuntimeInformation.ProcessArchitecture == Architecture.Arm64 ? "osx-arm64" : "osx-x64";
electronPackerPlatform = "mac";
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
Expand Down

0 comments on commit 464eaca

Please sign in to comment.