mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-07-28 19:44:08 +00:00
implement first Electron-API Bridge functions - Add little sample in WebApp
This commit is contained in:
@@ -4,16 +4,17 @@ using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Quobject.SocketIoClientDotNet.Client;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectronNET.API
|
||||
{
|
||||
public static class App
|
||||
{
|
||||
public static IpcMain IpcMain { get; private set; }
|
||||
|
||||
private static Socket _socket;
|
||||
private static JsonSerializer _jsonSerializer;
|
||||
|
||||
public static IpcMain IpcMain { get; private set; }
|
||||
|
||||
public static void OpenWindow(int width, int height, bool show)
|
||||
{
|
||||
_jsonSerializer = new JsonSerializer()
|
||||
@@ -26,7 +27,8 @@ namespace ElectronNET.API
|
||||
{
|
||||
Console.WriteLine("Verbunden!");
|
||||
|
||||
var browserWindowOptions = new BrowserWindowOptions() {
|
||||
var browserWindowOptions = new BrowserWindowOptions()
|
||||
{
|
||||
Height = height,
|
||||
Width = width,
|
||||
Show = show
|
||||
@@ -42,5 +44,70 @@ namespace ElectronNET.API
|
||||
{
|
||||
_socket.Emit("createNotification", JObject.FromObject(notificationOptions, _jsonSerializer));
|
||||
}
|
||||
|
||||
public static void Quit()
|
||||
{
|
||||
_socket.Emit("appQuit");
|
||||
}
|
||||
|
||||
public static void Exit(int exitCode = 0)
|
||||
{
|
||||
_socket.Emit("appExit", exitCode);
|
||||
}
|
||||
|
||||
public static void Relaunch()
|
||||
{
|
||||
_socket.Emit("appRelaunch");
|
||||
}
|
||||
|
||||
public static void Relaunch(RelaunchOptions relaunchOptions)
|
||||
{
|
||||
_socket.Emit("appRelaunch", JObject.FromObject(relaunchOptions, _jsonSerializer));
|
||||
}
|
||||
|
||||
public static void Focus()
|
||||
{
|
||||
_socket.Emit("appFocus");
|
||||
}
|
||||
|
||||
public static void Hide()
|
||||
{
|
||||
_socket.Emit("appHide");
|
||||
}
|
||||
|
||||
public static void Show()
|
||||
{
|
||||
_socket.Emit("appShow");
|
||||
}
|
||||
|
||||
public async static Task<string> GetAppPathAsync()
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<string>();
|
||||
|
||||
_socket.On("appGetAppPathCompleted", (path) =>
|
||||
{
|
||||
taskCompletionSource.SetResult(path.ToString());
|
||||
});
|
||||
|
||||
_socket.Emit("appGetAppPath");
|
||||
|
||||
return await taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
public async static Task<string> GetPathAsync(PathName pathName)
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<string>();
|
||||
|
||||
_socket.On("appGetPathCompleted", (path) =>
|
||||
{
|
||||
taskCompletionSource.SetResult(path.ToString());
|
||||
});
|
||||
|
||||
_socket.Emit("appGetPath", pathName.ToString());
|
||||
|
||||
return await taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
public static void Blub2() { }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,9 +12,5 @@
|
||||
<PackageReference Include="SocketIoClientDotNet" Version="1.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
|
||||
<Exec Command="$(ProjectDir)devCleanup.cmd" />
|
||||
</Target>
|
||||
|
||||
|
||||
</Project>
|
||||
|
||||
24
ElectronNET.API/Entities/PathName.cs
Normal file
24
ElectronNET.API/Entities/PathName.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace ElectronNET.API.Entities
|
||||
{
|
||||
public enum PathName
|
||||
{
|
||||
home,
|
||||
appData,
|
||||
userData,
|
||||
temp,
|
||||
exe,
|
||||
module,
|
||||
desktop,
|
||||
documents,
|
||||
downloads,
|
||||
music,
|
||||
pictures,
|
||||
videos,
|
||||
logs,
|
||||
pepperFlashSystemPlugin
|
||||
}
|
||||
}
|
||||
8
ElectronNET.API/Entities/RelaunchOptions.cs
Normal file
8
ElectronNET.API/Entities/RelaunchOptions.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace ElectronNET.API.Entities
|
||||
{
|
||||
public class RelaunchOptions
|
||||
{
|
||||
public string[] Args { get; set; }
|
||||
public string ExecPath { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
rd /s /q %userprofile%\.nuget\packages\electronnet.api
|
||||
rd /s /q "%userprofile%\.nuget\packages\electronnet.api\"
|
||||
|
||||
32
ElectronNET.Host/api/app.js
Normal file
32
ElectronNET.Host/api/app.js
Normal file
@@ -0,0 +1,32 @@
|
||||
"use strict";
|
||||
exports.__esModule = true;
|
||||
module.exports = function (socket, app) {
|
||||
socket.on('appQuit', function () {
|
||||
app.quit();
|
||||
});
|
||||
socket.on('appExit', function (exitCode) {
|
||||
if (exitCode === void 0) { exitCode = 0; }
|
||||
app.exit(exitCode);
|
||||
});
|
||||
socket.on('appRelaunch', function (options) {
|
||||
app.relaunch(options);
|
||||
});
|
||||
socket.on('appFocus', function () {
|
||||
app.focus();
|
||||
});
|
||||
socket.on('appHide', function () {
|
||||
app.hide();
|
||||
});
|
||||
socket.on('appShow', function () {
|
||||
app.show();
|
||||
});
|
||||
socket.on('appGetAppPath', function () {
|
||||
var path = app.getAppPath();
|
||||
socket.emit('appGetAppPathCompleted', path);
|
||||
});
|
||||
socket.on('appGetPath', function (name) {
|
||||
var path = app.getPath(name);
|
||||
socket.emit('appGetPathCompleted', path);
|
||||
});
|
||||
};
|
||||
//# sourceMappingURL=app.js.map
|
||||
1
ElectronNET.Host/api/app.js.map
Normal file
1
ElectronNET.Host/api/app.js.map
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"app.js","sourceRoot":"","sources":["app.ts"],"names":[],"mappings":";;AAEA,MAAM,CAAC,OAAO,GAAG,UAAC,MAAuB,EAAE,GAAiB;IAExD,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE;QACjB,GAAG,CAAC,IAAI,EAAE,CAAC;IACf,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,UAAC,QAAY;QAAZ,yBAAA,EAAA,YAAY;QAC9B,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACvB,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,EAAE,CAAC,aAAa,EAAE,UAAC,OAAO;QAC7B,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IAC1B,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,EAAE,CAAC,UAAU,EAAE;QAClB,GAAG,CAAC,KAAK,EAAE,CAAC;IAChB,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE;QACjB,GAAG,CAAC,IAAI,EAAE,CAAC;IACf,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE;QACjB,GAAG,CAAC,IAAI,EAAE,CAAC;IACf,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,EAAE,CAAC,eAAe,EAAE;QACvB,IAAM,IAAI,GAAG,GAAG,CAAC,UAAU,EAAE,CAAC;QAC9B,MAAM,CAAC,IAAI,CAAC,wBAAwB,EAAE,IAAI,CAAC,CAAC;IAChD,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,EAAE,CAAC,YAAY,EAAE,UAAC,IAAI;QACzB,IAAM,IAAI,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAC/B,MAAM,CAAC,IAAI,CAAC,qBAAqB,EAAE,IAAI,CAAC,CAAC;IAC7C,CAAC,CAAC,CAAC;AACP,CAAC,CAAA"}
|
||||
38
ElectronNET.Host/api/app.ts
Normal file
38
ElectronNET.Host/api/app.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import {} from 'electron';
|
||||
|
||||
module.exports = (socket: SocketIO.Server, app: Electron.App) => {
|
||||
|
||||
socket.on('appQuit', () => {
|
||||
app.quit();
|
||||
});
|
||||
|
||||
socket.on('appExit', (exitCode = 0) => {
|
||||
app.exit(exitCode);
|
||||
});
|
||||
|
||||
socket.on('appRelaunch', (options) => {
|
||||
app.relaunch(options);
|
||||
});
|
||||
|
||||
socket.on('appFocus', () => {
|
||||
app.focus();
|
||||
});
|
||||
|
||||
socket.on('appHide', () => {
|
||||
app.hide();
|
||||
});
|
||||
|
||||
socket.on('appShow', () => {
|
||||
app.show();
|
||||
});
|
||||
|
||||
socket.on('appGetAppPath', () => {
|
||||
const path = app.getAppPath();
|
||||
socket.emit('appGetAppPathCompleted', path);
|
||||
});
|
||||
|
||||
socket.on('appGetPath', (name) => {
|
||||
const path = app.getPath(name);
|
||||
socket.emit('appGetPathCompleted', path);
|
||||
});
|
||||
}
|
||||
@@ -3,7 +3,7 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
const process = require('child_process').spawn;
|
||||
const portfinder = require('detect-port');
|
||||
let io, window, apiProcess, loadURL, ipc;
|
||||
let io, window, apiProcess, loadURL, ipc, appApi;
|
||||
|
||||
app.on('ready', () => {
|
||||
portfinder(8000, (error, port) => {
|
||||
@@ -17,6 +17,7 @@ function startSocketApiBridge(port) {
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
console.log('ASP.NET Core Application connected...');
|
||||
appApi = require('./api/app')(socket, app);
|
||||
|
||||
socket.on('createBrowserWindow', (options) => {
|
||||
console.log(options);
|
||||
|
||||
9
ElectronNET.Host/package-lock.json
generated
9
ElectronNET.Host/package-lock.json
generated
@@ -4,6 +4,15 @@
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"@types/electron": {
|
||||
"version": "1.6.10",
|
||||
"resolved": "https://registry.npmjs.org/@types/electron/-/electron-1.6.10.tgz",
|
||||
"integrity": "sha512-MOCVyzIwkBEloreoCVrTV108vSf8fFIJPsGruLCoAoBZdxtnJUqKA4lNonf/2u1twSjAspPEfmEheC+TLm/cMw==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"electron": "1.7.8"
|
||||
}
|
||||
},
|
||||
"@types/node": {
|
||||
"version": "7.0.43",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-7.0.43.tgz",
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
"socket.io": "^2.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/electron": "^1.6.10",
|
||||
"@types/socket.io": "^1.4.31"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,12 @@ namespace ElectronNET.WebApp.Controllers
|
||||
App.IpcMain.Send("Goodbye", "Elephant!");
|
||||
});
|
||||
|
||||
App.IpcMain.On("GetPath", async (args) =>
|
||||
{
|
||||
string pathName = await App.GetPathAsync(PathName.pictures);
|
||||
App.IpcMain.Send("GetPathComplete", pathName);
|
||||
});
|
||||
|
||||
return View();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,11 @@
|
||||
<br /><br />
|
||||
<input id="callButton" type="button" value="Call Notification" />
|
||||
|
||||
<div>
|
||||
<p id="picturesPathLabel"></p>
|
||||
<input id="picturesButton" type="button" value="Get Pictures-Path" />
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const { ipcRenderer } = require("electron");
|
||||
|
||||
@@ -27,6 +32,15 @@
|
||||
ipcRenderer.on("Goodbye", (sender, data) => {
|
||||
alert('Goodbye ' + data);
|
||||
});
|
||||
|
||||
document.getElementById("picturesButton").addEventListener("click", () => {
|
||||
ipcRenderer.send("GetPath");
|
||||
});
|
||||
|
||||
ipcRenderer.on("GetPathComplete", (sender, data) => {
|
||||
document.getElementById("picturesPathLabel").innerText = data;
|
||||
});
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user