@@ -10,19 +10,25 @@ import SwiftUI
10
10
/// A window overlay for SwiftUI.
11
11
struct WindowOverlay < Content: View > : AppKitOrUIKitViewControllerRepresentable {
12
12
private let content : Content
13
- private let isKeyAndVisible : Binding < Bool >
14
-
15
- init ( content: Content , isKeyAndVisible: Binding < Bool > ) {
13
+ private let canBecomeKey : Bool
14
+ private let isVisible : Binding < Bool >
15
+
16
+ init (
17
+ content: Content ,
18
+ canBecomeKey: Bool ,
19
+ isVisible: Binding < Bool >
20
+ ) {
16
21
self . content = content
17
- self . isKeyAndVisible = isKeyAndVisible
22
+ self . canBecomeKey = canBecomeKey
23
+ self . isVisible = isVisible
18
24
}
19
25
20
26
func makeAppKitOrUIKitViewController( context: Context ) -> AppKitOrUIKitViewControllerType {
21
- . init( content: content, isKeyAndVisible : isKeyAndVisible )
27
+ . init( content: content, canBecomeKey : canBecomeKey , isVisible : isVisible )
22
28
}
23
29
24
30
func updateAppKitOrUIKitViewController( _ viewController: AppKitOrUIKitViewControllerType , context: Context ) {
25
- viewController. isKeyAndVisible = isKeyAndVisible
31
+ viewController. isVisible = isVisible
26
32
viewController. content = content
27
33
28
34
viewController. updateWindow ( )
@@ -59,16 +65,18 @@ extension WindowOverlay {
59
65
}
60
66
}
61
67
62
- var isKeyAndVisible : Binding < Bool >
68
+ var canBecomeKey : Bool
69
+ var isVisible : Binding < Bool >
63
70
var contentWindow : AppKitOrUIKitHostingWindow < Content > ?
64
71
#if os(macOS)
65
72
var contentWindowController : NSWindowController ?
66
73
#endif
67
74
68
- init ( content: Content , isKeyAndVisible : Binding < Bool > ) {
75
+ init ( content: Content , canBecomeKey : Bool , isVisible : Binding < Bool > ) {
69
76
self . content = content
70
- self . isKeyAndVisible = isKeyAndVisible
71
-
77
+ self . canBecomeKey = canBecomeKey
78
+ self . isVisible = isVisible
79
+
72
80
super. init ( nibName: nil , bundle: nil )
73
81
74
82
#if os(macOS)
@@ -77,11 +85,11 @@ extension WindowOverlay {
77
85
}
78
86
79
87
func updateWindow( ) {
80
- if let contentWindow = contentWindow, contentWindow. isHidden == !isKeyAndVisible . wrappedValue {
88
+ if let contentWindow = contentWindow, contentWindow. isHidden == !isVisible . wrappedValue {
81
89
return
82
90
}
83
91
84
- if isKeyAndVisible . wrappedValue {
92
+ if isVisible . wrappedValue {
85
93
#if !os(macOS)
86
94
guard let window = view? . window, let windowScene = window. windowScene else {
87
95
return
@@ -109,7 +117,8 @@ extension WindowOverlay {
109
117
#endif
110
118
111
119
contentWindow. rootView = content
112
- contentWindow. isKeyAndVisible = isKeyAndVisible
120
+ contentWindow. _canBecomeKey = canBecomeKey
121
+ contentWindow. isVisible = isVisible
113
122
114
123
#if os(macOS)
115
124
contentWindow. title = " "
@@ -119,7 +128,7 @@ extension WindowOverlay {
119
128
contentWindow. isHidden = false
120
129
contentWindow. isUserInteractionEnabled = true
121
130
contentWindow. windowLevel = . init( rawValue: window. windowLevel. rawValue + 1 )
122
-
131
+
123
132
contentWindow. makeKeyAndVisible ( )
124
133
125
134
contentWindow. rootViewController? . view. setNeedsDisplay ( )
@@ -155,7 +164,7 @@ extension WindowOverlay {
155
164
@objc
156
165
public func windowWillClose( _ notification: Notification ? ) {
157
166
if ( notification? . object as? AppKitOrUIKitHostingWindow < Content > ) === contentWindow {
158
- isKeyAndVisible . wrappedValue = false
167
+ isVisible . wrappedValue = false
159
168
}
160
169
}
161
170
#endif
@@ -165,15 +174,28 @@ extension WindowOverlay {
165
174
// MARK: - Helpers -
166
175
167
176
extension View {
168
- /// Makes a window key and visible when a given condition is true
177
+ /// Makes a window visible when a given condition is true.
178
+ ///
179
+ /// - Parameters:
180
+ /// - isVisible: A binding to whether the window is visible.
181
+ /// - content: A closure returning the content of the window.
182
+ public func windowOverlay< Content: View > (
183
+ isVisible: Binding < Bool > ,
184
+ @ViewBuilder _ content: ( ) -> Content
185
+ ) -> some View {
186
+ background ( WindowOverlay ( content: content ( ) , canBecomeKey: false , isVisible: isVisible) )
187
+ }
188
+
189
+ /// Makes a window key and visible when a given condition is true.
190
+ ///
169
191
/// - Parameters:
170
192
/// - isKeyAndVisible: A binding to whether the window is key and visible.
171
193
/// - content: A closure returning the content of the window.
172
194
public func windowOverlay< Content: View > (
173
195
isKeyAndVisible: Binding < Bool > ,
174
196
@ViewBuilder _ content: ( ) -> Content
175
197
) -> some View {
176
- background ( WindowOverlay ( content: content ( ) , isKeyAndVisible : isKeyAndVisible) )
198
+ background ( WindowOverlay ( content: content ( ) , canBecomeKey : true , isVisible : isKeyAndVisible) )
177
199
}
178
200
}
179
201
0 commit comments