@@ -18,6 +18,28 @@ use epaint::{util::FloatOrd, RectShape};
18
18
/// - [`ImageSource::Texture`] will use the provided texture.
19
19
///
20
20
/// See [`load`] for more information.
21
+ ///
22
+ /// ### Examples
23
+ /// // Using it in a layout:
24
+ /// ```
25
+ /// # egui::__run_test_ui(|ui| {
26
+ /// ui.add(
27
+ /// egui::Image::new(egui::include_image!("../../assets/ferris.png"))
28
+ /// .rounding(5.0)
29
+ /// );
30
+ /// # });
31
+ /// ```
32
+ ///
33
+ /// // Using it just to paint:
34
+ /// ```
35
+ /// # egui::__run_test_ui(|ui| {
36
+ /// # let rect = egui::Rect::from_min_size(Default::default(), egui::Vec2::splat(100.0));
37
+ /// egui::Image::new(egui::include_image!("../../assets/ferris.png"))
38
+ /// .rounding(5.0)
39
+ /// .tint(egui::Color32::LIGHT_BLUE)
40
+ /// .paint_at(ui, rect);
41
+ /// # });
42
+ /// ```
21
43
#[ must_use = "You should put this widget in an ui with `ui.add(widget);`" ]
22
44
#[ derive( Debug , Clone ) ]
23
45
pub struct Image < ' a > {
@@ -281,6 +303,16 @@ impl<'a> Image<'a> {
281
303
}
282
304
283
305
/// Paint the image in the given rectangle.
306
+ ///
307
+ /// ```
308
+ /// # egui::__run_test_ui(|ui| {
309
+ /// # let rect = egui::Rect::from_min_size(Default::default(), egui::Vec2::splat(100.0));
310
+ /// egui::Image::new(egui::include_image!("../../assets/ferris.png"))
311
+ /// .rounding(5.0)
312
+ /// .tint(egui::Color32::LIGHT_BLUE)
313
+ /// .paint_at(ui, rect);
314
+ /// # });
315
+ /// ```
284
316
#[ inline]
285
317
pub fn paint_at ( & self , ui : & mut Ui , rect : Rect ) {
286
318
paint_texture_load_result (
@@ -300,13 +332,15 @@ impl<'a> Widget for Image<'a> {
300
332
let ui_size = self . calc_size ( ui. available_size ( ) , original_image_size) ;
301
333
302
334
let ( rect, response) = ui. allocate_exact_size ( ui_size, self . sense ) ;
303
- paint_texture_load_result (
304
- ui,
305
- & tlr,
306
- rect,
307
- self . show_loading_spinner ,
308
- & self . image_options ,
309
- ) ;
335
+ if ui. is_rect_visible ( rect) {
336
+ paint_texture_load_result (
337
+ ui,
338
+ & tlr,
339
+ rect,
340
+ self . show_loading_spinner ,
341
+ & self . image_options ,
342
+ ) ;
343
+ }
310
344
texture_load_result_response ( & self . source , & tlr, response)
311
345
}
312
346
}
@@ -532,7 +566,7 @@ pub fn paint_texture_load_result(
532
566
) {
533
567
match tlr {
534
568
Ok ( TexturePoll :: Ready { texture } ) => {
535
- paint_image_at ( ui, rect, options, texture) ;
569
+ paint_texture_at ( ui. painter ( ) , rect, options, texture) ;
536
570
}
537
571
Ok ( TexturePoll :: Pending { .. } ) => {
538
572
let show_loading_spinner =
@@ -678,16 +712,16 @@ impl Default for ImageOptions {
678
712
}
679
713
}
680
714
681
- /// Paint a `SizedTexture` as an image according to some `ImageOptions` at a given `rect`.
682
- pub fn paint_image_at ( ui : & Ui , rect : Rect , options : & ImageOptions , texture : & SizedTexture ) {
683
- if !ui . is_rect_visible ( rect) {
684
- return ;
685
- }
686
-
715
+ pub fn paint_texture_at (
716
+ painter : & Painter ,
717
+ rect : Rect ,
718
+ options : & ImageOptions ,
719
+ texture : & SizedTexture ,
720
+ ) {
687
721
if options. bg_fill != Default :: default ( ) {
688
722
let mut mesh = Mesh :: default ( ) ;
689
723
mesh. add_colored_rect ( rect, options. bg_fill ) ;
690
- ui . painter ( ) . add ( Shape :: mesh ( mesh) ) ;
724
+ painter. add ( Shape :: mesh ( mesh) ) ;
691
725
}
692
726
693
727
match options. rotation {
@@ -702,10 +736,10 @@ pub fn paint_image_at(ui: &Ui, rect: Rect, options: &ImageOptions, texture: &Siz
702
736
let mut mesh = Mesh :: with_texture ( texture. id ) ;
703
737
mesh. add_rect_with_uv ( rect, options. uv , options. tint ) ;
704
738
mesh. rotate ( rot, rect. min + origin * rect. size ( ) ) ;
705
- ui . painter ( ) . add ( Shape :: mesh ( mesh) ) ;
739
+ painter. add ( Shape :: mesh ( mesh) ) ;
706
740
}
707
741
None => {
708
- ui . painter ( ) . add ( RectShape {
742
+ painter. add ( RectShape {
709
743
rect,
710
744
rounding : options. rounding ,
711
745
fill : options. tint ,
0 commit comments