Created
November 20, 2020 12:56
-
-
Save OskarGroth/3b729b251fce91f3386bd9ea76f7c8bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
import SwiftUI | |
public extension View { | |
func bindWindow(_ window: Binding<NSWindow?>) -> some View { | |
return background(WindowAccessor(window)) | |
} | |
} | |
public struct WindowAccessor: NSViewRepresentable { | |
@Binding public var window: NSWindow? | |
public init(_ window: Binding<NSWindow?>) { | |
self._window = window | |
} | |
public func makeNSView(context: Context) -> NSView { | |
return WindowAccessorView(binding: $window) | |
} | |
public func updateNSView(_ nsView: NSView, context: Context) {} | |
} | |
class WindowAccessorView: NSView { | |
@Binding var windowBinding: NSWindow? | |
init(binding: Binding<NSWindow?>) { | |
self._windowBinding = binding | |
super.init(frame: .zero) | |
} | |
override func viewDidMoveToWindow() { | |
super.viewDidMoveToWindow() | |
windowBinding = window | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment