@@ -135,60 +135,69 @@ where
135
135
}
136
136
}
137
137
138
- pub struct Canvas < P , H : FnMut ( InputContext , InputEvent ) -> bool > {
138
+ pub struct Canvas < P , H , D > {
139
139
pub bounds : BoundingBox ,
140
140
pub parent_index : usize ,
141
141
pub canvas_properties : P ,
142
142
pub state : WidgetState ,
143
143
handler : Option < H > ,
144
+ on_draw : D ,
145
+ draw : bool ,
144
146
}
145
147
146
- impl Canvas < ( ) , fn ( InputContext , InputEvent ) -> bool > {
148
+ impl Canvas < ( ) , ( ) , ( ) > {
147
149
pub const STATE_SELECTED : Selected = Selected ;
148
150
pub const STATE_UNSELECTED : Unselected = Unselected ;
149
151
}
150
152
151
- impl < P > Canvas < P , fn ( InputContext , InputEvent ) -> bool > {
152
- pub fn new ( ) -> Canvas < P , fn ( InputContext , InputEvent ) -> bool >
153
+ impl < P > Canvas < P , ( ) , ( ) >
154
+ where
155
+ P : CanvasProperties ,
156
+ {
157
+ pub fn new ( ) -> Canvas < P , fn ( InputContext , InputEvent ) -> bool , fn ( & mut Cropped < ' _ , P :: Canvas > ) >
153
158
where
154
159
P : Default ,
155
160
{
156
- Self {
161
+ Canvas {
157
162
parent_index : 0 ,
158
163
bounds : BoundingBox :: default ( ) ,
159
164
canvas_properties : P :: default ( ) ,
160
165
state : WidgetState :: default ( ) ,
161
166
handler : None ,
167
+ on_draw : |_| { } ,
168
+ draw : true ,
162
169
}
163
170
}
164
171
165
- pub fn with_properties ( properties : P ) -> Canvas < P , fn ( InputContext , InputEvent ) -> bool >
172
+ pub fn with_properties (
173
+ properties : P ,
174
+ ) -> Canvas < P , fn ( InputContext , InputEvent ) -> bool , fn ( & mut Cropped < ' _ , P :: Canvas > ) >
166
175
where
167
176
P : Default ,
168
177
{
169
- Self {
178
+ Canvas {
170
179
parent_index : 0 ,
171
180
bounds : BoundingBox :: default ( ) ,
172
181
canvas_properties : properties,
173
182
state : WidgetState :: default ( ) ,
174
183
handler : None ,
184
+ on_draw : |_| { } ,
185
+ draw : true ,
175
186
}
176
187
}
177
188
}
178
189
179
- impl < P , H > Canvas < P , H >
190
+ impl < P , H , D > Canvas < P , H , D >
180
191
where
192
+ P : CanvasProperties ,
181
193
H : FnMut ( InputContext , InputEvent ) -> bool ,
194
+ D : FnMut ( & mut Cropped < ' _ , P :: Canvas > ) ,
182
195
{
183
- pub fn canvas ( & mut self ) -> Cropped < ' _ , P :: Canvas >
184
- where
185
- P : CanvasProperties ,
186
- {
187
- let bounds = self . bounding_box ( ) . to_rectangle ( ) ;
188
- self . canvas_properties . canvas ( ) . cropped ( & bounds)
196
+ pub fn invalidate ( & mut self ) {
197
+ self . draw = true ;
189
198
}
190
199
191
- pub fn with_input_handler < H2 > ( self , handler : H2 ) -> Canvas < P , H2 >
200
+ pub fn with_input_handler < H2 > ( self , handler : H2 ) -> Canvas < P , H2 , D >
192
201
where
193
202
H2 : FnMut ( InputContext , InputEvent ) -> bool ,
194
203
{
@@ -198,21 +207,41 @@ where
198
207
canvas_properties : self . canvas_properties ,
199
208
state : self . state ,
200
209
handler : Some ( handler) ,
210
+ on_draw : self . on_draw ,
211
+ draw : self . draw ,
212
+ }
213
+ }
214
+
215
+ pub fn with_on_draw < D2 > ( self , on_draw : D2 ) -> Canvas < P , H , D2 >
216
+ where
217
+ P : CanvasProperties ,
218
+ D2 : FnMut ( & mut Cropped < ' _ , P :: Canvas > ) ,
219
+ {
220
+ Canvas {
221
+ parent_index : self . parent_index ,
222
+ bounds : self . bounds ,
223
+ canvas_properties : self . canvas_properties ,
224
+ state : self . state ,
225
+ handler : self . handler ,
226
+ on_draw,
227
+ draw : self . draw ,
201
228
}
202
229
}
203
230
}
204
231
205
- impl < P , H > WrapperBindable for Canvas < P , H >
232
+ impl < P , H , D > WrapperBindable for Canvas < P , H , D >
206
233
where
207
234
P : CanvasProperties ,
208
235
H : FnMut ( InputContext , InputEvent ) -> bool ,
236
+ D : FnMut ( & mut Cropped < ' _ , P :: Canvas > ) ,
209
237
{
210
238
}
211
239
212
- impl < P , H > Widget for Canvas < P , H >
240
+ impl < P , H , D > Widget for Canvas < P , H , D >
213
241
where
214
242
P : CanvasProperties ,
215
243
H : FnMut ( InputContext , InputEvent ) -> bool ,
244
+ D : FnMut ( & mut Cropped < ' _ , P :: Canvas > ) ,
216
245
{
217
246
fn bounding_box ( & self ) -> BoundingBox {
218
247
self . bounds
@@ -309,14 +338,26 @@ where
309
338
}
310
339
}
311
340
312
- impl < C , DT , P , H > WidgetRenderer < EgCanvas < DT > > for Canvas < P , H >
341
+ impl < C , DT , P , H , D > WidgetRenderer < EgCanvas < DT > > for Canvas < P , H , D >
313
342
where
314
343
C : PixelColor ,
315
344
DT : DrawTarget < Color = C > ,
316
345
P : CanvasProperties < Color = C > ,
317
346
H : FnMut ( InputContext , InputEvent ) -> bool ,
347
+ D : FnMut ( & mut Cropped < ' _ , P :: Canvas > ) ,
318
348
{
319
349
fn draw ( & mut self , canvas : & mut EgCanvas < DT > ) -> Result < ( ) , DT :: Error > {
350
+ if self . draw {
351
+ self . draw = false ;
352
+ let bounds = self . bounding_box ( ) . to_rectangle ( ) ;
353
+ let clear_color = self . canvas_properties . clear_color ( ) ;
354
+ let mut canvas = self . canvas_properties . canvas ( ) . cropped ( & bounds) ;
355
+
356
+ _ = canvas. clear ( clear_color) ;
357
+
358
+ ( self . on_draw ) ( & mut canvas) ;
359
+ }
360
+
320
361
let bounds = self . bounding_box ( ) . to_rectangle ( ) ;
321
362
self . canvas_properties
322
363
. canvas ( )
0 commit comments