zng_app/
handler.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
//! Handler types and macros.

use std::any::Any;
use std::future::Future;
use std::marker::PhantomData;
use std::time::Duration;
use std::{mem, thread};

#[doc(hidden)]
pub use zng_clone_move::*;

use zng_handle::{Handle, WeakHandle};
use zng_task::{self as task, UiTask};

use crate::INSTANT;

/// Represents a handler in a widget context.
///
/// There are different flavors of handlers, you can use macros to declare then.
/// See [`hn!`], [`hn_once!`] or [`async_hn!`], [`async_hn_once!`] to start.
#[diagnostic::on_unimplemented(
    note = "use `hn!(|args: &{A}| {{ }})` to declare a widget handler from a `FnMut` closure",
    note = "use `hn_once!`, `async_hn!` or `async_hn_once!` for other closure types"
)]
pub trait WidgetHandler<A: Clone + 'static>: Any + Send {
    /// Called every time the handler's event happens in the widget context.
    ///
    /// Returns `true` when the event handler is async and it has not finished handling the event.
    ///
    /// [`update`]: WidgetHandler::update
    /// [`info`]: crate::widget::node::UiNode::info
    /// [`subscribe`]: WidgetHandler::subscribe
    fn event(&mut self, args: &A) -> bool;

    /// Called every widget update.
    ///
    /// Returns `false` when all pending async tasks are completed. Note that event properties
    /// will call this method every update even if it is returning `false`.
    ///
    /// [`update`]: WidgetHandler::update
    fn update(&mut self) -> bool {
        false
    }

    /// Box the handler.
    ///
    /// The type `Box<dyn WidgetHandler<A>>` implements `WidgetHandler<A>` and just returns itself
    /// in this method, avoiding double boxing.
    fn boxed(self) -> Box<dyn WidgetHandler<A>>
    where
        Self: Sized,
    {
        Box::new(self)
    }
    /// Boxes the handler if the `feature = "dyn_closure"` is active, otherwise retains the same handler type.    
    #[cfg(feature = "dyn_closure")]
    fn cfg_boxed(self) -> Box<dyn WidgetHandler<A>>
    where
        Self: Sized,
    {
        self.boxed()
    }
    /// Boxes the handler if the `feature = "dyn_closure"` is active, otherwise retains the same handler type.
    #[cfg(not(feature = "dyn_closure"))]
    fn cfg_boxed(self) -> Self
    where
        Self: Sized,
    {
        self
    }
}
impl<A: Clone + 'static> WidgetHandler<A> for Box<dyn WidgetHandler<A>> {
    fn event(&mut self, args: &A) -> bool {
        self.as_mut().event(args)
    }

    fn update(&mut self) -> bool {
        self.as_mut().update()
    }

    fn boxed(self) -> Box<dyn WidgetHandler<A>>
    where
        Self: Sized,
    {
        self
    }
}

#[doc(hidden)]
pub struct FnMutWidgetHandler<H> {
    handler: H,
}
impl<A, H> WidgetHandler<A> for FnMutWidgetHandler<H>
where
    A: Clone + 'static,
    H: FnMut(&A) + Send + 'static,
{
    fn event(&mut self, args: &A) -> bool {
        (self.handler)(args);
        false
    }
}

#[doc(hidden)]
#[cfg(not(feature = "dyn_closure"))]
pub fn hn<A, H>(handler: H) -> FnMutWidgetHandler<H>
where
    A: Clone + 'static,
    H: FnMut(&A) + Send + 'static,
{
    FnMutWidgetHandler { handler }
}
#[doc(hidden)]
#[cfg(feature = "dyn_closure")]
pub fn hn<A, H>(handler: H) -> FnMutWidgetHandler<Box<dyn FnMut(&A) + Send>>
where
    A: Clone + 'static,
    H: FnMut(&A) + Send + 'static,
{
    FnMutWidgetHandler {
        handler: Box::new(handler),
    }
}

///<span data-del-macro-root></span> Declare a mutable *clone-move* event handler.
///
/// The macro input is a closure with optional *clone-move* variables, internally it uses [`clmv!`] so
/// the input is the same syntax.
///
/// # Examples
///
/// The example declares an event handler for the `on_click` property.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # use zng_app::handler::hn;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() -> impl zng_app::handler::WidgetHandler<ClickArgs> {
/// # let
/// on_click = hn!(|_| {
///     println!("Clicked!");
/// });
/// # on_click }
/// ```
///
/// The closure input is `&ClickArgs` for this property. Note that
/// if you want to use the event args you must annotate the input type, the context type is inferred.
///
/// ```
/// # #[derive(Clone)] pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize }
/// # use zng_app::handler::hn;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() -> impl zng_app::handler::WidgetHandler<ClickArgs> {
/// # let
/// on_click = hn!(|args: &ClickArgs| {
///     println!("Clicked {}!", args.click_count);
/// });
/// # on_click }
/// ```
///
/// Internally the [`clmv!`] macro is used so you can *clone-move* variables into the handler.
///
/// ```
/// # #[derive(Clone)] pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize }
/// # use zng_txt::formatx;
/// # use zng_var::{var, Var};
/// # use zng_app::handler::hn;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() -> impl zng_app::handler::WidgetHandler<ClickArgs> {
/// let foo = var(0);
///
/// // ..
///
/// # let
/// on_click = hn!(foo, |args: &ClickArgs| {
///     foo.set(args.click_count);
/// });
///
/// // can still use after:
/// let bar = foo.map(|c| formatx!("click_count: {c}"));
///
/// # on_click }
/// ```
///
/// In the example above only a clone of `foo` is moved into the handler. Note that handlers always capture by move, if `foo` was not
/// listed in the *clone-move* section it would not be available after the handler is created. See [`clmv!`] for details.
///
/// [`clmv!`]: zng_clone_move::clmv
#[macro_export]
macro_rules! hn {
    ($($tt:tt)+) => {
        $crate::handler::hn($crate::handler::clmv!{ $($tt)+ })
    }
}
#[doc(inline)]
pub use crate::hn;
use crate::{AppControlFlow, HeadlessApp};

