Skip to content

Commit

Permalink
add style option to disable image spinners
Browse files Browse the repository at this point in the history
  • Loading branch information
jprochazk committed Sep 13, 2023
1 parent b955a4f commit 6589fdf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
8 changes: 8 additions & 0 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ pub struct Style {
///
/// This only affects a few egui widgets.
pub explanation_tooltips: bool,

/// Show a spinner when loading an image.
pub image_loading_spinners: bool,
}

impl Style {
Expand Down Expand Up @@ -738,6 +741,7 @@ impl Default for Style {
animation_time: 1.0 / 12.0,
debug: Default::default(),
explanation_tooltips: false,
image_loading_spinners: true,
}
}
}
Expand Down Expand Up @@ -990,6 +994,7 @@ impl Style {
animation_time,
debug,
explanation_tooltips,
image_loading_spinners,
} = self;

visuals.light_dark_radio_buttons(ui);
Expand Down Expand Up @@ -1057,6 +1062,9 @@ impl Style {
"Show explanatory text when hovering DragValue:s and other egui widgets",
);

ui.checkbox(image_loading_spinners, "Image loading spinners")
.on_hover_text("Show a spinner when an Image is loading");

ui.vertical_centered(|ui| reset_button(ui, self));
}
}
Expand Down
52 changes: 39 additions & 13 deletions crates/egui/src/widgets/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Image<'a> {
image_options: ImageOptions,
sense: Sense,
size: ImageSize,
show_spinner: Option<bool>,
}

impl<'a> Image<'a> {
Expand All @@ -50,6 +51,7 @@ impl<'a> Image<'a> {
image_options: Default::default(),
sense: Sense::hover(),
size,
show_spinner: None,
}
}

Expand Down Expand Up @@ -221,6 +223,15 @@ impl<'a> Image<'a> {
}
self
}

/// Show a spinner when the image is loading.
///
/// By default this uses the value of [`Style::image_loading_spinners`].
#[inline]
pub fn show_spinner(mut self, show: bool) -> Self {
self.show_spinner = Some(show);
self
}
}

impl<'a> Image<'a> {
Expand Down Expand Up @@ -294,21 +305,36 @@ impl<'a> Widget for Image<'a> {
self.paint_at(ui, rect, &texture);
response
}
Ok(TexturePoll::Pending { size }) => match size {
Some(size) => {
let size = self.calculate_size(ui.available_size(), size);
ui.allocate_ui(size, |ui| {
ui.with_layout(Layout::centered_and_justified(Direction::TopDown), |ui| {
ui.spinner()
.on_hover_text(format!("Loading {:?}…", self.uri()))
Ok(TexturePoll::Pending { size }) => {
let spinner = |ui: &mut Ui| {
let show_spinner = self
.show_spinner
.unwrap_or(ui.style().image_loading_spinners);
if show_spinner {
ui.spinner()
.on_hover_text(format!("Loading {:?}…", self.uri()))
} else {
ui.allocate_response(
Vec2::splat(ui.style().spacing.interact_size.y),
Sense::hover(),
)
}
};

match size {
Some(size) => {
let size = self.calculate_size(ui.available_size(), size);
ui.allocate_ui(size, |ui| {
ui.with_layout(
Layout::centered_and_justified(Direction::TopDown),
spinner,
)
})
})
.response
.response
}
None => spinner(ui),
}
None => ui
.spinner()
.on_hover_text(format!("Loading {:?}…", self.uri())),
},
}
Err(err) => ui
.colored_label(ui.visuals().error_fg_color, "⚠")
.on_hover_text(err.to_string()),
Expand Down

0 comments on commit 6589fdf

Please sign in to comment.