forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStyles.swift
More file actions
167 lines (147 loc) · 5.34 KB
/
Styles.swift
File metadata and controls
167 lines (147 loc) · 5.34 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import AppKit
import MarkdownUI
import SharedUIComponents
import SwiftUI
extension Color {
static var contentBackground: Color {
Color(nsColor: NSColor(name: nil, dynamicProvider: { appearance in
if appearance.isDarkMode {
return #colorLiteral(red: 0.1580096483, green: 0.1730263829, blue: 0.2026666105, alpha: 1)
}
return #colorLiteral(red: 0.9896564803, green: 0.9896564803, blue: 0.9896564803, alpha: 1)
}))
}
static var userChatContentBackground: Color {
Color(nsColor: NSColor(name: nil, dynamicProvider: { appearance in
if appearance.isDarkMode {
return #colorLiteral(red: 0.2284317913, green: 0.2145925438, blue: 0.3214019983, alpha: 1)
}
return #colorLiteral(red: 0.9458052187, green: 0.9311983998, blue: 0.9906365955, alpha: 1)
}))
}
}
extension NSAppearance {
var isDarkMode: Bool {
if bestMatch(from: [.darkAqua, .aqua]) == .darkAqua {
return true
} else {
return false
}
}
}
extension View {
var messageBubbleCornerRadius: Double { 8 }
func codeBlockLabelStyle() -> some View {
relativeLineSpacing(.em(0.225))
.markdownTextStyle {
FontFamilyVariant(.monospaced)
FontSize(.em(0.85))
}
.padding(16)
.padding(.top, 14)
}
func codeBlockStyle(
_ configuration: CodeBlockConfiguration,
backgroundColor: Color,
labelColor: Color
) -> some View {
background(backgroundColor)
.clipShape(RoundedRectangle(cornerRadius: 6))
.overlay(alignment: .top) {
HStack(alignment: .center) {
Text(configuration.language ?? "code")
.foregroundStyle(labelColor)
.font(.callout.bold())
.padding(.leading, 8)
.lineLimit(1)
Spacer()
CopyButton {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(configuration.content, forType: .string)
}
}
}
.overlay {
RoundedRectangle(cornerRadius: 6).stroke(Color.primary.opacity(0.05), lineWidth: 1)
}
.markdownMargin(top: 4, bottom: 16)
}
}
final class VerticalScrollingFixHostingView<Content>: NSHostingView<Content> where Content: View {
override func wantsForwardedScrollEvents(for axis: NSEvent.GestureAxis) -> Bool {
return axis == .vertical
}
}
struct VerticalScrollingFixViewRepresentable<Content>: NSViewRepresentable where Content: View {
let content: Content
func makeNSView(context: Context) -> NSHostingView<Content> {
return VerticalScrollingFixHostingView<Content>(rootView: content)
}
func updateNSView(_ nsView: NSHostingView<Content>, context: Context) {}
}
struct VerticalScrollingFixWrapper<Content>: View where Content: View {
let content: () -> Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}
var body: some View {
VerticalScrollingFixViewRepresentable(content: self.content())
}
}
extension View {
/// https://stackoverflow.com/questions/64920744/swiftui-nested-scrollviews-problem-on-macos
@ViewBuilder func workaroundForVerticalScrollingBugInMacOS() -> some View {
VerticalScrollingFixWrapper { self }
}
}
struct RoundedCorners: Shape {
var tl: CGFloat = 0.0
var tr: CGFloat = 0.0
var bl: CGFloat = 0.0
var br: CGFloat = 0.0
func path(in rect: CGRect) -> Path {
Path { path in
let w = rect.size.width
let h = rect.size.height
// Make sure we do not exceed the size of the rectangle
let tr = min(min(self.tr, h / 2), w / 2)
let tl = min(min(self.tl, h / 2), w / 2)
let bl = min(min(self.bl, h / 2), w / 2)
let br = min(min(self.br, h / 2), w / 2)
path.move(to: CGPoint(x: w / 2.0, y: 0))
path.addLine(to: CGPoint(x: w - tr, y: 0))
path.addArc(
center: CGPoint(x: w - tr, y: tr),
radius: tr,
startAngle: Angle(degrees: -90),
endAngle: Angle(degrees: 0),
clockwise: false
)
path.addLine(to: CGPoint(x: w, y: h - br))
path.addArc(
center: CGPoint(x: w - br, y: h - br),
radius: br,
startAngle: Angle(degrees: 0),
endAngle: Angle(degrees: 90),
clockwise: false
)
path.addLine(to: CGPoint(x: bl, y: h))
path.addArc(
center: CGPoint(x: bl, y: h - bl),
radius: bl,
startAngle: Angle(degrees: 90),
endAngle: Angle(degrees: 180),
clockwise: false
)
path.addLine(to: CGPoint(x: 0, y: tl))
path.addArc(
center: CGPoint(x: tl, y: tl),
radius: tl,
startAngle: Angle(degrees: 180),
endAngle: Angle(degrees: 270),
clockwise: false
)
path.closeSubpath()
}
}
}