#[doc(hidden)]
pub struct FnOnceWidgetHandler<H> {
    handler: Option<H>,
}
impl<A, H> WidgetHandler<A> for FnOnceWidgetHandler<H>
where
    A: Clone + 'static,
    H: FnOnce(&A) + Send + 'static,
{
    fn event(&mut self, args: &A) -> bool {
        if let Some(handler) = self.handler.take() {
            handler(args);
        }
        false
    }
}
#[doc(hidden)]
#[cfg(not(feature = "dyn_closure"))]
pub fn hn_once<A, H>(handler: H) -> FnOnceWidgetHandler<H>
where
    A: Clone + 'static,
    H: FnOnce(&A) + Send + 'static,
{
    FnOnceWidgetHandler { handler: Some(handler) }
}
#[doc(hidden)]
#[cfg(feature = "dyn_closure")]
pub fn hn_once<A, H>(handler: H) -> FnOnceWidgetHandler<Box<dyn FnOnce(&A) + Send>>
where
    A: Clone + 'static,
    H: FnOnce(&A) + Send + 'static,
{
    FnOnceWidgetHandler {
        handler: Some(Box::new(handler)),
    }
}

///<span data-del-macro-root></span> Declare a *clone-move* event handler that is only called once.
///
/// The macro input is a closure with optional *clone-move* variables, internally it uses [`clmv!`] so
/// the input is the same syntax.
///
/// # Examples
///
/// The example captures `data` by move and then destroys it in the first call, this cannot be done using [`hn!`] because
/// the `data` needs to be available for all event calls. In this case the closure is only called once, subsequent events
/// are ignored by the handler.
///
/// ```
/// # use zng_app::handler::hn_once;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() -> impl zng_app::handler::WidgetHandler<()> {
/// let data = vec![1, 2, 3];
/// # let
/// on_click = hn_once!(|_| {
///     for i in data {
///         print!("{i}, ");
///     }
/// });
/// # on_click }
/// ```
///
/// Other then declaring a `FnOnce` this macro behaves like [`hn!`], so the same considerations apply. You can *clone-move* variables,
/// the type of the input is the event arguments and must be annotated.
///
/// ```
/// # use zng_app::handler::hn_once;
/// # let _scope = zng_app::APP.minimal();
/// # #[derive(Clone)]
/// # pub struct ClickArgs { click_count: usize }
/// # fn assert_type() -> impl zng_app::handler::WidgetHandler<ClickArgs> {
/// let data = vec![1, 2, 3];
/// # let
/// on_click = hn_once!(data, |args: &ClickArgs| {
///     drop(data);
/// });
///
/// println!("{data:?}");
/// # on_click }
/// ```
///
/// [`clmv!`]: zng_clone_move::clmv
#[macro_export]
macro_rules! hn_once {
    ($($tt:tt)+) => {
        $crate::handler::hn_once($crate::handler::clmv! { $($tt)+ })
    }
}
#[doc(inline)]
pub use crate::hn_once;

#[doc(hidden)]
pub struct AsyncFnMutWidgetHandler<H> {
    handler: H,
    tasks: Vec<UiTask<()>>,
}
impl<A, F, H> WidgetHandler<A> for AsyncFnMutWidgetHandler<H>
where
    A: Clone + 'static,
    F: Future<Output = ()> + Send + 'static,
    H: FnMut(A) -> F + Send + 'static,
{
    fn event(&mut self, args: &A) -> bool {
        let handler = &mut self.handler;
        let mut task = UiTask::new(Some(WIDGET.id()), handler(args.clone()));
        let need_update = task.update().is_none();
        if need_update {
            self.tasks.push(task);
        }
        need_update
    }

    fn update(&mut self) -> bool {
        self.tasks.retain_mut(|t| t.update().is_none());
        !self.tasks.is_empty()
    }
}
#[doc(hidden)]
#[cfg(not(feature = "dyn_closure"))]
pub fn async_hn<A, F, H>(handler: H) -> AsyncFnMutWidgetHandler<H>
where
    A: Clone + 'static,
    F: Future<Output = ()> + Send + 'static,
    H: FnMut(A) -> F + Send + 'static,
{
    AsyncFnMutWidgetHandler { handler, tasks: vec![] }
}

#[cfg(feature = "dyn_closure")]
type BoxedAsyncHn<A> = Box<dyn FnMut(A) -> std::pin::Pin<Box<dyn Future<Output = ()> + Send>> + Send>;

#[doc(hidden)]
#[cfg(feature = "dyn_closure")]
pub fn async_hn<A, F, H>(mut handler: H) -> AsyncFnMutWidgetHandler<BoxedAsyncHn<A>>
where
    A: Clone + 'static,
    F: Future<Output = ()> + Send + 'static,
    H: FnMut(A) -> F + Send + 'static,
{
    AsyncFnMutWidgetHandler {
        handler: Box::new(move |args| Box::pin(handler(args))),
        tasks: vec![],
    }
}

