1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 13:50:04 -04:00

Remove things that have been deprecated for over a year (#7099)

Removes all things that were marked `#[deprecated]` more than 12 months
ago
This commit is contained in:
Emil Ernerfeldt
2025-05-28 09:47:15 +02:00
committed by GitHub
parent 2cf6a3a9a6
commit 92fea8a18f
15 changed files with 21 additions and 399 deletions

View File

@@ -1,222 +1,6 @@
#![allow(deprecated)]
use egui::{load::SizedTexture, mutex::Mutex, ColorImage, TextureOptions, Vec2};
#[cfg(feature = "svg")]
use egui::SizeHint;
/// An image to be shown in egui.
///
/// Load once, and save somewhere in your app state.
///
/// Use the `svg` and `image` features to enable more constructors.
///
/// ⚠ This type is deprecated: Consider using [`egui::Image`] instead.
#[deprecated = "consider using `egui::Image` instead"]
pub struct RetainedImage {
debug_name: String,
/// Texel size.
///
/// Same as [`Self.image`]`.size`
texel_size: [usize; 2],
/// Original SVG size (if this is an SVG), or same as [`Self::texel_size`].
source_size: Vec2,
/// Cleared once [`Self::texture`] has been loaded.
image: Mutex<ColorImage>,
/// Lazily loaded when we have an egui context.
texture: Mutex<Option<egui::TextureHandle>>,
options: TextureOptions,
}
impl RetainedImage {
pub fn from_color_image(debug_name: impl Into<String>, image: ColorImage) -> Self {
Self {
debug_name: debug_name.into(),
texel_size: image.size,
source_size: image.source_size,
image: Mutex::new(image),
texture: Default::default(),
options: Default::default(),
}
}
/// Load a (non-svg) image.
///
/// `image_bytes` should be the raw contents of an image file (`.png`, `.jpg`, …).
///
/// Requires the "image" feature. You must also opt-in to the image formats you need
/// with e.g. `image = { version = "0.25", features = ["jpeg", "png"] }`.
///
/// # Errors
/// On invalid image or unsupported image format.
#[cfg(feature = "image")]
pub fn from_image_bytes(
debug_name: impl Into<String>,
image_bytes: &[u8],
) -> Result<Self, String> {
Ok(Self::from_color_image(
debug_name,
load_image_bytes(image_bytes).map_err(|err| err.to_string())?,
))
}
/// Pass in the bytes of an SVG that you've loaded.
///
/// # Errors
/// On invalid image
#[cfg(feature = "svg")]
pub fn from_svg_bytes(
debug_name: impl Into<String>,
svg_bytes: &[u8],
options: &resvg::usvg::Options<'_>,
) -> Result<Self, String> {
Self::from_svg_bytes_with_size(debug_name, svg_bytes, Default::default(), options)
}
/// Pass in the str of an SVG that you've loaded.
///
/// # Errors
/// On invalid image
#[cfg(feature = "svg")]
pub fn from_svg_str(
debug_name: impl Into<String>,
svg_str: &str,
options: &resvg::usvg::Options<'_>,
) -> Result<Self, String> {
Self::from_svg_bytes_with_size(debug_name, svg_str.as_bytes(), Default::default(), options)
}
/// Pass in the bytes of an SVG that you've loaded
/// and the scaling option to resize the SVG with.
///
/// # Errors
/// On invalid image
#[cfg(feature = "svg")]
pub fn from_svg_bytes_with_size(
debug_name: impl Into<String>,
svg_bytes: &[u8],
size_hint: SizeHint,
options: &resvg::usvg::Options<'_>,
) -> Result<Self, String> {
Ok(Self::from_color_image(
debug_name,
load_svg_bytes_with_size(svg_bytes, size_hint, options)?,
))
}
/// Set the texture filters to use for the image.
///
/// **Note:** If the texture has already been uploaded to the GPU, this will require
/// re-uploading the texture with the updated filter.
///
/// # Example
/// ```rust
/// # use egui_extras::RetainedImage;
/// # use egui::{Color32, epaint::{ColorImage, textures::TextureOptions}};
/// # let pixels = vec![Color32::BLACK];
/// # let color_image = ColorImage::new([1, 1], pixels);
/// #
/// // Upload a pixel art image without it getting blurry when resized
/// let image = RetainedImage::from_color_image("my_image", color_image)
/// .with_options(TextureOptions::NEAREST);
/// ```
#[inline]
pub fn with_options(mut self, options: TextureOptions) -> Self {
self.options = options;
// If the texture has already been uploaded, this will force it to be re-uploaded with the
// updated filter.
*self.texture.lock() = None;
self
}
/// The size of the image data (number of pixels wide/high).
pub fn texel_size(&self) -> [usize; 2] {
self.texel_size
}
/// The size of the original SVG image (if any).
///
/// Note that this can differ from [`Self::texel_size`] if the SVG was rasterized at a different
/// resolution than the size of the original SVG.
pub fn source_size(&self) -> Vec2 {
self.source_size
}
#[deprecated = "use `texel_size` or `source_size` instead"]
pub fn size(&self) -> [usize; 2] {
self.texel_size
}
/// The width of the image.
#[deprecated = "use `texel_size` or `source_size` instead"]
pub fn width(&self) -> usize {
self.texel_size[0]
}
/// The height of the image.
#[deprecated = "use `texel_size` or `source_size` instead"]
pub fn height(&self) -> usize {
self.texel_size[1]
}
/// The size of the image data (number of pixels wide/high).
#[deprecated = "use `texel_size` or `source_size` instead"]
pub fn size_vec2(&self) -> egui::Vec2 {
let [w, h] = self.texel_size;
egui::vec2(w as f32, h as f32)
}
/// The debug name of the image, e.g. the file name.
pub fn debug_name(&self) -> &str {
&self.debug_name
}
/// The texture id for this image.
pub fn texture_id(&self, ctx: &egui::Context) -> egui::TextureId {
self.texture
.lock()
.get_or_insert_with(|| {
let image: &mut ColorImage = &mut self.image.lock();
let image = std::mem::take(image);
ctx.load_texture(&self.debug_name, image, self.options)
})
.id()
}
/// Show the image with the given maximum size.
pub fn show_max_size(&self, ui: &mut egui::Ui, max_size: egui::Vec2) -> egui::Response {
let mut desired_size = self.source_size();
desired_size *= (max_size.x / desired_size.x).min(1.0);
desired_size *= (max_size.y / desired_size.y).min(1.0);
self.show_size(ui, desired_size)
}
/// Show the image with the original size (one image pixel = one gui point).
pub fn show(&self, ui: &mut egui::Ui) -> egui::Response {
self.show_size(ui, self.source_size())
}
/// Show the image with the given scale factor (1.0 = original size).
pub fn show_scaled(&self, ui: &mut egui::Ui, scale: f32) -> egui::Response {
self.show_size(ui, self.source_size() * scale)
}
/// Show the image with the given size.
pub fn show_size(&self, ui: &mut egui::Ui, desired_size: egui::Vec2) -> egui::Response {
// We need to convert the SVG to a texture to display it:
// Future improvement: tell backend to do mip-mapping of the image to
// make it look smoother when downsized.
ui.image(SizedTexture::new(self.texture_id(ui.ctx()), desired_size))
}
}
// ----------------------------------------------------------------------------
/// Load a (non-svg) image.
@@ -227,7 +11,7 @@ impl RetainedImage {
/// # Errors
/// On invalid image or unsupported image format.
#[cfg(feature = "image")]
pub fn load_image_bytes(image_bytes: &[u8]) -> Result<ColorImage, egui::load::LoadError> {
pub fn load_image_bytes(image_bytes: &[u8]) -> Result<egui::ColorImage, egui::load::LoadError> {
profiling::function_scope!();
let image = image::load_from_memory(image_bytes).map_err(|err| match err {
image::ImageError::Unsupported(err) => match err.kind() {
@@ -247,7 +31,10 @@ pub fn load_image_bytes(image_bytes: &[u8]) -> Result<ColorImage, egui::load::Lo
// TODO(emilk): if this is a PNG, looks for DPI info to calculate the source size,
// e.g. for screenshots taken on a high-DPI/retina display.
Ok(ColorImage::from_rgba_unmultiplied(size, pixels.as_slice()))
Ok(egui::ColorImage::from_rgba_unmultiplied(
size,
pixels.as_slice(),
))
}
/// Load an SVG and rasterize it into an egui image.
@@ -260,7 +47,7 @@ pub fn load_image_bytes(image_bytes: &[u8]) -> Result<ColorImage, egui::load::Lo
pub fn load_svg_bytes(
svg_bytes: &[u8],
options: &resvg::usvg::Options<'_>,
) -> Result<ColorImage, String> {
) -> Result<egui::ColorImage, String> {
load_svg_bytes_with_size(svg_bytes, Default::default(), options)
}
@@ -275,7 +62,7 @@ pub fn load_svg_bytes_with_size(
svg_bytes: &[u8],
size_hint: SizeHint,
options: &resvg::usvg::Options<'_>,
) -> Result<ColorImage, String> {
) -> Result<egui::ColorImage, String> {
use egui::Vec2;
use resvg::{
tiny_skia::Pixmap,
@@ -323,7 +110,7 @@ pub fn load_svg_bytes_with_size(
&mut pixmap.as_mut(),
);
let image = ColorImage::from_rgba_premultiplied([w as _, h as _], pixmap.data())
let image = egui::ColorImage::from_rgba_premultiplied([w as _, h as _], pixmap.data())
.with_source_size(source_size);
Ok(image)