Skip to content

Instantly share code, notes, and snippets.

@krzyzanowskim
Created June 3, 2023 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krzyzanowskim/8f756c103bd8ace6d3cf1fbb2559ba8b to your computer and use it in GitHub Desktop.
Save krzyzanowskim/8f756c103bd8ace6d3cf1fbb2559ba8b to your computer and use it in GitHub Desktop.
A SwiftUI wrapper around NSBox
/// A SwiftUI wrapper around `NSBox`.
public struct Box<Content>: View where Content: View {
private let title: String?
private let content: () -> Content
public init(_ title: String? = nil, @ViewBuilder content: @escaping () -> Content) {
self.title = title
self.content = content
}
public var body: some View {
VStack(alignment: .leading) {
if let title = title {
SwiftUI.Text(title)
}
BoxContent(content: content)
}
}
}
private struct BoxContent<Content>: NSViewRepresentable where Content: View {
private let content: () -> Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}
func updateNSView(_ box: NSBox, context: Context) {
}
func makeNSView(context: Context) -> NSBox {
let box = NSBox()
box.titlePosition = .noTitle
box.contentViewMargins = NSSize(width: 1, height: 2)
let contentView = NSHostingView(rootView: content())
contentView.wantsLayer = true
contentView.layer?.cornerRadius = 4
contentView.layer?.masksToBounds = true
box.contentView = contentView
return box
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment