-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eframe: Set app icon on Mac and Windows (#2940)
* eframe: Set app icon on Mac and Windows Also: correctly set window title on Mac when launching from another process, e.g. python. Co-authored-by: Wumpf <[email protected]> * lint fixes * Fix web build * fix typo * Try fix windows build --------- Co-authored-by: Wumpf <[email protected]>
- Loading branch information
Showing
10 changed files
with
363 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/// 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 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> { | ||
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> { | ||
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> { | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.