tauri-apps / tao

The TAO of cross-platform windowing. A library in Rust built for Tauri.
Apache License 2.0
1.54k stars 180 forks source link

feat(macOS): add `window.set_visible_on_fullscreen()` #189

Open probablykasper opened 3 years ago

probablykasper commented 3 years ago

Would allow for Spotlight-like panel windows.

Related:

StackOverflow answers for showing window over fullscreen app:

This might be how Electron implements it? https://github.com/electron/electron/blob/81c143318ba04c9aff2a5bcb80e4a1e3d55e019f/shell/browser/native_window_mac.mm#L1297-L1318

probablykasper commented 3 years ago

@srsholmes was talking about how this could work in https://github.com/tauri-apps/tauri/issues/2258 (as well as behavior across multiple spaces, which idk if there should be another issue for?)

amrbashir commented 3 years ago

on hold until tauri-apps/tao#185 is done

probablykasper commented 3 years ago

@amrbashir I don't think showing over fullscreen apps is necessarily the same as showing on all workspaces, though I don't know for sure

amrbashir commented 3 years ago

I guess that is true, it doesn't have to be coupled with set_visible_on_all_workspaces but it will be only for mac. I will transfer to tao.

srsholmes commented 3 years ago

This can be achieved (at least on macos) by the work in https://github.com/tauri-apps/tao/issues/185. It might be possibly what the

      ns_window.setLevel_(10000);

is doing.

I havn't tested it without the setlevel, but I am able to have my tauri app show over full screen apps with the tweaks mentioned in that issue.

ParthJadhav commented 1 year ago

Hey @srsholmes @probablykasper :

#[tauri::command]
pub fn show_tauri_window(window: Window) {
    let ns_window = window.ns_window().unwrap() as id;
    unsafe {
        let mut collection_behavior = ns_window.collectionBehavior();
        collection_behavior |= NSWindowCollectionBehavior::NSWindowCollectionBehaviorCanJoinAllSpaces;
        window.show().unwrap();
        ns_window.setLevel_(10000);
    }
}

As suggested this should be able to show the window above any fullscreen application. But it doesn't do that on release versions. It works fine on dev but not on realease.

MacOS: Ventura Cpu: M2

Kharya1337 commented 11 months ago

Hi Any updates on this one?

mrzhjyw commented 9 months ago

No new developments?

G07cha commented 6 months ago

I was able to make it work in release app by combining @ParthJadhav's example and adding

<key>LSUIElement</key>
<true/>

to Info.plist as mentioned in https://stackoverflow.com/a/35452482/6475535

KarthikeyanKanniappan commented 5 months ago

Hi, Do we have any update regarding this Issue

amrbashir commented 5 months ago

@pewsheen can you research this and see if we can squeeze it before v2 rc?

pewsheen commented 4 months ago

After some tries, I can make it work by setting ActivationPolicy::Accessory and setVisibleOnAllWorkspaces(true)

Can someone else confirm this works?

fn main() {
    tauri::Builder::default()
        .setup(| app | {
            app.set_activation_policy(tauri::ActivationPolicy::Accessory);
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
import { Window } from '@tauri-apps/api/window';
const window = Window.getByLabel('main');
window?.setVisibleOnAllWorkspaces(true);
window?.setAlwaysOnTop(true);
cdrani commented 4 months ago

I can confirm. Tested using M1 on v2. I didn't use the js bindings, but setting the activation policy does work in having the window always on top. However, it doesn't seem to no-op on windows and linux. I tried placing it behind a macos cfg only, but it still fails for the other OSs on test builds in an action. The below is from an ubuntu-22.04 test build:


error[E0433]: failed to resolve: could not find `ActivationPolicy` in `tauri`
   --> src/app/setup.rs:81:42
    |
81  |         app.set_activation_policy(tauri::ActivationPolicy::Accessory);
    |                                          ^^^^^^^^^^^^^^^^ could not find `ActivationPolicy` in `tauri`
    |
note: found an item that was configured out
   --> /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tauri-2.0.0-beta.17/src/lib.rs:209:18
    |
209 | pub use runtime::ActivationPolicy;
    |                  ^^^^^^^^^^^^^^^^
    = note: the item is gated behind the `macos` feature

error[E0599]: no method named `set_activation_policy` found for mutable reference `&mut tauri::App` in the current scope
  --> src/app/setup.rs:81:13
   |
81 |         app.set_activation_policy(tauri::ActivationPolicy::Accessory);
   |             ^^^^^^^^^^^^^^^^^^^^^ method not found in `&mut App`
pewsheen commented 4 months ago

Try add target_os above the line:

#[cfg(target_os = "macos")]
app.set_activation_policy(tauri::ActivationPolicy::Accessory);

I can confirm. Tested using M1 on v2. I didn't use the js bindings, but setting the activation policy does work in having the window always on top. However, it doesn't seem to no-op on windows and linux. I tried placing it behind a macos cfg only, but it still fails for the other OSs on test builds in an action. The below is from an ubuntu-22.04 test build:

cdrani commented 4 months ago

Weird. This was what I was using originally, but still causing failed builds.

if cfg!(target_os = "macos") {
    app.set_activation_policy(tauri::ActivationPolicy::Accessory);
}

...

but the builds succeed now with this:

#[cfg(target_os = "macos")]
{
    app.set_activation_policy(tauri::ActivationPolicy::Accessory);
}

...

Ideally we don't need a cfg guard at all, and it's just a no-op on unsupported OSs.

pewsheen commented 4 months ago

I think that was because cfg! marco still compiles the code, it returns a boolean. #[cfg] will conditionally compile the code.