Skip to content

Commit

Permalink
Add glass template
Browse files Browse the repository at this point in the history
  • Loading branch information
aheze committed Sep 27, 2022
1 parent 73f69fb commit a258282
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Example/PrismExample/PrismExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -35,6 +36,7 @@
3C35DD3028E27CEE006B7A47 /* GalleryCardView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GalleryCardView.swift; sourceTree = "<group>"; };
3C35DD3228E27E19006B7A47 /* Gradient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Gradient.swift; sourceTree = "<group>"; };
3C35DD3428E27FD5006B7A47 /* Image.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Image.swift; sourceTree = "<group>"; };
3C35DD3628E28269006B7A47 /* Glass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Glass.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -94,6 +96,7 @@
3C35DD2828E25D53006B7A47 /* Color.swift */,
3C35DD3228E27E19006B7A47 /* Gradient.swift */,
3C35DD3428E27FD5006B7A47 /* Image.swift */,
3C35DD3628E28269006B7A47 /* Glass.swift */,
3C35DD2628E25C79006B7A47 /* DetailView.swift */,
);
path = Examples;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion Example/PrismExample/PrismExample/Examples/DetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ struct DetailView<Content: View, Controls: View>: View {
y: 2
)
.padding(.horizontal, 20)
.padding(.top, 12)

VStack {
Grid {
controls
.fontWeight(.semibold)
Divider()
sliders
}
Expand All @@ -47,7 +49,6 @@ struct DetailView<Content: View, Controls: View>: View {
.padding(.horizontal, 20)
.background(UIColor.secondarySystemBackground.color)
}
.padding(.vertical, 12)
}

var sliders: some View {
Expand Down
52 changes: 52 additions & 0 deletions Example/PrismExample/PrismExample/Examples/Glass.swift
Original file line number Diff line number Diff line change
@@ -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")
}
}
5 changes: 5 additions & 0 deletions Example/PrismExample/PrismExample/Gallery/GalleryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum DetailKind: String {
case color = "Color"
case gradient = "Gradient"
case image = "Image"
case glass = "Glass"
}

struct GalleryView: View {
Expand All @@ -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
Expand All @@ -39,6 +42,8 @@ struct GalleryView: View {
GradientDetailView()
case .image:
ImageDetailView()
case .glass:
GlassDetailView()
}
}
}
Expand Down
63 changes: 63 additions & 0 deletions Sources/Templates/PrismGlassView.swift
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit a258282

Please sign in to comment.