@@ -2,9 +2,7 @@ use super::{glow_wrapping::WrappedGlowPainter, *};
2
2
3
3
use crate :: epi;
4
4
5
- use egui:: mutex:: { Mutex , MutexGuard } ;
6
5
use egui:: TexturesDelta ;
7
-
8
6
pub use egui:: { pos2, Color32 } ;
9
7
10
8
// ----------------------------------------------------------------------------
@@ -36,38 +34,24 @@ impl WebInput {
36
34
37
35
use std:: sync:: atomic:: Ordering :: SeqCst ;
38
36
39
- /// Stores when to do the next repaint.
40
- pub struct NeedRepaint ( Mutex < f64 > ) ;
37
+ pub struct NeedRepaint ( std:: sync:: atomic:: AtomicBool ) ;
41
38
42
39
impl Default for NeedRepaint {
43
40
fn default ( ) -> Self {
44
- Self ( Mutex :: new ( f64 :: NEG_INFINITY ) ) // start with a repaint
41
+ Self ( true . into ( ) )
45
42
}
46
43
}
47
44
48
45
impl NeedRepaint {
49
- /// Returns the time (in [`now_sec`] scale) when
50
- /// we should next repaint.
51
- pub fn when_to_repaint ( & self ) -> f64 {
52
- * self . 0 . lock ( )
53
- }
54
-
55
- /// Unschedule repainting.
56
- pub fn clear ( & self ) {
57
- * self . 0 . lock ( ) = f64:: INFINITY ;
46
+ pub fn fetch_and_clear ( & self ) -> bool {
47
+ self . 0 . swap ( false , SeqCst )
58
48
}
59
49
60
- pub fn repaint_after ( & self , num_seconds : f64 ) {
61
- let mut repaint_time = self . 0 . lock ( ) ;
62
- * repaint_time = repaint_time. min ( now_sec ( ) + num_seconds) ;
63
- }
64
-
65
- pub fn repaint_asap ( & self ) {
66
- * self . 0 . lock ( ) = f64:: NEG_INFINITY ;
50
+ pub fn set_true ( & self ) {
51
+ self . 0 . store ( true , SeqCst ) ;
67
52
}
68
53
}
69
54
70
-
71
55
pub struct IsDestroyed ( std:: sync:: atomic:: AtomicBool ) ;
72
56
73
57
impl Default for IsDestroyed {
@@ -238,7 +222,7 @@ impl AppRunner {
238
222
{
239
223
let needs_repaint = needs_repaint. clone ( ) ;
240
224
egui_ctx. set_request_repaint_callback ( move || {
241
- needs_repaint. repaint_asap ( ) ;
225
+ needs_repaint. 0 . store ( true , SeqCst ) ;
242
226
} ) ;
243
227
}
244
228
@@ -297,15 +281,16 @@ impl AppRunner {
297
281
Ok ( ( ) )
298
282
}
299
283
300
- pub fn destroy ( & mut self ) {
284
+ pub fn destroy ( & mut self ) -> Result < ( ) , JsValue > {
301
285
tracing:: debug!( "destroying..." ) ;
302
286
303
- self . events_to_unsubscribe
304
- . drain ( .. )
305
- . for_each ( |x| x . unsubscribe ( ) ) ;
287
+ for x in self . events_to_unsubscribe . drain ( .. ) {
288
+ x . unsubscribe ( ) ? ;
289
+ }
306
290
307
291
self . painter . destroy ( ) ;
308
292
self . is_destroyed . set_true ( ) ;
293
+ Ok ( ( ) )
309
294
}
310
295
311
296
/// Returns how long to wait until the next repaint.
@@ -408,11 +393,48 @@ impl AppRunner {
408
393
409
394
pub type AppRunnerRef = Arc < Mutex < AppRunner > > ;
410
395
396
+ pub struct TargetEvent {
397
+ target : EventTarget ,
398
+ event_name : String ,
399
+ closure : Closure < dyn FnMut ( web_sys:: Event ) > ,
400
+ }
401
+
402
+ pub struct RepetitativeHandle {
403
+ pub handle : i32 ,
404
+ pub closure : Closure < dyn FnMut ( ) > ,
405
+ }
406
+
407
+ pub enum EventToUnsubscribe {
408
+ TargetEvent ( TargetEvent ) ,
409
+ RepititativeHandle ( RepetitativeHandle ) ,
410
+ }
411
+
412
+ impl EventToUnsubscribe {
413
+ pub fn unsubscribe ( self ) -> Result < ( ) , JsValue > {
414
+ use wasm_bindgen:: JsCast ;
415
+
416
+ match self {
417
+ EventToUnsubscribe :: TargetEvent ( handle) => {
418
+ handle. target . remove_event_listener_with_callback (
419
+ handle. event_name . as_str ( ) ,
420
+ handle. closure . as_ref ( ) . unchecked_ref ( ) ,
421
+ ) ?;
422
+ Ok ( ( ) )
423
+ }
424
+ EventToUnsubscribe :: RepititativeHandle ( handle) => {
425
+ let window = web_sys:: window ( ) . unwrap ( ) ;
426
+ window. clear_interval_with_handle ( handle. handle ) ;
427
+ Ok ( ( ) )
428
+ }
429
+ }
430
+ }
431
+ }
411
432
pub struct AppRunnerContainer {
412
433
pub runner : AppRunnerRef ,
413
434
/// Set to `true` if there is a panic.
414
435
/// Used to ignore callbacks after a panic.
415
436
pub panicked : Arc < AtomicBool > ,
437
+ pub events : Vec < EventToUnsubscribe > ,
416
438
}
417
439
418
440
impl AppRunnerContainer {
@@ -457,9 +479,6 @@ impl AppRunnerContainer {
457
479
458
480
self . events . push ( EventToUnsubscribe :: TargetEvent ( handle) ) ;
459
481
460
- // Bypass closure drop so that event handler can call the closure
461
- // closure.forget();
462
-
463
482
Ok ( ( ) )
464
483
}
465
484
}
0 commit comments