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
121 lines (111 loc) · 3.71 KB
/
Badge.swift
File metadata and controls
121 lines (111 loc) · 3.71 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
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
let tooltip: String?
init(text: String, level: Level, icon: String? = nil, isSelected: Bool = false, tooltip: String? = nil) {
self.text = text
self.level = level
self.icon = icon
self.isSelected = isSelected
self.tooltip = tooltip
}
}
struct Badge: View {
let text: String
let attributedText: AttributedString?
let level: BadgeItem.Level
let icon: String?
let isSelected: Bool
let tooltip: String?
init(badgeItem: BadgeItem) {
text = badgeItem.text
attributedText = nil
level = badgeItem.level
icon = badgeItem.icon
isSelected = badgeItem.isSelected
tooltip = badgeItem.tooltip
}
init(text: String, level: BadgeItem.Level, icon: String? = nil, isSelected: Bool = false, tooltip: String? = nil) {
self.text = text
self.attributedText = nil
self.level = level
self.icon = icon
self.isSelected = isSelected
self.tooltip = tooltip
}
init(attributedText: AttributedString, level: BadgeItem.Level, icon: String? = nil, isSelected: Bool = false, tooltip: String? = nil) {
self.text = String(attributedText.characters)
self.attributedText = attributedText
self.level = level
self.icon = icon
self.isSelected = isSelected
self.tooltip = tooltip
}
var body: some View {
HStack(alignment: .center, spacing: 2) {
if let icon = icon {
Image(systemName: icon)
.font(.caption2)
.padding(.vertical, 1)
}
if let attributedText = attributedText, attributedText.characters.count > 0 {
Text(attributedText)
.fontWeight(.semibold)
.font(.caption2)
.lineLimit(1)
.truncationMode(.middle)
} else if !text.isEmpty {
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
)
)
.help(tooltip ?? text)
}
}
extension BadgeItem {
static func disabledByPolicy(feature: String, isPlural: Bool = false) -> BadgeItem {
let verb = isPlural ? "are" : "is"
let pronoun = isPlural ? "them" : "it"
return .init(
text: "Disabled by organization policy",
level: .warning,
icon: "exclamationmark.triangle.fill",
tooltip: "\(feature) \(verb) disabled by your organization's policy. Please contact your administrator to enable \(pronoun)."
)
}
}