///<span data-del-macro-root></span> Declare an async *clone-move* event handler.
///
/// The macro input is a closure with optional *clone-move* variables, internally it uses [`async_clmv_fn!`] so
/// the input is the same syntax.
///
/// # Examples
///
/// The example declares an async event handler for the `on_click` property.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # use zng_app::handler::async_hn;
/// # use zng_task as task;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() -> impl zng_app::handler::WidgetHandler<ClickArgs> {
/// # let
/// on_click = async_hn!(|_| {
///     println!("Clicked!");
///
///     task::run(async {
///         println!("In other thread!");
///     }).await;
///
///     println!("Back in UI thread, in a widget update.");
/// });
/// # on_click }
/// ```
///
/// The closure input is `ClickArgs` for this property. Note that
/// if you want to use the event args you must annotate the input type.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # use zng_app::handler::async_hn;
/// # use zng_app::widget::WIDGET;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() -> impl zng_app::handler::WidgetHandler<ClickArgs> {
/// # let
/// on_click = async_hn!(|args: ClickArgs| {
///     println!("Clicked {} {} times!", WIDGET.id(), args.click_count);
///     
/// });
/// # on_click }
/// ```
///
/// Internally the [`async_clmv_fn!`] macro is used so you can *clone-move* variables into the handler.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # use zng_app::handler::async_hn;
/// # use zng_var::{var, Var};
/// # use zng_task as task;
/// # use zng_txt::formatx;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() -> impl zng_app::handler::WidgetHandler<ClickArgs> {
/// let enabled = var(true);
///
/// // ..
///
/// # let
/// on_click = async_hn!(enabled, |args: ClickArgs| {
///     enabled.set(false);
///
///     task::run(async move {
///         println!("do something {}", args.click_count);
///     }).await;
///
///     enabled.set(true);
/// });
///
/// // can still use after:
/// # let
/// text = enabled.map(|&e| if e { "Click Me!" } else { "Busy.." });
/// enabled;
///
/// # on_click }
/// ```
///
/// In the example above only a clone of `enabled` is moved into the handler. Note that handlers always capture by move, if `enabled` was not
/// listed in the *clone-move* section it would not be available after the handler is created. See [`async_clmv_fn!`] for details.
///
/// The example also demonstrates a common pattern with async handlers, most events are only raised when the widget is enabled, so you can
/// disable the widget while the async task is running. This way you don't block the UI running a task but the user cannot spawn a second
/// task while the first is still running.
///
/// ## Futures and Clone-Move
///
/// You may want to always *clone-move* captures for async handlers, because they then automatically get cloned again for each event. This
/// needs to happen because you can have more then one *handler task* running at the same type, and both want access to the captured variables.
///
/// This second cloning can be avoided by using the [`async_hn_once!`] macro instead, but only if you expect a single event.
///
/// [`async_clmv_fn!`]: zng_clone_move::async_clmv_fn
#[macro_export]
macro_rules! async_hn {
    ($($tt:tt)+) => {
        $crate::handler::async_hn($crate::handler::async_clmv_fn! { $($tt)+ })
    }
}
#[doc(inline)]
pub use crate::async_hn;

enum AsyncFnOnceWhState<H> {
    NotCalled(H),
    Pending(UiTask<()>),
    Done,
}
#[doc(hidden)]
pub struct AsyncFnOnceWidgetHandler<H> {
    state: AsyncFnOnceWhState<H>,
}
impl<A, F, H> WidgetHandler<A> for AsyncFnOnceWidgetHandler<H>
where
    A: Clone + 'static,
    F: Future<Output = ()> + Send + 'static,
    H: FnOnce(A) -> F + Send + 'static,
{
    fn event(&mut self, args: &A) -> bool {
        match mem::replace(&mut self.state, AsyncFnOnceWhState::Done) {
            AsyncFnOnceWhState::NotCalled(handler) => {
                let mut task = UiTask::new(Some(WIDGET.id()), handler(args.clone()));
                let is_pending = task.update().is_none();
                if is_pending {
                    self.state = AsyncFnOnceWhState::Pending(task);
                }
                is_pending
            }
            AsyncFnOnceWhState::Pending(t) => {
                self.state = AsyncFnOnceWhState::Pending(t);
                false
            }
            AsyncFnOnceWhState::Done => false,
        }
    }

    fn update(&mut self) -> bool {
        let mut is_pending = false;
        if let AsyncFnOnceWhState::Pending(t) = &mut self.state {
            is_pending = t.update().is_none();
            if !is_pending {
                self.state = AsyncFnOnceWhState::Done;
            }
        }
        is_pending
    }
}
#[doc(hidden)]
#[cfg(not(feature = "dyn_closure"))]
pub fn async_hn_once<A, F, H>(handler: H) -> AsyncFnOnceWidgetHandler<H>
where
    A: Clone + 'static,
    F: Future<Output = ()> + Send + 'static,
    H: FnOnce(A) -> F + Send + 'static,
{
    AsyncFnOnceWidgetHandler {
        state: AsyncFnOnceWhState::NotCalled(handler),
    }
}

#[cfg(feature = "dyn_closure")]
type BoxedAsyncHnOnce<A> = Box<dyn FnOnce(A) -> std::pin::Pin<Box<dyn Future<Output = ()> + Send>> + Send>;

#[doc(hidden)]
#[cfg(feature = "dyn_closure")]
pub fn async_hn_once<A, F, H>(handler: H) -> AsyncFnOnceWidgetHandler<BoxedAsyncHnOnce<A>>
where
    A: Clone + 'static,
    F: Future<Output = ()> + Send + 'static,
    H: FnOnce(A) -> F + Send + 'static,
{
    AsyncFnOnceWidgetHandler {
        state: AsyncFnOnceWhState::NotCalled(Box::new(move |args| Box::pin(handler(args)))),
    }
}

