Skip to content

Commit a3dfd08

Browse files
authored
More image polish (#3343)
* docs * paint_texture_at * comment * Fix doc-tests
1 parent 2bbceb8 commit a3dfd08

File tree

5 files changed

+67
-21
lines changed

5 files changed

+67
-21
lines changed

crates/egui/src/painter.rs

+12
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,18 @@ impl Painter {
350350
/// unless you want to crop or flip the image.
351351
///
352352
/// `tint` is a color multiplier. Use [`Color32::WHITE`] if you don't want to tint the image.
353+
///
354+
/// Usually it is easier to use [`crate::Image::paint_at`] instead:
355+
///
356+
/// ```
357+
/// # egui::__run_test_ui(|ui| {
358+
/// # let rect = egui::Rect::from_min_size(Default::default(), egui::Vec2::splat(100.0));
359+
/// egui::Image::new(egui::include_image!("../assets/ferris.png"))
360+
/// .rounding(5.0)
361+
/// .tint(egui::Color32::LIGHT_BLUE)
362+
/// .paint_at(ui, rect);
363+
/// # });
364+
/// ```
353365
pub fn image(&self, texture_id: epaint::TextureId, rect: Rect, uv: Rect, tint: Color32) {
354366
self.add(Shape::image(texture_id, rect, uv, tint));
355367
}

crates/egui/src/widgets/image.rs

+51-17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ use epaint::{util::FloatOrd, RectShape};
1818
/// - [`ImageSource::Texture`] will use the provided texture.
1919
///
2020
/// 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+
/// ```
2143
#[must_use = "You should put this widget in an ui with `ui.add(widget);`"]
2244
#[derive(Debug, Clone)]
2345
pub struct Image<'a> {
@@ -281,6 +303,16 @@ impl<'a> Image<'a> {
281303
}
282304

283305
/// 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+
/// ```
284316
#[inline]
285317
pub fn paint_at(&self, ui: &mut Ui, rect: Rect) {
286318
paint_texture_load_result(
@@ -300,13 +332,15 @@ impl<'a> Widget for Image<'a> {
300332
let ui_size = self.calc_size(ui.available_size(), original_image_size);
301333

302334
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+
}
310344
texture_load_result_response(&self.source, &tlr, response)
311345
}
312346
}
@@ -532,7 +566,7 @@ pub fn paint_texture_load_result(
532566
) {
533567
match tlr {
534568
Ok(TexturePoll::Ready { texture }) => {
535-
paint_image_at(ui, rect, options, texture);
569+
paint_texture_at(ui.painter(), rect, options, texture);
536570
}
537571
Ok(TexturePoll::Pending { .. }) => {
538572
let show_loading_spinner =
@@ -678,16 +712,16 @@ impl Default for ImageOptions {
678712
}
679713
}
680714

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+
) {
687721
if options.bg_fill != Default::default() {
688722
let mut mesh = Mesh::default();
689723
mesh.add_colored_rect(rect, options.bg_fill);
690-
ui.painter().add(Shape::mesh(mesh));
724+
painter.add(Shape::mesh(mesh));
691725
}
692726

693727
match options.rotation {
@@ -702,10 +736,10 @@ pub fn paint_image_at(ui: &Ui, rect: Rect, options: &ImageOptions, texture: &Siz
702736
let mut mesh = Mesh::with_texture(texture.id);
703737
mesh.add_rect_with_uv(rect, options.uv, options.tint);
704738
mesh.rotate(rot, rect.min + origin * rect.size());
705-
ui.painter().add(Shape::mesh(mesh));
739+
painter.add(Shape::mesh(mesh));
706740
}
707741
None => {
708-
ui.painter().add(RectShape {
742+
painter.add(RectShape {
709743
rect,
710744
rounding: options.rounding,
711745
fill: options.tint,

crates/egui/src/widgets/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub mod text_edit;
2222
pub use button::*;
2323
pub use drag_value::DragValue;
2424
pub use hyperlink::*;
25-
pub use image::{paint_image_at, Image, ImageFit, ImageOptions, ImageSize, ImageSource};
25+
pub use image::{paint_texture_at, Image, ImageFit, ImageOptions, ImageSize, ImageSource};
2626
pub use label::*;
2727
pub use progress_bar::ProgressBar;
2828
pub use selected_label::SelectableLabel;

crates/egui/src/widgets/spinner.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Spinner {
3535
/// Paint the spinner in the given rectangle.
3636
pub fn paint_at(&self, ui: &Ui, rect: Rect) {
3737
if ui.is_rect_visible(rect) {
38-
ui.ctx().request_repaint();
38+
ui.ctx().request_repaint(); // because it is animated
3939

4040
let color = self
4141
.color

crates/egui_plot/src/items/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1235,8 +1235,8 @@ impl PlotItem for PlotImage {
12351235
};
12361236
let screen_rotation = -*rotation as f32;
12371237

1238-
egui::paint_image_at(
1239-
ui,
1238+
egui::paint_texture_at(
1239+
ui.painter(),
12401240
image_screen_rect,
12411241
&ImageOptions {
12421242
uv: *uv,

0 commit comments

Comments
 (0)