How to have a window (view) on mac os to always be on top and also opens on every other desktop (SwiftUI MACOS)

Viewed 713

I have a view called sidebar. The idea is to have this view always on top no matter what, whether the user is currently on fullscreen of a different app or different desktop.

I have successfully achieved the always on top on the same desktop the app got opened, but not when it's fullscreen. This is my code:

var sidebarWindow:NSWindow
let window = NSRect(x: 0, y: 0, width: 0, height: 0)

sidebarWindow = NSWindow(
        contentRect: window,
        styleMask: [.titled,.miniaturizable],
        backing: .buffered, defer: false)

...

sidebarWindow.level = .floating
sidebarWindow.makeKeyAndOrderFront(nil)

...

   if let myScreen = NSScreen.main {
        let windowFrame = myScreen.frame
        let h = windowFrame.size.height
        let w = windowFrame.size.width
        sidebarWindow.setFrameOrigin(NSPoint(x: w-55, y:  h/2-180))
    }

    sidebarWindow.contentView = NSHostingView(rootView: SideBar(thisWindow: sidebarWindow))

But, as I described above, this Sidebar() (sidebarWindow) is not visible when I'm entering other app in full screen, as well as when I'm changing my desktop to other desktop.

How can I make SideBar() to always be on top no matter what, whether the user is currently on fullscreen of a different app or different desktop?

Sorry if it's unclear.

Thanks a lot, any help would be appreciated!

0 Answers
Related