|
| 1 | +import AppKit |
| 2 | +import SwiftUI |
| 3 | +import Sub2APIStatusCore |
| 4 | + |
| 5 | +enum SocialShareCardRenderer { |
| 6 | + @MainActor |
| 7 | + static func image(for summary: SocialShareSummary) -> NSImage? { |
| 8 | + let view = SocialShareCard(summary: summary) |
| 9 | + .frame(width: 920, height: 520) |
| 10 | + let renderer = ImageRenderer(content: view) |
| 11 | + renderer.proposedSize = ProposedViewSize(width: 920, height: 520) |
| 12 | + renderer.scale = 2 |
| 13 | + return renderer.nsImage |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +struct SocialShareCard: View { |
| 18 | + let summary: SocialShareSummary |
| 19 | + |
| 20 | + var body: some View { |
| 21 | + ZStack { |
| 22 | + LinearGradient( |
| 23 | + colors: [ |
| 24 | + Color(red: 0.04, green: 0.06, blue: 0.08), |
| 25 | + Color(red: 0.08, green: 0.14, blue: 0.16), |
| 26 | + Color(red: 0.04, green: 0.05, blue: 0.07), |
| 27 | + ], |
| 28 | + startPoint: .topLeading, |
| 29 | + endPoint: .bottomTrailing |
| 30 | + ) |
| 31 | + |
| 32 | + VStack(alignment: .leading, spacing: 24) { |
| 33 | + HStack(alignment: .top) { |
| 34 | + VStack(alignment: .leading, spacing: 8) { |
| 35 | + Text(summary.title) |
| 36 | + .font(.system(size: 34, weight: .bold, design: .rounded)) |
| 37 | + .foregroundStyle(.white) |
| 38 | + Text(summary.tagline) |
| 39 | + .font(.system(size: 18, weight: .medium, design: .rounded)) |
| 40 | + .foregroundStyle(Color.white.opacity(0.68)) |
| 41 | + } |
| 42 | + |
| 43 | + Spacer() |
| 44 | + |
| 45 | + Text("Sub2API Status Bar") |
| 46 | + .font(.system(size: 15, weight: .semibold, design: .rounded)) |
| 47 | + .foregroundStyle(Color.white.opacity(0.86)) |
| 48 | + .padding(.horizontal, 14) |
| 49 | + .padding(.vertical, 8) |
| 50 | + .background(Color.white.opacity(0.12), in: RoundedRectangle(cornerRadius: 8)) |
| 51 | + } |
| 52 | + |
| 53 | + HStack(alignment: .lastTextBaseline, spacing: 14) { |
| 54 | + Text(summary.primaryMetric) |
| 55 | + .font(.system(size: 90, weight: .black, design: .rounded)) |
| 56 | + .foregroundStyle(.white) |
| 57 | + .lineLimit(1) |
| 58 | + .minimumScaleFactor(0.78) |
| 59 | + Text(summary.primaryLabel.uppercased()) |
| 60 | + .font(.system(size: 18, weight: .bold, design: .rounded)) |
| 61 | + .foregroundStyle(Color(red: 0.54, green: 0.92, blue: 0.76)) |
| 62 | + .lineLimit(2) |
| 63 | + } |
| 64 | + |
| 65 | + HStack(spacing: 14) { |
| 66 | + shareMetric(title: "Spend", value: summary.spendText, tint: Color(red: 0.54, green: 0.92, blue: 0.76)) |
| 67 | + shareMetric(title: "Requests", value: summary.requestsText, tint: Color(red: 0.44, green: 0.72, blue: 1.0)) |
| 68 | + shareMetric(title: "Cost / MTok", value: summary.unitCostText, tint: Color(red: 0.96, green: 0.72, blue: 0.33)) |
| 69 | + } |
| 70 | + |
| 71 | + VStack(alignment: .leading, spacing: 11) { |
| 72 | + detailRow(icon: "sparkles", label: "Top model", value: summary.topModelText) |
| 73 | + detailRow(icon: "gauge", label: "Quota", value: summary.quotaText) |
| 74 | + detailRow(icon: "chart.line.uptrend.xyaxis", label: "Trend", value: summary.trendText) |
| 75 | + } |
| 76 | + |
| 77 | + Spacer(minLength: 0) |
| 78 | + |
| 79 | + HStack { |
| 80 | + Text(summary.generatedText) |
| 81 | + .font(.system(size: 13, weight: .medium, design: .rounded)) |
| 82 | + .foregroundStyle(Color.white.opacity(0.5)) |
| 83 | + Spacer() |
| 84 | + Text("#AIUsage #BuildInPublic") |
| 85 | + .font(.system(size: 15, weight: .bold, design: .rounded)) |
| 86 | + .foregroundStyle(Color(red: 0.54, green: 0.92, blue: 0.76)) |
| 87 | + } |
| 88 | + } |
| 89 | + .padding(34) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private func shareMetric(title: String, value: String, tint: Color) -> some View { |
| 94 | + VStack(alignment: .leading, spacing: 7) { |
| 95 | + Text(title) |
| 96 | + .font(.system(size: 13, weight: .bold, design: .rounded)) |
| 97 | + .foregroundStyle(Color.white.opacity(0.54)) |
| 98 | + Text(value) |
| 99 | + .font(.system(size: 28, weight: .black, design: .rounded).monospacedDigit()) |
| 100 | + .foregroundStyle(tint) |
| 101 | + .lineLimit(1) |
| 102 | + .minimumScaleFactor(0.72) |
| 103 | + } |
| 104 | + .frame(maxWidth: .infinity, alignment: .leading) |
| 105 | + .padding(16) |
| 106 | + .background(Color.white.opacity(0.1), in: RoundedRectangle(cornerRadius: 8)) |
| 107 | + .overlay( |
| 108 | + RoundedRectangle(cornerRadius: 8) |
| 109 | + .stroke(Color.white.opacity(0.09), lineWidth: 1) |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + private func detailRow(icon: String, label: String, value: String) -> some View { |
| 114 | + HStack(spacing: 10) { |
| 115 | + SafeSystemImage(systemName: icon, fallbackName: "circle.fill") |
| 116 | + .font(.system(size: 15, weight: .bold)) |
| 117 | + .foregroundStyle(Color(red: 0.54, green: 0.92, blue: 0.76)) |
| 118 | + .frame(width: 26, height: 26) |
| 119 | + .background(Color.white.opacity(0.1), in: RoundedRectangle(cornerRadius: 6)) |
| 120 | + Text(label) |
| 121 | + .font(.system(size: 14, weight: .bold, design: .rounded)) |
| 122 | + .foregroundStyle(Color.white.opacity(0.56)) |
| 123 | + .frame(width: 82, alignment: .leading) |
| 124 | + Text(value) |
| 125 | + .font(.system(size: 17, weight: .semibold, design: .rounded)) |
| 126 | + .foregroundStyle(Color.white.opacity(0.9)) |
| 127 | + .lineLimit(1) |
| 128 | + .minimumScaleFactor(0.72) |
| 129 | + Spacer(minLength: 0) |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments