tauri-apps / tauri

Build smaller, faster, and more secure desktop and mobile applications with a web frontend.
https://tauri.app
Apache License 2.0
84.57k stars 2.54k forks source link

[bug] setLevel_ and setCollectionBehavior_ not work in fullscreen of release but work in fullscreen of dev #5566

Open Sneaken opened 2 years ago

Sneaken commented 2 years ago

Describe the bug

The following code works fine in the development environment, but will not take effect after packaging.

fn main() {
    tauri::Builder::default()
        .setup(|app| {
            let main_window = app.get_window("main").unwrap();

            #[cfg(target_os = "macos")]
            {
                use cocoa::appkit::{NSMainMenuWindowLevel, NSWindow, NSWindowCollectionBehavior};
                use cocoa::base::id;
                let ns_win = main_window.ns_window().unwrap() as id;
                unsafe {
                    ns_win.setLevel_(((NSMainMenuWindowLevel + 1) as u64).try_into().unwrap());
                    ns_win.setCollectionBehavior_(
                        NSWindowCollectionBehavior::NSWindowCollectionBehaviorCanJoinAllSpaces,
                    );
                }
            }

            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Reproduction

  1. yarn create tauri-app
  2. cd [workspace]
  3. yarn
  4. modify Cargo.toml
    [target.'cfg(target_os = "macos")'.dependencies]
    cocoa = "0.24.1"
  5. replace main fn
    
    use tauri::Manager;

fn main() { tauri::Builder::default() .setup(|app| { let main_window = app.get_window("main").unwrap();

        #[cfg(target_os = "macos")]
        {
            use cocoa::appkit::{NSMainMenuWindowLevel, NSWindow, NSWindowCollectionBehavior};
            use cocoa::base::id;
            let ns_win = main_window.ns_window().unwrap() as id;
            unsafe {
                ns_win.setLevel_(((NSMainMenuWindowLevel + 1) as u64).try_into().unwrap());
                ns_win.setCollectionBehavior_(
                    NSWindowCollectionBehavior::NSWindowCollectionBehaviorCanJoinAllSpaces,
                );
            }
        }

        Ok(())
    })
    .run(tauri::generate_context!())
    .expect("error while running tauri application");

}


6. yarn tarui dev // everything is ok
7. yarn tarui build // setLevel_ and setCollectionBehavior_ not work in fullscreen

### Expected behavior

Apps can also be pinned to the top in full screen

### Platform and versions

Environment
  › OS: Mac OS 13.0.0 X64
  › Node.js: 16.15.1
  › npm: 8.11.0
  › pnpm: 7.8.0
  › yarn: 1.22.19
  › rustup: 1.25.1
  › rustc: 1.63.0
  › cargo: 1.63.0
  › Rust toolchain: stable-aarch64-apple-darwin 

Packages
  › @tauri-apps/cli [NPM]: 1.1.1
  › @tauri-apps/api [NPM]: 1.1.0
  › tauri [RUST]: 1.1.1,
  › tauri-build [RUST]: 1.1.1,
  › tao [RUST]: 0.14.0,
  › wry [RUST]: 0.21.1,

App
  › build-type: bundle
  › CSP: unset
  › distDir: ../dist
  › devPath: http://localhost:1420/
  › framework: React

App directory structure
  ├─ dist
  ├─ node_modules
  ├─ public
  ├─ src-tauri
  ├─ .git
  ├─ .vscode
  ├─ .idea
  └─ src

### Stack trace

_No response_

### Additional context

_No response_
CcSimple commented 1 year ago

Is this supported now?

Jinghao1209 commented 1 year ago

Maybe this can help you: By BillGoldenWater: https://github.com/BillGoldenWater/ChaosDanmuTool/blob/dev/backend/src/utils/window_utils/macos.rs Discord: https://discord.com/channels/616186924390023171/1113520012800577569

ayangweb commented 3 weeks ago

Hi all👋, setLevel_ Although it does the desired effect, the system input runs behind the window when typing. Has anyone encountered this problem and solved it please?

issue: https://github.com/EcoPasteHub/EcoPaste/issues/621

ahkohd commented 3 weeks ago

Normal NSWindow cannot be drawn over other full-screen windows.

You can convert an NSWindow to an NSPanel and set the necessary flags to allow it to draw over other full-screen windows. See this PR:

https://github.com/lumehq/lume/pull/203/commits/5a0e8539f5f83df3c161779b33e38bd43a9307c3

// Set a higher level
panel.set_level(NSMainMenuWindowLevel + 1);

// Prevents your panel from activating the owing application 
panel.set_style_mask(NSWindowStyleMaskNonActivatingPanel);

// Allow your panel to join fullscreen spaces; you can tweak this configuration
panel.set_collection_behaviour(
    NSWindowCollectionBehavior::NSWindowCollectionBehaviorCanJoinAllSpaces
      | NSWindowCollectionBehavior::NSWindowCollectionBehaviorStationary
      | NSWindowCollectionBehavior::NSWindowCollectionBehaviorFullScreenAuxiliary,
  );

Also, refer to this issue: https://github.com/tauri-apps/tauri/issues/9556