tauri-apps / tao

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

[macOS] `NSPanel` behavior needed: TaoWindow subclassing `NSWindow` with fixed overwrites (?) #414

Open Hupka opened 2 years ago

Hupka commented 2 years ago

Hey folks,

I need a feature in Tauri, and I am attempting to work my way upstream through the libraries which Tauri builds upon.

Context

I need Tauri/Tao windows to behave like NSPanel in macOS. NSPanel is not part of the core-foundation/cocoa library, wherefore I am working to contribute there (core-foundation/#511). The change is relatively straightforward and has already been tested successfully. 👌🏻

I am now investigating how Tao is working with the cocoa crate to instantiate windows. And happened to find a code block from 5 years ago [❗Implemetation today] that seems to actually subclass NSWindow introducing the class TaoWindow. For this subclass, the methods canBecomeMainWindow and canBecomeKeyWindow are overwritten to always return yes. Strangely, this is the default behavior of NSWindow 🤔 .

Q1: Is Tao here actually subclassing NSWindow overwriting default behavior? Or is it just potentially rewriting responses coming from macOS when calling the methods listed there?

Goal

I want to help make the contribution needed to allow Tao to spawn views as either NSWindow or NSPanel.

Solution

Because of the questions I wrote about above, it is unclear how to proceed. Early investigations made me consider introducing a new field in PlatformSpecificWindowBuilderAttributes to spawn a window in either an NSWindow or NSPanel. Theoretically, the code change would be minimal if the underlying core-foundation/cocoa library provides this class. NSPanelshares all methods of NSWindow, wherefore no breaking changes would be introduced. NSPanel introduces three additional methods which, for the time being, could be just invoked directly on the ns_window().

If I get some guidance on how to conceptually introduce such changes, I'm offering to make the contribution myself. 🤓

probablykasper commented 2 years ago

So according to the commit it's there to make windows resizable when there's no decorations. I would've assumed that you could remove decorations while keeping NSWindowStyleMask::NSResizableWindowMask?

@lemarier @wusyong and @keiya01 seem to have touched that code, so maybe they have some ideas

qqpann commented 1 year ago

I'd like to see this feature implemented. @Hupka Is there any update on this? Or could you provide more hints about how to make this happen?

ahkohd commented 1 year ago

I have some exciting news! After much experimentation, I finally created a fully functional NSPanel from Tauri's NSWindow. Please take a look at https://github.com/ahkohd/tauri-macos-spotlight-example/pull/6 for more details. It would be great to get your feedback on this new development.

My next objective is to develop a Tauri plugin, tauri-nspanel. This plugin will be a valuable tool for creating NSPanels in Tauri's NSWindow, allowing us to streamline the process.

A rough API to convert to NSPanel might look like this.

#[cfg(target="macos")]
window.to_panel();
ahkohd commented 1 year ago

Yoyoyo, it's here https://github.com/ahkohd/tauri-nspanel