Change second instance activation to raise event instead of trying to blindly open any window

This commit is contained in:
rafael-aero
2021-09-15 10:58:43 +02:00
parent 236c31abe0
commit c2904f3f68
3 changed files with 67 additions and 29 deletions

View File

@@ -60,6 +60,37 @@ namespace ElectronNET.API
private event Action _appActivate;
/// <summary>
/// Emitted on the first instance when the user opens a second instance of the app, and the app is single instance
/// <para/>
/// </summary>
public event Action<string[]> ActivateFromSecondInstance
{
add
{
if (_appActivateFromSecondInstance == null)
{
BridgeConnector.On<string[]>("app-activate-from-second-instance", (args) =>
{
_appActivateFromSecondInstance(args);
});
}
_appActivateFromSecondInstance += value;
}
remove
{
_appActivateFromSecondInstance -= value;
if (_appActivateFromSecondInstance == null)
{
BridgeConnector.Off("app-activate-from-second-instance");
}
}
}
private event Action<string[]> _appActivateFromSecondInstance;
/// <summary>
/// Emitted when all windows have been closed.
/// <para/>

View File

@@ -6,7 +6,7 @@ let electronSocket;
export = (socket: Socket) => {
electronSocket = socket;
socket.on('register-tray-click', (id) => {
if (tray.value) {
if (tray.value && !tray.value.isDestroyed()) {
tray.value.on('click', (event, bounds) => {
electronSocket.emit('tray-click-event' + id, [(<any>event).__proto__, bounds]);
});
@@ -14,7 +14,7 @@ export = (socket: Socket) => {
});
socket.on('register-tray-right-click', (id) => {
if (tray.value) {
if (tray.value && !tray.value.isDestroyed()) {
tray.value.on('right-click', (event, bounds) => {
electronSocket.emit('tray-right-click-event' + id, [(<any>event).__proto__, bounds]);
});
@@ -22,7 +22,7 @@ export = (socket: Socket) => {
});
socket.on('register-tray-double-click', (id) => {
if (tray.value) {
if (tray.value && !tray.value.isDestroyed()) {
tray.value.on('double-click', (event, bounds) => {
electronSocket.emit('tray-double-click-event' + id, [(<any>event).__proto__, bounds]);
});
@@ -30,7 +30,7 @@ export = (socket: Socket) => {
});
socket.on('register-tray-balloon-show', (id) => {
if (tray.value) {
if (tray.value && !tray.value.isDestroyed()) {
tray.value.on('balloon-show', () => {
electronSocket.emit('tray-balloon-show-event' + id);
});
@@ -38,7 +38,7 @@ export = (socket: Socket) => {
});
socket.on('register-tray-balloon-click', (id) => {
if (tray.value) {
if (tray.value && !tray.value.isDestroyed()) {
tray.value.on('balloon-click', () => {
electronSocket.emit('tray-balloon-click-event' + id);
});
@@ -46,7 +46,7 @@ export = (socket: Socket) => {
});
socket.on('register-tray-balloon-closed', (id) => {
if (tray.value) {
if (tray.value && !tray.value.isDestroyed()) {
tray.value.on('balloon-closed', () => {
electronSocket.emit('tray-balloon-closed-event' + id);
});
@@ -69,51 +69,51 @@ export = (socket: Socket) => {
});
socket.on('tray-destroy', () => {
if (tray.value) {
if (tray.value && !tray.value.isDestroyed()) {
tray.value.destroy();
}
});
socket.on('tray-setImage', (image) => {
if (tray.value) {
if (tray.value && !tray.value.isDestroyed()) {
tray.value.setImage(image);
}
});
socket.on('tray-setPressedImage', (image) => {
if (tray.value) {
if (tray.value && !tray.value.isDestroyed()) {
const img = nativeImage.createFromPath(image);
tray.value.setPressedImage(img);
}
});
socket.on('tray-setToolTip', (toolTip) => {
if (tray.value) {
if (tray.value && !tray.value.isDestroyed()) {
tray.value.setToolTip(toolTip);
}
});
socket.on('tray-setTitle', (title) => {
if (tray.value) {
if (tray.value && !tray.value.isDestroyed()) {
tray.value.setTitle(title);
}
});
socket.on('tray-displayBalloon', (options) => {
if (tray.value) {
if (tray.value && !tray.value.isDestroyed()) {
tray.value.displayBalloon(options);
}
});
socket.on('tray-isDestroyed', () => {
if (tray.value) {
if (tray.value && !tray.value.isDestroyed()) {
const isDestroyed = tray.value.isDestroyed();
electronSocket.emit('tray-isDestroyedCompleted', isDestroyed);
}
});
socket.on('register-tray-on-event', (eventName, listenerName) => {
if (tray.value){
if (tray.value && !tray.value.isDestroyed()){
tray.value.on(eventName, (...args) => {
if (args.length > 1) {
electronSocket.emit(listenerName, args[1]);
@@ -125,7 +125,7 @@ export = (socket: Socket) => {
});
socket.on('register-tray-once-event', (eventName, listenerName) => {
if (tray.value){
if (tray.value && !tray.value.isDestroyed()){
tray.value.once(eventName, (...args) => {
if (args.length > 1) {
electronSocket.emit(listenerName, args[1]);

View File

@@ -57,23 +57,30 @@ const manifestJsonFile = require(manifestJsonFilePath);
if (manifestJsonFile.singleInstance || manifestJsonFile.aspCoreBackendPort) {
const mainInstance = app.requestSingleInstanceLock();
app.on('second-instance', (events, args = []) => {
args.forEach(parameter => {
const words = parameter.split('=');
if(words.length > 1) {
app.commandLine.appendSwitch(words[0].replace('--', ''), words[1]);
} else {
app.commandLine.appendSwitch(words[0].replace('--', ''));
}
});
let socket = global['electronsocket'];
const windows = BrowserWindow.getAllWindows();
if (windows.length) {
if (windows[0].isMinimized()) {
windows[0].restore();
}
windows[0].focus();
if (socket) {
socket.emit('app-activate-from-second-instance', args);
}
//args.forEach(parameter => {
// const words = parameter.split('=');
// if(words.length > 1) {
// app.commandLine.appendSwitch(words[0].replace('--', ''), words[1]);
// } else {
// app.commandLine.appendSwitch(words[0].replace('--', ''));
// }
//});
//const windows = BrowserWindow.getAllWindows();
//if (windows.length) {
// if (windows[0].isMinimized()) {
// windows[0].restore();
// }
// windows[0].focus();
//}
});
if (!mainInstance) {