implement the first App-API Events

This commit is contained in:
Gregor Biswanger
2017-10-14 19:57:49 +02:00
parent 9abece156f
commit 90d0cc6189
6 changed files with 363 additions and 11 deletions

View File

@@ -9,7 +9,254 @@ namespace ElectronNET.API
{
public sealed class App
{
private static App _app;
/// <summary>
/// Emitted when all windows have been closed.
///
/// If you do not subscribe to this event and all windows are closed,
/// the default behavior is to quit the app; however, if you subscribe,
/// you control whether the app quits or not.If the user pressed Cmd + Q,
/// or the developer called app.quit(), Electron will first try to close
/// all the windows and then emit the will-quit event, and in this case the
/// window-all-closed event would not be emitted.
/// </summary>
public event Action WindowAllClosed
{
add
{
if (_windowAllClosed == null)
{
BridgeConnector.Socket.On("app-window-all-closed", () =>
{
_windowAllClosed();
});
BridgeConnector.Socket.Emit("register-app-window-all-closed-event");
}
_windowAllClosed += value;
}
remove
{
_windowAllClosed -= value;
}
}
private event Action _windowAllClosed;
/// <summary>
/// Emitted before the application starts closing its windows.
///
/// Note: If application quit was initiated by autoUpdater.quitAndInstall() then before-quit is emitted after
/// emitting close event on all windows and closing them.
/// </summary>
public event Action BeforeQuit
{
add
{
if (_beforeQuit == null)
{
BridgeConnector.Socket.On("app-before-quit", () =>
{
_beforeQuit();
});
BridgeConnector.Socket.Emit("register-app-before-quit-event");
}
_beforeQuit += value;
}
remove
{
_beforeQuit -= value;
}
}
private event Action _beforeQuit;
/// <summary>
/// Emitted when all windows have been closed and the application will quit.
///
/// See the description of the window-all-closed event for the differences between the will-quit and
/// window-all-closed events.
/// </summary>
public event Action WillQuit
{
add
{
if (_willQuit == null)
{
BridgeConnector.Socket.On("app-will-quit", () =>
{
_willQuit();
});
BridgeConnector.Socket.Emit("register-app-will-quit-event");
}
_willQuit += value;
}
remove
{
_willQuit -= value;
}
}
private event Action _willQuit;
/// <summary>
/// Emitted when the application is quitting.
/// </summary>
public event Action Quitting
{
add
{
if (_quitting == null)
{
BridgeConnector.Socket.On("app-quit", () =>
{
_quitting();
});
BridgeConnector.Socket.Emit("register-app-quit-event");
}
_quitting += value;
}
remove
{
_quitting -= value;
}
}
private event Action _quitting;
/// <summary>
/// Emitted when a BrowserWindow gets blurred.
/// </summary>
public event Action BrowserWindowBlur
{
add
{
if (_browserWindowBlur == null)
{
BridgeConnector.Socket.On("app-browser-window-blur", () =>
{
_browserWindowBlur();
});
BridgeConnector.Socket.Emit("register-app-browser-window-blur-event");
}
_browserWindowBlur += value;
}
remove
{
_browserWindowBlur -= value;
}
}
private event Action _browserWindowBlur;
/// <summary>
/// Emitted when a BrowserWindow gets focused.
/// </summary>
public event Action BrowserWindowFocus
{
add
{
if (_browserWindowFocus == null)
{
BridgeConnector.Socket.On("app-browser-window-focus", () =>
{
_browserWindowFocus();
});
BridgeConnector.Socket.Emit("register-app-browser-window-focus-event");
}
_browserWindowFocus += value;
}
remove
{
_browserWindowFocus -= value;
}
}
private event Action _browserWindowFocus;
/// <summary>
/// Emitted when a new BrowserWindow is created.
/// </summary>
public event Action BrowserWindowCreated
{
add
{
if (_browserWindowCreated == null)
{
BridgeConnector.Socket.On("app-browser-window-created", () =>
{
_browserWindowCreated();
});
BridgeConnector.Socket.Emit("register-app-browser-window-created-event");
}
_browserWindowCreated += value;
}
remove
{
_browserWindowCreated -= value;
}
}
private event Action _browserWindowCreated;
/// <summary>
/// Emitted when a new webContents is created.
/// </summary>
public event Action WebContentsCreated
{
add
{
if (_webContentsCreated == null)
{
BridgeConnector.Socket.On("app-web-contents-created", () =>
{
_webContentsCreated();
});
BridgeConnector.Socket.Emit("register-app-web-contents-created-event");
}
_webContentsCreated += value;
}
remove
{
_webContentsCreated -= value;
}
}
private event Action _webContentsCreated;
/// <summary>
/// Emitted when Chromes accessibility support changes.
/// This event fires when assistive technologies, such as screen readers, are enabled or disabled.
/// See https://www.chromium.org/developers/design-documents/accessibility for more details.
/// </summary>
public event Action<bool> AccessibilitySupportChanged
{
add
{
if (_accessibilitySupportChanged == null)
{
BridgeConnector.Socket.On("app-accessibility-support-changed", (state) =>
{
_accessibilitySupportChanged((bool)state);
});
BridgeConnector.Socket.Emit("register-app-accessibility-support-changed-event");
}
_accessibilitySupportChanged += value;
}
remove
{
_accessibilitySupportChanged -= value;
}
}
private event Action<bool> _accessibilitySupportChanged;
private App() { }
@@ -26,11 +273,14 @@ namespace ElectronNET.API
}
}
private static App _app;
private JsonSerializer _jsonSerializer = new JsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
// TODO: Auslagern in eigenes Window-Management
public void OpenWindow(int width, int height, bool show)
{
var browserWindowOptions = new BrowserWindowOptions()
@@ -43,6 +293,7 @@ namespace ElectronNET.API
BridgeConnector.Socket.Emit("createBrowserWindow", JObject.FromObject(browserWindowOptions, _jsonSerializer));
}
// TODO: Auslagern in eigenes Notification-API
public void CreateNotification(NotificationOptions notificationOptions)
{
BridgeConnector.Socket.Emit("createNotification", JObject.FromObject(notificationOptions, _jsonSerializer));

View File

@@ -1,6 +1,51 @@
"use strict";
exports.__esModule = true;
module.exports = function (socket, app) {
socket.on('register-app-window-all-closed-event', function () {
app.on('window-all-closed', function () {
socket.emit('app-window-all-closed');
});
});
socket.on('register-app-before-quit-event', function () {
app.on('before-quit', function () {
socket.emit('app-before-quit');
});
});
socket.on('register-app-will-quit-event', function () {
app.on('will-quit', function () {
socket.emit('app-will-quit');
});
});
socket.on('register-app-quit-event', function () {
app.on('quit', function () {
socket.emit('app-quit');
});
});
socket.on('register-app-browser-window-blur-event', function () {
app.on('browser-window-blur', function () {
socket.emit('app-browser-window-blur');
});
});
socket.on('register-app-browser-window-focus-event', function () {
app.on('browser-window-focus', function () {
socket.emit('app-browser-window-focus');
});
});
socket.on('register-app-browser-window-created-event', function () {
app.on('browser-window-created', function () {
socket.emit('app-browser-window-created');
});
});
socket.on('register-app-web-contents-created-event', function () {
app.on('web-contents-created', function () {
socket.emit('app-web-contents-created');
});
});
socket.on('register-app-accessibility-support-changed-event', function () {
app.on('accessibility-support-changed', function (event, accessibilitySupportEnabled) {
socket.emit('app-accessibility-support-changed', accessibilitySupportEnabled);
});
});
socket.on('appQuit', function () {
app.quit();
});
@@ -28,6 +73,15 @@ module.exports = function (socket, app) {
var path = app.getPath(name);
socket.emit('appGetPathCompleted', path);
});
// const nativeImages = {};
// function addNativeImage(nativeImage: Electron.NativeImage) {
// if(Object.keys(nativeImages).length === 0) {
// nativeImage['1'] = nativeImage;
// } else {
// let indexCount = Object.keys(nativeImages).length + 1;
// nativeImage[indexCount] = nativeImage;
// }
// }
socket.on('appGetFileIcon', function (path, options) {
if (options) {
app.getFileIcon(path, options, function (error, nativeImage) {

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,60 @@
import { nativeImage as NativeImage } from 'electron';
module.exports = (socket: SocketIO.Server, app: Electron.App) => {
socket.on('register-app-window-all-closed-event', () => {
app.on('window-all-closed', () => {
socket.emit('app-window-all-closed');
});
});
socket.on('register-app-before-quit-event', () => {
app.on('before-quit', () => {
socket.emit('app-before-quit');
});
});
socket.on('register-app-will-quit-event', () => {
app.on('will-quit', () => {
socket.emit('app-will-quit');
});
});
socket.on('register-app-quit-event', () => {
app.on('quit', () => {
socket.emit('app-quit');
});
});
socket.on('register-app-browser-window-blur-event', () => {
app.on('browser-window-blur', () => {
socket.emit('app-browser-window-blur');
});
});
socket.on('register-app-browser-window-focus-event', () => {
app.on('browser-window-focus', () => {
socket.emit('app-browser-window-focus');
});
});
socket.on('register-app-browser-window-created-event', () => {
app.on('browser-window-created', () => {
socket.emit('app-browser-window-created');
});
});
socket.on('register-app-web-contents-created-event', () => {
app.on('web-contents-created', () => {
socket.emit('app-web-contents-created');
});
});
socket.on('register-app-accessibility-support-changed-event', () => {
app.on('accessibility-support-changed', (event, accessibilitySupportEnabled) => {
socket.emit('app-accessibility-support-changed', accessibilitySupportEnabled);
});
});
socket.on('appQuit', () => {
app.quit();

View File

@@ -21,15 +21,7 @@ namespace ElectronNET.WebApp.Controllers
Electron.IpcMain.On("GetPath", async (args) =>
{
string pathName = await Electron.App.GetPathAsync(PathName.pictures);
//App.IpcMain.Send("GetPathComplete", pathName);
var result = await Electron.App.GetPathAsync(PathName.exe);
//var imagePath = Path.Combine(result, "Electron.png");
Electron.IpcMain.Send("GetPathComplete", result);
//var image = await App.GetFileIconAsync(result);
Electron.IpcMain.Send("GetPathComplete", pathName);
});
return View();

View File

@@ -1,4 +1,5 @@
using ElectronNET.API;
using ElectronNET.API.Entities;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;