forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolActivityTimelineLayout.swift
More file actions
179 lines (162 loc) · 5.77 KB
/
Copy pathToolActivityTimelineLayout.swift
File metadata and controls
179 lines (162 loc) · 5.77 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
167
168
169
170
171
172
173
174
175
176
177
178
179
import OmiTheme
import SwiftUI
/// Shared geometry for the live tool-activity rail. Different SF Symbols have
/// different optical bearings; every glyph is forced into the same centered
/// square so the connector and the row of icons stay on one axis. The header
/// hugs its label so the disclosure sits beside the tool name instead of the
/// trailing edge of the chat column.
enum ToolActivityTimelineLayout {
static let iconGlyphSize = OmiType.body
static let iconColumn: CGFloat = 18
static let connectorWidth: CGFloat = 1
static let connectorBottomTrim: CGFloat = 6
static let disclosureSize = OmiType.micro
static let labelToDisclosureSpacing: CGFloat = 2
static let rowIconSpacing = OmiSpacing.xxs
static var headerHeight: CGFloat { iconColumn }
static var rowMinHeight: CGFloat { iconColumn }
static var connectorTopInset: CGFloat { iconColumn - 1 }
/// Left edge of the 1pt connector, centered on the icon column.
static var connectorOriginX: CGFloat {
(iconColumn - connectorWidth) / 2
}
static var expandedContentLeadingInset: CGFloat {
iconColumn + rowIconSpacing
}
static func iconGlyphFrame(inColumn column: CGFloat = iconColumn) -> CGRect {
CGRect(
x: (column - iconGlyphSize) / 2,
y: (column - iconGlyphSize) / 2,
width: iconGlyphSize,
height: iconGlyphSize
)
}
struct HeaderFrames: Equatable {
var label: CGRect
var disclosure: CGRect?
var width: CGFloat
}
/// Intrinsic header frames. The production header is an HStack of these
/// sizes, not a flexible spacer that fills the chat column.
static func headerFrames(labelWidth: CGFloat, hasDisclosure: Bool) -> HeaderFrames {
let label = CGRect(x: 0, y: 0, width: labelWidth, height: headerHeight)
guard hasDisclosure else {
return HeaderFrames(label: label, disclosure: nil, width: labelWidth)
}
let disclosure = CGRect(
x: labelWidth + labelToDisclosureSpacing,
y: (headerHeight - disclosureSize) / 2,
width: disclosureSize,
height: disclosureSize
)
return HeaderFrames(label: label, disclosure: disclosure, width: disclosure.maxX)
}
static func symbol(for name: String) -> String {
let cleanName = String(name.split(separator: "__").last ?? Substring(name)).lowercased()
let tokens = Set(cleanName.split { !$0.isLetter && !$0.isNumber }.map(String.init))
if tokens.contains("search") || cleanName.hasPrefix("grep") || cleanName.hasPrefix("glob") {
return "magnifyingglass"
}
if tokens.contains("read") || tokens.contains("fetch") { return "doc.text" }
if tokens.contains("write") || tokens.contains("edit") { return "pencil" }
if tokens.contains("bash") || tokens.contains("shell") || tokens.contains("command") {
return "terminal"
}
if cleanName.contains("agent") { return "person.2" }
if cleanName.contains("calendar") { return "calendar" }
if cleanName.contains("mail") || cleanName.contains("message") { return "envelope" }
if cleanName.contains("permission") { return "lock" }
if cleanName.contains("screen") || cleanName.contains("capture") { return "rectangle.dashed" }
return "sparkles"
}
}
struct ToolCallActivityHeadline<Header: View>: View {
let name: String
let status: ToolCallStatus
let header: Header
init(
name: String,
status: ToolCallStatus,
@ViewBuilder header: () -> Header
) {
self.name = name
self.status = status
self.header = header()
}
var body: some View {
HStack(alignment: .center, spacing: ToolActivityTimelineLayout.rowIconSpacing) {
ToolCallActivityIcon(name: name, status: status)
.accessibilityHidden(true)
header
}
}
}
struct ToolCallActivityIcon: View {
let name: String
let status: ToolCallStatus
var body: some View {
Group {
switch status {
case .running:
ProgressView()
.controlSize(.mini)
case .slow:
ProgressView()
.controlSize(.mini)
.tint(PageGlass.warning)
case .stalled:
alignedSymbol("exclamationmark.triangle.fill", color: PageGlass.warning)
case .completed:
alignedSymbol(ToolActivityTimelineLayout.symbol(for: name), color: Ink.secondary)
case .failed:
alignedSymbol("xmark.circle", color: Ink.errorRed)
}
}
.frame(
width: ToolActivityTimelineLayout.iconColumn,
height: ToolActivityTimelineLayout.iconColumn,
alignment: .center
)
.background(Ink.surface.opacity(0.94), in: Circle())
}
private func alignedSymbol(_ systemName: String, color: Color) -> some View {
Image(systemName: systemName)
.resizable()
.scaledToFit()
.foregroundColor(color)
.frame(
width: ToolActivityTimelineLayout.iconGlyphSize,
height: ToolActivityTimelineLayout.iconGlyphSize,
alignment: .center
)
}
}
struct ToolCallHeaderLabel: View {
let title: String
var summary: String? = nil
let showsDisclosure: Bool
var isExpanded = false
var body: some View {
HStack(alignment: .center, spacing: ToolActivityTimelineLayout.labelToDisclosureSpacing) {
Text(title)
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.secondary)
.fixedSize(horizontal: true, vertical: false)
if showsDisclosure {
Image(systemName: isExpanded ? "chevron.up" : "chevron.down")
.scaledFont(size: OmiType.micro)
.foregroundColor(Ink.secondary)
.fixedSize()
.accessibilityHidden(true)
}
if let summary, !summary.isEmpty {
Text(summary)
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.secondary.opacity(0.72))
.lineLimit(1)
.truncationMode(.middle)
}
}
.frame(minHeight: ToolActivityTimelineLayout.headerHeight, alignment: .leading)
}
}