tauri-docs icon indicating copy to clipboard operation
tauri-docs copied to clipboard

[bug] Path variant of tauri::Icon cannot be found, despite being a documented variant

Open Gambloide opened this issue 1 year ago • 2 comments

Describe the bug

The described issue seems to include a code bug and a docs issue.

The following docs describe how to dynamically update a tray icon. https://tauri.app/v1/guides/features/system-tray#updating-tray-icon

When following the rest of the guide and trying to update the icon with the provided example snipped, the following error is thrown when trying to compile the application:

error[E0599]: no variant or associated item named `Raw` found for enum `Icon` in the current scope
  --> src\main.rs:43:49
   |
43 |         app.tray_handle().set_icon(tauri::Icon::Raw(include_bytes!("../icons/custom/inactive.png"))).unwrap();
   |                                                 ^^^ variant or associated item not found in `Icon`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `app` due to previous error 

The docs state:

Note that tauri::Icon must be a Path variant on Linux, and Raw variant on Windows and macOS.

Aside from the given code snipped, this implies this variant should exist.

The Rust docs for tauri show the same: https://docs.rs/tauri/1.0.5/tauri/enum.Icon.html

So either both documentations are wrong, the variant is not present in the current builds or something else I am not aware of is wrong.

What is even more confusing is the start of this sentence:

Note that tauri::Icon must be a Path variant on Linux [...]

Path is not a variant of Icon as per https://docs.rs/tauri/1.0.5/tauri/enum.Icon.html. The only listed variants are File, Raw and RGBA.

Reproduction

This was done on windows, in powershell with a fresh rust and tauri install following https://tauri.app/v1/guides/getting-started/prerequisites/ then creating a new app with npm create tauri-app and lastly following https://tauri.app/v1/guides/features/system-tray

$ npm create tauri-app
  -- choose test, test, vanilla.js
$ cd test
$ npm install
$ npm run tauri dev
  -- stop the process by closing the window by pressing 

Configure the systemTray object in tauri.conf.json:

{
  "tauri": {
    "systemTray": {
      "iconPath": "icons/icon.png",
      "iconAsTemplate": true
    }
  }
}

Replace the contents of the main.rs with:

#![cfg_attr(
  all(not(debug_assertions), target_os = "windows"),
  windows_subsystem = "windows"
)]

use tauri::{CustomMenuItem, SystemTray, SystemTrayMenu, SystemTrayMenuItem, SystemTrayEvent};
use tauri::Manager;

fn main() {
  let quit = CustomMenuItem::new("quit".to_string(), "Quit");
  let hide = CustomMenuItem::new("hide".to_string(), "Hide");

  let tray_menu = SystemTrayMenu::new()
  .add_item(quit)
  .add_native_item(SystemTrayMenuItem::Separator)
  .add_item(hide);

  let system_tray = SystemTray::new().with_menu(tray_menu);

  tauri::Builder::default()
    .system_tray(system_tray)
    .on_system_tray_event(|app, event| match event {
      SystemTrayEvent::LeftClick {
        position: _,
        size: _,
        ..
      } => {
        println!("system tray received a left click");
      }
      SystemTrayEvent::RightClick {
        position: _,
        size: _,
        ..
      } => {
        println!("system tray received a right click");
      }
      SystemTrayEvent::DoubleClick {
        position: _,
        size: _,
        ..
      } => {
        println!("system tray received a double click");
        app.tray_handle().set_icon(tauri::Icon::Raw(include_bytes!("../icons/icon.png"))).unwrap();
      }
      SystemTrayEvent::MenuItemClick { id, .. } => {
        let item_handle = app.tray_handle().get_item(&id);
        match id.as_str() {
          "quit" => {
            std::process::exit(0);
          }
          "hide" => {
            let window = app.get_window("main").unwrap();

            if window.is_visible().unwrap() {
              window.hide().unwrap();
              item_handle.set_title("Show").unwrap();
            } else {
              window.show().unwrap();
              item_handle.set_title("Hide").unwrap();
            }
          }
          _ => {}
        }
      }
      _ => {}
    })
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

Then just:

$ npm run tauri dev

The folllowing error is shown:

error[E0599]: no variant or associated item named `Raw` found for enum `Icon` in the current scope
  --> src\main.rs:43:49
   |
43 |         app.tray_handle().set_icon(tauri::Icon::Raw(include_bytes!("../icons/icon.png"))).unwrap();
   |                                                 ^^^ variant or associated item not found in `Icon`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `app` due to previous error

Expected behavior

The application compiles without errors.

Platform and versions

> tauri
> tauri "info"


Environment
  › OS: Windows 10.0.19044 X64
  › Webview2: 103.0.1264.62
  › MSVC:
      - Visual Studio Build Tools 2019
      - Visual Studio Build Tools 2022
  › Node.js: 16.3.0
  › npm: 7.18.1
  › pnpm: Not installed!
  › yarn: Not installed!
  › rustup: 1.25.1
  › rustc: 1.62.1
  › cargo: 1.62.1
  › Rust toolchain: stable-x86_64-pc-windows-msvc

Packages
  › @tauri-apps/cli [NPM]: 1.0.5
  › @tauri-apps/api [NPM]: 1.0.2
  › tauri [RUST]: 1.0.5,
  › tauri-build [RUST]: 1.0.4,
  › tao [RUST]: 0.12.2,
  › wry [RUST]: 0.19.0,

App
  › build-type: bundle
  › CSP: unset
  › distDir: ../dist
  › devPath: ../dist

App directory structure
  ├─ dist
  ├─ node_modules
  └─ src-tauri

Stack trace

No response

Additional context

No response

Gambloide avatar Jul 22 '22 22:07 Gambloide

the docs on docs.rs is correct. you just need to enabled ico-ico feature or ico-png feature in Cargo.toml to access the Raw or File variants of Icon

Path is not a variant of Icon as per docs.rs/tauri/1.0.5/tauri/enum.Icon.html. The only listed variants are File, Raw and RGBA.

sorry about that, we recently changed the Icon enum and the docs are not up to date. It should be File not Path

amrbashir avatar Jul 23 '22 00:07 amrbashir

Thank you for the pointer. 👍 However, adding adding the feature as below is not enough:

[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.0.4", features = ["api-all", "icon-png", "system-tray"] }

This results in another error:

error[E0308]: mismatched types
  --> src\main.rs:42:53
   |
42 |         app.tray_handle().set_icon(tauri::Icon::Raw(include_bytes!("../icons/icon.png"))).unwrap();
   |                                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: try using a conversion method: `.to_vec()`
   |                                                     |
   |                                                     expected struct `Vec`, found `&[u8; 14183]`
   |
   = note: expected struct `Vec<u8>`
           found reference `&'static [u8; 14183]`
   = note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0308`.
error: could not compile `app` due to previous error

To resolve this, the following line in the docs

app.tray_handle().set_icon(tauri::Icon::Raw(include_bytes!("../icons/icon.png"))).unwrap();

needs to get a to_vec() added like so:

app.tray_handle().set_icon(tauri::Icon::Raw(include_bytes!("../icons/icon.png").to_vec())).unwrap();

Afterwards the code compiles and the application is able to change the tray icon during runtime.

Gambloide avatar Jul 23 '22 10:07 Gambloide

Hi 👋

Today I started my first tauri app and had the same issues.

Could you please add the information that

  • the Cargo.toml file needs to be updated
  • and to_vec() needs to be added to the example

to the doc?

Thanks in advance Matthias

ruettenm avatar Sep 09 '22 10:09 ruettenm