Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/astzweig/swiftui-window-reference
🖼️ A SwiftUI View that gets the NSWindow of the scene and puts it into the environment.
https://github.com/astzweig/swiftui-window-reference
macos-app nswindow swiftui view
Last synced: about 1 month ago
JSON representation
🖼️ A SwiftUI View that gets the NSWindow of the scene and puts it into the environment.
- Host: GitHub
- URL: https://github.com/astzweig/swiftui-window-reference
- Owner: astzweig
- License: eupl-1.2
- Created: 2024-07-11T13:27:14.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-07-11T15:26:29.000Z (6 months ago)
- Last Synced: 2024-11-24T14:46:47.470Z (about 2 months ago)
- Topics: macos-app, nswindow, swiftui, view
- Language: Swift
- Homepage:
- Size: 9.77 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WindowReference
WindowReference adds a new SwiftUI view that retrieves a reference to the scene
window ([NSWindow]) and puts it in the environment.[NSWindow]: https://developer.apple.com/documentation/appkit/nswindow
## Usage
Import `WindowReference` and use it as a (sub)view inside your view hierarchy.```swift
import SwiftUI
import WindowReference@main
struct YourApp: App {
var body: some Scene {
WindowGroup("Some Window Title") {
WindowReference(withWindowInitializer: self.initWindow(_:)) {
SomeOtherView()
}
}
}func initWindow(window: NSWindow) {
window.standardWindowButton(.miniaturizeButton)?.isHidden = true
window.standardWindowButton(.zoomButton)?.isHidden = true
}
}
```Inside any child view of `WindowReference` you can grab the window from the
environment.```swift
struct SomeOtherView: View {
@Environment(\.window) var window: NSWindow?var body: some View {
if let window = self.window {
Text("This view is inside a window with title \(window.title)")
} else {
Text("No window reference found :(")
}
}
}
```## Documentation
The library has enriched symbol documentation for [DocC].[DocC]: https://www.swift.org/documentation/docc/documenting-a-swift-framework-or-package
## Testing `WindowReference`
WindowReference includes an executable target that launches a SwiftUI app to
test `WindowReference`. Either execute it with```sh
$ swift run
```or select the `TestApp` under `Product > Scheme` in Xcode.
## Adding `WindowReference` as a Dependency
To use the `WindowReference` library in a SwiftUI project, add it to the
dependencies for your package:```swift
let package = Package(
// name, platforms, products, etc.
dependencies: [
// other dependencies
.package(url: "https://github.com/astzweig/swiftui-window-reference", from: "1.0.0"),
],
targets: [
.executableTarget(name: "", dependencies: [
// other dependencies
.product(name: "WindowReference", package: "swiftui-window-reference"),
]),
// other targets
]
)
```### Supported Versions
The minimum Swift version supported by swiftui-window-reference releases are
detailed below:swiftui-window-reference | Minimum Swift Version
---------------------------|----------------------
`0.0.1 ...` | 5.10