Skip to content

Commit 9310084

Browse files
committed
Use swiftui-window-reference
1 parent 07d76e2 commit 9310084

File tree

5 files changed

+19
-67
lines changed

5 files changed

+19
-67
lines changed

Package.swift

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.7
1+
// swift-tools-version: 5.10
22
import PackageDescription
33

44
let package = Package(
@@ -9,8 +9,16 @@ let package = Package(
99
name: "FramelessWindow",
1010
targets: ["FramelessWindow"])
1111
],
12+
dependencies: [
13+
.package(url: "https://github.com/astzweig/swiftui-window-reference", from: "1.0.0")
14+
],
1215
targets: [
13-
.target(name: "FramelessWindow"),
16+
.target(
17+
name: "FramelessWindow",
18+
dependencies: [
19+
.product(name: "WindowReference", package: "swiftui-window-reference")
20+
]
21+
),
1422
.executableTarget(
1523
name: "TestApp",
1624
dependencies: ["FramelessWindow"]

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import FramelessWindow
1515
@main
1616
struct YourApp: App {
1717
var body: some Scene {
18-
FramelessWindow(withId: "window-id") {
18+
FramelessWindow("Window Title", id: "window-id") {
1919
ContentView()
2020
}
2121
}
@@ -66,3 +66,4 @@ The minimum Swift version supported by swiftui-frameless-window releases are det
6666
swiftui-frameless-window | Minimum Swift Version
6767
---------------------------|----------------------
6868
`0.0.1 ...` | 5.7
69+
`2.0.0 ...` | 5.10

Sources/FramelessWindow/Environment.swift

-21
This file was deleted.
+6-42
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import SwiftUI
2+
import WindowReference
23

34
/**
45
A SwiftUI scene that creates a bare single window without the included default items.
@@ -7,10 +8,6 @@ import SwiftUI
78
default window buttons. Additionally it is excluded from the applications window menu.
89
*/
910
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-
1411
let titleKey: LocalizedStringKey
1512
let id: String
1613
let content: () -> Content
@@ -19,11 +16,10 @@ public struct FramelessWindow<Content: View>: Scene {
1916
/// into the environment.
2017
public var body: some Scene {
2118
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()
2522
}.windowStyle(.hiddenTitleBar)
26-
.onChange(of: self.scenePhase, perform: self.adaptWindowToScenePhase(_:))
2723
}
2824

2925
/**
@@ -37,48 +33,16 @@ public struct FramelessWindow<Content: View>: Scene {
3733
window menu, this value has no effect. Default value is `"Launcher"`.
3834
- content: The view content to display in the window.
3935
*/
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
4238
self.id = id
4339
self.content = content
4440
}
4541

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-
6942
/// Hide the miniaturize and zoom default window buttons and remove the window from the application window menu.
7043
private func modify(window: NSWindow) {
7144
window.standardWindowButton(.miniaturizeButton)?.isHidden = true
7245
window.standardWindowButton(.zoomButton)?.isHidden = true
7346
window.isExcludedFromWindowsMenu = true
7447
}
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-
}
8448
}

Sources/TestApp/main.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import FramelessWindow
33

44
struct TestApp: App {
55
var body: some Scene {
6-
FramelessWindow(withId: "single-window", andTitle: "Single Window") {
6+
FramelessWindow("Single Window", id: "single-window") {
77
VStack {
88
Image(systemName: "globe").font(.title).foregroundStyle(.blue)
99
Text("Hello, world!")

0 commit comments

Comments
 (0)