How to show modal window in SwiftUI for macOS

Issue #768

Use custom NSWindow, set level in becomeKey and call NSApp.runModal to show modal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
final class ModalWindow: NSWindow {
override func becomeKey() {
super.becomeKey()

level = .statusBar
}

override func close() {
super.close()

NSApp.stopModal()
}
}

let window = ModalWindow(
contentRect: .zero,
styleMask: [.titled, .closable],
backing: .buffered,
defer: false
)

window.titlebarAppearsTransparent = true
window.title = "Manage collections"

window.center()
window.isReleasedWhenClosed = false
self.window = window
let view = CollectionSettingsView(store: Store.shared)
.padding()
.frame(
width: Constants.settingsViewWidth,
height: 350,
alignment: .topLeading
)
let hosting = NSHostingView(rootView: view)
window.contentView = hosting
hosting.autoresizingMask = [.width, .height]

NSApp.runModal(for: window)

Comments

You need to set client_id and slot_id to show this AD unit. Please set it in _config.yml.