Reputation: 43
On macOS, setting window.styleMask = [] makes the window fully borderless, but it also becomes invisible to system tools like Desktop Manager, Magnet, or Moom, preventing them from detecting, resizing, or repositioning it.
class AppDelegate: NSObject, NSApplicationDelegate {
private lazy var window: CustomWindow = CustomWindow()
func applicationDidFinishLaunching(_ aNotification: Notification) {
window.styleMask = [] // Or: [.closable, .miniaturizable, .resizable, .fullSizeContentView]
window.backingType = .buffered
window.isReleasedWhenClosed = false
window.center()
window.setFrameAutosaveName("Main Window")
window.title = "Window"
window.isMovableByWindowBackground = true
window.contentView = NSHostingView(rootView: ContentView())
window.makeKeyAndOrderFront(window)
}
}
class CustomWindow: NSWindow {
override var canBecomeKey: Bool { return true }
override var canBecomeMain: Bool { return true }
}
I tried another approach with the following code, which solves the issue of the window being invisible to desktop tools, but it comes at the cost of having rounded corners, which I do not want.
How can I create a truly borderless window that is still trackable by system tools while completely removing the corner radius?
class AppDelegate: NSObject, NSApplicationDelegate {
private lazy var window: NSWindow = NSWindow()
func applicationDidFinishLaunching(_ aNotification: Notification) {
window.styleMask = [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView]
window.backingType = .buffered
window.isReleasedWhenClosed = false
window.center()
window.setFrameAutosaveName("Main Window")
window.title = "Window"
window.isMovableByWindowBackground = true
window.titleVisibility = .hidden
window.titlebarAppearsTransparent = true
window.standardWindowButton(.closeButton)?.isHidden = true
window.standardWindowButton(.miniaturizeButton)?.isHidden = true
window.standardWindowButton(.zoomButton)?.isHidden = true
window.contentView = NSHostingView(rootView: ContentView())
window.makeKeyAndOrderFront(window)
}
}
I am using a main.swift file to run the app on macOS 12.7.6 with Xcode 14.2 and a minimum deployment target of 12.0.
struct ContentView: View {
var body: some View {
Color.white
.frame(minWidth: 400.0, minHeight: 200.0)
.ignoresSafeArea()
.overlay(content: {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
}
.foregroundColor(Color.black)
.background(Color.white)
})
}
}
In summary, I am looking for one of these approaches to solve the issue:
1.Making a window with window.styleMask
set to [.closable, .miniaturizable, .resizable, .fullSizeContentView]
or []
detectable by desktop manager apps.
2.Using [.closable, .miniaturizable, .resizable, .fullSizeContentView, .titled]
for window.styleMask
, which is detectable by the desktop manager tool, but removing the window's corner radius as explained in the question.
New Info:
I found that the part where other desktop managers try to read and interact with my app is being read through the accessibilityFocusedUIElement
of the window. This can work if coded correctly. I need to keep my UI styled the way I want it, using attributes like [.closable, .miniaturizable, .resizable, .fullSizeContentView]
. However, the accessibilityFocusedUIElement
returns a fake one, like this, which is necessary for desktop manager tools to start recognizing my app: [.closable, .miniaturizable, .resizable, .fullSizeContentView, .titled]
. But that is not easy. I printed out the unmodified window.accessibilityFocusedUIElement
, and the result was like this: "output: Type:<Any>, value: <NSWindow_NSAccessibility_Test.CustomWindow: 0x7f97c1105020>"
. It shows that other apps are trying to read the accessibilityFocusedUIElement
of my app, likely the window. It also includes some encryption or serialization, such as "0x7f97c1105020," which changes each time I run the app.
extension CustomWindow {
override var accessibilityFocusedUIElement: Any {
}
}
Upvotes: 2
Views: 164