Created
June 3, 2023 10:36
-
-
Save krzyzanowskim/8f756c103bd8ace6d3cf1fbb2559ba8b to your computer and use it in GitHub Desktop.
A SwiftUI wrapper around NSBox
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
/// 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