egui icon indicating copy to clipboard operation
egui copied to clipboard

Cannot change icon dynamically on `macOS` and `Windows` using `ViewportCommand::Icon`

Open dscso opened this issue 1 year ago • 3 comments

Using the master and running the following code does not change the Icon, whereas other commands like fullscreen(true) work just fine. Setting the icon via ViewportBuilder is also no problem. Am I doing any mistakes or is this a bug? Thank you very much for this great library btw :)!

use eframe::egui;
use eframe::egui::{IconData, ViewportCommand};
use std::sync::Arc;

fn main() -> Result<(), eframe::Error> {
    let options = eframe::NativeOptions {
        viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
        ..Default::default()
    };
    eframe::run_native(
        "My egui App",
        options,
        Box::new(|cc| {
            // This gives us image support:
            egui_extras::install_image_loaders(&cc.egui_ctx);

            Box::<MyApp>::default()
        }),
    )
}

struct MyApp {}

impl Default for MyApp {
    fn default() -> Self {
        Self {}
    }
}

impl eframe::App for MyApp {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            ui.heading("My egui Application");
            if ui.button("Add icon").clicked() {
                let icon =
                    include_bytes!("logo-mac.png");
                let image = image::load_from_memory(icon)
                    .expect("Image could not be loaded from memory")
                    .into_rgba8();
                let (width, height) = image.dimensions();
                let icon = Some(Arc::new(IconData {
                    rgba: image.into_raw(),
                    width,
                    height,
                }));
                println!("Icon added");
                ctx.send_viewport_cmd(ViewportCommand::Icon(icon));
            }
        });
    }
}

OS: MacOS 14.2

dscso avatar Jan 15 '24 15:01 dscso

The API is not supported on winit on Mac:

https://docs.rs/winit/latest/winit/window/struct.Window.html#method.set_window_icon

You'll need to make a PR to winit to fix it!

emilk avatar Jan 15 '24 15:01 emilk

I thought so as well, but the Icon can be set on GUI startup, but can not be changed later. Do you think this is a winit issue? image

dscso avatar Jan 15 '24 15:01 dscso

Well… it's both/either, I guess.

I would love it if winit supported icons on Mac and Windows, but since it doesn't, we have this in eframe:

https://github.com/emilk/egui/blob/4c8b95f365983582ccb7a97a29bafeecd7f4d6bc/crates/eframe/src/native/app_icon.rs

And yes, that only works for setting the icon on startup.

emilk avatar Jan 15 '24 16:01 emilk