Newer Older
111 lines | 3.678kb
Bogdan Timofte authored 2 weeks ago
1
//
2
//  File.swift
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 08/05/2020.
6
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
7
//
8

            
9
import SwiftUI
10

            
11
struct RoundedRectangleBackground: ViewModifier {
12

            
13
    var cornerRadius: CGFloat
14
    var foregroundColor: Color
15
    var opacity: Double
16
    var blurRadius: CGFloat
17

            
18
    func body(content: Content) -> some View {
19
        content.background(
20
            RoundedRectangle(cornerRadius: cornerRadius)
21
                .foregroundColor(foregroundColor)
22
                .opacity(opacity)
23
        .blur(radius: blurRadius)
24
        )
25
    }
26
}
27

            
28
struct RoundedRectangleBorder: ViewModifier {
29

            
30
    var cornerRadius: CGFloat
31
    var foregroundColor: Color
32
    var lineWidth: CGFloat
33
    var blurRadius: CGFloat
34

            
35
    func body(content: Content) -> some View {
36
        content.background(
37
            RoundedRectangle(cornerRadius: cornerRadius)
38
                .stroke(lineWidth: lineWidth)
39
                .foregroundColor(foregroundColor)
40
                .blur(radius: blurRadius)
41
        )
42
    }
43
}
44

            
Bogdan Timofte authored 2 weeks ago
45
struct MeterCardStyle: ViewModifier {
46
    var tint: Color
47
    var fillOpacity: Double
48
    var strokeOpacity: Double
49
    var cornerRadius: CGFloat
50

            
51
    func body(content: Content) -> some View {
52
        content
53
            .background(
54
                RoundedRectangle(cornerRadius: cornerRadius, style: .continuous)
55
                    .fill(
56
                        LinearGradient(
57
                            colors: [
58
                                tint.opacity(fillOpacity),
59
                                Color.secondary.opacity(fillOpacity * 0.75)
60
                            ],
61
                            startPoint: .topLeading,
62
                            endPoint: .bottomTrailing
63
                        )
64
                    )
65
            )
66
            .overlay(
67
                RoundedRectangle(cornerRadius: cornerRadius, style: .continuous)
68
                    .stroke(tint.opacity(strokeOpacity), lineWidth: 1.25)
69
            )
70
            .shadow(color: tint.opacity(fillOpacity * 0.9), radius: 14, x: 0, y: 8)
71
    }
72
}
73

            
Bogdan Timofte authored 2 weeks ago
74
extension View {
75
    func withRoundedRectangleBackground( cornerRadius: CGFloat, foregroundColor: Color, opacity: Double, blurRadius: CGFloat = 0 ) -> some View {
76
        self.modifier(RoundedRectangleBackground(cornerRadius: cornerRadius, foregroundColor: foregroundColor, opacity: opacity, blurRadius: blurRadius))
77
    }
78

            
79
    func withRoundedRectangleBorder( cornerRadius: CGFloat, foregroundColor: Color, lineWidth: CGFloat, blurRadius: CGFloat = 0 ) -> some View {
80
        self.modifier(RoundedRectangleBorder(cornerRadius: cornerRadius, foregroundColor: foregroundColor, lineWidth: lineWidth, blurRadius: blurRadius ))
81
    }
Bogdan Timofte authored 2 weeks ago
82

            
83
    func meterCard(
84
        tint: Color = .primary,
85
        fillOpacity: Double = 0.14,
86
        strokeOpacity: Double = 0.22,
87
        cornerRadius: CGFloat = 22
88
    ) -> some View {
89
        self.modifier(
90
            MeterCardStyle(
91
                tint: tint,
92
                fillOpacity: fillOpacity,
93
                strokeOpacity: strokeOpacity,
94
                cornerRadius: cornerRadius
95
            )
96
        )
97
    }
Bogdan Timofte authored 2 weeks ago
98
}
99

            
100
// MARK: Local
101
extension Button {
102
    func asEnableFeatureButton(state: Bool) -> some View {
103
        self
Bogdan Timofte authored 2 weeks ago
104
            .foregroundColor(state ? .white : .blue)
105
            .padding(.horizontal, 12)
106
            .padding(.vertical, 8)
107
            .frame(minWidth: 84)
108
            .withRoundedRectangleBackground(cornerRadius: 15, foregroundColor: state ? .blue : .blue, opacity: state ? 0.88 : 0.12)
109
            .withRoundedRectangleBorder(cornerRadius: 15, foregroundColor: .blue, lineWidth: 1.5, blurRadius: 0.1)
Bogdan Timofte authored 2 weeks ago
110
    }
111
}