forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBadge.swift
More file actions
81 lines (74 loc) · 2.18 KB
/
Badge.swift
File metadata and controls
81 lines (74 loc) · 2.18 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
import SwiftUI
struct BadgeItem {
enum Level: String, Equatable {
case warning = "Warning"
case danger = "Danger"
case info = "Info"
}
let text: String
let level: Level
let icon: String?
let isSelected: Bool
init(text: String, level: Level, icon: String? = nil, isSelected: Bool = false) {
self.text = text
self.level = level
self.icon = icon
self.isSelected = isSelected
}
}
struct Badge: View {
let text: String
let level: BadgeItem.Level
let icon: String?
let isSelected: Bool
init(badgeItem: BadgeItem) {
text = badgeItem.text
level = badgeItem.level
icon = badgeItem.icon
isSelected = badgeItem.isSelected
}
init(text: String, level: BadgeItem.Level, icon: String? = nil, isSelected: Bool = false) {
self.text = text
self.level = level
self.icon = icon
self.isSelected = isSelected
}
var body: some View {
HStack(alignment: .center, spacing: 2) {
if let icon = icon {
Image(systemName: icon)
.font(.caption2)
.padding(.vertical, 1)
}
Text(text)
.fontWeight(.semibold)
.font(.caption2)
.lineLimit(1)
}
.padding(.vertical, 1)
.padding(.horizontal, 3)
.foregroundColor(
level == .info ? Color(nsColor: isSelected ? .white : .secondaryLabelColor)
: Color("\(level.rawValue)ForegroundColor")
)
.background(
level == .info ? Color(nsColor: .clear)
: Color("\(level.rawValue)BackgroundColor"),
in: RoundedRectangle(
cornerRadius: 9999,
style: .circular
)
)
.overlay(
RoundedRectangle(
cornerRadius: 9999,
style: .circular
)
.stroke(
level == .info ? Color(nsColor: isSelected ? .white : .tertiaryLabelColor)
: Color("\(level.rawValue)StrokeColor"),
lineWidth: 1
)
)
}
}