remove recreate behaviour from activate event on mac, and emit event to handler instead

This commit is contained in:
rafael-aero
2021-08-30 13:22:07 +02:00
parent 35b18a9501
commit bd45e23768
6 changed files with 55 additions and 27 deletions

View File

@@ -25,6 +25,41 @@ namespace ElectronNET.API
/// </summary>
public static bool SocketDebug { get; set; }
/// <summary>
/// Emitted when the user clicks on the dock on Mac
/// <para/>
/// </summary>
[SupportedOSPlatform("macos")]
public event Action Activate
{
add
{
if (_appActivate == null)
{
BridgeConnector.On("app-activate", () =>
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
_appActivate();
}
});
}
_appActivate += value;
}
remove
{
_appActivate -= value;
if (_appActivate == null)
{
BridgeConnector.Off("app-activate");
}
}
}
private event Action _appActivate;
/// <summary>
/// Emitted when all windows have been closed.
/// <para/>

View File

@@ -116,14 +116,14 @@ namespace ElectronNET.API
}
});
if (loadUrl.ToUpper() == "HTTP://LOCALHOST")
if (string.Equals(loadUrl, "HTTP://LOCALHOST", StringComparison.InvariantCultureIgnoreCase))
{
loadUrl = $"{loadUrl}:{BridgeSettings.WebPort}";
}
// Workaround Windows 10 / Electron Bug
// https://github.com/electron/electron/issues/4045
if (isWindows10())
if (IsWindows10())
{
options.Width = options.Width + 14;
options.Height = options.Height + 7;
@@ -140,22 +140,20 @@ namespace ElectronNET.API
{
// Workaround Windows 10 / Electron Bug
// https://github.com/electron/electron/issues/4045
if (isWindows10())
if (IsWindows10())
{
options.X = options.X - 7;
}
BridgeConnector.Emit("createBrowserWindow", JObject.FromObject(options, _keepDefaultValuesSerializer), loadUrl);
}
return taskCompletionSource.Task;
}
private bool isWindows10()
private bool IsWindows10()
{
return RuntimeInformation.OSDescription.Contains("Windows 10");
return OperatingSystem.IsWindowsVersionAtLeast(10);
}
/// <summary>

View File

@@ -18,6 +18,13 @@ module.exports = (socket, app) => {
electronSocket.emit('app-window-all-closed' + appWindowAllClosedEventId);
}
});
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
electronSocket.emit('app-activate');
});
socket.on('quit-app-window-all-closed-event', (quit) => {
isQuitWindowAllClosed = quit;
});

View File

@@ -20,6 +20,12 @@ export = (socket: Socket, app: Electron.App) => {
}
});
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
electronSocket.emit('app-activate');
});
socket.on('quit-app-window-all-closed-event', (quit) => {
isQuitWindowAllClosed = quit;
});

View File

@@ -4,7 +4,7 @@ const browserView_1 = require("./browserView");
const path = require('path');
const windows = (global['browserWindows'] = global['browserWindows'] || []);
let readyToShowWindowsIds = [];
let window, lastOptions, electronSocket;
let window, electronSocket;
let mainWindowURL;
const proxyToCredentialsMap = (global['proxyToCredentialsMap'] = global['proxyToCredentialsMap'] || []);
module.exports = (socket, app) => {
@@ -218,7 +218,6 @@ module.exports = (socket, app) => {
readyToShowWindowsIds.push(window.id);
}
});
lastOptions = options;
window.on('closed', (sender) => {
for (let index = 0; index < windows.length; index++) {
const windowItem = windows[index];
@@ -235,13 +234,6 @@ module.exports = (socket, app) => {
}
}
});
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (window === null && lastOptions) {
window = new electron_1.BrowserWindow(lastOptions);
}
});
if (loadUrl) {
window.loadURL(loadUrl);
}

View File

@@ -4,7 +4,7 @@ import { browserViewMediateService } from './browserView';
const path = require('path');
const windows: Electron.BrowserWindow[] = (global['browserWindows'] = global['browserWindows'] || []) as Electron.BrowserWindow[];
let readyToShowWindowsIds: number[] = [];
let window, lastOptions, electronSocket;
let window, electronSocket;
let mainWindowURL;
const proxyToCredentialsMap: { [proxy: string]: string } = (global['proxyToCredentialsMap'] = global['proxyToCredentialsMap'] || []) as { [proxy: string]: string };
@@ -245,8 +245,6 @@ export = (socket: Socket, app: Electron.App) => {
}
});
lastOptions = options;
window.on('closed', (sender) => {
for (let index = 0; index < windows.length; index++) {
const windowItem = windows[index];
@@ -264,14 +262,6 @@ export = (socket: Socket, app: Electron.App) => {
}
});
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (window === null && lastOptions) {
window = new BrowserWindow(lastOptions);
}
});
if (loadUrl) {
window.loadURL(loadUrl);
}