tauri-apps / tray-icon

Tray icons for Desktop Applications.
Apache License 2.0
183 stars 27 forks source link

feat: add `TrayIcon::rect()` #147

Closed pronebird closed 2 months ago

pronebird commented 2 months ago

This PR adds a call that retrieves the tray icon frame. I am still trying to plug it into tauri to test it. Maybe some of you can do that faster. I am having hard time overriding tray-icon and tauri in my app's cargo.toml.

Fixes https://github.com/tauri-apps/tray-icon/issues/86 for macOS

Cross commit for tauri can be cherry picked from here 065b7697da28dcf509c1955bc4226e2b631cf83e

pronebird commented 2 months ago

@amrbashir I need to test macos impl a bit more. coordinates are wrong.

pronebird commented 2 months ago

So the weird thing is that rect() returns something random right after creating a tray:

let tray_icon = tray::TrayIconBuilder::with_id(TRAY_ICON_IDENTIFIER)
        .on_tray_icon_event(on_tray_icon_event)
        .menu_on_left_click(false)
        .build(app_handle)?;
log::debug!("Created tray at: {:?}", tray_icon.rect());
Created tray at: Ok(Some(Rect { position: Physical(PhysicalPosition { x: 0, y: 2148 }), size: Physical(PhysicalSize { width: 32, height: 48 }) }))

Tray icon events return the correct coordinate:

tray_icon_event: Rect { position: Physical(PhysicalPosition { x: 2424, y: 48 }), size: Physical(PhysicalSize { width: 68, height: 48 }) }

However, the subsequent calls to rect() return the correct frame even if I drag the tray icon by holding cmd + mouse drag. I assume there could be some sort of race or we need to force layout the tray icon or something. Not sure.

amrbashir commented 2 months ago

I am not expert on macOS so I can't help, let's see if @pewsheen has any ideas

pronebird commented 2 months ago

@amrbashir I see. I don't have much time to poke around today. But I'll have a fresh look at it tomorrow. Otherwise it looks pretty good already. Thank you for providing Windows/Linux implementations 🙏

Lurrobert commented 2 months ago

hey, i would love to test it! any chance you can share how can I do it? should I fork tauri the repo and change the pointing repo to your fork?

pronebird commented 2 months ago

@Lurrobert

You'd have to clone tauri, perhaps from this commit that adds the relevant call to get the tray rect: https://github.com/pronebird/tauri/commit/66ef2ba750e52d4bfb927ca8bbbfdd4d5009dfff

Then also tray-icon repo, see the branch above.

In my Cargo.toml I simply point tauri and tray-icon to the local repo:

[workspace.dependencies]
tauri = { path = "../tauri/core/tauri" }
tauri-build = { path = "../tauri/core/tauri-build" }
tauri-plugin = { path = "../tauri/core/tauri-plugin" }

[patch.crates-io]
tauri = { path = "../tauri/core/tauri" }
tray-icon = { path = "../tray-icon" }
pewsheen commented 2 months ago

I did some experiments today, and the OS takes hundreds of milliseconds to set up the frame and ready. Maybe we can document it as a limitation for now.

Another thing is that the position of the tray item looks like it's bottom-left, but this is not related to this PR.

pronebird commented 2 months ago

I concur with @pewsheen's observation. Same issue when working with statusbar in Swift. In my tests it used to return (0.0, -24.0, 86.0, 24.0) until the system laid out the window. Platform behaviour.

amrbashir commented 2 months ago

Another thing is that the position of the tray item looks like it's bottom-left, but this is not related to this PR.

@pewsheen What does that mean? aren't we using flip_window_coordinates?

pewsheen commented 2 months ago

Another thing is that the position of the tray item looks like it's bottom-left, but this is not related to this PR.

@pewsheen What does that mean? aren't we using flip_window_coordinates?

The y coordinate seems to include the height of statusbar. So it's the bottom-left of the tray-icon, not the top-left

I'm not sure if this is intended.

pronebird commented 2 months ago

@pewsheen Yes the tray icon y corresponds to menubar height. I have described the same observation in https://github.com/tauri-apps/plugins-workspace/pull/1128. This has to be fixed, eventually. :)

amrbashir commented 2 months ago

The y coordinate seems to include the height of statusbar. So it's the bottom-left of the tray-icon, not the top-left

I'm not sure if this is intended.

It is not, it should be top-left, the height should be deducted then.

amrbashir commented 2 months ago

Gonna merge this since it is not an issue with this PR, but we need to fix that behavior in another PR

pronebird commented 2 months ago

@amrbashir I have added a follow up PR in tauri if you want it https://github.com/tauri-apps/tauri/pull/9615

amrbashir commented 2 months ago

thanks, will leave a review right away

pewsheen commented 2 months ago

Gonna merge this since it is not an issue with this PR, but we need to fix that behavior in another PR

I'll fix that one later

enri90 commented 1 month ago

I get this error

error[E0599]: no method named `rect` found for struct `tray_icon::TrayIcon` in the current scope
   --> /Users/enri/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tauri-2.0.0-beta.19/src/tray/mod.rs:430:63
    |
430 |         run_item_main_thread!(self, |self_: Self| self_.inner.rect().map(|rect| {
    |                                                               ^^^^ method not found in `TrayIcon`
> tauri "info"

[✔] Environment
    - OS: Mac OS 14.2.1 X64
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.78.0 (9b00956e5 2024-04-29)
    ✔ cargo: 1.78.0 (54d8815d0 2024-03-26)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-aarch64-apple-darwin (default)
    - node: 18.15.0
    - pnpm: 9.1.1
    - npm: 9.5.0
    - bun: 1.0.3

[-] Packages
    - tauri [RUST]: 2.0.0-beta.19
    - tauri-build [RUST]: 2.0.0-beta.15
    - wry [RUST]: 0.39.3
    - tao [RUST]: 0.28.0
    - @tauri-apps/api [NPM]: 2.0.0-beta.11
    - @tauri-apps/cli [NPM]: 2.0.0-beta.17
pronebird commented 1 month ago

@enri90 you have to pin tauri to the latest.

amrbashir commented 1 month ago

run cargo update -p tray-icon inside src-tauri

enri90 commented 1 month ago

Thank you