@@ -186,6 +186,28 @@ impl Persistence {
186
186
}
187
187
188
188
// ----------------------------------------------------------------------------
189
+ #[ derive( PartialEq , Clone , Copy ) ]
190
+ pub enum EpiQuit {
191
+ None ,
192
+ Recoverable ,
193
+ Unrecoverable ,
194
+ }
195
+
196
+ impl EpiQuit {
197
+ pub fn set_recoverable ( & mut self ) {
198
+ // Prevent setting an unrecoverable quit as recoverable
199
+ if * self != Self :: Unrecoverable {
200
+ * self = EpiQuit :: Recoverable ;
201
+ }
202
+ }
203
+
204
+ /// Tries to abort the quit if possible
205
+ pub fn set_aborted ( & mut self ) {
206
+ if * self != Self :: Unrecoverable {
207
+ * self = EpiQuit :: None ;
208
+ }
209
+ }
210
+ }
189
211
190
212
/// Everything needed to make a winit-based integration for [`epi`].
191
213
pub struct EpiIntegration {
@@ -195,7 +217,7 @@ pub struct EpiIntegration {
195
217
egui_winit : crate :: State ,
196
218
pub app : Box < dyn epi:: App > ,
197
219
/// When set, it is time to quit
198
- quit : bool ,
220
+ quit : EpiQuit ,
199
221
}
200
222
201
223
impl EpiIntegration {
@@ -228,7 +250,7 @@ impl EpiIntegration {
228
250
egui_ctx,
229
251
egui_winit : crate :: State :: new ( window) ,
230
252
app,
231
- quit : false ,
253
+ quit : EpiQuit :: None ,
232
254
} ;
233
255
234
256
slf. setup ( window) ;
@@ -243,7 +265,11 @@ impl EpiIntegration {
243
265
self . app
244
266
. setup ( & self . egui_ctx , & self . frame , self . persistence . storage ( ) ) ;
245
267
let app_output = self . frame . take_app_output ( ) ;
246
- self . quit |= app_output. quit ;
268
+
269
+ if app_output. quit {
270
+ self . quit . set_recoverable ( ) ;
271
+ }
272
+
247
273
let tex_alloc_data =
248
274
crate :: epi:: handle_app_output ( window, self . egui_ctx . pixels_per_point ( ) , app_output) ;
249
275
self . frame . lock ( ) . output . tex_allocation_data = tex_alloc_data; // Do it later
@@ -259,13 +285,18 @@ impl EpiIntegration {
259
285
}
260
286
261
287
/// If `true`, it is time to shut down.
262
- pub fn should_quit ( & self ) -> bool {
288
+ pub fn should_quit ( & self ) -> EpiQuit {
263
289
self . quit
264
290
}
265
291
266
292
pub fn on_event ( & mut self , event : & winit:: event:: WindowEvent < ' _ > ) {
267
293
use winit:: event:: WindowEvent ;
268
- self . quit |= matches ! ( event, WindowEvent :: CloseRequested | WindowEvent :: Destroyed ) ;
294
+ if * event == WindowEvent :: CloseRequested {
295
+ self . quit . set_recoverable ( ) ;
296
+ } else if * event == WindowEvent :: Destroyed {
297
+ self . quit = EpiQuit :: Unrecoverable ;
298
+ }
299
+
269
300
self . egui_winit . on_event ( & self . egui_ctx , event) ;
270
301
}
271
302
@@ -290,7 +321,11 @@ impl EpiIntegration {
290
321
. handle_output ( window, & self . egui_ctx , egui_output) ;
291
322
292
323
let app_output = self . frame . take_app_output ( ) ;
293
- self . quit |= app_output. quit ;
324
+
325
+ if app_output. quit {
326
+ self . quit . set_recoverable ( ) ;
327
+ }
328
+
294
329
let tex_allocation_data =
295
330
crate :: epi:: handle_app_output ( window, self . egui_ctx . pixels_per_point ( ) , app_output) ;
296
331
@@ -305,6 +340,15 @@ impl EpiIntegration {
305
340
. maybe_autosave ( & mut * self . app , & self . egui_ctx , window) ;
306
341
}
307
342
343
+ pub fn on_pre_exit ( & mut self ) -> bool {
344
+ let should_exit = self . app . on_pre_exit ( ) ;
345
+ if !should_exit {
346
+ self . quit . set_aborted ( ) ;
347
+ }
348
+
349
+ should_exit
350
+ }
351
+
308
352
pub fn on_exit ( & mut self , window : & winit:: window:: Window ) {
309
353
self . app . on_exit ( ) ;
310
354
self . persistence
0 commit comments