Skip to content

Instantly share code, notes, and snippets.

@leogdion
Created August 17, 2023 21:49
Show Gist options
  • Save leogdion/a9d665b8c1e6f9f62df3e539fde08404 to your computer and use it in GitHub Desktop.
Save leogdion/a9d665b8c1e6f9f62df3e539fde08404 to your computer and use it in GitHub Desktop.
Allow SwiftUI to modifier NSWindow
//
// NSWindowAdaptorModifier.swift
// Copyright (c) 2023 BrightDigit.
//
import AppKit
import Foundation
import SwiftUI
// swiftlint:disable strict_fileprivate
private struct NSWindowAdaptorHostingView: NSViewRepresentable {
private var callback: (NSWindow?) -> Void
fileprivate init(callback: @escaping (NSWindow?) -> Void) {
self.callback = callback
}
fileprivate func makeNSView(context _: Self.Context) -> NSView {
let view = NSView()
DispatchQueue.main.async { [weak view] in
self.callback(view?.window)
}
view.setFrameSize(.zero)
view.isHidden = true
view.frame = CGRect.zero
return view
}
fileprivate func updateNSView(_: NSView, context _: Context) {}
}
private struct NSWindowAdaptorModifier: ViewModifier {
private var callback: (NSWindow?) -> Void
fileprivate init(callback: @escaping (NSWindow?) -> Void) {
self.callback = callback
}
fileprivate func body(content: Content) -> some View {
content
.overlay(
NSWindowAdaptorHostingView(callback: callback)
.frame(width: 0, height: 0)
)
}
}
public extension View {
func nsWindowAdaptor(_ callback: @escaping (NSWindow?) -> Void) -> some View {
self.modifier(NSWindowAdaptorModifier(callback: callback))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment