From 5f0be6543b3f493fe075d98de12d42e1cd2696f8 Mon Sep 17 00:00:00 2001 From: Gregor Biswanger Date: Thu, 12 Oct 2017 02:24:27 +0200 Subject: [PATCH] implement first Electron-API Bridge functions - Add little sample in WebApp --- ElectronNET.API/App.cs | 73 ++++++++++++++++++- ElectronNET.API/ElectronNET.API.csproj | 4 - ElectronNET.API/Entities/PathName.cs | 24 ++++++ ElectronNET.API/Entities/RelaunchOptions.cs | 8 ++ ElectronNET.API/devCleanup.cmd | 2 +- ElectronNET.Host/api/app.js | 32 ++++++++ ElectronNET.Host/api/app.js.map | 1 + ElectronNET.Host/api/app.ts | 38 ++++++++++ ElectronNET.Host/main.js | 3 +- ElectronNET.Host/package-lock.json | 9 +++ ElectronNET.Host/package.json | 1 + .../Controllers/HomeController.cs | 6 ++ ElectronNET.WebApp/Views/Home/Index.cshtml | 14 ++++ 13 files changed, 206 insertions(+), 9 deletions(-) create mode 100644 ElectronNET.API/Entities/PathName.cs create mode 100644 ElectronNET.API/Entities/RelaunchOptions.cs create mode 100644 ElectronNET.Host/api/app.js create mode 100644 ElectronNET.Host/api/app.js.map create mode 100644 ElectronNET.Host/api/app.ts diff --git a/ElectronNET.API/App.cs b/ElectronNET.API/App.cs index c08bb91..bf073f4 100644 --- a/ElectronNET.API/App.cs +++ b/ElectronNET.API/App.cs @@ -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 GetAppPathAsync() + { + var taskCompletionSource = new TaskCompletionSource(); + + _socket.On("appGetAppPathCompleted", (path) => + { + taskCompletionSource.SetResult(path.ToString()); + }); + + _socket.Emit("appGetAppPath"); + + return await taskCompletionSource.Task; + } + + public async static Task GetPathAsync(PathName pathName) + { + var taskCompletionSource = new TaskCompletionSource(); + + _socket.On("appGetPathCompleted", (path) => + { + taskCompletionSource.SetResult(path.ToString()); + }); + + _socket.Emit("appGetPath", pathName.ToString()); + + return await taskCompletionSource.Task; + } + + public static void Blub2() { } } } diff --git a/ElectronNET.API/ElectronNET.API.csproj b/ElectronNET.API/ElectronNET.API.csproj index 5c20f7f..104a13b 100644 --- a/ElectronNET.API/ElectronNET.API.csproj +++ b/ElectronNET.API/ElectronNET.API.csproj @@ -12,9 +12,5 @@ - - - - diff --git a/ElectronNET.API/Entities/PathName.cs b/ElectronNET.API/Entities/PathName.cs new file mode 100644 index 0000000..48173d8 --- /dev/null +++ b/ElectronNET.API/Entities/PathName.cs @@ -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 + } +} diff --git a/ElectronNET.API/Entities/RelaunchOptions.cs b/ElectronNET.API/Entities/RelaunchOptions.cs new file mode 100644 index 0000000..55ff88e --- /dev/null +++ b/ElectronNET.API/Entities/RelaunchOptions.cs @@ -0,0 +1,8 @@ +namespace ElectronNET.API.Entities +{ + public class RelaunchOptions + { + public string[] Args { get; set; } + public string ExecPath { get; set; } + } +} diff --git a/ElectronNET.API/devCleanup.cmd b/ElectronNET.API/devCleanup.cmd index 23931d4..294a54c 100644 --- a/ElectronNET.API/devCleanup.cmd +++ b/ElectronNET.API/devCleanup.cmd @@ -1 +1 @@ -rd /s /q %userprofile%\.nuget\packages\electronnet.api +rd /s /q "%userprofile%\.nuget\packages\electronnet.api\" diff --git a/ElectronNET.Host/api/app.js b/ElectronNET.Host/api/app.js new file mode 100644 index 0000000..0190e5b --- /dev/null +++ b/ElectronNET.Host/api/app.js @@ -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 \ No newline at end of file diff --git a/ElectronNET.Host/api/app.js.map b/ElectronNET.Host/api/app.js.map new file mode 100644 index 0000000..44b340f --- /dev/null +++ b/ElectronNET.Host/api/app.js.map @@ -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"} \ No newline at end of file diff --git a/ElectronNET.Host/api/app.ts b/ElectronNET.Host/api/app.ts new file mode 100644 index 0000000..e13c153 --- /dev/null +++ b/ElectronNET.Host/api/app.ts @@ -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); + }); +} \ No newline at end of file diff --git a/ElectronNET.Host/main.js b/ElectronNET.Host/main.js index 2045e43..242e236 100644 --- a/ElectronNET.Host/main.js +++ b/ElectronNET.Host/main.js @@ -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); diff --git a/ElectronNET.Host/package-lock.json b/ElectronNET.Host/package-lock.json index 53050d4..8803dfc 100644 --- a/ElectronNET.Host/package-lock.json +++ b/ElectronNET.Host/package-lock.json @@ -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", diff --git a/ElectronNET.Host/package.json b/ElectronNET.Host/package.json index f219282..d9f8a56 100644 --- a/ElectronNET.Host/package.json +++ b/ElectronNET.Host/package.json @@ -15,6 +15,7 @@ "socket.io": "^2.0.3" }, "devDependencies": { + "@types/electron": "^1.6.10", "@types/socket.io": "^1.4.31" } } diff --git a/ElectronNET.WebApp/Controllers/HomeController.cs b/ElectronNET.WebApp/Controllers/HomeController.cs index 8868da4..f447614 100644 --- a/ElectronNET.WebApp/Controllers/HomeController.cs +++ b/ElectronNET.WebApp/Controllers/HomeController.cs @@ -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(); } } diff --git a/ElectronNET.WebApp/Views/Home/Index.cshtml b/ElectronNET.WebApp/Views/Home/Index.cshtml index 2bcac87..22197ef 100644 --- a/ElectronNET.WebApp/Views/Home/Index.cshtml +++ b/ElectronNET.WebApp/Views/Home/Index.cshtml @@ -17,6 +17,11 @@

+
+

+ +
+