Skip to content
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

Merged
merged 7 commits into from
Feb 26, 2021
Merged

Add icon support to eframe #193

merged 7 commits into from
Feb 26, 2021

Conversation

phoglund
Copy link
Contributor

Example code using this (uses the image crate to load images):

pub fn load_icon(icon_bytes: &Vec<u8>) -> Option<epi::IconData> {
    if let Ok(image) = image::load_from_memory(icon_bytes) {
        let image = image.to_rgba8();
        let (width, height) = image.dimensions();
        Some(epi::IconData {
            width,
            height,
            rgba: image.as_raw().to_vec(),
        })
    } else {
        None
    }
}

impl epi::App for EmbarkSubmitApp {
     ...
    fn icon_data(&self) -> Option<epi::IconData> {
        let icon_bytes = include_bytes!("../icons/my_icon.png");
        crate::icon::load_icon(&icon_bytes.to_vec())
    }

Copy link
Owner

@emilk emilk left a 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

egui_glium/src/backend.rs Outdated Show resolved Hide resolved
@phoglund phoglund requested a review from emilk February 26, 2021 12:11
@emilk emilk changed the title Add icon support to egui Add icon support to eframe Feb 26, 2021
@emilk emilk merged commit a859b2a into emilk:master Feb 26, 2021
@emilk
Copy link
Owner

emilk commented Feb 26, 2021

Thank you!

@felipetesc
Copy link

felipetesc commented Aug 17, 2021

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:

Screenshot 2021-08-17 192132

Taskbar Screenshot:
Screenshot 2021-08-17 192324

@emilk
Copy link
Owner

emilk commented Aug 18, 2021

@felipetesc Thanks for the example code! I think it's probably better to include the icon with include_bytes!, e.g. let icon_data = Image::decode_icon_data(include_bytes!("favicon32x32.png"));. That way you won't have any problems with working directories or forgetting to ship the icon with your binary.

@mjehrhart
Copy link

Does this work on macOS too or only windows?

@felipetesc
Copy link

I can't test on MacOS, don't have a Mac. If I could I would. If I remember correct, I used Linux too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants