qdot / systray-rs

Allows rust applications to show a platform specific system tray icon and menu.
BSD 3-Clause "New" or "Revised" License
170 stars 66 forks source link

I'm not able to set a image to be a icon #49

Closed Werner1201 closed 4 years ago

Werner1201 commented 4 years ago

I've been trying to set a temp.jpg as the Icon on the example It always gives to me a "OS Error" on the debugger. here's my code:

#![windows_subsystem = "windows"]
//use std::path;

//#[cfg(target_os = "windows")]
fn main() -> Result<(), systray::Error> {
    let mut app;
    match systray::Application::new() {
        Ok(w) => app = w,
        Err(_) => panic!("Can't create window!"),
    }
    // w.set_icon_from_file(&"C:\\Users\\qdot\\code\\git-projects\\systray-rs\\resources\\rust.ico".to_string());
    // w.set_tooltip(&"Whatever".to_string());
    let cam = "D\\pactw\\RustProjects\\teste_sys_tray\\temp.png";
    app.set_icon_from_file(&cam.to_string())?;

    app.add_menu_item("Print a thing", |_| {
        println!("file!()");
        Ok::<_, systray::Error>(())
    })?;

    app.add_menu_item("Add Menu Item", |window| {
        window.add_menu_item("Interior item", |_| {
            println!("what");
            Ok::<_, systray::Error>(())
        })?;
        window.add_menu_separator()?;
        Ok::<_, systray::Error>(())
    })?;

    app.add_menu_separator()?;

    app.add_menu_item("Quit", |window| {
        window.quit();
        Ok::<_, systray::Error>(())
    })?;

    println!("Waiting on message!");
    app.wait_for_message()?;
    Ok(())
}
Werner1201 commented 4 years ago

Well I discovered the problem, If I bring the image to the target/debug and make it directly from the name of the file It works. Now the question is why the library does not accept the Absolute path of the file

rafamerlin commented 4 years ago

I just tested on mine with the complete path of the image and it worked.

I think maybe the issue is that you're missing a : in your path: D:\\pactw\\RustProjects\\teste_sys_tray\\temp.png

Werner1201 commented 4 years ago

Okay, it worked adding the : but I have another question. How do I set the icon from a buffer and which type of buffer ? Because I'm using the Image library to generate images but the Image buffer that it generates isn't read by this crate

Em sex, 14 de ago de 2020 09:37, Rafael Merlin notifications@github.com escreveu:

I just tested on mine with the complete path of the image and it worked.

I think maybe the issue is that you're missing a : in your path: D:\pactw\RustProjects\teste_sys_tray\temp.png

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/qdot/systray-rs/issues/49#issuecomment-674053599, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACB3K2KML6UNKSYABRWXMVLSAUVY7ANCNFSM4P3B6BGA .

nicolasbauw commented 4 years ago

I personally used the set_icon_from_buffer() function and a buffer set with the include_bytes! macro (because I embed the icon in the binary) but this function seems to work only with .ICO data.

let enabled_icon = include_bytes!("../assets/checkmark.ico");
app.set_icon_from_buffer(enabled_icon, 128, 128)?;
Werner1201 commented 4 years ago

Okay I'll try that, but I think it'll not be as optimized as I thought. Because I'm using a request to a weather API and then creating an image with the temperature then setting this icon to the systray. What I'm seeking is to eliminate the need to create the file and only use things internally. The link to my project is here if you want to help me, I'm learning the language with this project since it uses various concepts of the language

Em sex, 14 de ago de 2020 10:26, Nicolas BAUW notifications@github.com escreveu:

I personally used the set_icon_from_buffer() function and a buffer set with the include_bytes! macro (because I embed the icon in the binary) but this function seems to work only with .ICO data.

let enabled_icon = include_bytes!("../assets/checkmark.ico"); app.set_icon_from_buffer(enabled_icon, 128, 128)?;

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/qdot/systray-rs/issues/49#issuecomment-674073886, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACB3K2MLUZ6FGMQULQMA2MDSAU3Q3ANCNFSM4P3B6BGA .

Werner1201 commented 4 years ago

As @nicolasbauw has already helped me in my project I will be closing this issue. If you want to Check out the solution, check my project here: (https://github.com/Werner1201/weather_tray-rs)