1
1
import SwiftUI
2
+ import WindowReference
2
3
3
4
/**
4
5
A SwiftUI scene that creates a bare single window without the included default items.
@@ -7,10 +8,6 @@ import SwiftUI
7
8
default window buttons. Additionally it is excluded from the applications window menu.
8
9
*/
9
10
public struct FramelessWindow < Content: View > : Scene {
10
- /// Store the window in local state to share within the environment.
11
- @State private var window : NSWindow ?
12
- @Environment ( \. scenePhase) private var scenePhase
13
-
14
11
let titleKey : LocalizedStringKey
15
12
let id : String
16
13
let content : ( ) -> Content
@@ -19,11 +16,10 @@ public struct FramelessWindow<Content: View>: Scene {
19
16
/// into the environment.
20
17
public var body : some Scene {
21
18
Window ( self . titleKey, id: self . id) {
22
- self . content ( )
23
- . ignoresSafeArea ( )
24
- . framelessWindow ( self . $window )
19
+ WindowReference ( withId : self . id , andWindowInitializer : self . modify ( window : ) ) {
20
+ self . content ( )
21
+ } . ignoresSafeArea ( )
25
22
} . windowStyle ( . hiddenTitleBar)
26
- . onChange ( of: self . scenePhase, perform: self . adaptWindowToScenePhase ( _: ) )
27
23
}
28
24
29
25
/**
@@ -37,48 +33,16 @@ public struct FramelessWindow<Content: View>: Scene {
37
33
window menu, this value has no effect. Default value is `"Launcher"`.
38
34
- content: The view content to display in the window.
39
35
*/
40
- public init ( withId id : String , andTitle titleKey : LocalizedStringKey = " Launcher " , @ViewBuilder content: @escaping ( ) -> Content ) {
41
- self . titleKey = titleKey
36
+ public init ( _ title : LocalizedStringKey = " Launcher " , id : String , @ViewBuilder content: @escaping ( ) -> Content ) {
37
+ self . titleKey = title
42
38
self . id = id
43
39
self . content = content
44
40
}
45
41
46
- /// Make window frameless on active states and remove it from environment on background state.
47
- private func adaptWindowToScenePhase( _ phase: ScenePhase ) {
48
- guard phase != . background else {
49
- self . removeWindowFromEnvironment ( )
50
- return
51
- }
52
- guard self . window == nil else { return }
53
- guard let window = self . getWindowById ( ) else { return }
54
-
55
- self . modify ( window: window)
56
- self . addWindowToEnvironment ( window)
57
- }
58
-
59
- /// Adds window to enviroment by assigning to own state variable.
60
- private func addWindowToEnvironment( _ window: NSWindow ) {
61
- self . window = window
62
- }
63
-
64
- /// Remove window from enviroment by assigning nil to own state variable.
65
- private func removeWindowFromEnvironment( ) {
66
- self . window = nil
67
- }
68
-
69
42
/// Hide the miniaturize and zoom default window buttons and remove the window from the application window menu.
70
43
private func modify( window: NSWindow ) {
71
44
window. standardWindowButton ( . miniaturizeButton) ? . isHidden = true
72
45
window. standardWindowButton ( . zoomButton) ? . isHidden = true
73
46
window. isExcludedFromWindowsMenu = true
74
47
}
75
-
76
- /// Return the window with the matching identifier from all windows of the current application.
77
- private func getWindowById( ) -> NSWindow ? {
78
- for window in NSApplication . shared. windows {
79
- guard window. identifier? . rawValue == self . id else { continue }
80
- return window
81
- }
82
- return nil
83
- }
84
48
}
0 commit comments