fix app quit event bugs

This commit is contained in:
Gregor Biswanger
2017-11-10 01:48:06 +01:00
parent 96722e7598
commit fdb026c7bd
10 changed files with 162 additions and 76 deletions

View File

@@ -3,6 +3,8 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
namespace ElectronNET.API
@@ -30,7 +32,10 @@ namespace ElectronNET.API
{
BridgeConnector.Socket.On("app-window-all-closed" + GetHashCode(), () =>
{
_windowAllClosed();
if (!Electron.WindowManager.IsQuitOnWindowAllClosed)
{
_windowAllClosed();
}
});
BridgeConnector.Socket.Emit("register-app-window-all-closed-event", GetHashCode());
@@ -54,15 +59,38 @@ namespace ElectronNET.API
/// 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
public event Func<Task> BeforeQuit
{
add
{
if (_beforeQuit == null)
{
BridgeConnector.Socket.On("app-before-quit" + GetHashCode(), () =>
BridgeConnector.Socket.On("app-before-quit" + GetHashCode(), async () =>
{
_beforeQuit();
await _beforeQuit();
if(_willQuit == null && _quitting == null)
{
Exit();
}
else if (_willQuit != null)
{
await _willQuit();
if(_quitting == null)
{
Exit();
} else
{
await _quitting();
Exit();
}
}
else if(_quitting != null)
{
await _quitting();
Exit();
}
});
BridgeConnector.Socket.Emit("register-app-before-quit-event", GetHashCode());
@@ -78,7 +106,7 @@ namespace ElectronNET.API
}
}
private event Action _beforeQuit;
private event Func<Task> _beforeQuit;
/// <summary>
/// Emitted when all windows have been closed and the application will quit.
@@ -86,15 +114,25 @@ namespace ElectronNET.API
/// 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
public event Func<Task> WillQuit
{
add
{
if (_willQuit == null)
{
BridgeConnector.Socket.On("app-will-quit" + GetHashCode(), () =>
BridgeConnector.Socket.On("app-will-quit" + GetHashCode(), async () =>
{
_willQuit();
await _willQuit();
if(_quitting == null)
{
Exit();
}
else
{
await _quitting();
Exit();
}
});
BridgeConnector.Socket.Emit("register-app-will-quit-event", GetHashCode());
@@ -110,23 +148,27 @@ namespace ElectronNET.API
}
}
private event Action _willQuit;
private event Func<Task> _willQuit;
/// <summary>
/// Emitted when the application is quitting.
/// </summary>
public event Action Quitting
public event Func<Task> Quitting
{
add
{
if (_quitting == null)
{
BridgeConnector.Socket.On("app-quit" + GetHashCode(), () =>
BridgeConnector.Socket.On("app-will-quit" + GetHashCode() + "quitting", async () =>
{
_quitting();
if(_willQuit == null)
{
await _quitting();
Exit();
}
});
BridgeConnector.Socket.Emit("register-app-quit-event", GetHashCode());
BridgeConnector.Socket.Emit("register-app-will-quit-event", GetHashCode() + "quitting");
}
_quitting += value;
}
@@ -135,11 +177,11 @@ namespace ElectronNET.API
_quitting -= value;
if (_quitting == null)
BridgeConnector.Socket.Off("app-quit" + GetHashCode());
BridgeConnector.Socket.Off("app-will-quit" + GetHashCode() + "quitting");
}
}
private event Action _quitting;
private event Func<Task> _quitting;
/// <summary>
/// Emitted when a BrowserWindow gets blurred.

View File

@@ -47,18 +47,21 @@ namespace ElectronNET.API
public Task<string[]> ShowOpenDialogAsync(BrowserWindow browserWindow, OpenDialogOptions options)
{
var taskCompletionSource = new TaskCompletionSource<string[]>();
string guid = Guid.NewGuid().ToString();
BridgeConnector.Socket.On("showOpenDialogComplete", (filePaths) =>
BridgeConnector.Socket.On("showOpenDialogComplete" + guid, (filePaths) =>
{
BridgeConnector.Socket.Off("showOpenDialogComplete");
BridgeConnector.Socket.Off("showOpenDialogComplete" + guid);
var result = ((JArray)filePaths).ToObject<string[]>();
taskCompletionSource.SetResult(result);
});
BridgeConnector.Socket.Emit("showOpenDialog",
JObject.FromObject(browserWindow, _jsonSerializer),
JObject.FromObject(options, _jsonSerializer));
BridgeConnector.Socket.Emit("showOpenDialog",
JObject.FromObject(browserWindow, _jsonSerializer),
JObject.FromObject(options, _jsonSerializer),
guid);
return taskCompletionSource.Task;
}
@@ -72,17 +75,19 @@ namespace ElectronNET.API
public Task<string> ShowSaveDialogAsync(BrowserWindow browserWindow, SaveDialogOptions options)
{
var taskCompletionSource = new TaskCompletionSource<string>();
string guid = Guid.NewGuid().ToString();
BridgeConnector.Socket.On("showSaveDialogComplete", (filename) =>
BridgeConnector.Socket.On("showSaveDialogComplete" + guid, (filename) =>
{
BridgeConnector.Socket.Off("showSaveDialogComplete");
BridgeConnector.Socket.Off("showSaveDialogComplete" + guid);
taskCompletionSource.SetResult(filename.ToString());
});
BridgeConnector.Socket.Emit("showSaveDialog",
JObject.FromObject(browserWindow, _jsonSerializer),
JObject.FromObject(options, _jsonSerializer));
JObject.FromObject(options, _jsonSerializer),
guid);
return taskCompletionSource.Task;
}
@@ -139,10 +144,11 @@ namespace ElectronNET.API
public Task<MessageBoxResult> ShowMessageBoxAsync(BrowserWindow browserWindow, MessageBoxOptions messageBoxOptions)
{
var taskCompletionSource = new TaskCompletionSource<MessageBoxResult>();
var guid = Guid.NewGuid().ToString();
BridgeConnector.Socket.On("showMessageBoxComplete", (args) =>
BridgeConnector.Socket.On("showMessageBoxComplete" + guid, (args) =>
{
BridgeConnector.Socket.Off("showMessageBoxComplete");
BridgeConnector.Socket.Off("showMessageBoxComplete" + guid);
var result = ((JArray)args);
@@ -156,12 +162,13 @@ namespace ElectronNET.API
if (browserWindow == null)
{
BridgeConnector.Socket.Emit("showMessageBox", JObject.FromObject(messageBoxOptions, _jsonSerializer));
BridgeConnector.Socket.Emit("showMessageBox", JObject.FromObject(messageBoxOptions, _jsonSerializer), guid);
} else
{
BridgeConnector.Socket.Emit("showMessageBox",
JObject.FromObject(browserWindow, _jsonSerializer),
JObject.FromObject(messageBoxOptions, _jsonSerializer));
JObject.FromObject(messageBoxOptions, _jsonSerializer),
guid);
}
return taskCompletionSource.Task;
@@ -205,16 +212,18 @@ namespace ElectronNET.API
public Task ShowCertificateTrustDialogAsync(BrowserWindow browserWindow, CertificateTrustDialogOptions options)
{
var taskCompletionSource = new TaskCompletionSource<object>();
string guid = Guid.NewGuid().ToString();
BridgeConnector.Socket.On("showCertificateTrustDialogComplete", () =>
BridgeConnector.Socket.On("showCertificateTrustDialogComplete" + guid, () =>
{
BridgeConnector.Socket.Off("showCertificateTrustDialogComplete");
BridgeConnector.Socket.Off("showCertificateTrustDialogComplete" + guid);
taskCompletionSource.SetResult(null);
});
BridgeConnector.Socket.Emit("showCertificateTrustDialog",
JObject.FromObject(browserWindow, _jsonSerializer),
JObject.FromObject(options, _jsonSerializer));
JObject.FromObject(options, _jsonSerializer),
guid);
return taskCompletionSource.Task;
}

View File

@@ -38,6 +38,23 @@ namespace ElectronNET.API
}
}
/// <summary>
/// Quit when all windows are closed. (Default is true)
/// </summary>
/// <value>
/// <c>true</c> if [quit window all closed]; otherwise, <c>false</c>.
/// </value>
public bool IsQuitOnWindowAllClosed
{
get { return _isQuitOnWindowAllClosed; }
set
{
BridgeConnector.Socket.Emit("quit-app-window-all-closed-event", value);
_isQuitOnWindowAllClosed = value;
}
}
private bool _isQuitOnWindowAllClosed = true;
/// <summary>
/// Gets the browser windows.
/// </summary>