1- use crate :: widget:: operation:: { self , Operation } ;
1+ use crate :: widget:: operation:: { self , Focusable , Operation , Scrollable } ;
22use crate :: widget:: Id ;
33
44use iced_futures:: MaybeSend ;
55
6+ use std:: rc:: Rc ;
7+
68/// An operation to be performed on the widget tree.
79#[ allow( missing_debug_implementations) ]
810pub struct Action < T > ( Box < dyn Operation < T > > ) ;
@@ -24,7 +26,7 @@ impl<T> Action<T> {
2426 {
2527 Action ( Box :: new ( Map {
2628 operation : self . 0 ,
27- f : Box :: new ( f) ,
29+ f : Rc :: new ( f) ,
2830 } ) )
2931 }
3032
@@ -37,7 +39,7 @@ impl<T> Action<T> {
3739#[ allow( missing_debug_implementations) ]
3840struct Map < A , B > {
3941 operation : Box < dyn Operation < A > > ,
40- f : Box < dyn Fn ( A ) -> B > ,
42+ f : Rc < dyn Fn ( A ) -> B > ,
4143}
4244
4345impl < A , B > Operation < B > for Map < A , B >
@@ -50,30 +52,44 @@ where
5052 id : Option < & Id > ,
5153 operate_on_children : & mut dyn FnMut ( & mut dyn Operation < B > ) ,
5254 ) {
53- struct MapRef < ' a , A , B > {
55+ struct MapRef < ' a , A > {
5456 operation : & ' a mut dyn Operation < A > ,
55- f : & ' a dyn Fn ( A ) -> B ,
5657 }
5758
58- impl < ' a , A , B > Operation < B > for MapRef < ' a , A , B > {
59+ impl < ' a , A , B > Operation < B > for MapRef < ' a , A > {
5960 fn container (
6061 & mut self ,
6162 id : Option < & Id > ,
6263 operate_on_children : & mut dyn FnMut ( & mut dyn Operation < B > ) ,
6364 ) {
64- let Self { operation, f } = self ;
65+ let Self { operation, .. } = self ;
6566
6667 operation. container ( id, & mut |operation| {
67- operate_on_children ( & mut MapRef { operation, f } ) ;
68+ operate_on_children ( & mut MapRef { operation } ) ;
6869 } ) ;
6970 }
71+
72+ fn scrollable (
73+ & mut self ,
74+ state : & mut dyn Scrollable ,
75+ id : Option < & Id > ,
76+ ) {
77+ self . operation . scrollable ( state, id) ;
78+ }
79+
80+ fn focusable (
81+ & mut self ,
82+ state : & mut dyn Focusable ,
83+ id : Option < & Id > ,
84+ ) {
85+ self . operation . focusable ( state, id) ;
86+ }
7087 }
7188
72- let Self { operation, f } = self ;
89+ let Self { operation, .. } = self ;
7390
7491 MapRef {
7592 operation : operation. as_mut ( ) ,
76- f,
7793 }
7894 . container ( id, operate_on_children) ;
7995 }
@@ -101,4 +117,19 @@ where
101117 ) {
102118 self . operation . text_input ( state, id) ;
103119 }
120+
121+ fn finish ( & self ) -> operation:: Outcome < B > {
122+ match self . operation . finish ( ) {
123+ operation:: Outcome :: None => operation:: Outcome :: None ,
124+ operation:: Outcome :: Some ( output) => {
125+ operation:: Outcome :: Some ( ( self . f ) ( output) )
126+ }
127+ operation:: Outcome :: Chain ( next) => {
128+ operation:: Outcome :: Chain ( Box :: new ( Map {
129+ operation : next,
130+ f : self . f . clone ( ) ,
131+ } ) )
132+ }
133+ }
134+ }
104135}
0 commit comments