Skip to content

Commit 0a54735

Browse files
committed
Merge branch 'main' of https://github.com/ElectronNET/Electron.NET into develop
2 parents 1ac371b + e854451 commit 0a54735

File tree

13 files changed

+272
-0
lines changed

13 files changed

+272
-0
lines changed

docs/Using/Custom_main.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Using custom_main.js
2+
3+
This guide explains how to include and use a `custom_main.js` file in your Electron.NET application for advanced Electron/Node.js customization.
4+
5+
## Why use custom_main.js?
6+
7+
- Register custom protocol handlers (e.g., `myapp://`) — protocols must be registered before the app is fully initialized
8+
- Integrate Node.js modules (e.g., telemetry, OS APIs)
9+
- Control startup logic (abort, environment checks)
10+
- Set up IPC messaging or preload scripts
11+
12+
## Step-by-Step Process
13+
14+
### 1. Create the custom_main.js file
15+
16+
Place your custom logic in `electron/custom_main.js`:
17+
18+
```javascript
19+
module.exports.onStartup = function(host) {
20+
// Example: Register a global shortcut for opening dev tools
21+
const { app, globalShortcut, BrowserWindow } = require('electron');
22+
app.on('ready', () => {
23+
const ret = globalShortcut.register('Control+Shift+I', () => {
24+
BrowserWindow.getAllWindows().forEach(win => win.webContents.openDevTools());
25+
console.log('Ctrl+Shift+I is pressed: DevTools opened!');
26+
});
27+
});
28+
app.on('will-quit', () => {
29+
globalShortcut.unregisterAll();
30+
});
31+
return true;
32+
};
33+
```
34+
35+
### 2. Configure your .csproj to copy custom_main.js to output
36+
37+
Add this to your `.csproj` file:
38+
39+
```xml
40+
<ItemGroup>
41+
<None Update="electron\custom_main.js">
42+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
43+
<TargetPath>.electron\custom_main.js</TargetPath>
44+
</None>
45+
</ItemGroup>
46+
```
47+
48+
### 3. Build and run your app
49+
50+
Use the standard build/run commands:
51+
52+
```powershell
53+
dotnet build
54+
dotnet run
55+
```
56+
57+
Electron.NET will automatically load and execute your `custom_main.js` before initializing the .NET backend.
58+
59+
## Advanced Usage
60+
61+
Use environment variables to control features:
62+
63+
```javascript
64+
const env = process.env.ASPNETCORE_ENVIRONMENT || 'Production';
65+
if (env === 'Development') { /* enable dev features */ }
66+
```
67+
68+
## Notes
69+
70+
- `custom_main.js` must use CommonJS syntax (`module.exports.onStartup = ...`).
71+
- Place the file in your source directory and copy it to `.electron` using `.csproj`.
72+
- Electron.NET will abort startup if `onStartup` returns `false`.
73+
74+
### Complete example is available here [ElectronNetSampleApp](https://github.com/niteshsinghal85/ElectronNetSampleApp)

docs/_Sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [Startup-Methods](Using/Startup-Methods.md)
2525
- [Debugging](Using/Debugging.md)
2626
- [Package Building](Using/Package-Building.md)
27+
- [Adding a `custom_main.js`](Using/Custom_main.md)
2728

