Created
October 26, 2022 08:41
-
-
Save circAssimilate/61e2ad3f2384898d7b5569dbf86f489c to your computer and use it in GitHub Desktop.
SwiftUI View with a HostingView helper.
This file contains 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 SwiftUI | |
// MARK: - CustomIconView | |
struct CustomIconView: View { | |
let containerSize: CGSize | |
let imageSize: CGSize | |
let nsImage: NSImage? | |
@Environment(\.colorScheme) var colorScheme | |
var isDarkMode: Bool { | |
colorScheme == .dark | |
} | |
var body: some View { | |
Group { | |
if let nsImage = nsImage { | |
DeadCenterView { | |
Image(nsImage: nsImage) | |
.resizable() | |
.frame(width: imageSize.width, height: imageSize.height) | |
.scaledToFit() | |
.shadow( | |
color: Color.black.opacity(0.15), | |
radius: 0, | |
x: 0, | |
y: 0 | |
) | |
} | |
} | |
} | |
.frame(width: containerSize.width, height: containerSize.height) | |
.background( | |
ZStack { | |
Rectangle().fill(.ultraThinMaterial) | |
RadialGradient( | |
gradient: Gradient( | |
colors: [ | |
Color.white, | |
Color.white.opacity(1), | |
Color.white.opacity(0.9), | |
Color.white.opacity(0.1), | |
Color.white.opacity(0.0), | |
] | |
), | |
center: .center, | |
startRadius: 0, | |
endRadius: 30 | |
) | |
.blendMode(.lighten) | |
} | |
) | |
.brandBorder( | |
Color.white.opacity(isDarkMode ? 0.05 : 0.15), | |
cornerRadius: 8 | |
) | |
.shadow( | |
color: Color.black.opacity(0.05), | |
radius: 4, | |
x: 0, | |
y: 0 | |
) | |
} | |
} | |
// MARK: - SomeHostedView | |
class SomeHostedView<ContentView: View>: NSView { | |
private(set) var hostingView: NSHostingView<ContentView>? | |
// MARK: Lifecycle | |
init(content: ContentView, frame: CGRect) { | |
super.init(frame: frame) | |
let rootView = NSHostingView(rootView: content) | |
if let hostingView = hostingView { | |
hostingView.rootView = content | |
} else { | |
let hostingView = rootView | |
addSubview(hostingView) | |
hostingView.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate([ | |
hostingView.leadingAnchor.constraint(equalTo: leadingAnchor), | |
hostingView.trailingAnchor.constraint(equalTo: trailingAnchor), | |
hostingView.topAnchor.constraint(greaterThanOrEqualTo: topAnchor), | |
hostingView.centerYAnchor.constraint(equalTo: centerYAnchor), | |
hostingView.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor), | |
]) | |
self.hostingView = hostingView | |
} | |
} | |
required init?(coder _: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
// MARK: - HostedFavicon | |
struct HostedFavicon: NSViewRepresentable { | |
let view = SomeHostedView( | |
content: CustomIconView( | |
containerSize: AppSegmentGroupItemDrawingView.AppIconSize, | |
imageSize: AppSegmentGroupItemDrawingView.FaviconSize, | |
nsImage: NSImage(named: "quick-start-dan-circle") | |
), | |
frame: CGRect( | |
x: 0, | |
y: 0, | |
width: AppSegmentGroupItemDrawingView.AppIconSize.width, | |
height: AppSegmentGroupItemDrawingView.AppIconSize.height | |
) | |
) | |
func makeNSView(context _: Context) -> some SomeHostedView<CustomIconView> { view } | |
func updateNSView(_: NSViewType, context _: Context) { } | |
} | |
// MARK: - HostedFavicon_Previews | |
struct HostedFavicon_Previews: PreviewProvider { | |
static var previews: some View { | |
HostedFavicon() | |
.frame(width: 50, height: 50) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment