1- use iced_core:: { Point , Rectangle } ;
2- use std:: fmt:: Debug ;
1+ use iced_core:: { Point , Rectangle , widget :: Tree } ;
2+ use std:: { fmt:: Debug , ops :: Deref } ;
33
44pub trait TypedLayout : Clone + Copy + Debug {
55 fn position ( & self ) -> Point ;
66 fn bounds ( & self ) -> Rectangle ;
77}
88
9+ pub trait TypedTreeRef < ' a > : Debug {
10+ fn tree_ref ( & self ) -> & Tree ;
11+ }
12+
13+ pub trait TypedTreeMut < ' a > : TypedTreeRef < ' a > {
14+ fn tree_mut ( & mut self ) -> & mut Tree ;
15+ }
16+
917/// A macro that facilitates type safety for layout traversal.
1018/// Generates a newtype (wrapper) for the [`::iced_runtime::Layout`] type and functions to access
1119/// this type from other typed layout types specified in `traverse` and `children_of`.
@@ -20,7 +28,9 @@ macro_rules! typed_layout {
2028 parent_type_name: $traverse_parent_type_name: ident,
2129 fn_name: $traverse_fn_name: ident,
2230 fn_args: [ $( $traverse_fn_arg_name: ident: $traverse_fn_arg_ty: ty) ,* $( , ) ?] ,
23- fn : $traverse_fn: expr,
31+ layout_fn: $traverse_layout_fn: expr,
32+ tree_ref_fn: $traverse_tree_ref_fn: expr,
33+ tree_mut_fn: $traverse_tree_mut_fn: expr,
2434 } ,
2535 ) *
2636 ] ,
@@ -36,6 +46,12 @@ macro_rules! typed_layout {
3646 #[ derive( Clone , Copy , Debug ) ]
3747 pub struct [ < $type_name Layout >] <' a>( :: iced_core:: Layout <' a>) ;
3848
49+ #[ derive( Debug ) ]
50+ pub struct [ < $type_name TreeRef >] <' a>( & ' a :: iced_core:: widget:: Tree ) ;
51+
52+ #[ derive( Debug ) ]
53+ pub struct [ < $type_name TreeMut >] <' a>( & ' a mut :: iced_core:: widget:: Tree ) ;
54+
3955 impl <' a> TypedLayout for [ < $type_name Layout >] <' a> {
4056 fn position( & self ) -> :: iced_core:: Point {
4157 self . 0 . position( )
@@ -46,6 +62,18 @@ macro_rules! typed_layout {
4662 }
4763 }
4864
65+ impl <' a> TypedTreeRef for [ < $type_name TreeRef >] <' a> {
66+ fn tree_ref( & self ) -> & Tree {
67+ & self . 0
68+ }
69+ }
70+
71+ impl <' a> TypedTreeMut for [ < $type_name TreeMut >] <' a> {
72+ fn tree_mut( & mut self ) -> & mut Tree {
73+ & mut self . 0
74+ }
75+ }
76+
4977 // impl<'a> ::std::ops::Deref for [< $type_name Layout >]<'a> {
5078 // type Target = ::iced_runtime::Layout<'a>;
5179
@@ -66,6 +94,18 @@ macro_rules! typed_layout {
6694 }
6795 }
6896
97+ impl <' a> From <& ' a :: iced_core:: widget:: Tree > for [ < $type_name TreeRef >] <' a> {
98+ fn from( tree: & ' a :: iced_core:: widget:: Tree ) -> Self {
99+ Self ( tree)
100+ }
101+ }
102+
103+ impl <' a> From <[ < $type_name TreeRef >] <' a>> for :: iced_core:: widget:: Tree {
104+ fn from( tree: [ < $type_name Tree >] ) -> Self {
105+ tree. 0
106+ }
107+ }
108+
69109 $(
70110 $(
71111 impl <' a> [ < $traverse_parent_type_name Layout >] <' a> {
@@ -76,10 +116,23 @@ macro_rules! typed_layout {
76116 use :: iced_core:: Layout ;
77117 // let [< $traverse_parent_type_name Layout >](parent) = self;
78118 let parent = self . into( ) ;
79- let layout = ( $traverse_fn ) ( parent, $( $traverse_fn_arg_name, ) * ) ;
119+ let layout = ( $traverse_layout_fn ) ( parent, $( $traverse_fn_arg_name, ) * ) ;
80120 [ < $type_name Layout >] :: from( layout)
81121 }
82122 }
123+
124+ impl [ < $traverse_parent_type_name Tree >] {
125+ pub fn [ < $traverse_fn_name >] (
126+ self ,
127+ $( $traverse_fn_arg_name: $traverse_fn_arg_ty, ) *
128+ ) -> [ < $type_name Tree >] <' a> {
129+ use :: iced_core:: widget:: Tree ;
130+ // let [< $traverse_parent_type_name Tree >](parent) = self;
131+ let parent = self . into( ) ;
132+ let layout = ( $traverse_tree_fn) ( parent, $( $traverse_fn_arg_name, ) * ) ;
133+ [ < $type_name Tree >] :: from( layout)
134+ }
135+ }
83136 ) *
84137 ) ?
85138
@@ -94,6 +147,17 @@ macro_rules! typed_layout {
94147 } )
95148 }
96149 }
150+
151+ impl [ < $children_of_parent_type_name Tree >] {
152+ pub fn [ < $children_of_fn_name >] (
153+ self ,
154+ ) -> impl Iterator <Item =[ < $type_name Tree >] > {
155+ let [ < $children_of_parent_type_name Tree >] ( parent) = self ;
156+ parent. children( ) . map( |layout| {
157+ [ < $type_name Tree >] :: from( layout)
158+ } )
159+ }
160+ }
97161 ) ?
98162 }
99163 }
0 commit comments