File size: 7,298 Bytes
b8370f5 | 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | import Foundation
import SwiftUI
struct ChatTalkTranscriptStripModel: Equatable {
let recentTranscript: [String]
let partialTranscript: String
init(recentTranscript: [String], partialTranscript: String, limit: Int = 20) {
let finals = recentTranscript
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
self.recentTranscript = Array(finals.suffix(max(0, limit)))
self.partialTranscript = partialTranscript.trimmingCharacters(in: .whitespacesAndNewlines)
}
var hasTranscript: Bool {
!self.recentTranscript.isEmpty || !self.partialTranscript.isEmpty
}
var collapsedText: String? {
if !self.partialTranscript.isEmpty { return self.partialTranscript }
return self.recentTranscript.last
}
}
@MainActor
struct ChatTalkActivityStrip: View {
let control: OpenClawChatTalkControl
@State private var isExpanded = false
var body: some View {
let model = ChatTalkTranscriptStripModel(
recentTranscript: self.control.recentTranscript,
partialTranscript: self.control.partialTranscript)
if self.control.isEnabled {
VStack(alignment: .leading, spacing: 6) {
if model.hasTranscript {
Button {
withAnimation(.easeInOut(duration: 0.16)) {
self.isExpanded.toggle()
}
} label: {
self.header(model: model, showsDisclosure: true)
}
.buttonStyle(.plain)
.accessibilityLabel(self.isExpanded
? String(localized: "Hide talk transcript")
: String(localized: "Show talk transcript"))
} else {
self.header(model: model, showsDisclosure: false)
}
if self.isExpanded, model.hasTranscript {
ScrollView {
VStack(alignment: .leading, spacing: 5) {
ForEach(Array(model.recentTranscript.enumerated()), id: \.offset) { _, line in
Text(line)
.font(OpenClawChatTypography.caption)
.foregroundStyle(.primary)
.frame(maxWidth: .infinity, alignment: .leading)
}
if !model.partialTranscript.isEmpty {
Text(model.partialTranscript)
.font(OpenClawChatTypography.caption.italic())
.foregroundStyle(OpenClawChatTheme.accent)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
}
.frame(maxHeight: 104)
.transition(.opacity.combined(with: .move(edge: .top)))
}
}
.padding(.horizontal, 10)
.padding(.vertical, 7)
.background(
RoundedRectangle(cornerRadius: 10, style: .continuous)
.fill(OpenClawChatTheme.subtleCard))
.overlay(
RoundedRectangle(cornerRadius: 10, style: .continuous)
.strokeBorder(OpenClawChatTheme.composerBorder, lineWidth: 1))
.padding(.horizontal, 14)
.accessibilityIdentifier("chat-talk-activity")
}
}
private func header(model: ChatTalkTranscriptStripModel, showsDisclosure: Bool) -> some View {
HStack(spacing: 7) {
Circle()
.fill(OpenClawChatTheme.accent)
.frame(width: 7, height: 7)
Text(self.control.statusText)
.font(OpenClawChatTypography.captionSemiBold)
.lineLimit(1)
if let collapsedText = model.collapsedText, !self.isExpanded {
Text(collapsedText)
.font(OpenClawChatTypography.caption)
.foregroundStyle(model.partialTranscript.isEmpty ? .secondary : OpenClawChatTheme.accent)
.italic(!model.partialTranscript.isEmpty)
.lineLimit(1)
}
Spacer(minLength: 0)
if showsDisclosure {
Image(systemName: self.isExpanded ? "chevron.up" : "chevron.down")
.font(.caption2.weight(.semibold))
.foregroundStyle(.secondary)
}
}
.contentShape(Rectangle())
}
}
struct ChatTalkButtonGlyph: View {
let control: OpenClawChatTalkControl
private static let activePalette = TalkWaveformPalette(
active: [.white, .white.opacity(0.78), .white.opacity(0.55)],
inactive: [.white.opacity(0.65)])
var body: some View {
if self.control.isEnabled {
TalkWaveformView(phase: self.waveformPhase, palette: Self.activePalette)
.frame(width: 23, height: 16)
.allowsHitTesting(false)
} else {
Image(systemName: "waveform")
}
}
private var waveformPhase: TalkWaveformPhase {
if self.control.isListening {
return .listening(
level: self.control.level,
speechActive: !self.control.partialTranscript.isEmpty)
}
if self.control.isSpeaking {
return .speaking(level: self.control.level)
}
return .thinking
}
}
private struct ChatTalkInputDeviceMenuModifier: ViewModifier {
let control: OpenClawChatTalkControl
func body(content: Content) -> some View {
if self.control.selectInputDevice != nil {
content.contextMenu {
Button {
self.control.selectInputDevice?(nil)
} label: {
self.menuLabel(
String(localized: "System Default"),
selected: self.control.selectedInputDeviceID == nil)
}
if !self.control.inputDevices.isEmpty {
Divider()
ForEach(self.control.inputDevices) { device in
Button {
self.control.selectInputDevice?(device.id)
} label: {
self.menuLabel(
device.name,
selected: self.control.selectedInputDeviceID == device.id)
}
}
}
}
} else {
content
}
}
@ViewBuilder
private func menuLabel(_ text: String, selected: Bool) -> some View {
if selected {
Label(text, systemImage: "checkmark")
.font(OpenClawChatTypography.body)
} else {
Text(text)
.font(OpenClawChatTypography.body)
}
}
}
extension View {
func chatTalkInputDeviceMenu(_ control: OpenClawChatTalkControl) -> some View {
self.modifier(ChatTalkInputDeviceMenuModifier(control: control))
}
}
|