-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add icon support to eframe #193
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool, thank you!
Please add a line in eframe/CHANGELOG.md
too
Co-authored-by: Emil Ernerfeldt <[email protected]>
Thank you! |
Example built changing https://github.com/emilk/egui/blob/master/egui_demo_lib/src/apps/http_app.rs; Just another sample (tested on Windows 10): use eframe::{egui};
use eframe::epi::IconData;
use image;
#[allow(dead_code)]
pub struct Image {
size: (usize, usize),
pixels: Vec<egui::Color32>,
}
#[allow(dead_code)]
impl Image {
pub fn open_icon_data(path : &str) -> std::option::Option<IconData>{
let image_buffer = image::open(path).unwrap();
let img = image_buffer.to_rgba8();
let size = (img.width() as u32, img.height() as u32);
let pixels = img.into_vec();
let icon_data = eframe::epi::IconData{
rgba: pixels,
width: size.0 ,
height: size.1,
};
Some(icon_data)
}
pub fn open_image(path : &str) -> Option<Image>{
let image_buffer = image::open(path).unwrap();
let img = image_buffer.to_rgb8();
let size = (img.width() as usize, img.height() as usize);
let pixels = img.into_vec();
let pixels = pixels
.chunks(4)
.map(|p| egui::Color32::from_rgba_unmultiplied(p[0], p[1], p[2], p[3]))
.collect();
Some(Image { size, pixels })
}
pub fn decode(bytes: &[u8]) -> Option<Image> {
use image::GenericImageView;
let image = image::load_from_memory(bytes).ok()?;
let image_buffer = image.to_rgba8();
let size = (image.width() as usize, image.height() as usize);
let pixels = image_buffer.into_vec();
assert_eq!(size.0 * size.1 * 4, pixels.len());
let pixels = pixels
.chunks(4)
.map(|p| egui::Color32::from_rgba_unmultiplied(p[0], p[1], p[2], p[3]))
.collect();
Some(Image { size, pixels })
}
} Usage: #![forbid(unsafe_code)]
#![cfg_attr(not(debug_assertions), deny(warnings))] // Forbid warnings in release builds
#![warn(clippy::all, rust_2018_idioms)]
mod view;
mod resources;
//mod created at src/resources/img.rs
use resources::img::Image;
//app.rs moved from https://github.com/emilk/egui_template/src/app.rs to https://github.com/emilk/egui_template/src/view/app.rs
use crate::view::app::App;
//#[cfg(not(target_arch = "wasm32"))]
fn main() {
let icon_data = Image::open_icon_data("favicon32x32.png");
let app = App::default();
let mut native_options = eframe::NativeOptions::default();
native_options.icon_data = icon_data;
eframe::run_native(Box::new(app), native_options);
}
} The file favicon32x32.png was placed at the same level to cargo.toml Window Screenshot: |
@felipetesc Thanks for the example code! I think it's probably better to include the icon with |
Does this work on macOS too or only windows? |
I can't test on MacOS, don't have a Mac. If I could I would. If I remember correct, I used Linux too. |
Example code using this (uses the image crate to load images):