]> git.r.bdr.sh - rbdr/patterns/blob - Sources/Patterns/PatternView.swift
Use CoreGraphics to draw instead of SwifTUI
[rbdr/patterns] / Sources / Patterns / PatternView.swift
1 import SwiftUI
2
3 public struct PatternView: View {
4
5 @Binding public var design: TileDesign
6 public var pixelSize: CGFloat
7 public var foregroundColor: Color
8 public var backgroundColor: Color
9
10 private let image: CGImage
11 private var patternSize: CGFloat {
12 pixelSize * 8.0;
13 }
14
15 public init(design: Binding<TileDesign>, pixelSize: CGFloat = 2.0, foregroundColor: Color = .black, backgroundColor: Color = .white) {
16 self._design = design
17 self.pixelSize = pixelSize
18 self.foregroundColor = foregroundColor
19 self.backgroundColor = backgroundColor
20
21 #if os(iOS) || os(watchOS) || os(tvOS)
22 let foregroundCGColor = UIColor(foregroundColor).cgColor
23 let backgroundCGColor = UIColor(backgroundColor).cgColor
24 #else
25 let foregroundCGColor = NSColor(foregroundColor).cgColor
26 let backgroundCGColor = NSColor(backgroundColor).cgColor
27 #endif
28
29 self.image = TileImage.image(design.wrappedValue, pixelSize: pixelSize, foregroundColor: foregroundCGColor, backgroundColor: backgroundCGColor)
30 }
31
32 public var body: some View {
33 GeometryReader { gr in
34 Image(image, scale: 1, label: Text("Test")).resizable(resizingMode: .tile)
35 }.drawingGroup()
36 }
37 }
38
39 struct Pattern_Previews: PreviewProvider {
40 static var previews: some View {
41 VStack {
42 Text("Default")
43 PatternView(design: .constant(TileDesign.grid))
44 Text("Color override")
45 PatternView(design: .constant(TileDesign.balls), foregroundColor: .pink, backgroundColor: .cyan)
46 Text("Pixel size override")
47 PatternView(design: .constant(TileDesign.shingles), pixelSize: 8.0)
48 }
49 }
50 }