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

The tray menu does not appear in Windows 11 #221

Closed
15traven opened this issue Jan 8, 2025 · 2 comments
Closed

The tray menu does not appear in Windows 11 #221

15traven opened this issue Jan 8, 2025 · 2 comments

Comments

@15traven
Copy link

15traven commented Jan 8, 2025

Windows 11 20H2 build 22621.1702

Here is my code

Cargo.toml

[dependencies]
tray-icon = "0.19.2"

main.rs

 let exit_item = MenuItem::new("Exit", true, None);
 let menu = Menu::new();
 let _ = menu.append_items(&[
     &PredefinedMenuItem::about(
         None, 
         Some(AboutMetadata {
             name: Some("Test".to_string()),
             ..Default::default()
         })
     ),
     &PredefinedMenuItem::separator(),
     &exit_item
 ]);
 
 let tray_icon = TrayIconBuilder::new()
     .with_menu(Box::new(menu))
     .with_icon(connected_icon.clone())
     .build()
     .unwrap();

Nothing happens when I click the tray icon regardless of the mouse button


If I add the common-controls-v6 feature, I get the error:

error: process didn't exit successfully: `target\debug\mercury.exe` (exit code: 0xc0000139, STATUS_ENTRYPOINT_NOT_FOUND)
@FabianLars
Copy link
Member

Can you provide a full minimal reproduction? It's a bit hard to help without any code context.
Also, did you try the repo examples? Do they work ok?

About the common-controls-v6 error, that's more or less expected. You will have to embedd a Manifest into your executable for this to work.

@15traven
Copy link
Author

15traven commented Jan 8, 2025

I tried tao example. That doesn't work either.

Here is the complete code

use std::time::Duration;
use tokio::time::sleep;
use tray_icon::{
    menu::{
        AboutMetadata, Menu, MenuItem, PredefinedMenuItem
    }, 
    TrayIconBuilder,
};

async fn check_connection() -> Result<(), reqwest::Error>{
    let res = reqwest::get("https://google.com").await;
    match res {
        Ok(_) => Ok(()),
        Err(err) => Err(err)
    }
}

fn load_icon(path: &std::path::Path) -> tray_icon::Icon {
    let (icon_rgba, icon_width, icon_height) = {
        let image = image::open(path)
            .expect("Failed to open icon path")
            .into_rgba8();
        let (width, height) = image.dimensions();
        let rgba = image.into_raw();
        (rgba, width, height)
    };
    tray_icon::Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
}

#[tokio::main]
async fn main() {
    let connected_icon_path = concat!(env!("CARGO_MANIFEST_DIR"), "/assets/connected_icon.png");
    let connected_icon = load_icon(std::path::Path::new(connected_icon_path));

    let disconnected_icon_path = concat!(env!("CARGO_MANIFEST_DIR"), "/assets/disconnected_icon.png");
    let disconnected_icon = load_icon(std::path::Path::new(disconnected_icon_path));

    let exit_item = MenuItem::new("Exit", true, None);
    let menu = Menu::new();
    let _ = menu.append_items(&[
        &PredefinedMenuItem::about(
            None, 
            Some(AboutMetadata {
                name: Some("Mercury".to_string()),
                ..Default::default()
            })
        ),
        &PredefinedMenuItem::separator(),
        &exit_item
    ]);
    
    let tray_icon = TrayIconBuilder::new()
        .with_menu(Box::new(menu))
        .with_icon(connected_icon.clone())
        .build()
        .unwrap();

   loop {
        match check_connection().await {
            Ok(_) => {
                let _ = tray_icon.set_icon(Some(connected_icon.clone()));
            }
            Err(_err) => {
                let _ = tray_icon.set_icon(Some(disconnected_icon.clone()));
            }
        }
        sleep(Duration::from_secs(20)).await;
   }
}

@15traven 15traven closed this as not planned Won't fix, can't repro, duplicate, stale Jan 9, 2025
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

No branches or pull requests

2 participants