mirror of https://github.com/emilk/egui.git
Browse Source
* Part of https://github.com/emilk/egui/issues/3556 This PR replaces a bunch of options in `eframe::NativeOptions` with `egui::ViewportBuilder`. For instance: ``` diff let options = eframe::NativeOptions { - initial_window_size: Some(egui::vec2(320.0, 240.0)), - drag_and_drop_support: true, + viewport: egui::ViewportBuilder::default() + .with_inner_size([320.0, 240.0]) + .with_drag_and_drop(true), centered: true, ..Default::default() }; ```pull/3574/head
Emil Ernerfeldt
12 months ago
committed by
GitHub
26 changed files with 420 additions and 435 deletions
@ -1,76 +0,0 @@ |
|||
/// Image data for an application icon.
|
|||
///
|
|||
/// Use a square image, e.g. 256x256 pixels.
|
|||
/// You can use a transparent background.
|
|||
#[derive(Clone)] |
|||
pub struct IconData { |
|||
/// RGBA pixels, with separate/unmultiplied alpha.
|
|||
pub rgba: Vec<u8>, |
|||
|
|||
/// Image width. This should be a multiple of 4.
|
|||
pub width: u32, |
|||
|
|||
/// Image height. This should be a multiple of 4.
|
|||
pub height: u32, |
|||
} |
|||
|
|||
impl std::fmt::Debug for IconData { |
|||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|||
f.debug_struct("IconData") |
|||
.field("width", &self.width) |
|||
.field("height", &self.height) |
|||
.finish_non_exhaustive() |
|||
} |
|||
} |
|||
|
|||
impl IconData { |
|||
/// Convert into [`image::RgbaImage`]
|
|||
///
|
|||
/// # Errors
|
|||
/// If this is not a valid png.
|
|||
pub fn try_from_png_bytes(png_bytes: &[u8]) -> Result<Self, image::ImageError> { |
|||
crate::profile_function!(); |
|||
let image = image::load_from_memory(png_bytes)?; |
|||
Ok(Self::from_image(image)) |
|||
} |
|||
|
|||
fn from_image(image: image::DynamicImage) -> Self { |
|||
let image = image.into_rgba8(); |
|||
Self { |
|||
width: image.width(), |
|||
height: image.height(), |
|||
rgba: image.into_raw(), |
|||
} |
|||
} |
|||
|
|||
/// Convert into [`image::RgbaImage`]
|
|||
///
|
|||
/// # Errors
|
|||
/// If `width*height != 4 * rgba.len()`, or if the image is too big.
|
|||
pub fn to_image(&self) -> Result<image::RgbaImage, String> { |
|||
crate::profile_function!(); |
|||
let Self { |
|||
rgba, |
|||
width, |
|||
height, |
|||
} = self.clone(); |
|||
image::RgbaImage::from_raw(width, height, rgba).ok_or_else(|| "Invalid IconData".to_owned()) |
|||
} |
|||
|
|||
/// Encode as PNG.
|
|||
///
|
|||
/// # Errors
|
|||
/// The image is invalid, or the PNG encoder failed.
|
|||
pub fn to_png_bytes(&self) -> Result<Vec<u8>, String> { |
|||
crate::profile_function!(); |
|||
let image = self.to_image()?; |
|||
let mut png_bytes: Vec<u8> = Vec::new(); |
|||
image |
|||
.write_to( |
|||
&mut std::io::Cursor::new(&mut png_bytes), |
|||
image::ImageOutputFormat::Png, |
|||
) |
|||
.map_err(|err| err.to_string())?; |
|||
Ok(png_bytes) |
|||
} |
|||
} |
@ -0,0 +1,62 @@ |
|||
//! Helpers for loading [`egui::IconData`].
|
|||
|
|||
use egui::IconData; |
|||
|
|||
/// Helpers for working with [`IconData`].
|
|||
pub trait IconDataExt { |
|||
/// Convert into [`image::RgbaImage`]
|
|||
///
|
|||
/// # Errors
|
|||
/// If `width*height != 4 * rgba.len()`, or if the image is too big.
|
|||
fn to_image(&self) -> Result<image::RgbaImage, String>; |
|||
|
|||
/// Encode as PNG.
|
|||
///
|
|||
/// # Errors
|
|||
/// The image is invalid, or the PNG encoder failed.
|
|||
fn to_png_bytes(&self) -> Result<Vec<u8>, String>; |
|||
} |
|||
|
|||
/// Load the contents of .png file.
|
|||
///
|
|||
/// # Errors
|
|||
/// If this is not a valid png.
|
|||
pub fn from_png_bytes(png_bytes: &[u8]) -> Result<IconData, image::ImageError> { |
|||
crate::profile_function!(); |
|||
let image = image::load_from_memory(png_bytes)?; |
|||
Ok(from_image(image)) |
|||
} |
|||
|
|||
fn from_image(image: image::DynamicImage) -> IconData { |
|||
let image = image.into_rgba8(); |
|||
IconData { |
|||
width: image.width(), |
|||
height: image.height(), |
|||
rgba: image.into_raw(), |
|||
} |
|||
} |
|||
|
|||
impl IconDataExt for IconData { |
|||
fn to_image(&self) -> Result<image::RgbaImage, String> { |
|||
crate::profile_function!(); |
|||
let Self { |
|||
rgba, |
|||
width, |
|||
height, |
|||
} = self.clone(); |
|||
image::RgbaImage::from_raw(width, height, rgba).ok_or_else(|| "Invalid IconData".to_owned()) |
|||
} |
|||
|
|||
fn to_png_bytes(&self) -> Result<Vec<u8>, String> { |
|||
crate::profile_function!(); |
|||
let image = self.to_image()?; |
|||
let mut png_bytes: Vec<u8> = Vec::new(); |
|||
image |
|||
.write_to( |
|||
&mut std::io::Cursor::new(&mut png_bytes), |
|||
image::ImageOutputFormat::Png, |
|||
) |
|||
.map_err(|err| err.to_string())?; |
|||
Ok(png_bytes) |
|||
} |
|||
} |
Loading…
Reference in new issue