///<span data-del-macro-root></span> Declare an async *clone-move* event handler that is only called once.
///
/// The macro input is a closure with optional *clone-move* variables, internally it uses [`async_clmv_fn_once!`] so
/// the input is the same syntax.
///
/// # Examples
///
/// The example captures `data` by move and then moves it again to another thread. This is not something you can do using [`async_hn!`]
/// because that handler expects to be called many times. We expect `on_open` to only be called once, so we can don't need to capture by
/// *clone-move* here just to use `data`.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # use zng_app::handler::async_hn_once;
/// # use zng_task as task;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() -> impl zng_app::handler::WidgetHandler<ClickArgs> {
/// let data = vec![1, 2, 3];
/// # let
/// on_open = async_hn_once!(|_| {
///     task::run(async move {
///          for i in data {
///              print!("{i}, ");
///          }    
///     }).await;
///
///     println!("Done!");
/// });
/// # on_open }
/// ```
///
/// You can still *clone-move* to have access to the variable after creating the handler, in this case the `data` will be cloned into the handler
/// but will just be moved to the other thread, avoiding a needless clone.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # use zng_app::handler::async_hn_once;
/// # use zng_task as task;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() -> impl zng_app::handler::WidgetHandler<ClickArgs> {
/// let data = vec![1, 2, 3];
/// # let
/// on_open = async_hn_once!(data, |_| {
///     task::run(async move {
///          for i in data {
///              print!("{i}, ");
///          }    
///     }).await;
///
///     println!("Done!");
/// });
/// println!("{data:?}");
/// # on_open }
/// ```
///
/// [`async_clmv_fn_once!`]: zng_clone_move::async_clmv_fn_once
#[macro_export]
macro_rules! async_hn_once {
    ($($tt:tt)+) => {
        $crate::handler::async_hn_once($crate::handler::async_clmv_fn_once! { $($tt)+ })
    }
}
#[doc(inline)]
pub use crate::async_hn_once;

/// Represents a weak handle to an [`AppHandler`] subscription.
pub trait AppWeakHandle: Send {
    /// Dynamic clone.
    fn clone_boxed(&self) -> Box<dyn AppWeakHandle>;

    /// Unsubscribes the [`AppHandler`].
    ///
    /// This stops the handler from being called again and causes it to be dropped in a future app update.
    fn unsubscribe(&self);
}
impl<D: Send + Sync + 'static> AppWeakHandle for WeakHandle<D> {
    fn clone_boxed(&self) -> Box<dyn AppWeakHandle> {
        Box::new(self.clone())
    }

    fn unsubscribe(&self) {
        if let Some(handle) = self.upgrade() {
            handle.force_drop();
        }
    }
}

/// Arguments for a call of [`AppHandler::event`].
pub struct AppHandlerArgs<'a> {
    /// Handle to the [`AppHandler`] subscription.
    pub handle: &'a dyn AppWeakHandle,
    /// If the handler is invoked in a *preview* context.
    pub is_preview: bool,
}

/// Represents an event handler in the app context.
///
/// There are different flavors of handlers, you can use macros to declare then.
/// See [`app_hn!`], [`app_hn_once!`] or [`async_app_hn!`], [`async_app_hn_once!`] to start.
#[diagnostic::on_unimplemented(
    note = "use `app_hn!(|args: &{A}, _| {{ }})` to declare an app handler closure",
    note = "use `app_hn_once!`, `async_app_hn!` or `async_app_hn_once!` for other closure types"
)]
pub trait AppHandler<A: Clone + 'static>: Any + Send {
    /// Called every time the event happens.
    ///
    /// The `handler_args` can be used to unsubscribe the handler. Async handlers are expected to schedule
    /// their tasks to run somewhere in the app, usually in the [`UPDATES.on_update`]. The `handle` is
    /// **not** expected to cancel running async tasks, only to drop `self` before the next event happens.
    ///
    /// [`UPDATES.on_update`]: crate::update::UPDATES::on_update
    fn event(&mut self, args: &A, handler_args: &AppHandlerArgs);

    /// Boxes the handler.
    ///
    /// The type `Box<dyn AppHandler<A>>` implements `AppHandler<A>` and just returns itself
    /// in this method, avoiding double boxing.
    fn boxed(self) -> Box<dyn AppHandler<A>>
    where
        Self: Sized,
    {
        Box::new(self)
    }

    /// Boxes the handler if the `feature = "dyn_closure"` is enabled, otherwise maintain the same type.
    #[cfg(feature = "dyn_closure")]
    fn cfg_boxed(self) -> Box<dyn AppHandler<A>>
    where
        Self: Sized,
    {
        self.boxed()
    }

    /// Boxes the handler if the `feature = "dyn_closure"` is enabled, otherwise maintain the same type.    
    #[cfg(not(feature = "dyn_closure"))]
    fn cfg_boxed(self) -> Self
    where
        Self: Sized,
    {
        self
    }
}
impl<A: Clone + 'static> AppHandler<A> for Box<dyn AppHandler<A>> {
    fn event(&mut self, args: &A, handler_args: &AppHandlerArgs) {
        self.as_mut().event(args, handler_args)
    }

    fn boxed(self) -> Box<dyn AppHandler<A>> {
        self
    }
}

#[doc(hidden)]
pub struct FnMutAppHandler<H> {
    handler: H,
}
impl<A, H> AppHandler<A> for FnMutAppHandler<H>
where
    A: Clone + 'static,
    H: FnMut(&A, &dyn AppWeakHandle) + Send + 'static,
{
    fn event(&mut self, args: &A, handler_args: &AppHandlerArgs) {
        (self.handler)(args, handler_args.handle);
    }
}
#[doc(hidden)]
#[cfg(not(feature = "dyn_closure"))]
pub fn app_hn<A, H>(handler: H) -> FnMutAppHandler<H>
where
    A: Clone + 'static,
    H: FnMut(&A, &dyn AppWeakHandle) + Send + 'static,
{
    FnMutAppHandler { handler }
}

#[cfg(feature = "dyn_closure")]
type BoxedAppHn<A> = Box<dyn FnMut(&A, &dyn AppWeakHandle) + Send>;

