forked from CodeEditApp/CodeEdit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIconButtonStyle.swift
More file actions
120 lines (108 loc) · 3.98 KB
/
IconButtonStyle.swift
File metadata and controls
120 lines (108 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// IconButtonStyle.swift
// CodeEdit
//
// Created by Austin Condiff on 11/9/23.
//
import SwiftUI
struct IconButtonStyle: ButtonStyle {
var isActive: Bool?
var font: Font?
var size: CGSize?
init(isActive: Bool? = nil, font: Font? = nil, size: CGFloat? = nil) {
self.isActive = isActive
self.font = font
self.size = size == nil ? nil : CGSize(width: size ?? 0, height: size ?? 0)
}
init(isActive: Bool? = nil, font: Font? = nil, size: CGSize? = nil) {
self.isActive = isActive
self.font = font
self.size = size
}
init(isActive: Bool? = nil, font: Font? = nil) {
self.isActive = isActive
self.font = font
self.size = nil
}
func makeBody(configuration: ButtonStyle.Configuration) -> some View {
IconButton(
configuration: configuration,
isActive: isActive,
font: font,
size: size
)
}
struct IconButton: View {
let configuration: ButtonStyle.Configuration
var isActive: Bool
var font: Font
var size: CGSize?
@Environment(\.controlActiveState)
private var controlActiveState
@Environment(\.isEnabled)
private var isEnabled: Bool
@Environment(\.colorScheme)
private var colorScheme
init(configuration: ButtonStyle.Configuration, isActive: Bool?, font: Font?, size: CGFloat?) {
self.configuration = configuration
self.isActive = isActive ?? false
self.font = font ?? Font.system(size: 14.5, weight: .regular, design: .default)
self.size = size == nil ? nil : CGSize(width: size ?? 0, height: size ?? 0)
}
init(configuration: ButtonStyle.Configuration, isActive: Bool?, font: Font?, size: CGSize?) {
self.configuration = configuration
self.isActive = isActive ?? false
self.font = font ?? Font.system(size: 14.5, weight: .regular, design: .default)
self.size = size ?? nil
}
init(configuration: ButtonStyle.Configuration, isActive: Bool?, font: Font?) {
self.configuration = configuration
self.isActive = isActive ?? false
self.font = font ?? Font.system(size: 14.5, weight: .regular, design: .default)
self.size = nil
}
var body: some View {
configuration.label
.font(font)
.foregroundColor(
isActive
? Color(.controlAccentColor)
: Color(.secondaryLabelColor)
)
.frame(width: size?.width, height: size?.height, alignment: .center)
.contentShape(Rectangle())
.brightness(
configuration.isPressed
? colorScheme == .dark
? 0.5
: isActive ? -0.25 : -0.75
: 0
)
.opacity(controlActiveState == .inactive ? 0.5 : 1)
.symbolVariant(isActive ? .fill : .none)
}
}
}
extension ButtonStyle where Self == IconButtonStyle {
static func icon(
isActive: Bool? = false,
font: Font? = Font.system(size: 14.5, weight: .regular, design: .default),
size: CGFloat? = 24
) -> IconButtonStyle {
return IconButtonStyle(isActive: isActive, font: font, size: size)
}
static func icon(
isActive: Bool? = false,
font: Font? = Font.system(size: 14.5, weight: .regular, design: .default),
size: CGSize? = CGSize(width: 24, height: 24)
) -> IconButtonStyle {
return IconButtonStyle(isActive: isActive, font: font, size: size)
}
static func icon(
isActive: Bool? = false,
font: Font? = Font.system(size: 14.5, weight: .regular, design: .default)
) -> IconButtonStyle {
return IconButtonStyle(isActive: isActive, font: font)
}
static var icon: IconButtonStyle { .init() }
}