Newer Older
65 lines | 2.121kb
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

            
45
extension View {
46
    func withRoundedRectangleBackground( cornerRadius: CGFloat, foregroundColor: Color, opacity: Double, blurRadius: CGFloat = 0 ) -> some View {
47
        self.modifier(RoundedRectangleBackground(cornerRadius: cornerRadius, foregroundColor: foregroundColor, opacity: opacity, blurRadius: blurRadius))
48
    }
49

            
50
    func withRoundedRectangleBorder( cornerRadius: CGFloat, foregroundColor: Color, lineWidth: CGFloat, blurRadius: CGFloat = 0 ) -> some View {
51
        self.modifier(RoundedRectangleBorder(cornerRadius: cornerRadius, foregroundColor: foregroundColor, lineWidth: lineWidth, blurRadius: blurRadius ))
52
    }
53
}
54

            
55
// MARK: Local
56
extension Button {
57
    func asEnableFeatureButton(state: Bool) -> some View {
58
        self
59
            .foregroundColor( state ? .primary : .blue )
60
            .padding(5)
61
            .withRoundedRectangleBackground(cornerRadius: 15, foregroundColor: state ? .blue : .clear, opacity: 0.5)
62
            .withRoundedRectangleBorder(cornerRadius: 15, foregroundColor: .blue, lineWidth: 1, blurRadius: 0.1)
63
    }
64
}
65