Skip to content

Commit ca382bf

Browse files
implement PreventDefault function for stop quiting application
1 parent 531446f commit ca382bf

File tree

5 files changed

+71
-25
lines changed

5 files changed

+71
-25
lines changed

ElectronNET.API/App.cs

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,53 @@ public event Action WindowAllClosed
5959
/// Note: If application quit was initiated by autoUpdater.quitAndInstall() then before-quit is emitted after
6060
/// emitting close event on all windows and closing them.
6161
/// </summary>
62-
public event Func<Task> BeforeQuit
62+
public event Func<QuitEventArgs, Task> BeforeQuit
6363
{
6464
add
6565
{
6666
if (_beforeQuit == null)
6767
{
6868
BridgeConnector.Socket.On("app-before-quit" + GetHashCode(), async () =>
6969
{
70-
await _beforeQuit();
70+
await _beforeQuit(new QuitEventArgs());
7171

72-
if(_willQuit == null && _quitting == null)
72+
if (_preventQuit)
7373
{
74-
Exit();
74+
_preventQuit = false;
7575
}
76-
else if (_willQuit != null)
76+
else
7777
{
78-
await _willQuit();
79-
80-
if(_quitting == null)
78+
if (_willQuit == null && _quitting == null)
8179
{
8280
Exit();
83-
} else
81+
}
82+
else if (_willQuit != null)
83+
{
84+
await _willQuit(new QuitEventArgs());
85+
86+
if (_preventQuit)
87+
{
88+
_preventQuit = false;
89+
}
90+
else
91+
{
92+
if (_quitting == null)
93+
{
94+
Exit();
95+
}
96+
else
97+
{
98+
await _quitting();
99+
Exit();
100+
}
101+
}
102+
}
103+
else if (_quitting != null)
84104
{
85105
await _quitting();
86106
Exit();
87107
}
88108
}
89-
else if(_quitting != null)
90-
{
91-
await _quitting();
92-
Exit();
93-
}
94109
});
95110

96111
BridgeConnector.Socket.Emit("register-app-before-quit-event", GetHashCode());
@@ -106,32 +121,39 @@ public event Func<Task> BeforeQuit
106121
}
107122
}
108123

109-
private event Func<Task> _beforeQuit;
124+
private event Func<QuitEventArgs, Task> _beforeQuit;
110125

111126
/// <summary>
112127
/// Emitted when all windows have been closed and the application will quit.
113128
///
114129
/// See the description of the window-all-closed event for the differences between the will-quit and
115130
/// window-all-closed events.
116131
/// </summary>
117-
public event Func<Task> WillQuit
132+
public event Func<QuitEventArgs, Task> WillQuit
118133
{
119134
add
120135
{
121136
if (_willQuit == null)
122137
{
123138
BridgeConnector.Socket.On("app-will-quit" + GetHashCode(), async () =>
124139
{
125-
await _willQuit();
140+
await _willQuit(new QuitEventArgs());
126141

127-
if(_quitting == null)
142+
if (_preventQuit)
128143
{
129-
Exit();
144+
_preventQuit = false;
130145
}
131146
else
132147
{
133-
await _quitting();
134-
Exit();
148+
if (_quitting == null)
149+
{
150+
Exit();
151+
}
152+
else
153+
{
154+
await _quitting();
155+
Exit();
156+
}
135157
}
136158
});
137159

@@ -148,7 +170,7 @@ public event Func<Task> WillQuit
148170
}
149171
}
150172

151-
private event Func<Task> _willQuit;
173+
private event Func<QuitEventArgs, Task> _willQuit;
152174

153175
/// <summary>
154176
/// Emitted when the application is quitting.
@@ -1378,5 +1400,12 @@ public void DockSetIcon(string image)
13781400
{
13791401
BridgeConnector.Socket.Emit("appDockSetIcon", image);
13801402
}
1403+
1404+
internal void PreventQuit()
1405+
{
1406+
_preventQuit = true;
1407+
}
1408+
1409+
private bool _preventQuit = false;
13811410
}
13821411
}

ElectronNET.API/QuitEventArgs.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace ElectronNET.API
2+
{
3+
/// <summary>
4+
///
5+
/// </summary>
6+
public sealed class QuitEventArgs
7+
{
8+
/// <summary>
9+
/// Will prevent the default behaviour, which is terminating the application.
10+
/// </summary>
11+
public void PreventDefault()
12+
{
13+
Electron.App.PreventQuit();
14+
}
15+
}
16+
}

ElectronNET.WebApp/Controllers/ShortcutsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public IActionResult Index()
2222
await Electron.Dialog.ShowMessageBoxAsync(options);
2323
});
2424

25-
Electron.App.WillQuit += () => Task.Run(() => Electron.GlobalShortcut.UnregisterAll());
25+
Electron.App.WillQuit += (args) => Task.Run(() => Electron.GlobalShortcut.UnregisterAll());
2626
}
2727

2828
return View();

ElectronNET.WebApp/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Extensions.Configuration;
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Logging;
8+
using System.Threading.Tasks;
89

910
namespace ElectronNET.WebApp
1011
{
@@ -61,7 +62,7 @@ public async void ElectronBootstrap()
6162
});
6263

6364
browserWindow.OnReadyToShow += () => browserWindow.Show();
64-
browserWindow.SetTitle(Configuration["DemoTitleInSettings"] );
65+
browserWindow.SetTitle(Configuration["DemoTitleInSettings"]);
6566
}
6667
}
6768
}

ElectronNET.WebApp/Views/Shortcuts/Index.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
await Electron.Dialog.ShowMessageBoxAsync(options);
4848
});
4949

50-
Electron.App.WillQuit += () => Task.Run(() => Electron.GlobalShortcut.UnregisterAll());</code></pre>
50+
Electron.App.WillQuit += (args) => Task.Run(() => Electron.GlobalShortcut.UnregisterAll());</code></pre>
5151

5252
<div class="demo-protip">
5353
<h2>ProTip</h2>

0 commit comments

Comments
 (0)