2829
# API Reference
2930

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using ElectronNET.API;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace ElectronNET.Samples.ElectronHostHook.Controllers
5+
{
6+
public class HomeController : Controller
7+
{
8+
public async Task<IActionResult> Index()
9+
{
10+
string message = "Electron not active";
11+
if (HybridSupport.IsElectronActive)
12+
{
13+
// Call the HostHook defined in ElectronHostHook/index.ts
14+
var result = await Electron.HostHook.CallAsync<string>("ping", "Hello from C#");
15+
message = $"Sent 'Hello from C#', Received: '{result}'";
16+
}
17+
18+
return View("Index", message);
19+
}
20+
}
21+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
*.js
3+
*.js.map
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Socket } from "socket.io";
2+
3+
export class Connector {
4+
constructor(private socket: Socket, public app: any) {
5+
}
6+
7+
on(key: string, javaScriptCode: Function): void {
8+
this.socket.on(key, (...args: any[]) => {
9+
const id: string = args.pop();
10+
try {
11+
javaScriptCode(...args, (data) => {
12+
if (data) {
13+
this.socket.emit(`${key}Complete${id}`, data);
14+
}
15+
});
16+
} catch (error) {
17+
this.socket.emit(`${key}Error${id}`, `Host Hook Exception`, error);
18+
}
19+
});
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Connector } from "./connector";
2+
import { Socket } from "socket.io";
3+
4+
export class HookService extends Connector {
5+
constructor(socket: Socket, public app: any) {
6+
super(socket, app);
7+
}
8+
9+
onHostReady(): void {
10+
// execute your own JavaScript Host logic here
11+
this.on("ping", (msg, done) => {
12+
console.log("Received ping from C#:", msg);
13+
done("pong: " + msg);
14+
});
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "electron-host-hook",
3+
"version": "1.0.0",
4+
"description": "Connector for Electron.NET projects.",
5+
"main": "index.js",
6+
"dependencies": {
7+
"socket.io": "^4.8.1"
8+
},
9+
"devDependencies": {
10+
"typescript": "^5.9.3"
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "ES2019",
5+
"sourceMap": true,
6+
"skipLibCheck": true
7+
},
8+
"exclude": ["node_modules"]
9+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<ElectronNetDevMode>true</ElectronNetDevMode>
4+
</PropertyGroup>
5+
6+
<Import Project="..\ElectronNET\build\ElectronNET.Core.props" Condition="$(ElectronNetDevMode)" />
7+
8+
<PropertyGroup>
9+
<TargetFramework>net8.0</TargetFramework>
10+
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
11+
<AspNetCoreModuleName>AspNetCoreModule</AspNetCoreModuleName>
12+
<IsPackable>false</IsPackable>
13+
<TypeScriptModuleKind>commonjs</TypeScriptModuleKind>
14+
<TypeScriptUseNodeJS>true</TypeScriptUseNodeJS>
15+
<TypeScriptTSConfig>ElectronHostHook/tsconfig.json</TypeScriptTSConfig>
16+
<TypeScriptCompileOnSaveEnabled>true</TypeScriptCompileOnSaveEnabled>
17+
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
18+
</PropertyGroup>
19+
20+
<ItemGroup>
21+
<TypeScriptCompile Remove="**\node_modules\**" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\ElectronNET.API\ElectronNET.API.csproj" Condition="$(ElectronNetDevMode)" />
26+
<ProjectReference Include="..\ElectronNET.AspNet\ElectronNET.AspNet.csproj" Condition="$(ElectronNetDevMode)" />
27+
</ItemGroup>
28+
29+
<ItemGroup>
30+
<PackageReference Include="ElectronNET.Core" Version="0.2.0" Condition="'$(ElectronNetDevMode)' != 'true'" />
31+
<PackageReference Include="ElectronNET.Core.AspNet" Version="0.2.0" Condition="'$(ElectronNetDevMode)' != 'true'" />
32+
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="5.9.3" />
33+
</ItemGroup>
34+
35+
<Import Project="..\ElectronNET\build\ElectronNET.Core.targets" Condition="$(ElectronNetDevMode)" />
36+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using ElectronNET.API;
2+
3+
namespace ElectronNET.Samples.ElectronHostHook
4+
{
5+
public class Program
6+
{
7+
public static void Main(string[] args)
8+
{
9+
var builder = WebApplication.CreateBuilder(args);
10+
11+
builder.WebHost.UseElectron(args, async () =>
12+
{
13+
var window = await Electron.WindowManager.CreateWindowAsync();
14+
});
15+
16+
builder.Services.AddElectron();
17+
builder.Services.AddControllersWithViews();
18+
19+
var app = builder.Build();
20+
21+
app.UseStaticFiles();
22+
app.UseRouting();
23+
24+
app.MapControllerRoute(
25+
name: "default",
26+
pattern: "{controller=Home}/{action=Index}/{id?}");
27+
28+
app.Run();
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)