#[doc(hidden)]
#[cfg(feature = "dyn_closure")]
pub fn app_hn<A, H>(handler: H) -> FnMutAppHandler<BoxedAppHn<A>>
where
    A: Clone + 'static,
    H: FnMut(&A, &dyn AppWeakHandle) + Send + 'static,
{
    FnMutAppHandler {
        handler: Box::new(handler),
    }
}

///<span data-del-macro-root></span> Declare a mutable *clone-move* app event handler.
///
/// The macro input is a closure with optional *clone-move* variables, internally it uses [`clmv!`] so
/// the input is the same syntax.
///
/// # Examples
///
/// The example declares an event handler for the `CLICK_EVENT`.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # zng_app::event::event! { pub static CLICK_EVENT: ClickArgs; }
/// # use zng_app::handler::app_hn;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() {
/// CLICK_EVENT.on_event(app_hn!(|_, _| {
///     println!("Clicked Somewhere!");
/// })).perm();
/// # }
/// ```
///
/// The closure input is `&A, &dyn AppWeakHandle` with `&A` equaling `&ClickArgs` for this event. Note that
/// if you want to use the event args you must annotate the input type, the context and handle type is inferred.
///
/// The handle can be used to unsubscribe the event handler, if [`unsubscribe`](AppWeakHandle::unsubscribe) is called the handler
/// will be dropped some time before the next event update.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # zng_app::event::event! { pub static CLICK_EVENT: ClickArgs; }
/// # use zng_app::handler::app_hn;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() {
/// CLICK_EVENT.on_event(app_hn!(|args: &ClickArgs, handle| {
///     println!("Clicked {}!", args.target);
///     handle.unsubscribe();
/// })).perm();
/// # }
/// ```
///
/// Internally the [`clmv!`] macro is used so you can *clone-move* variables into the handler.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # zng_app::event::event! { pub static CLICK_EVENT: ClickArgs; }
/// # use zng_txt::{formatx, ToTxt};
/// # use zng_var::{var, Var};
/// # use zng_app::handler::app_hn;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() {
/// let foo = var("".to_txt());
///
/// CLICK_EVENT.on_event(app_hn!(foo, |args: &ClickArgs, _| {
///     foo.set(args.target.to_txt());
/// })).perm();
///
/// // can still use after:
/// let bar = foo.map(|c| formatx!("last click: {c}"));
///
/// # }
/// ```
///
/// In the example above only a clone of `foo` is moved into the handler. Note that handlers always capture by move, if `foo` was not
/// listed in the *clone-move* section it would not be available after the handler is created. See [`clmv!`] for details.
///
/// [`clmv!`]: zng_clone_move::clmv
#[macro_export]
macro_rules! app_hn {
    ($($tt:tt)+) => {
        $crate::handler::app_hn($crate::handler::clmv!{ $($tt)+ })
    }
}
#[doc(inline)]
pub use crate::app_hn;

#[doc(hidden)]
pub struct FnOnceAppHandler<H> {
    handler: Option<H>,
}
impl<A, H> AppHandler<A> for FnOnceAppHandler<H>
where
    A: Clone + 'static,
    H: FnOnce(&A) + Send + 'static,
{
    fn event(&mut self, args: &A, handler_args: &AppHandlerArgs) {
        if let Some(handler) = self.handler.take() {
            handler(args);
            handler_args.handle.unsubscribe();
        } else {
            tracing::error!("`app_hn_once!` called after requesting unsubscribe");
        }
    }
}
#[doc(hidden)]
#[cfg(not(feature = "dyn_closure"))]
pub fn app_hn_once<A, H>(handler: H) -> FnOnceAppHandler<H>
where
    A: Clone + 'static,
    H: FnOnce(&A) + Send + 'static,
{
    FnOnceAppHandler { handler: Some(handler) }
}
#[doc(hidden)]
#[cfg(feature = "dyn_closure")]
pub fn app_hn_once<A, H>(handler: H) -> FnOnceAppHandler<Box<dyn FnOnce(&A) + Send>>
where
    A: Clone + 'static,
    H: FnOnce(&A) + Send + 'static,
{
    FnOnceAppHandler {
        handler: Some(Box::new(handler)),
    }
}

///<span data-del-macro-root></span> Declare a *clone-move* app event handler that is only called once.
///
/// The macro input is a closure with optional *clone-move* variables, internally it uses [`clmv!`] so
/// the input is the same syntax.
///
/// # Examples
///
/// The example captures `data` by move and then destroys it in the first call, this cannot be done using [`app_hn!`] because
/// the `data` needs to be available for all event calls. In this case the closure is only called once, subsequent events
/// are ignored by the handler and it automatically requests unsubscribe.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # zng_app::event::event! { pub static CLICK_EVENT: ClickArgs; }
/// # use zng_app::handler::app_hn_once;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() {
/// let data = vec![1, 2, 3];
///
/// CLICK_EVENT.on_event(app_hn_once!(|_| {
///     for i in data {
///         print!("{i}, ");
///     }
/// })).perm();
/// # }
/// ```
///
/// Other then declaring a `FnOnce` this macro behaves like [`app_hn!`], so the same considerations apply. You can *clone-move* variables,
/// the type of the input is the event arguments and must be annotated.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # zng_app::event::event! { pub static CLICK_EVENT: ClickArgs; }
/// # use zng_app::handler::app_hn_once;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() {
/// let data = vec![1, 2, 3];
///
/// CLICK_EVENT.on_event(app_hn_once!(data, |args: &ClickArgs| {
///     drop(data);
/// })).perm();
///
/// println!("{data:?}");
/// # }
/// ```
///
/// [`clmv!`]: zng_clone_move::clmv
#[macro_export]
macro_rules! app_hn_once {
    ($($tt:tt)+) => {
        $crate::handler::app_hn_once($crate::handler::clmv! { $($tt)+ })
    }
}
#[doc(inline)]
pub use crate::app_hn_once;

