From a258282b518893a6e23d8addde225f24cda46428 Mon Sep 17 00:00:00 2001 From: Zheng Date: Mon, 26 Sep 2022 18:02:43 -0700 Subject: [PATCH] Add glass template --- .../PrismExample.xcodeproj/project.pbxproj | 4 ++ .../PrismExample/Examples/DetailView.swift | 3 +- .../PrismExample/Examples/Glass.swift | 52 +++++++++++++++ .../PrismExample/Gallery/GalleryView.swift | 5 ++ Sources/Templates/PrismGlassView.swift | 63 +++++++++++++++++++ 5 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 Example/PrismExample/PrismExample/Examples/Glass.swift create mode 100644 Sources/Templates/PrismGlassView.swift diff --git a/Example/PrismExample/PrismExample.xcodeproj/project.pbxproj b/Example/PrismExample/PrismExample.xcodeproj/project.pbxproj index 6ce659b..ee6c5cd 100644 --- a/Example/PrismExample/PrismExample.xcodeproj/project.pbxproj +++ b/Example/PrismExample/PrismExample.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ 3C35DD3128E27CEE006B7A47 /* GalleryCardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C35DD3028E27CEE006B7A47 /* GalleryCardView.swift */; }; 3C35DD3328E27E19006B7A47 /* Gradient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C35DD3228E27E19006B7A47 /* Gradient.swift */; }; 3C35DD3528E27FD5006B7A47 /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C35DD3428E27FD5006B7A47 /* Image.swift */; }; + 3C35DD3728E28269006B7A47 /* Glass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C35DD3628E28269006B7A47 /* Glass.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -35,6 +36,7 @@ 3C35DD3028E27CEE006B7A47 /* GalleryCardView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GalleryCardView.swift; sourceTree = ""; }; 3C35DD3228E27E19006B7A47 /* Gradient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Gradient.swift; sourceTree = ""; }; 3C35DD3428E27FD5006B7A47 /* Image.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Image.swift; sourceTree = ""; }; + 3C35DD3628E28269006B7A47 /* Glass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Glass.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -94,6 +96,7 @@ 3C35DD2828E25D53006B7A47 /* Color.swift */, 3C35DD3228E27E19006B7A47 /* Gradient.swift */, 3C35DD3428E27FD5006B7A47 /* Image.swift */, + 3C35DD3628E28269006B7A47 /* Glass.swift */, 3C35DD2628E25C79006B7A47 /* DetailView.swift */, ); path = Examples; @@ -192,6 +195,7 @@ 3C35DD2E28E261DE006B7A47 /* ViewModel.swift in Sources */, 3C35DD3128E27CEE006B7A47 /* GalleryCardView.swift in Sources */, 3C35DD2C28E25DFD006B7A47 /* GalleryView.swift in Sources */, + 3C35DD3728E28269006B7A47 /* Glass.swift in Sources */, 3C35DD3328E27E19006B7A47 /* Gradient.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Example/PrismExample/PrismExample/Examples/DetailView.swift b/Example/PrismExample/PrismExample/Examples/DetailView.swift index ac0d728..573b66a 100644 --- a/Example/PrismExample/PrismExample/Examples/DetailView.swift +++ b/Example/PrismExample/PrismExample/Examples/DetailView.swift @@ -35,10 +35,12 @@ struct DetailView: View { y: 2 ) .padding(.horizontal, 20) + .padding(.top, 12) VStack { Grid { controls + .fontWeight(.semibold) Divider() sliders } @@ -47,7 +49,6 @@ struct DetailView: View { .padding(.horizontal, 20) .background(UIColor.secondarySystemBackground.color) } - .padding(.vertical, 12) } var sliders: some View { diff --git a/Example/PrismExample/PrismExample/Examples/Glass.swift b/Example/PrismExample/PrismExample/Examples/Glass.swift new file mode 100644 index 0000000..59ceeea --- /dev/null +++ b/Example/PrismExample/PrismExample/Examples/Glass.swift @@ -0,0 +1,52 @@ +// +// Glass.swift +// PrismExample +// +// Created by A. Zheng (github.com/aheze) on 9/26/22. +// Copyright © 2022 A. Zheng. All rights reserved. +// + +import Prism +import SwiftUI + +struct GlassGalleryView: View { + @ObservedObject var model: ViewModel + + var body: some View { + GalleryCardView(model: model, kind: .glass) { + PrismGlassView( + tilt: model.configuration.tilt, + size: model.configuration.size, + extrusion: model.configuration.extrusion, + levitation: model.configuration.levitation, + shadowColor: model.configuration.shadowColor, + shadowOpacity: model.configuration.shadowOpacity, + color: .blue, + opacity: 0.5 + ) + } + } +} + +struct GlassDetailView: View { + @State var color = Color.blue + @State var opacity = CGFloat(0.5) + var body: some View { + DetailView { configuration in + PrismGlassView( + tilt: configuration.tilt, + size: configuration.size, + extrusion: configuration.extrusion, + levitation: configuration.levitation, + shadowColor: configuration.shadowColor, + shadowOpacity: configuration.shadowOpacity, + color: color, + opacity: opacity + ) + } controls: { + ExampleColorView(title: "Color", value: $color) + ExampleSliderView(title: "Opacity", value: $opacity, range: 0 ... 1) + } + .navigationTitle("Glass") + } +} diff --git a/Example/PrismExample/PrismExample/Gallery/GalleryView.swift b/Example/PrismExample/PrismExample/Gallery/GalleryView.swift index 335c846..b421801 100644 --- a/Example/PrismExample/PrismExample/Gallery/GalleryView.swift +++ b/Example/PrismExample/PrismExample/Gallery/GalleryView.swift @@ -13,6 +13,7 @@ enum DetailKind: String { case color = "Color" case gradient = "Gradient" case image = "Image" + case glass = "Glass" } struct GalleryView: View { @@ -29,6 +30,8 @@ struct GalleryView: View { GradientGalleryView(model: model) ImageGalleryView(model: model) + + GlassGalleryView(model: model) } .padding(.horizontal, 20) .navigationDestination(for: DetailKind.self) { kind in @@ -39,6 +42,8 @@ struct GalleryView: View { GradientDetailView() case .image: ImageDetailView() + case .glass: + GlassDetailView() } } } diff --git a/Sources/Templates/PrismGlassView.swift b/Sources/Templates/PrismGlassView.swift new file mode 100644 index 0000000..f2bdbdd --- /dev/null +++ b/Sources/Templates/PrismGlassView.swift @@ -0,0 +1,63 @@ +// +// PrismGlassView.swift +// Prism +// +// Created by A. Zheng (github.com/aheze) on 9/26/22. +// Copyright © 2022 A. Zheng. All rights reserved. +// + +import SwiftUI + +public struct PrismGlassView: View { + var configuration: PrismConfiguration + var color: Color + var opacity: CGFloat + + public init( + configuration: PrismConfiguration, + color: Color, + opacity: CGFloat + ) { + self.configuration = configuration + self.color = color + self.opacity = opacity + } + + public var body: some View { + PrismView(configuration: configuration) { + color + .opacity(opacity - 0.2) + } left: { + color.brightness(-0.1) + .opacity(opacity - 0.1) + } right: { + color.brightness(-0.3) + .opacity(opacity) + } + } +} + +public extension PrismGlassView { + init( + tilt: CGFloat, + size: CGSize, + extrusion: CGFloat, + levitation: CGFloat = CGFloat(0), + shadowColor: SwiftUI.Color = Color.black, + shadowOpacity: CGFloat = CGFloat(0.25), + color: Color, + opacity: CGFloat + ) { + let configuration = PrismConfiguration( + tilt: tilt, + size: size, + extrusion: extrusion, + levitation: levitation, + shadowColor: shadowColor, + shadowOpacity: shadowOpacity + ) + self.configuration = configuration + self.color = color + self.opacity = opacity + } +}