I am working on a project for removing maximize, minimize and close buttons which we can see in every window in every App/projects build with Xcode for macOS in SwiftUI-life cycle. How could I do this?

PS: Please consider I am coding on SwiftUI-life cycle/macOS and if your code or way is not compatible with that then I could not make use of it. thanks for help and your time.
Update, date 05-March-2022: The question is still open and looks for a SwiftUI approach. You can answer this question with SwiftUI api.
Came across this question when looking to do similar myself so thought I'd add answer for posterity - or at least until Apple decides to make SwiftUI work nicely on macOS.
Anyway, the trick is to get hold of the window that holds the SwiftUI View and then tweak that. Easiest approach seems to be to listen for NSApplication's didBecomeActiveNotification and then grab a reference from NSApplication.shared mainWindow, keyWindow (or possibly filter it out by title from windows) as works best for the app in question.
Example of this using mainWindow for a helloWorld program...
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.padding()
.frame(minWidth: 200, minHeight: 200)
.navigationTitle("Undecorated")
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification), perform: { _ in
NSApp.mainWindow?.standardWindowButton(.zoomButton)?.isHidden = true
NSApp.mainWindow?.standardWindowButton(.closeButton)?.isHidden = true
NSApp.mainWindow?.standardWindowButton(.miniaturizeButton)?.isHidden = true
})
}
}
Notes
Although the buttons are gone from the window it is important to note that the menu entries and hotkeys for maximising, hiding and killing the window are still fully operational. That functionallity has to be removed separately from customising the window appearance.
The alternative method I've seen to get hold of the window is to add an dummy "Find My Window" type View to the UI that uses AppKit to create it, and grabs a reference to the window when it does so. There is a nice exposition of this approach over on LostMoa's blog here.
Would be very interested if anyone has found anything better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With