#[doc(hidden)]
pub struct AsyncFnMutAppHandler<H> {
    handler: H,
}
impl<A, F, H> AppHandler<A> for AsyncFnMutAppHandler<H>
where
    A: Clone + 'static,
    F: Future<Output = ()> + Send + 'static,
    H: FnMut(A, Box<dyn AppWeakHandle>) -> F + Send + 'static,
{
    fn event(&mut self, args: &A, handler_args: &AppHandlerArgs) {
        let handler = &mut self.handler;
        let mut task = UiTask::new(None, handler(args.clone(), handler_args.handle.clone_boxed()));
        if task.update().is_none() {
            if handler_args.is_preview {
                UPDATES
                    .on_pre_update(app_hn!(|_, handle| {
                        if task.update().is_some() {
                            handle.unsubscribe();
                        }
                    }))
                    .perm();
            } else {
                UPDATES
                    .on_update(app_hn!(|_, handle| {
                        if task.update().is_some() {
                            handle.unsubscribe();
                        }
                    }))
                    .perm();
            }
        }
    }
}
#[doc(hidden)]
#[cfg(not(feature = "dyn_closure"))]
pub fn async_app_hn<A, F, H>(handler: H) -> AsyncFnMutAppHandler<H>
where
    A: Clone + 'static,
    F: Future<Output = ()> + Send + 'static,
    H: FnMut(A, Box<dyn AppWeakHandle>) -> F + Send + 'static,
{
    AsyncFnMutAppHandler { handler }
}

#[cfg(feature = "dyn_closure")]
type BoxedAsyncAppHn<A> = Box<dyn FnMut(A, Box<dyn AppWeakHandle>) -> std::pin::Pin<Box<dyn Future<Output = ()> + Send>> + Send>;

#[doc(hidden)]
#[cfg(feature = "dyn_closure")]
pub fn async_app_hn<A, F, H>(mut handler: H) -> AsyncFnMutAppHandler<BoxedAsyncAppHn<A>>
where
    A: Clone + 'static,
    F: Future<Output = ()> + Send + 'static,
    H: FnMut(A, Box<dyn AppWeakHandle>) -> F + Send + 'static,
{
    AsyncFnMutAppHandler {
        handler: Box::new(move |args, handle| Box::pin(handler(args, handle))),
    }
}

