|
| 1 | +import AppKit |
| 2 | +import AXorcist |
| 3 | +import DesignSystem |
| 4 | +import Diagnostics |
| 5 | +@preconcurrency import ScreenCaptureKit |
| 6 | +import SwiftUI |
| 7 | + |
| 8 | +struct ClaudeTerminalScreenshotPopover: View { |
| 9 | + let instance: ClaudeInstance |
| 10 | + |
| 11 | + @State private var screenshotImage: NSImage? |
| 12 | + @State private var isLoading = false |
| 13 | + @State private var errorMessage: String? |
| 14 | + @StateObject private var screenshotAnalyzer = CursorScreenshotAnalyzer() |
| 15 | + |
| 16 | + private let logger = Logger(category: .ui) |
| 17 | + |
| 18 | + var body: some View { |
| 19 | + VStack(spacing: Spacing.medium) { |
| 20 | + // Header |
| 21 | + HStack { |
| 22 | + Image(systemName: "terminal") |
| 23 | + .foregroundColor(ColorPalette.accent) |
| 24 | + Text("Terminal Screenshot") |
| 25 | + .font(Typography.callout(.semibold)) |
| 26 | + Spacer() |
| 27 | + } |
| 28 | + |
| 29 | + // Screenshot content |
| 30 | + Group { |
| 31 | + if isLoading { |
| 32 | + VStack(spacing: Spacing.small) { |
| 33 | + ProgressView() |
| 34 | + .progressViewStyle(CircularProgressViewStyle()) |
| 35 | + Text("Capturing terminal window...") |
| 36 | + .font(Typography.caption1()) |
| 37 | + .foregroundColor(ColorPalette.textSecondary) |
| 38 | + } |
| 39 | + .frame(width: 400, height: 300) |
| 40 | + } else if let image = screenshotImage { |
| 41 | + Image(nsImage: image) |
| 42 | + .resizable() |
| 43 | + .aspectRatio(contentMode: .fit) |
| 44 | + .frame(maxWidth: 600, maxHeight: 400) |
| 45 | + .cornerRadius(8) |
| 46 | + .shadow(radius: 2) |
| 47 | + } else if let error = errorMessage { |
| 48 | + VStack(spacing: Spacing.small) { |
| 49 | + Image(systemName: "exclamationmark.triangle") |
| 50 | + .font(.largeTitle) |
| 51 | + .foregroundColor(ColorPalette.warning) |
| 52 | + Text("Failed to capture screenshot") |
| 53 | + .font(Typography.callout(.medium)) |
| 54 | + Text(error) |
| 55 | + .font(Typography.caption1()) |
| 56 | + .foregroundColor(ColorPalette.textSecondary) |
| 57 | + .multilineTextAlignment(.center) |
| 58 | + } |
| 59 | + .frame(width: 400, height: 300) |
| 60 | + } else { |
| 61 | + VStack(spacing: Spacing.small) { |
| 62 | + Image(systemName: "photo") |
| 63 | + .font(.largeTitle) |
| 64 | + .foregroundColor(ColorPalette.textSecondary) |
| 65 | + Text("No screenshot available") |
| 66 | + .font(Typography.callout(.medium)) |
| 67 | + .foregroundColor(ColorPalette.textSecondary) |
| 68 | + } |
| 69 | + .frame(width: 400, height: 300) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Instance info |
| 74 | + VStack(alignment: .leading, spacing: Spacing.xSmall) { |
| 75 | + HStack { |
| 76 | + Text("Folder:") |
| 77 | + .font(Typography.caption1(.medium)) |
| 78 | + .foregroundColor(ColorPalette.textSecondary) |
| 79 | + Text(instance.folderName) |
| 80 | + .font(Typography.caption1()) |
| 81 | + .foregroundColor(ColorPalette.text) |
| 82 | + Spacer() |
| 83 | + } |
| 84 | + |
| 85 | + HStack { |
| 86 | + Text("TTY:") |
| 87 | + .font(Typography.caption1(.medium)) |
| 88 | + .foregroundColor(ColorPalette.textSecondary) |
| 89 | + Text(instance.ttyPath) |
| 90 | + .font(Typography.caption2()) |
| 91 | + .foregroundColor(ColorPalette.textTertiary) |
| 92 | + .textSelection(.enabled) |
| 93 | + Spacer() |
| 94 | + } |
| 95 | + |
| 96 | + HStack { |
| 97 | + Text("PID:") |
| 98 | + .font(Typography.caption1(.medium)) |
| 99 | + .foregroundColor(ColorPalette.textSecondary) |
| 100 | + Text(String(instance.pid)) |
| 101 | + .font(Typography.caption2()) |
| 102 | + .foregroundColor(ColorPalette.textTertiary) |
| 103 | + Spacer() |
| 104 | + } |
| 105 | + } |
| 106 | + .padding(.horizontal, Spacing.small) |
| 107 | + .padding(.vertical, Spacing.xSmall) |
| 108 | + .background(ColorPalette.backgroundTertiary) |
| 109 | + .cornerRadius(6) |
| 110 | + |
| 111 | + // Action buttons |
| 112 | + HStack(spacing: Spacing.medium) { |
| 113 | + DSButton("Refresh", style: .secondary, size: .small) { |
| 114 | + captureScreenshot() |
| 115 | + } |
| 116 | + .disabled(isLoading) |
| 117 | + |
| 118 | + if screenshotImage != nil { |
| 119 | + DSButton("Save to Desktop", style: .primary, size: .small) { |
| 120 | + saveImageToDesktop() |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + .padding(Spacing.large) |
| 126 | + .frame(minWidth: 500, maxWidth: 700) |
| 127 | + .withDesignSystem() |
| 128 | + .onAppear { |
| 129 | + captureScreenshot() |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private func captureScreenshot() { |
| 134 | + isLoading = true |
| 135 | + errorMessage = nil |
| 136 | + screenshotImage = nil |
| 137 | + |
| 138 | + Task { |
| 139 | + do { |
| 140 | + // First, find the terminal window using TTY mapping |
| 141 | + guard let terminalWindow = await findTerminalWindow() else { |
| 142 | + await MainActor.run { |
| 143 | + self.isLoading = false |
| 144 | + self.errorMessage = "Could not find terminal window for this Claude instance" |
| 145 | + } |
| 146 | + return |
| 147 | + } |
| 148 | + |
| 149 | + // Find the SCWindow for this terminal window |
| 150 | + let scWindow = await findSCWindow(for: terminalWindow) |
| 151 | + |
| 152 | + // Capture the screenshot |
| 153 | + let image = try await screenshotAnalyzer.captureCursorWindow(targetSCWindow: scWindow) |
| 154 | + |
| 155 | + await MainActor.run { |
| 156 | + self.screenshotImage = image |
| 157 | + self.isLoading = false |
| 158 | + |
| 159 | + if image == nil { |
| 160 | + self.errorMessage = "Could not capture window. The window may be minimized or hidden." |
| 161 | + } |
| 162 | + } |
| 163 | + } catch { |
| 164 | + await MainActor.run { |
| 165 | + self.isLoading = false |
| 166 | + self.errorMessage = error.localizedDescription |
| 167 | + logger.error("Failed to capture screenshot for Claude instance \(instance.id): \(error)") |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @MainActor |
| 174 | + private func findTerminalWindow() async -> Element? { |
| 175 | + // Use TTYWindowMappingService to find the window |
| 176 | + if !instance.ttyPath.isEmpty { |
| 177 | + return TTYWindowMappingService.shared.findWindowForTTY(instance.ttyPath) |
| 178 | + } |
| 179 | + return nil |
| 180 | + } |
| 181 | + |
| 182 | + private func findSCWindow(for element: Element) async -> SCWindow? { |
| 183 | + do { |
| 184 | + let content = try await SCShareableContent.excludingDesktopWindows(false, onScreenWindowsOnly: true) |
| 185 | + |
| 186 | + // Get window ID from the element using WindowInfoHelper |
| 187 | + guard let windowID = WindowInfoHelper.getWindowID(from: element) else { |
| 188 | + logger.warning("Could not get window ID from element") |
| 189 | + return nil |
| 190 | + } |
| 191 | + |
| 192 | + // Find matching SCWindow |
| 193 | + return content.windows.first { window in |
| 194 | + window.windowID == windowID |
| 195 | + } |
| 196 | + } catch { |
| 197 | + logger.error("Failed to get shareable content: \(error)") |
| 198 | + return nil |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + private func saveImageToDesktop() { |
| 203 | + guard let image = screenshotImage else { return } |
| 204 | + |
| 205 | + let desktopURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first! |
| 206 | + let timestamp = DateFormatter().apply { formatter in |
| 207 | + formatter.dateFormat = "yyyy-MM-dd_HH-mm-ss" |
| 208 | + }.string(from: Date()) |
| 209 | + let filename = "CodeLooper_Terminal_\(instance.folderName)_\(timestamp).png" |
| 210 | + let fileURL = desktopURL.appendingPathComponent(filename) |
| 211 | + |
| 212 | + if let tiffData = image.tiffRepresentation, |
| 213 | + let bitmapRep = NSBitmapImageRep(data: tiffData), |
| 214 | + let pngData = bitmapRep.representation(using: .png, properties: [:]) { |
| 215 | + |
| 216 | + do { |
| 217 | + try pngData.write(to: fileURL) |
| 218 | + logger.info("Screenshot saved to: \(fileURL.path)") |
| 219 | + |
| 220 | + // Show in Finder |
| 221 | + NSWorkspace.shared.activateFileViewerSelecting([fileURL]) |
| 222 | + } catch { |
| 223 | + logger.error("Failed to save screenshot: \(error)") |
| 224 | + self.errorMessage = "Failed to save screenshot: \(error.localizedDescription)" |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +// MARK: - Extension for DateFormatter |
| 231 | + |
| 232 | +private extension DateFormatter { |
| 233 | + func apply(_ closure: (DateFormatter) -> Void) -> DateFormatter { |
| 234 | + closure(self) |
| 235 | + return self |
| 236 | + } |
| 237 | +} |
| 238 | + |
0 commit comments