From ca382bf60501d4b8db722889e3e31f1d4cfaf34f Mon Sep 17 00:00:00 2001 From: Gregor Biswanger Date: Fri, 10 Nov 2017 03:11:13 +0100 Subject: [PATCH] implement PreventDefault function for stop quiting application --- ElectronNET.API/App.cs | 73 +++++++++++++------ ElectronNET.API/QuitEventArgs.cs | 16 ++++ .../Controllers/ShortcutsController.cs | 2 +- ElectronNET.WebApp/Startup.cs | 3 +- .../Views/Shortcuts/Index.cshtml | 2 +- 5 files changed, 71 insertions(+), 25 deletions(-) create mode 100644 ElectronNET.API/QuitEventArgs.cs diff --git a/ElectronNET.API/App.cs b/ElectronNET.API/App.cs index 94e0b14..68463e3 100644 --- a/ElectronNET.API/App.cs +++ b/ElectronNET.API/App.cs @@ -59,7 +59,7 @@ 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. /// - public event Func BeforeQuit + public event Func BeforeQuit { add { @@ -67,30 +67,45 @@ namespace ElectronNET.API { BridgeConnector.Socket.On("app-before-quit" + GetHashCode(), async () => { - await _beforeQuit(); + await _beforeQuit(new QuitEventArgs()); - if(_willQuit == null && _quitting == null) + if (_preventQuit) { - Exit(); + _preventQuit = false; } - else if (_willQuit != null) + else { - await _willQuit(); - - if(_quitting == null) + if (_willQuit == null && _quitting == null) { Exit(); - } else + } + else if (_willQuit != null) + { + await _willQuit(new QuitEventArgs()); + + if (_preventQuit) + { + _preventQuit = false; + } + else + { + if (_quitting == null) + { + Exit(); + } + else + { + await _quitting(); + Exit(); + } + } + } + else if (_quitting != null) { await _quitting(); Exit(); } } - else if(_quitting != null) - { - await _quitting(); - Exit(); - } }); BridgeConnector.Socket.Emit("register-app-before-quit-event", GetHashCode()); @@ -106,7 +121,7 @@ namespace ElectronNET.API } } - private event Func _beforeQuit; + private event Func _beforeQuit; /// /// Emitted when all windows have been closed and the application will quit. @@ -114,7 +129,7 @@ namespace ElectronNET.API /// See the description of the window-all-closed event for the differences between the will-quit and /// window-all-closed events. /// - public event Func WillQuit + public event Func WillQuit { add { @@ -122,16 +137,23 @@ namespace ElectronNET.API { BridgeConnector.Socket.On("app-will-quit" + GetHashCode(), async () => { - await _willQuit(); + await _willQuit(new QuitEventArgs()); - if(_quitting == null) + if (_preventQuit) { - Exit(); + _preventQuit = false; } else { - await _quitting(); - Exit(); + if (_quitting == null) + { + Exit(); + } + else + { + await _quitting(); + Exit(); + } } }); @@ -148,7 +170,7 @@ namespace ElectronNET.API } } - private event Func _willQuit; + private event Func _willQuit; /// /// Emitted when the application is quitting. @@ -1378,5 +1400,12 @@ namespace ElectronNET.API { BridgeConnector.Socket.Emit("appDockSetIcon", image); } + + internal void PreventQuit() + { + _preventQuit = true; + } + + private bool _preventQuit = false; } } diff --git a/ElectronNET.API/QuitEventArgs.cs b/ElectronNET.API/QuitEventArgs.cs new file mode 100644 index 0000000..582023c --- /dev/null +++ b/ElectronNET.API/QuitEventArgs.cs @@ -0,0 +1,16 @@ +namespace ElectronNET.API +{ + /// + /// + /// + public sealed class QuitEventArgs + { + /// + /// Will prevent the default behaviour, which is terminating the application. + /// + public void PreventDefault() + { + Electron.App.PreventQuit(); + } + } +} diff --git a/ElectronNET.WebApp/Controllers/ShortcutsController.cs b/ElectronNET.WebApp/Controllers/ShortcutsController.cs index 246e229..e96eee5 100644 --- a/ElectronNET.WebApp/Controllers/ShortcutsController.cs +++ b/ElectronNET.WebApp/Controllers/ShortcutsController.cs @@ -22,7 +22,7 @@ namespace ElectronNET.WebApp.Controllers await Electron.Dialog.ShowMessageBoxAsync(options); }); - Electron.App.WillQuit += () => Task.Run(() => Electron.GlobalShortcut.UnregisterAll()); + Electron.App.WillQuit += (args) => Task.Run(() => Electron.GlobalShortcut.UnregisterAll()); } return View(); diff --git a/ElectronNET.WebApp/Startup.cs b/ElectronNET.WebApp/Startup.cs index 83b01d2..b21bc17 100644 --- a/ElectronNET.WebApp/Startup.cs +++ b/ElectronNET.WebApp/Startup.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using System.Threading.Tasks; namespace ElectronNET.WebApp { @@ -61,7 +62,7 @@ namespace ElectronNET.WebApp }); browserWindow.OnReadyToShow += () => browserWindow.Show(); - browserWindow.SetTitle(Configuration["DemoTitleInSettings"] ); + browserWindow.SetTitle(Configuration["DemoTitleInSettings"]); } } } diff --git a/ElectronNET.WebApp/Views/Shortcuts/Index.cshtml b/ElectronNET.WebApp/Views/Shortcuts/Index.cshtml index c7f363d..dfc0683 100644 --- a/ElectronNET.WebApp/Views/Shortcuts/Index.cshtml +++ b/ElectronNET.WebApp/Views/Shortcuts/Index.cshtml @@ -47,7 +47,7 @@ await Electron.Dialog.ShowMessageBoxAsync(options); }); -Electron.App.WillQuit += () => Task.Run(() => Electron.GlobalShortcut.UnregisterAll()); +Electron.App.WillQuit += (args) => Task.Run(() => Electron.GlobalShortcut.UnregisterAll());

ProTip