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

macOS - Cannot change app dock icon dynamically #2985

Closed
suptejas opened this issue Nov 29, 2021 · 3 comments
Closed

macOS - Cannot change app dock icon dynamically #2985

suptejas opened this issue Nov 29, 2021 · 3 comments

Comments

@suptejas
Copy link

suptejas commented Nov 29, 2021

Describe the bug

window.set_icon() always throws errors on macOS (with icns) and does not set the dock icon. This works fine with Windows (ico) and Linux (png) in which both the window icon and the dock icon is changed.

As far as I know, this can be done using the dock api on electron with something like:
dock.setIcon(icon)

To Reproduce

Steps to reproduce the behavior:

  1. Create a new Tauri app on the latest version of macOS
  2. Go to src-tauri/src/main.rs
  3. Add a setup(|app|) function to the app build stage.
  4. let main_window = app.get_window("main").unwrap()
main_window
  .set_icon(Icon::File(
    PathBuf::from_str("<path to icns file>").unwrap(),
  ))
  .unwrap();
  1. See error:
    image
thread 'main' panicked at 'could not determine icon extension', /Users/xtremedevx/.cargo/registry/src/github.com-1ecc6299db9ec823/tauri-runtime-wry-0.2.1/src/lib.rs:467:8

Expected behavior

Since macOS does not have a window icon, there should be a way to change the icon of the app on the dock.

app.set_dock_icon() or another method could be used to set the icon of the app on the dock.

Platform and Versions (required):

Operating System - Mac OS, version 12.0.1 X64

Node.js environment
  Node.js - 16.13.0
  @tauri-apps/cli - 1.0.0-beta.10
  @tauri-apps/api - Not installed

Global packages
  npm - 8.1.0
  yarn - 1.22.17

Rust environment
  rustc - 1.56.1
  cargo - 1.56.0

App directory structure
/dist
/node_modules
/src-tauri
/.git
/src

App
  tauri.rs - 1.0.0-beta.8
  build-type - bundle
  CSP - default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'
  distDir - ../dist
  devPath - ../src
@probablykasper
Copy link
Member

probablykasper commented Nov 29, 2021

Related: #2358

The window.set_icon method probably shouldn't exist on macOS.

Similar to what's been suggested for handling menu bars, it could work like this:

  • App and AppHandle provides a set_app_icon() method.
  • Linux/Windows only: Window provides a set_icon() method which overrides the app icon, or resets it to the app icon if None is passed.

(Disclaimer: I'm not 100% sure how Linux/Windows window icons work)

@lucasfernog
Copy link
Member

For future reference, this is the code to switch the app dock icon:

#[macro_use]
extern crate objc;

fn change_icon() {
  unsafe {
    use cocoa::{
      appkit::NSImage,
      base::{id, nil},
      foundation::NSData,
    };
    let app: id = msg_send![class!(NSApplication), sharedApplication];
    let icon = vec![1, 2, 3]; // icon bytes here
    let data = NSData::dataWithBytes_length_(
      nil,
      icon.as_ptr() as *const std::os::raw::c_void,
      icon.len() as u64,
    );
	let app_icon = NSImage::initWithData_(NSImage::alloc(nil), data);
    let _: () = msg_send![app, setApplicationIconImage: app_icon];
}

@amrbashir
Copy link
Member

closing this as it will be solved by #2358

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

No branches or pull requests

4 participants