tauri icon indicating copy to clipboard operation
tauri copied to clipboard

set_activation_policy is not accessible during runtime [bug]

Open kurtbuilds opened this issue 1 year ago • 2 comments

Describe the bug

set_activation_policy is an API that enables showing/hiding an application's icon in the doc and the application switcher. It can be set dynamically at runtime. I want to use this behavior to create an application that usually lives in the menu bar, but the menu bar can be clicked to bring up the window.

However, it's a method on App, rather than AppHandle, which means it can't be used in places I should be able to.

tauri::Builder::default()
.setup(|app| {
    app.on_tray_icon_event(move |handle, event| match event.click_type {
        _ => { // any click
           // I can't use app in here because it's been moved
           // this is what I want/should be able to do, but this API does not exist.  
           handle.set_activation_policy(ActivationPolicy::Regular);
        }
    });
    Ok(());
});

I think the method should be moved entirely from app to app handle. Alternatively, it should be duplicated on AppHandle in addition to App.

If the change is welcome, I'm happy to create a PR.

Right now, my workaround is duplicating the internal logic from tao:

fn manually_set_activation_policy(policy: ActivationPolicy) {
    use objc::{sel, sel_impl};
    use tao::platform::macos::{ActivationPolicy as TaoActivationPolicy};
    let cls = objc::runtime::Class::get("NSApplication").unwrap();
    let app: cocoa::base::id = unsafe { objc::msg_send![cls, sharedApplication] };
    let policy: TaoActivationPolicy = match policy {
        ActivationPolicy::Regular => TaoActivationPolicy::Regular,
        ActivationPolicy::Accessory => TaoActivationPolicy::Accessory,
        ActivationPolicy::Prohibited => TaoActivationPolicy::Prohibited,
        _ => panic!("Unsupported activation policy"),
    };
    let policy: cocoa::appkit::NSApplicationActivationPolicy = policy.into();
    unsafe { objc::msg_send![app, setActivationPolicy: policy] }
}

Reproduction

No response

Expected behavior

No response

Full tauri info output

tauri 2.0 beta

Stack trace

No response

Additional context

No response

kurtbuilds avatar Mar 21 '24 22:03 kurtbuilds

Agree, very much needed!

thisislvca avatar Feb 11 '25 08:02 thisislvca

I would definitely support this. I found this multiple times to be required. Rn i am using the above workaround to solve my problem.

proxie-ghanshyam avatar Nov 28 '25 19:11 proxie-ghanshyam