///<span data-del-macro-root></span> Declare an async *clone-move* app event handler.
///
/// The macro input is a closure with optional *clone-move* variables, internally it uses [`async_clmv_fn!`] so
/// the input is the same syntax.
///
/// The handler generates a future for each event, the future is polled immediately if it does not finish it is scheduled
/// to update in [`on_pre_update`](crate::update::UPDATES::on_pre_update) or [`on_update`](crate::update::UPDATES::on_update) depending
/// on if the handler was assigned to a *preview* event or not.
///
/// Note that this means [`propagation`](crate::event::AnyEventArgs::propagation) can only be meaningfully stopped before the
/// first `.await`, after, the event has already propagated.
///
/// # Examples
///
/// The example declares an async event handler for the `CLICK_EVENT`.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # zng_app::event::event! { pub static CLICK_EVENT: ClickArgs; }
/// # use zng_app::handler::async_app_hn;
/// # use zng_task as task;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() {
/// CLICK_EVENT.on_event(async_app_hn!(|_, _| {
///     println!("Clicked Somewhere!");
///
///     task::run(async {
///         println!("In other thread!");
///     }).await;
///
///     println!("Back in UI thread, in an app update.");
/// })).perm();
/// # }
/// ```
///
/// The closure input is `A, Box<dyn AppWeakHandle>` for all handlers and `A` is `ClickArgs` for this example. Note that
/// if you want to use the event args you must annotate the input type, the context and handle types are inferred.
///
/// The handle can be used to unsubscribe the event handler, if [`unsubscribe`](AppWeakHandle::unsubscribe) is called the handler
/// will be dropped some time before the next event update. Running tasks are not canceled by unsubscribing, the only way to *cancel*
/// then is by returning early inside the async blocks.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # zng_app::event::event! { pub static CLICK_EVENT: ClickArgs; }
/// # use zng_app::handler::async_app_hn;
/// # use zng_task as task;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() {
/// CLICK_EVENT.on_event(async_app_hn!(|args: ClickArgs, handle| {
///     println!("Clicked {}!", args.target);
///     task::run(async move {
///         handle.unsubscribe();
///     });
/// })).perm();
/// # }
/// ```
///
/// Internally the [`async_clmv_fn!`] macro is used so you can *clone-move* variables into the handler.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # zng_app::event::event! { pub static CLICK_EVENT: ClickArgs; }
/// # use zng_app::handler::async_app_hn;
/// # use zng_var::{var, Var};
/// # use zng_task as task;
/// # use zng_txt::{formatx, ToTxt};
/// #
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() {
/// let status = var("pending..".to_txt());
///
/// CLICK_EVENT.on_event(async_app_hn!(status, |args: ClickArgs, _| {
///     status.set(formatx!("processing {}..", args.target));
///
///     task::run(async move {
///         println!("do something slow");
///     }).await;
///
///     status.set(formatx!("finished {}", args.target));
/// })).perm();
///
/// // can still use after:
/// let text = status;
///
/// # }
/// ```
///
/// In the example above only a clone of `status` is moved into the handler. Note that handlers always capture by move, if `status` was not
/// listed in the *clone-move* section it would not be available after the handler is created. See [`async_clmv_fn!`] for details.
///
/// ## Futures and Clone-Move
///
/// You may want to always *clone-move* captures for async handlers, because they then automatically get cloned again for each event. This
/// needs to happen because you can have more then one *handler task* running at the same type, and both want access to the captured variables.
///
/// This second cloning can be avoided by using the [`async_hn_once!`] macro instead, but only if you expect a single event.
///
/// [`async_clmv_fn!`]: zng_clone_move::async_clmv_fn
#[macro_export]
macro_rules! async_app_hn {
    ($($tt:tt)+) => {
        $crate::handler::async_app_hn($crate::handler::async_clmv_fn! { $($tt)+ })
    }
}
#[doc(inline)]
pub use crate::async_app_hn;

#[doc(hidden)]
pub struct AsyncFnOnceAppHandler<H> {
    handler: Option<H>,
}

impl<A, F, H> AppHandler<A> for AsyncFnOnceAppHandler<H>
where
    A: Clone + 'static,
    F: Future<Output = ()> + Send + 'static,
    H: FnOnce(A) -> F + Send + 'static,
{
    fn event(&mut self, args: &A, handler_args: &AppHandlerArgs) {
        if let Some(handler) = self.handler.take() {
            handler_args.handle.unsubscribe();

            let mut task = UiTask::new(None, handler(args.clone()));
            if task.update().is_none() {
                if handler_args.is_preview {
                    UPDATES
                        .on_pre_update(app_hn!(|_, handle| {
                            if task.update().is_some() {
                                handle.unsubscribe();
                            }
                        }))
                        .perm();
                } else {
                    UPDATES
                        .on_update(app_hn!(|_, handle| {
                            if task.update().is_some() {
                                handle.unsubscribe();
                            }
                        }))
                        .perm();
                }
            }
        } else {
            tracing::error!("`async_app_hn_once!` called after requesting unsubscribe");
        }
    }
}
#[doc(hidden)]
#[cfg(not(feature = "dyn_closure"))]
pub fn async_app_hn_once<A, F, H>(handler: H) -> AsyncFnOnceAppHandler<H>
where
    A: Clone + 'static,
    F: Future<Output = ()> + Send + 'static,
    H: FnOnce(A) -> F + Send + 'static,
{
    AsyncFnOnceAppHandler { handler: Some(handler) }
}

#[cfg(feature = "dyn_closure")]
type BoxedAsyncAppHnOnce<A> = Box<dyn FnOnce(A) -> std::pin::Pin<Box<dyn Future<Output = ()> + Send>> + Send>;

#[doc(hidden)]
#[cfg(feature = "dyn_closure")]
pub fn async_app_hn_once<A, F, H>(handler: H) -> AsyncFnOnceAppHandler<BoxedAsyncAppHnOnce<A>>
where
    A: Clone + 'static,
    F: Future<Output = ()> + Send + 'static,
    H: FnOnce(A) -> F + Send + 'static,
{
    AsyncFnOnceAppHandler {
        handler: Some(Box::new(move |args| Box::pin(handler(args)))),
    }
}

///<span data-del-macro-root></span> Declare an async *clone-move* app event handler that is only called once.
///
/// The macro input is a closure with optional *clone-move* variables, internally it uses [`async_clmv_fn_once!`] so
/// the input is the same syntax.
///
/// # Examples
///
/// The example captures `data` by move and then moves it again to another thread. This is not something you can do using [`async_app_hn!`]
/// because that handler expects to be called many times. We want to handle `CLICK_EVENT` once in this example, so we can don't need
/// to capture by *clone-move* just to use `data`.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # use zng_app::handler::async_hn_once;
/// # use zng_task as task;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() -> impl zng_app::handler::WidgetHandler<ClickArgs> {
/// let data = vec![1, 2, 3];
/// # let
/// on_open = async_hn_once!(|_| {
///     task::run(async move {
///          for i in data {
///              print!("{i}, ");
///          }    
///     }).await;
///
///     println!("Done!");
/// });
/// # on_open }
/// ```
///
/// You can still *clone-move* to have access to the variable after creating the handler, in this case the `data` will be cloned into the handler
/// but will just be moved to the other thread, avoiding a needless clone.
///
/// ```
/// # zng_app::event::event_args! { pub struct ClickArgs { pub target: zng_txt::Txt, pub click_count: usize, .. fn delivery_list(&self, _l: &mut UpdateDeliveryList) { } } }
/// # use zng_app::handler::async_hn_once;
/// # use zng_task as task;
/// # let _scope = zng_app::APP.minimal();
/// # fn assert_type() -> impl zng_app::handler::WidgetHandler<ClickArgs> {
/// let data = vec![1, 2, 3];
/// # let
/// on_open = async_hn_once!(data, |_| {
///     task::run(async move {
///          for i in data {
///              print!("{i}, ");
///          }    
///     }).await;
///
///     println!("Done!");
/// });
/// println!("{data:?}");
/// # on_open }
/// ```
///
/// [`async_clmv_fn_once!`]: zng_clone_move::async_clmv_fn_once
#[macro_export]
macro_rules! async_app_hn_once {
    ($($tt:tt)+) => {
        $crate::handler::async_app_hn_once($crate::handler::async_clmv_fn_once! { $($tt)+ })
    }
}
#[doc(inline)]
pub use crate::async_app_hn_once;
use crate::update::UPDATES;
use crate::widget::{UiTaskWidget, WIDGET};

/// Widget handler wrapper that filters the events, only delegating to `self` when `filter` returns `true`.
pub struct FilterWidgetHandler<A, H, F> {
    _args: PhantomData<fn() -> A>,
    handler: H,
    filter: F,
}
impl<A, H, F> FilterWidgetHandler<A, H, F>
where
    A: Clone + 'static,
    H: WidgetHandler<A>,
    F: FnMut(&A) -> bool + Send + 'static,
{
    /// New filter handler.
    pub fn new(handler: H, filter: F) -> Self {
        Self {
            handler,
            filter,
            _args: PhantomData,
        }
    }
}
impl<A, H, F> WidgetHandler<A> for FilterWidgetHandler<A, H, F>
where
    A: Clone + 'static,
    H: WidgetHandler<A>,
    F: FnMut(&A) -> bool + Send + 'static,
{
    fn event(&mut self, args: &A) -> bool {
        if (self.filter)(args) {
            self.handler.event(args)
        } else {
            false
        }
    }

    fn update(&mut self) -> bool {
        self.handler.update()
    }
}

/// App handler wrapper that filters the events, only delegating to `self` when `filter` returns `true`.
pub struct FilterAppHandler<A, H, F> {
    _args: PhantomData<fn() -> A>,
    handler: H,
    filter: F,
}
impl<A, H, F> FilterAppHandler<A, H, F>
where
    A: Clone + 'static,
    H: AppHandler<A>,
    F: FnMut(&A) -> bool + Send + 'static,
{
    /// New filter handler.
    pub fn new(handler: H, filter: F) -> Self {
        Self {
            handler,
            filter,
            _args: PhantomData,
        }
    }
}
impl<A, H, F> AppHandler<A> for FilterAppHandler<A, H, F>
where
    A: Clone + 'static,
    H: AppHandler<A>,
    F: FnMut(&A) -> bool + Send + 'static,
{
    fn event(&mut self, args: &A, handler_args: &AppHandlerArgs) {
        if (self.filter)(args) {
            self.handler.event(args, handler_args);
        }
    }
}

impl HeadlessApp {
    /// Calls an [`AppHandler<A>`] once and blocks until the update tasks started during the call complete.
    ///
    /// This function *spins* until all update tasks are completed. Timers or send events can
    /// be received during execution but the loop does not sleep, it just spins requesting an update
    /// for each pass.
    pub fn block_on<A>(&mut self, handler: &mut dyn AppHandler<A>, args: &A, timeout: Duration) -> Result<(), String>
    where
        A: Clone + 'static,
    {
        self.block_on_multi(vec![handler], args, timeout)
    }

    /// Calls multiple [`AppHandler<A>`] once each and blocks until all update tasks are complete.
    ///
    /// This function *spins* until all update tasks are completed. Timers or send events can
    /// be received during execution but the loop does not sleep, it just spins requesting an update
    /// for each pass.
    pub fn block_on_multi<A>(&mut self, handlers: Vec<&mut dyn AppHandler<A>>, args: &A, timeout: Duration) -> Result<(), String>
    where
        A: Clone + 'static,
    {
        let (pre_len, pos_len) = UPDATES.handler_lens();

        let handler_args = AppHandlerArgs {
            handle: &Handle::dummy(()).downgrade(),
            is_preview: false,
        };
        for handler in handlers {
            handler.event(args, &handler_args);
        }

        let mut pending = UPDATES.new_update_handlers(pre_len, pos_len);

        if !pending.is_empty() {
            let start_time = INSTANT.now();
            while {
                pending.retain(|h| h());
                !pending.is_empty()
            } {
                UPDATES.update(None);
                let flow = self.update(false);
                if INSTANT.now().duration_since(start_time) >= timeout {
                    return Err(format!(
                        "block_on reached timeout of {timeout:?} before the handler task could finish",
                    ));
                }

                match flow {
                    AppControlFlow::Poll => continue,
                    AppControlFlow::Wait => {
                        thread::yield_now();
                        continue;
                    }
                    AppControlFlow::Exit => return Ok(()),
                }
            }
        }

        Ok(())
    }

    /// Polls a `future` and updates the app repeatedly until it completes or the `timeout` is reached.
    pub fn block_on_fut<F: Future>(&mut self, future: F, timeout: Duration) -> Result<F::Output, String> {
        let future = task::with_deadline(future, timeout);
        let mut future = std::pin::pin!(future);

        let waker = UPDATES.waker(None);
        let mut cx = std::task::Context::from_waker(&waker);

        loop {
            let mut fut_poll = future.as_mut().poll(&mut cx);
            let flow = self.update_observe(
                || {
                    if fut_poll.is_pending() {
                        fut_poll = future.as_mut().poll(&mut cx);
                    }
                },
                true,
            );

            match fut_poll {
                std::task::Poll::Ready(r) => match r {
                    Ok(r) => return Ok(r),
                    Err(e) => return Err(e.to_string()),
                },
                std::task::Poll::Pending => {}
            }

            match flow {
                AppControlFlow::Poll => continue,
                AppControlFlow::Wait => {
                    thread::yield_now();
                    continue;
                }
                AppControlFlow::Exit => return Err("app exited".to_owned()),
            }
        }
    }

    /// Calls the `handler` once and [`block_on`] it with a 60 seconds timeout using the minimal headless app.
    ///
    /// [`block_on`]: Self::block_on
    #[track_caller]
    #[cfg(any(test, doc, feature = "test_util"))]
    pub fn doc_test<A, H>(args: A, mut handler: H)
    where
        A: Clone + 'static,
        H: AppHandler<A>,
    {
        let mut app = crate::APP.minimal().run_headless(false);
        app.block_on(&mut handler, &args, DOC_TEST_BLOCK_ON_TIMEOUT).unwrap();
    }

    /// Calls the `handlers` once each and [`block_on_multi`] with a 60 seconds timeout.
    ///
    /// [`block_on_multi`]: Self::block_on_multi
    #[track_caller]
    #[cfg(any(test, doc, feature = "test_util"))]
    pub fn doc_test_multi<A>(args: A, mut handlers: Vec<Box<dyn AppHandler<A>>>)
    where
        A: Clone + 'static,
    {
        let mut app = crate::APP.minimal().run_headless(false);
        app.block_on_multi(handlers.iter_mut().map(|h| h.as_mut()).collect(), &args, DOC_TEST_BLOCK_ON_TIMEOUT)
            .unwrap()
    }
}

#[cfg(any(test, doc, feature = "test_util"))]
const DOC_TEST_BLOCK_ON_TIMEOUT: Duration = Duration::from_secs(60);