mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-05-05 02:16:44 +00:00
feat(mac): native Swift menubar app + one-command install
Introduces mac/ with a native SwiftUI menubar app that replaces the previous SwiftBar plugin entirely. Install via `npx codeburn menubar`, which downloads the .app from GitHub Releases, strips Gatekeeper quarantine, and drops it into ~/Applications. Highlights - mac/ SwiftUI app: agent tabs, Today/7/30/Month/All period switcher, Trend/Forecast/Pulse/Stats/Plan insights, activity + model breakdowns, optimize findings, CSV/JSON export, Star-on-GitHub banner, live 60s refresh, instant currency switching with offline FX cache. - Security: CodeburnCLI argv-based spawn (no shell interpretation), SafeFile symlink guards + O_NOFOLLOW writes, FX rate clamping to [0.0001, 1_000_000], keychain filtered to account == "default", removed byte-window credential log, in-flight refresh guard, POSIX flock on config.json writes, TerminalLauncher validates argv before AppleScript interpolation. - Performance: shared static NumberFormatter (thousands of allocations per popover redraw eliminated), concurrent pipe drain with 20 MB cap + 60s timeout in DataClient, Observation-tracked reactive UI, 5-min payload cache keyed on (period, provider). - CLI: new `codeburn menubar` subcommand that downloads + installs + launches the .app (no clone, no build). New `status --format menubar-json` payload builder. `export` rewritten to produce a folder of one-table-per-file CSVs with a `.codeburn-export` marker so arbitrary -o paths cannot be silently deleted. - Removed: src/menubar.ts (SwiftBar plugin generator), install-menubar / uninstall-menubar subcommands, `status --format menubar` directive output, tests/menubar.test.ts, tests/security/menubar-injection.test.ts. - Release: .github/workflows/release-menubar.yml builds universal binary, assembles .app, ad-hoc signs, zips, uploads on mac-v* tag push. Runs on the free macos-latest runner. Tests - 230 TypeScript tests pass - 10 Swift CapacityEstimator tests pass - TypeScript typecheck clean - Swift release build clean
This commit is contained in:
parent
69268a9e91
commit
495a254338
46 changed files with 6433 additions and 575 deletions
87
mac/Sources/CodeBurnMenubar/Views/ActivitySection.swift
Normal file
87
mac/Sources/CodeBurnMenubar/Views/ActivitySection.swift
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
import SwiftUI
|
||||
|
||||
struct ActivitySection: View {
|
||||
@Environment(AppStore.self) private var store
|
||||
@State private var isExpanded: Bool = true
|
||||
|
||||
var body: some View {
|
||||
CollapsibleSection(
|
||||
caption: "Activity",
|
||||
isExpanded: $isExpanded,
|
||||
trailing: {
|
||||
HStack(spacing: 8) {
|
||||
Text("Cost").frame(minWidth: 54, alignment: .trailing)
|
||||
Text("Turns").frame(minWidth: 52, alignment: .trailing)
|
||||
Text("1-shot").frame(minWidth: 44, alignment: .trailing)
|
||||
}
|
||||
.font(.system(size: 10, weight: .medium))
|
||||
.foregroundStyle(.tertiary)
|
||||
.tracking(-0.05)
|
||||
}
|
||||
) {
|
||||
VStack(alignment: .leading, spacing: 7) {
|
||||
let maxCost = store.payload.current.topActivities.map(\.cost).max() ?? 1
|
||||
ForEach(store.payload.current.topActivities, id: \.name) { activity in
|
||||
ActivityRow(activity: activity, maxCost: maxCost)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ActivityRow: View {
|
||||
let activity: ActivityEntry
|
||||
let maxCost: Double
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 8) {
|
||||
FixedBar(fraction: activity.cost / maxCost)
|
||||
.frame(width: 56, height: 6)
|
||||
|
||||
Text(activity.name)
|
||||
.font(.system(size: 12.5, weight: .medium))
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
|
||||
Text(activity.cost.asCompactCurrency())
|
||||
.font(.codeMono(size: 12, weight: .medium))
|
||||
.tracking(-0.2)
|
||||
.frame(minWidth: 54, alignment: .trailing)
|
||||
|
||||
Text("\(activity.turns)")
|
||||
.font(.system(size: 11))
|
||||
.monospacedDigit()
|
||||
.foregroundStyle(.secondary)
|
||||
.frame(minWidth: 52, alignment: .trailing)
|
||||
|
||||
Text(oneShotText)
|
||||
.font(.system(size: 10.5))
|
||||
.monospacedDigit()
|
||||
.foregroundStyle(.secondary)
|
||||
.frame(minWidth: 44, alignment: .trailing)
|
||||
}
|
||||
.padding(.horizontal, 2)
|
||||
.padding(.vertical, 1)
|
||||
}
|
||||
|
||||
private var oneShotText: String {
|
||||
guard let rate = activity.oneShotRate else { return "—" }
|
||||
return "\(Int(rate * 100))%"
|
||||
}
|
||||
}
|
||||
|
||||
/// Fixed-width horizontal bar that shows a fill fraction.
|
||||
struct FixedBar: View {
|
||||
let fraction: Double
|
||||
|
||||
var body: some View {
|
||||
GeometryReader { geo in
|
||||
ZStack(alignment: .leading) {
|
||||
RoundedRectangle(cornerRadius: 2)
|
||||
.fill(.secondary.opacity(0.15))
|
||||
RoundedRectangle(cornerRadius: 2)
|
||||
.fill(Theme.brandAccent)
|
||||
.frame(width: max(0, min(geo.size.width, geo.size.width * CGFloat(fraction))))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
92
mac/Sources/CodeBurnMenubar/Views/AgentTabStrip.swift
Normal file
92
mac/Sources/CodeBurnMenubar/Views/AgentTabStrip.swift
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
import SwiftUI
|
||||
|
||||
struct AgentTabStrip: View {
|
||||
@Environment(AppStore.self) private var store
|
||||
|
||||
var body: some View {
|
||||
ScrollView(.horizontal, showsIndicators: false) {
|
||||
HStack(spacing: 5) {
|
||||
ForEach(visibleFilters) { filter in
|
||||
Button {
|
||||
Task { await store.switchTo(provider: filter) }
|
||||
} label: {
|
||||
AgentTab(
|
||||
filter: filter,
|
||||
cost: cost(for: filter),
|
||||
isActive: store.selectedProvider == filter
|
||||
)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.top, 8)
|
||||
.padding(.bottom, 4)
|
||||
}
|
||||
}
|
||||
|
||||
/// Drive tab visibility and per-tab cost labels from the *all-provider* payload (today),
|
||||
/// not the currently selected provider's payload. Without this, switching to Codex (which
|
||||
/// has no data) would hide every other tab including Claude.
|
||||
private var allProvidersToday: MenubarPayload {
|
||||
store.todayPayload ?? store.payload
|
||||
}
|
||||
|
||||
private var visibleFilters: [ProviderFilter] {
|
||||
let activeKeys = Set(allProvidersToday.current.providers.keys.map { $0.lowercased() })
|
||||
return ProviderFilter.allCases.filter { filter in
|
||||
if filter == .all { return true }
|
||||
return activeKeys.contains(filter.rawValue.lowercased())
|
||||
}
|
||||
}
|
||||
|
||||
private func cost(for filter: ProviderFilter) -> Double? {
|
||||
switch filter {
|
||||
case .all:
|
||||
return allProvidersToday.current.cost
|
||||
default:
|
||||
let key = filter.rawValue.lowercased()
|
||||
return allProvidersToday.current.providers[key]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct AgentTab: View {
|
||||
let filter: ProviderFilter
|
||||
let cost: Double?
|
||||
let isActive: Bool
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 5) {
|
||||
Text(filter.rawValue)
|
||||
.font(.system(size: 11.5, weight: .medium))
|
||||
.tracking(-0.05)
|
||||
if let cost, cost > 0 {
|
||||
Text(cost.asCompactCurrency())
|
||||
.font(.codeMono(size: 10.5, weight: .medium))
|
||||
.foregroundStyle(isActive ? AnyShapeStyle(.white.opacity(0.8)) : AnyShapeStyle(.secondary))
|
||||
.tracking(-0.2)
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 10)
|
||||
.padding(.vertical, 4)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 6)
|
||||
.fill(isActive ? AnyShapeStyle(Theme.brandAccent) : AnyShapeStyle(Color.secondary.opacity(0.08)))
|
||||
)
|
||||
.foregroundStyle(isActive ? AnyShapeStyle(.white) : AnyShapeStyle(.secondary))
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
}
|
||||
|
||||
extension ProviderFilter {
|
||||
var color: Color {
|
||||
switch self {
|
||||
case .all: return Theme.brandAccent
|
||||
case .claude: return Theme.categoricalClaude
|
||||
case .codex: return Theme.categoricalCodex
|
||||
case .cursor: return Theme.categoricalCursor
|
||||
case .copilot: return Color(red: 0x6D/255.0, green: 0x8F/255.0, blue: 0xA6/255.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
290
mac/Sources/CodeBurnMenubar/Views/FindingsSection.swift
Normal file
290
mac/Sources/CodeBurnMenubar/Views/FindingsSection.swift
Normal file
|
|
@ -0,0 +1,290 @@
|
|||
import SwiftUI
|
||||
|
||||
private let winColor = Theme.brandAccent
|
||||
private let riskColor = Theme.brandAccent
|
||||
private let improveColor = Theme.brandAccent
|
||||
|
||||
/// Three-category insights panel: wins, improvements, risks.
|
||||
/// Wins/risks are derived from current + history; improvements come from the optimize findings.
|
||||
struct FindingsSection: View {
|
||||
@Environment(AppStore.self) private var store
|
||||
@State private var isExpanded: Bool = true
|
||||
|
||||
var body: some View {
|
||||
let groups = computeTipGroups(payload: store.payload)
|
||||
if groups.allSatisfy({ $0.items.isEmpty }) { return AnyView(EmptyView()) }
|
||||
|
||||
return AnyView(
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
Button {
|
||||
withAnimation(.easeInOut(duration: 0.18)) { isExpanded.toggle() }
|
||||
} label: {
|
||||
HStack(alignment: .firstTextBaseline) {
|
||||
HStack(spacing: 6) {
|
||||
Image(systemName: "lightbulb.fill")
|
||||
.font(.system(size: 11, weight: .semibold))
|
||||
.foregroundStyle(Theme.brandAccent)
|
||||
Text("Tips for you")
|
||||
.font(.system(size: 12.5, weight: .semibold))
|
||||
.foregroundStyle(.primary)
|
||||
}
|
||||
Spacer()
|
||||
Text("\(groups.flatMap { $0.items }.count) signals")
|
||||
.font(.system(size: 10.5))
|
||||
.foregroundStyle(.secondary)
|
||||
Image(systemName: "chevron.right")
|
||||
.font(.system(size: 9, weight: .semibold))
|
||||
.rotationEffect(.degrees(isExpanded ? 90 : 0))
|
||||
.opacity(0.55)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
|
||||
if isExpanded {
|
||||
VStack(alignment: .leading, spacing: 10) {
|
||||
ForEach(groups) { group in
|
||||
if !group.items.isEmpty {
|
||||
TipsGroup(group: group)
|
||||
}
|
||||
}
|
||||
|
||||
if store.payload.optimize.findingCount > 0 {
|
||||
Button {
|
||||
openOptimize()
|
||||
} label: {
|
||||
HStack(spacing: 4) {
|
||||
Text("Open Full Optimize")
|
||||
.font(.system(size: 11.5, weight: .semibold))
|
||||
Image(systemName: "arrow.forward")
|
||||
.font(.system(size: 9, weight: .semibold))
|
||||
}
|
||||
.foregroundStyle(Theme.brandAccent)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
.transition(.opacity)
|
||||
}
|
||||
}
|
||||
.padding(12)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 8)
|
||||
.fill(Color.secondary.opacity(0.06))
|
||||
)
|
||||
.padding(.horizontal, 14)
|
||||
.padding(.vertical, 8)
|
||||
)
|
||||
}
|
||||
|
||||
private func openOptimize() {
|
||||
TerminalLauncher.open(subcommand: ["optimize"])
|
||||
}
|
||||
}
|
||||
|
||||
private struct TipsGroup: View {
|
||||
let group: TipGroup
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 5) {
|
||||
HStack(spacing: 5) {
|
||||
Image(systemName: group.icon)
|
||||
.font(.system(size: 10, weight: .bold))
|
||||
.foregroundStyle(group.color)
|
||||
Text(group.label)
|
||||
.font(.system(size: 10.5, weight: .semibold))
|
||||
.foregroundStyle(group.color)
|
||||
.textCase(.uppercase)
|
||||
.tracking(0.4)
|
||||
}
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
ForEach(group.items) { item in
|
||||
HStack(alignment: .firstTextBaseline, spacing: 6) {
|
||||
Circle().fill(group.color).frame(width: 3, height: 3).padding(.top, 4)
|
||||
Text(item.text)
|
||||
.font(.system(size: 11.5))
|
||||
.foregroundStyle(.primary)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
if let trailing = item.trailing {
|
||||
Text(trailing)
|
||||
.font(.codeMono(size: 11, weight: .medium))
|
||||
.foregroundStyle(.secondary)
|
||||
.tracking(-0.2)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct TipGroup: Identifiable {
|
||||
let id = UUID()
|
||||
let label: String
|
||||
let icon: String
|
||||
let color: Color
|
||||
let items: [TipItem]
|
||||
}
|
||||
|
||||
private struct TipItem: Identifiable {
|
||||
let id = UUID()
|
||||
let text: String
|
||||
let trailing: String?
|
||||
}
|
||||
|
||||
private func computeTipGroups(payload: MenubarPayload) -> [TipGroup] {
|
||||
let stats = computeHistoryStats(history: payload.history.daily)
|
||||
|
||||
// What's working
|
||||
var wins: [TipItem] = []
|
||||
let cacheHit = payload.current.cacheHitPercent
|
||||
if cacheHit >= 80 {
|
||||
wins.append(TipItem(
|
||||
text: "Cache hit at \(Int(cacheHit))% — most prompts reuse cache",
|
||||
trailing: nil
|
||||
))
|
||||
}
|
||||
if let oneShot = payload.current.oneShotRate, oneShot >= 0.75 {
|
||||
wins.append(TipItem(
|
||||
text: "\(Int(oneShot * 100))% one-shot — edits landing first try",
|
||||
trailing: nil
|
||||
))
|
||||
}
|
||||
if let delta = stats.weekDeltaPercent, delta < -10 {
|
||||
wins.append(TipItem(
|
||||
text: "Spend down \(Int(abs(delta)))% vs last 7 days",
|
||||
trailing: nil
|
||||
))
|
||||
}
|
||||
if stats.activeStreakDays >= 5 {
|
||||
wins.append(TipItem(
|
||||
text: "\(stats.activeStreakDays)-day usage streak",
|
||||
trailing: nil
|
||||
))
|
||||
}
|
||||
|
||||
// What to improve (existing optimize findings)
|
||||
var improvements: [TipItem] = []
|
||||
for finding in payload.optimize.topFindings.prefix(3) {
|
||||
improvements.append(TipItem(
|
||||
text: finding.title,
|
||||
trailing: finding.savingsUSD.asCompactCurrency()
|
||||
))
|
||||
}
|
||||
|
||||
// Risks
|
||||
var risks: [TipItem] = []
|
||||
if let delta = stats.weekDeltaPercent, delta > 25 {
|
||||
risks.append(TipItem(
|
||||
text: "Spend up \(Int(delta))% vs prior 7 days",
|
||||
trailing: nil
|
||||
))
|
||||
}
|
||||
if cacheHit > 0 && cacheHit < 50 {
|
||||
risks.append(TipItem(
|
||||
text: "Cache hit only \(Int(cacheHit))% — paying for cold prompts",
|
||||
trailing: nil
|
||||
))
|
||||
}
|
||||
if let oneShot = payload.current.oneShotRate, oneShot < 0.5 {
|
||||
risks.append(TipItem(
|
||||
text: "\(Int(oneShot * 100))% one-shot — lots of iteration",
|
||||
trailing: nil
|
||||
))
|
||||
}
|
||||
if let projected = stats.projectedMonth, let prevMonth = stats.previousMonthTotal, projected > prevMonth * 1.3 {
|
||||
risks.append(TipItem(
|
||||
text: "On pace for \(projected.asCompactCurrency()) this month (+\(Int(((projected - prevMonth) / prevMonth) * 100))% vs last)",
|
||||
trailing: nil
|
||||
))
|
||||
}
|
||||
|
||||
return [
|
||||
TipGroup(label: "What's working", icon: "checkmark.circle.fill", color: winColor, items: wins),
|
||||
TipGroup(label: "What to improve", icon: "arrow.up.right.circle.fill", color: improveColor, items: improvements),
|
||||
TipGroup(label: "Risks", icon: "exclamationmark.triangle.fill", color: riskColor, items: risks),
|
||||
]
|
||||
}
|
||||
|
||||
private struct HistoryStats {
|
||||
let weekDeltaPercent: Double?
|
||||
let activeStreakDays: Int
|
||||
let projectedMonth: Double?
|
||||
let previousMonthTotal: Double?
|
||||
}
|
||||
|
||||
private func computeHistoryStats(history: [DailyHistoryEntry]) -> HistoryStats {
|
||||
var calendar = Calendar(identifier: .gregorian)
|
||||
calendar.timeZone = TimeZone(identifier: "UTC")!
|
||||
let formatter: DateFormatter = {
|
||||
let f = DateFormatter()
|
||||
f.dateFormat = "yyyy-MM-dd"
|
||||
f.timeZone = TimeZone(identifier: "UTC")
|
||||
return f
|
||||
}()
|
||||
let now = Date()
|
||||
let today = calendar.startOfDay(for: now)
|
||||
let costByDate = Dictionary(uniqueKeysWithValues: history.map { ($0.date, $0.cost) })
|
||||
|
||||
let lastWeekStart = calendar.date(byAdding: .day, value: -6, to: today)
|
||||
let priorWeekStart = calendar.date(byAdding: .day, value: -13, to: today)
|
||||
let priorWeekEnd = calendar.date(byAdding: .day, value: -7, to: today)
|
||||
var weekDeltaPercent: Double? = nil
|
||||
if let lws = lastWeekStart, let pws = priorWeekStart, let pwe = priorWeekEnd {
|
||||
let lwsStr = formatter.string(from: lws)
|
||||
let pwsStr = formatter.string(from: pws)
|
||||
let pweStr = formatter.string(from: pwe)
|
||||
let thisWeek = history.filter { $0.date >= lwsStr }.reduce(0.0) { $0 + $1.cost }
|
||||
let prior = history.filter { $0.date >= pwsStr && $0.date <= pweStr }.reduce(0.0) { $0 + $1.cost }
|
||||
if prior > 0 {
|
||||
weekDeltaPercent = ((thisWeek - prior) / prior) * 100
|
||||
}
|
||||
}
|
||||
|
||||
var streak = 0
|
||||
for offset in 0..<60 {
|
||||
guard let d = calendar.date(byAdding: .day, value: -offset, to: today) else { break }
|
||||
let key = formatter.string(from: d)
|
||||
if (costByDate[key] ?? 0) > 0 { streak += 1 } else { break }
|
||||
}
|
||||
|
||||
var projectedMonth: Double? = nil
|
||||
var previousMonthTotal: Double? = nil
|
||||
let comps = calendar.dateComponents([.year, .month, .day], from: now)
|
||||
if
|
||||
let firstOfMonth = calendar.date(from: DateComponents(year: comps.year, month: comps.month, day: 1)),
|
||||
let rangeOfMonth = calendar.range(of: .day, in: .month, for: firstOfMonth)
|
||||
{
|
||||
let firstStr = formatter.string(from: firstOfMonth)
|
||||
let mtd = history.filter { $0.date >= firstStr }.reduce(0.0) { $0 + $1.cost }
|
||||
let dayOfMonth = comps.day ?? 1
|
||||
if dayOfMonth > 0 {
|
||||
projectedMonth = (mtd / Double(dayOfMonth)) * Double(rangeOfMonth.count)
|
||||
}
|
||||
|
||||
if
|
||||
let prevMonth = calendar.date(byAdding: .month, value: -1, to: firstOfMonth),
|
||||
let prevRange = calendar.range(of: .day, in: .month, for: prevMonth),
|
||||
let prevFirst = calendar.date(from: DateComponents(
|
||||
year: calendar.component(.year, from: prevMonth),
|
||||
month: calendar.component(.month, from: prevMonth),
|
||||
day: 1
|
||||
)),
|
||||
let prevLast = calendar.date(byAdding: .day, value: prevRange.count - 1, to: prevFirst)
|
||||
{
|
||||
let prevFirstStr = formatter.string(from: prevFirst)
|
||||
let prevLastStr = formatter.string(from: prevLast)
|
||||
let prevTotal = history.filter { $0.date >= prevFirstStr && $0.date <= prevLastStr }
|
||||
.reduce(0.0) { $0 + $1.cost }
|
||||
if prevTotal > 0 { previousMonthTotal = prevTotal }
|
||||
}
|
||||
}
|
||||
|
||||
return HistoryStats(
|
||||
weekDeltaPercent: weekDeltaPercent,
|
||||
activeStreakDays: streak,
|
||||
projectedMonth: projectedMonth,
|
||||
previousMonthTotal: previousMonthTotal
|
||||
)
|
||||
}
|
||||
1219
mac/Sources/CodeBurnMenubar/Views/HeatmapSection.swift
Normal file
1219
mac/Sources/CodeBurnMenubar/Views/HeatmapSection.swift
Normal file
File diff suppressed because it is too large
Load diff
55
mac/Sources/CodeBurnMenubar/Views/HeroSection.swift
Normal file
55
mac/Sources/CodeBurnMenubar/Views/HeroSection.swift
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import SwiftUI
|
||||
|
||||
struct HeroSection: View {
|
||||
@Environment(AppStore.self) private var store
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
SectionCaption(text: caption)
|
||||
|
||||
HStack(alignment: .firstTextBaseline) {
|
||||
Text(store.payload.current.cost.asCurrency())
|
||||
.font(.system(size: 32, weight: .semibold, design: .rounded))
|
||||
.monospacedDigit()
|
||||
.tracking(-1)
|
||||
.foregroundStyle(
|
||||
LinearGradient(
|
||||
colors: [Theme.brandAccent, Theme.brandEmberDeep],
|
||||
startPoint: .top,
|
||||
endPoint: .bottom
|
||||
)
|
||||
)
|
||||
|
||||
Spacer()
|
||||
|
||||
VStack(alignment: .trailing, spacing: 2) {
|
||||
Text("\(store.payload.current.calls.asThousandsSeparated()) calls")
|
||||
.font(.system(size: 11))
|
||||
.monospacedDigit()
|
||||
.foregroundStyle(.secondary)
|
||||
Text("\(store.payload.current.sessions) sessions")
|
||||
.font(.system(size: 10.5))
|
||||
.monospacedDigit()
|
||||
.foregroundStyle(.tertiary)
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 14)
|
||||
.padding(.top, 10)
|
||||
.padding(.bottom, 12)
|
||||
}
|
||||
|
||||
private var caption: String {
|
||||
let label = store.payload.current.label.isEmpty ? store.selectedPeriod.rawValue : store.payload.current.label
|
||||
if store.selectedPeriod == .today {
|
||||
return "\(label) · \(todayDate)"
|
||||
}
|
||||
return label
|
||||
}
|
||||
|
||||
private var todayDate: String {
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateFormat = "EEE MMM d"
|
||||
return formatter.string(from: Date())
|
||||
}
|
||||
}
|
||||
401
mac/Sources/CodeBurnMenubar/Views/MenuBarContent.swift
Normal file
401
mac/Sources/CodeBurnMenubar/Views/MenuBarContent.swift
Normal file
|
|
@ -0,0 +1,401 @@
|
|||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// Popover root. Assembles all sections matching the HTML design spec.
|
||||
struct MenuBarContent: View {
|
||||
@Environment(AppStore.self) private var store
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
Header()
|
||||
|
||||
Divider()
|
||||
|
||||
AgentTabStrip()
|
||||
|
||||
Divider()
|
||||
|
||||
ZStack {
|
||||
ScrollView(.vertical, showsIndicators: false) {
|
||||
VStack(spacing: 0) {
|
||||
HeroSection()
|
||||
Divider().opacity(0.5)
|
||||
PeriodSegmentedControl()
|
||||
Divider().opacity(0.5)
|
||||
if isFilteredEmpty {
|
||||
EmptyProviderState(provider: store.selectedProvider, period: store.selectedPeriod)
|
||||
} else {
|
||||
HeatmapSection()
|
||||
.padding(.horizontal, 14)
|
||||
.padding(.top, 10)
|
||||
.padding(.bottom, 10)
|
||||
.zIndex(10)
|
||||
Divider().opacity(0.5)
|
||||
ActivitySection()
|
||||
Divider().opacity(0.5)
|
||||
ModelsSection()
|
||||
Divider().opacity(0.5)
|
||||
FindingsSection()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if store.isLoading {
|
||||
BurnLoadingOverlay(periodLabel: store.selectedPeriod.rawValue)
|
||||
.transition(.opacity)
|
||||
}
|
||||
}
|
||||
.frame(height: 520)
|
||||
.animation(.easeInOut(duration: 0.2), value: store.isLoading)
|
||||
|
||||
Divider()
|
||||
|
||||
FooterBar()
|
||||
|
||||
StarBanner()
|
||||
}
|
||||
}
|
||||
|
||||
/// True when a specific provider tab is selected and that provider has no spend in the
|
||||
/// currently selected period. The .all tab is exempt -- it always shows aggregated data.
|
||||
private var isFilteredEmpty: Bool {
|
||||
guard store.selectedProvider != .all else { return false }
|
||||
return store.payload.current.cost <= 0 && store.payload.current.calls == 0
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private struct EmptyProviderState: View {
|
||||
let provider: ProviderFilter
|
||||
let period: Period
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 10) {
|
||||
Image(systemName: "tray")
|
||||
.font(.system(size: 26))
|
||||
.foregroundStyle(.tertiary)
|
||||
Text("No \(provider.rawValue) data for \(periodPhrase)")
|
||||
.font(.system(size: 12, weight: .medium))
|
||||
.foregroundStyle(.secondary)
|
||||
.multilineTextAlignment(.center)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.padding(.vertical, 60)
|
||||
}
|
||||
|
||||
private var periodPhrase: String {
|
||||
switch period {
|
||||
case .today: "today"
|
||||
case .sevenDays: "the last 7 days"
|
||||
case .thirtyDays: "the last 30 days"
|
||||
case .month: "this month"
|
||||
case .all: "all time"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Translucent overlay that blurs whatever's behind it (the previous tab/period content)
|
||||
/// and centers an animated burning flame -- the brand mark filling up bottom-to-top in
|
||||
/// yellow→orange→red, looping.
|
||||
private struct BurnLoadingOverlay: View {
|
||||
let periodLabel: String
|
||||
@State private var fillProgress: CGFloat = 0
|
||||
@State private var glowing: Bool = false
|
||||
|
||||
private let flameSize: CGFloat = 64
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
// Blur backdrop -- ultraThinMaterial uses live blur of underlying content.
|
||||
Rectangle()
|
||||
.fill(.ultraThinMaterial)
|
||||
|
||||
VStack(spacing: 14) {
|
||||
BurnFlame(size: flameSize, fillProgress: fillProgress, glowing: glowing)
|
||||
Text("Loading \(periodLabel)…")
|
||||
.font(.system(size: 11.5, weight: .medium))
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
withAnimation(.easeInOut(duration: 1.4).repeatForever(autoreverses: true)) {
|
||||
fillProgress = 1.0
|
||||
}
|
||||
withAnimation(.easeInOut(duration: 0.9).repeatForever(autoreverses: true)) {
|
||||
glowing = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct BurnFlame: View {
|
||||
let size: CGFloat
|
||||
let fillProgress: CGFloat
|
||||
let glowing: Bool
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
// Soft outer glow that pulses, matching the brand terracotta palette.
|
||||
Image(systemName: "flame.fill")
|
||||
.font(.system(size: size, weight: .regular))
|
||||
.foregroundStyle(Theme.brandEmberGlow.opacity(glowing ? 0.55 : 0.20))
|
||||
.blur(radius: glowing ? 14 : 6)
|
||||
|
||||
// Empty (cool) flame as base
|
||||
Image(systemName: "flame")
|
||||
.font(.system(size: size, weight: .regular))
|
||||
.foregroundStyle(Theme.brandAccent.opacity(0.25))
|
||||
|
||||
// Burning gradient (brand orange) masked by an animated bottom-up rectangle
|
||||
Image(systemName: "flame.fill")
|
||||
.font(.system(size: size, weight: .regular))
|
||||
.foregroundStyle(
|
||||
LinearGradient(
|
||||
colors: [
|
||||
Theme.brandEmberGlow,
|
||||
Theme.brandAccentDark,
|
||||
Theme.brandAccent,
|
||||
Theme.brandEmberDeep
|
||||
],
|
||||
startPoint: .bottom,
|
||||
endPoint: .top
|
||||
)
|
||||
)
|
||||
.mask(
|
||||
GeometryReader { geo in
|
||||
Rectangle()
|
||||
.frame(height: geo.size.height * fillProgress)
|
||||
.frame(maxHeight: .infinity, alignment: .bottom)
|
||||
}
|
||||
)
|
||||
}
|
||||
.frame(width: size, height: size)
|
||||
}
|
||||
}
|
||||
|
||||
private struct Header: View {
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 1) {
|
||||
(
|
||||
Text("Code").foregroundStyle(.primary)
|
||||
+ Text("Burn").foregroundStyle(Theme.brandAccent)
|
||||
)
|
||||
.font(.system(size: 13, weight: .semibold))
|
||||
.tracking(-0.15)
|
||||
Text("AI Coding Cost Tracker")
|
||||
.font(.system(size: 10.5))
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.padding(.horizontal, 14)
|
||||
.padding(.top, 10)
|
||||
.padding(.bottom, 8)
|
||||
}
|
||||
}
|
||||
|
||||
struct FlameMark: View {
|
||||
var body: some View {
|
||||
ZStack {
|
||||
RoundedRectangle(cornerRadius: 5)
|
||||
.fill(
|
||||
LinearGradient(
|
||||
colors: [Theme.brandAccentDark, Theme.brandEmberDeep],
|
||||
startPoint: .topLeading,
|
||||
endPoint: .bottomTrailing
|
||||
)
|
||||
)
|
||||
.shadow(color: .black.opacity(0.2), radius: 1, y: 0.5)
|
||||
Image(systemName: "flame.fill")
|
||||
.font(.system(size: 12, weight: .semibold))
|
||||
.foregroundStyle(.white)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private let starBannerGitHubURL = URL(string: "https://github.com/AgentSeal/codeburn")!
|
||||
|
||||
/// Shown at the very bottom on first launch. A small terracotta strip nudges users to star the
|
||||
/// repo; clicking opens GitHub, clicking the close icon hides it forever (persisted to
|
||||
/// UserDefaults so it never returns across launches).
|
||||
struct StarBanner: View {
|
||||
@AppStorage("codeburn.starBannerDismissed") private var dismissed: Bool = false
|
||||
|
||||
var body: some View {
|
||||
if !dismissed {
|
||||
HStack(spacing: 8) {
|
||||
Image(systemName: "star.fill")
|
||||
.font(.system(size: 10, weight: .semibold))
|
||||
.foregroundStyle(Theme.brandAccent)
|
||||
|
||||
Button {
|
||||
NSWorkspace.shared.open(starBannerGitHubURL)
|
||||
} label: {
|
||||
HStack(spacing: 4) {
|
||||
Text("Enjoying CodeBurn?")
|
||||
.foregroundStyle(.primary)
|
||||
Text("Star us on GitHub")
|
||||
.foregroundStyle(Theme.brandAccent)
|
||||
.underline(true, pattern: .solid)
|
||||
}
|
||||
.font(.system(size: 10.5, weight: .medium))
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
|
||||
Spacer()
|
||||
|
||||
Button {
|
||||
dismissed = true
|
||||
} label: {
|
||||
Image(systemName: "xmark")
|
||||
.font(.system(size: 9, weight: .semibold))
|
||||
.foregroundStyle(.secondary)
|
||||
.padding(4)
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.help("Hide this banner")
|
||||
}
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 6)
|
||||
.background(Theme.brandAccent.opacity(0.08))
|
||||
.overlay(alignment: .top) {
|
||||
Rectangle()
|
||||
.fill(Color.secondary.opacity(0.18))
|
||||
.frame(height: 0.5)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct FooterBar: View {
|
||||
@Environment(AppStore.self) private var store
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 6) {
|
||||
Menu {
|
||||
ForEach(SupportedCurrency.allCases) { currency in
|
||||
Button {
|
||||
applyCurrency(code: currency.rawValue)
|
||||
} label: {
|
||||
if currency.rawValue == store.currency {
|
||||
Label("\(currency.displayName) (\(currency.rawValue))", systemImage: "checkmark")
|
||||
} else {
|
||||
Text("\(currency.displayName) (\(currency.rawValue))")
|
||||
}
|
||||
}
|
||||
}
|
||||
} label: {
|
||||
Label(store.currency, systemImage: "dollarsign.circle")
|
||||
.font(.system(size: 11, weight: .medium))
|
||||
.labelStyle(.titleAndIcon)
|
||||
}
|
||||
.menuStyle(.button)
|
||||
.menuIndicator(.hidden)
|
||||
.buttonStyle(.bordered)
|
||||
.controlSize(.small)
|
||||
.fixedSize()
|
||||
|
||||
Button {
|
||||
Task { await store.refresh(includeOptimize: true) }
|
||||
} label: {
|
||||
Image(systemName: store.isLoading ? "arrow.triangle.2.circlepath" : "arrow.clockwise")
|
||||
.font(.system(size: 11, weight: .medium))
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
.controlSize(.small)
|
||||
.disabled(store.isLoading)
|
||||
|
||||
Menu {
|
||||
Button("CSV (folder)") { runExport(format: .csv) }
|
||||
Button("JSON") { runExport(format: .json) }
|
||||
} label: {
|
||||
Label("Export", systemImage: "square.and.arrow.down")
|
||||
.font(.system(size: 11, weight: .medium))
|
||||
.labelStyle(.titleAndIcon)
|
||||
}
|
||||
.menuStyle(.button)
|
||||
.menuIndicator(.hidden)
|
||||
.buttonStyle(.bordered)
|
||||
.controlSize(.small)
|
||||
.fixedSize()
|
||||
|
||||
Spacer()
|
||||
|
||||
Button { openReport() } label: {
|
||||
Label("Open Full Report", systemImage: "terminal")
|
||||
.font(.system(size: 11, weight: .semibold))
|
||||
.labelStyle(.titleAndIcon)
|
||||
}
|
||||
.buttonStyle(.borderedProminent)
|
||||
.controlSize(.small)
|
||||
.tint(Theme.brandAccent)
|
||||
}
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 8)
|
||||
}
|
||||
|
||||
private func openReport() {
|
||||
TerminalLauncher.open(subcommand: ["report"])
|
||||
}
|
||||
|
||||
private enum ExportFormat {
|
||||
case csv, json
|
||||
var cliName: String { self == .csv ? "csv" : "json" }
|
||||
var suffix: String { self == .csv ? "" : ".json" }
|
||||
}
|
||||
|
||||
/// Runs `codeburn export` directly into ~/Downloads and reveals the result in Finder. CSV
|
||||
/// produces a folder of clean one-table-per-file CSVs; JSON produces a single structured
|
||||
/// file. The CLI is spawned with argv (no shell interpretation), so the output path cannot
|
||||
/// be abused to inject shell commands even if a pathological value slips through.
|
||||
private func runExport(format: ExportFormat) {
|
||||
Task {
|
||||
let downloads = (NSHomeDirectory() as NSString).appendingPathComponent("Downloads")
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateFormat = "yyyy-MM-dd"
|
||||
let base = "codeburn-\(formatter.string(from: Date()))"
|
||||
let outputPath = (downloads as NSString).appendingPathComponent(base + format.suffix)
|
||||
|
||||
let process = CodeburnCLI.makeProcess(subcommand: [
|
||||
"export", "-f", format.cliName, "-o", outputPath
|
||||
])
|
||||
|
||||
do {
|
||||
try process.run()
|
||||
process.waitUntilExit()
|
||||
if process.terminationStatus == 0 {
|
||||
NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: outputPath)])
|
||||
} else {
|
||||
NSLog("CodeBurn: \(format.cliName.uppercased()) export exited with status \(process.terminationStatus)")
|
||||
}
|
||||
} catch {
|
||||
NSLog("CodeBurn: \(format.cliName.uppercased()) export failed: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Instant-feeling currency switch. Updates the symbol and any cached FX rate on the main
|
||||
/// thread right away so the UI redraws the next frame, then fetches a fresh rate in the
|
||||
/// background. CLI config is persisted so other codeburn commands stay in sync.
|
||||
private func applyCurrency(code: String) {
|
||||
store.currency = code
|
||||
let symbol = CurrencyState.symbolForCode(code)
|
||||
|
||||
Task {
|
||||
let cached = await FXRateCache.shared.cachedRate(for: code)
|
||||
await MainActor.run {
|
||||
CurrencyState.shared.apply(code: code, rate: cached, symbol: symbol)
|
||||
}
|
||||
|
||||
let fresh = await FXRateCache.shared.rate(for: code)
|
||||
if let fresh, fresh != cached {
|
||||
await MainActor.run {
|
||||
CurrencyState.shared.apply(code: code, rate: fresh, symbol: symbol)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CLICurrencyConfig.persist(code: code)
|
||||
}
|
||||
}
|
||||
97
mac/Sources/CodeBurnMenubar/Views/ModelsSection.swift
Normal file
97
mac/Sources/CodeBurnMenubar/Views/ModelsSection.swift
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
import SwiftUI
|
||||
|
||||
struct ModelsSection: View {
|
||||
@Environment(AppStore.self) private var store
|
||||
@State private var isExpanded: Bool = true
|
||||
|
||||
var body: some View {
|
||||
CollapsibleSection(
|
||||
caption: "Models",
|
||||
isExpanded: $isExpanded,
|
||||
trailing: {
|
||||
HStack(spacing: 8) {
|
||||
Text("Cost").frame(minWidth: 54, alignment: .trailing)
|
||||
Text("Calls").frame(minWidth: 52, alignment: .trailing)
|
||||
}
|
||||
.font(.system(size: 10, weight: .medium))
|
||||
.foregroundStyle(.tertiary)
|
||||
.tracking(-0.05)
|
||||
}
|
||||
) {
|
||||
VStack(alignment: .leading, spacing: 7) {
|
||||
let maxCost = store.payload.current.topModels.map(\.cost).max() ?? 1
|
||||
ForEach(store.payload.current.topModels, id: \.name) { model in
|
||||
ModelRow(model: model, maxCost: maxCost)
|
||||
}
|
||||
|
||||
TokensLine()
|
||||
.padding(.top, 5)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct ModelRow: View {
|
||||
let model: ModelEntry
|
||||
let maxCost: Double
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 8) {
|
||||
FixedBar(fraction: model.cost / maxCost)
|
||||
.frame(width: 56, height: 6)
|
||||
|
||||
Text(model.name)
|
||||
.font(.system(size: 12.5, weight: .medium))
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
|
||||
Text(model.cost.asCompactCurrency())
|
||||
.font(.codeMono(size: 12, weight: .medium))
|
||||
.tracking(-0.2)
|
||||
.frame(minWidth: 54, alignment: .trailing)
|
||||
|
||||
Text("\(model.calls)")
|
||||
.font(.system(size: 11))
|
||||
.monospacedDigit()
|
||||
.foregroundStyle(.secondary)
|
||||
.frame(minWidth: 52, alignment: .trailing)
|
||||
}
|
||||
.padding(.horizontal, 2)
|
||||
.padding(.vertical, 1)
|
||||
}
|
||||
}
|
||||
|
||||
private struct TokensLine: View {
|
||||
@Environment(AppStore.self) private var store
|
||||
|
||||
var body: some View {
|
||||
let t = store.payload.current
|
||||
let cacheHit = String(format: "%.0f", t.cacheHitPercent)
|
||||
|
||||
HStack(spacing: 4) {
|
||||
Text("Tokens")
|
||||
.foregroundStyle(.tertiary)
|
||||
Text(formatTokens(t.inputTokens) + " in")
|
||||
.foregroundStyle(.secondary)
|
||||
Text("·")
|
||||
.foregroundStyle(.tertiary)
|
||||
Text(formatTokens(t.outputTokens) + " out")
|
||||
.foregroundStyle(.secondary)
|
||||
Text("·")
|
||||
.foregroundStyle(.tertiary)
|
||||
Text(cacheHit + "% cache hit")
|
||||
.foregroundStyle(.secondary)
|
||||
Spacer()
|
||||
}
|
||||
.font(.system(size: 10.5))
|
||||
.monospacedDigit()
|
||||
}
|
||||
|
||||
private func formatTokens(_ n: Int) -> String {
|
||||
if n >= 1_000_000 {
|
||||
return String(format: "%.1fM", Double(n) / 1_000_000)
|
||||
} else if n >= 1_000 {
|
||||
return String(format: "%.1fK", Double(n) / 1_000)
|
||||
}
|
||||
return "\(n)"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
import SwiftUI
|
||||
|
||||
struct PeriodSegmentedControl: View {
|
||||
@Environment(AppStore.self) private var store
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 1) {
|
||||
ForEach(Period.allCases) { period in
|
||||
Button {
|
||||
Task { await store.switchTo(period: period) }
|
||||
} label: {
|
||||
Text(period.rawValue)
|
||||
.font(.system(size: 11, weight: .medium))
|
||||
.foregroundStyle(store.selectedPeriod == period ? AnyShapeStyle(.primary) : AnyShapeStyle(.secondary))
|
||||
.frame(maxWidth: .infinity)
|
||||
.padding(.vertical, 4)
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 5)
|
||||
.fill(store.selectedPeriod == period ? Color(NSColor.windowBackgroundColor).opacity(0.85) : .clear)
|
||||
.shadow(color: .black.opacity(store.selectedPeriod == period ? 0.06 : 0), radius: 1, y: 0.5)
|
||||
)
|
||||
}
|
||||
}
|
||||
.padding(2)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 7)
|
||||
.fill(Color.secondary.opacity(0.08))
|
||||
)
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.top, 6)
|
||||
.padding(.bottom, 10)
|
||||
}
|
||||
}
|
||||
85
mac/Sources/CodeBurnMenubar/Views/SectionCaption.swift
Normal file
85
mac/Sources/CodeBurnMenubar/Views/SectionCaption.swift
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
import SwiftUI
|
||||
|
||||
struct SectionCaption: View {
|
||||
let text: String
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 5) {
|
||||
Circle()
|
||||
.fill(Theme.brandAccent.opacity(0.7))
|
||||
.frame(width: 3, height: 3)
|
||||
Text(text)
|
||||
.font(.system(size: 11.5, weight: .medium))
|
||||
.foregroundStyle(.secondary)
|
||||
.tracking(-0.1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Collapsible section shell with a clickable caption, optional inline trailing
|
||||
/// view (e.g. column headers), and a chevron.
|
||||
struct CollapsibleSection<Trailing: View, Content: View>: View {
|
||||
let caption: String
|
||||
@Binding var isExpanded: Bool
|
||||
let trailing: Trailing
|
||||
let content: Content
|
||||
|
||||
init(
|
||||
caption: String,
|
||||
isExpanded: Binding<Bool>,
|
||||
@ViewBuilder trailing: () -> Trailing,
|
||||
@ViewBuilder content: () -> Content
|
||||
) {
|
||||
self.caption = caption
|
||||
self._isExpanded = isExpanded
|
||||
self.trailing = trailing()
|
||||
self.content = content()
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 7) {
|
||||
Button {
|
||||
withAnimation(.easeInOut(duration: 0.18)) {
|
||||
isExpanded.toggle()
|
||||
}
|
||||
} label: {
|
||||
HStack(spacing: 8) {
|
||||
HStack(spacing: 5) {
|
||||
Circle()
|
||||
.fill(Theme.brandAccent.opacity(0.7))
|
||||
.frame(width: 3, height: 3)
|
||||
Text(caption)
|
||||
.font(.system(size: 11.5, weight: .medium))
|
||||
.tracking(-0.1)
|
||||
}
|
||||
Spacer()
|
||||
trailing
|
||||
Image(systemName: "chevron.right")
|
||||
.font(.system(size: 9, weight: .semibold))
|
||||
.rotationEffect(.degrees(isExpanded ? 90 : 0))
|
||||
.opacity(0.55)
|
||||
}
|
||||
.foregroundStyle(.secondary)
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
|
||||
if isExpanded {
|
||||
content
|
||||
.transition(.opacity)
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 14)
|
||||
.padding(.vertical, 11)
|
||||
}
|
||||
}
|
||||
|
||||
extension CollapsibleSection where Trailing == EmptyView {
|
||||
init(
|
||||
caption: String,
|
||||
isExpanded: Binding<Bool>,
|
||||
@ViewBuilder content: () -> Content
|
||||
) {
|
||||
self.init(caption: caption, isExpanded: isExpanded, trailing: { EmptyView() }, content: content)
|
||||
}
|
||||
}
|
||||
99
mac/Sources/CodeBurnMenubar/Views/SparklineView.swift
Normal file
99
mac/Sources/CodeBurnMenubar/Views/SparklineView.swift
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
import SwiftUI
|
||||
|
||||
struct SparklineView: View {
|
||||
let points: [Double]
|
||||
|
||||
var body: some View {
|
||||
GeometryReader { geo in
|
||||
let cgPoints = makePoints(in: geo.size)
|
||||
let smooth = smoothPath(cgPoints)
|
||||
|
||||
ZStack {
|
||||
// Gradient fill under the curve
|
||||
let fill = closedPath(smooth, width: geo.size.width, height: geo.size.height)
|
||||
fill.fill(
|
||||
LinearGradient(
|
||||
colors: [Theme.brandAccent.opacity(0.25), .clear],
|
||||
startPoint: .top,
|
||||
endPoint: .bottom
|
||||
)
|
||||
)
|
||||
|
||||
// Smooth accent stroke
|
||||
smooth.stroke(
|
||||
Theme.brandAccent.opacity(0.85),
|
||||
style: StrokeStyle(lineWidth: 1.6, lineCap: .round, lineJoin: .round)
|
||||
)
|
||||
|
||||
// Highlighted current-day point
|
||||
if let last = cgPoints.last {
|
||||
Circle()
|
||||
.fill(Theme.brandAccent)
|
||||
.frame(width: 6, height: 6)
|
||||
.overlay(
|
||||
Circle()
|
||||
.stroke(Color(NSColor.windowBackgroundColor).opacity(0.9), lineWidth: 1.3)
|
||||
)
|
||||
.position(last)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Geometry
|
||||
|
||||
private func makePoints(in size: CGSize) -> [CGPoint] {
|
||||
guard !points.isEmpty else { return [] }
|
||||
let w = size.width
|
||||
let h = size.height
|
||||
let maxV = points.max() ?? 1
|
||||
let minV = points.min() ?? 0
|
||||
let range = max(maxV - minV, 1)
|
||||
let count = max(points.count - 1, 1)
|
||||
let topPad: CGFloat = 5
|
||||
let bottomPad: CGFloat = 5
|
||||
let usable = max(h - topPad - bottomPad, 1)
|
||||
|
||||
return points.enumerated().map { idx, v in
|
||||
CGPoint(
|
||||
x: w * CGFloat(idx) / CGFloat(count),
|
||||
y: h - bottomPad - usable * CGFloat(v - minV) / CGFloat(range)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Catmull-Rom → cubic bezier. Standard smooth interpolation, no overshoot.
|
||||
private func smoothPath(_ pts: [CGPoint]) -> Path {
|
||||
var path = Path()
|
||||
guard pts.count >= 2 else { return path }
|
||||
path.move(to: pts[0])
|
||||
|
||||
let tension: CGFloat = 0.5
|
||||
for i in 0..<(pts.count - 1) {
|
||||
let p0 = i > 0 ? pts[i - 1] : pts[i]
|
||||
let p1 = pts[i]
|
||||
let p2 = pts[i + 1]
|
||||
let p3 = i + 2 < pts.count ? pts[i + 2] : p2
|
||||
|
||||
let cp1 = CGPoint(
|
||||
x: p1.x + (p2.x - p0.x) * tension / 3,
|
||||
y: p1.y + (p2.y - p0.y) * tension / 3
|
||||
)
|
||||
let cp2 = CGPoint(
|
||||
x: p2.x - (p3.x - p1.x) * tension / 3,
|
||||
y: p2.y - (p3.y - p1.y) * tension / 3
|
||||
)
|
||||
path.addCurve(to: p2, control1: cp1, control2: cp2)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
/// Close the path along the bottom to form a fill region.
|
||||
private func closedPath(_ line: Path, width: CGFloat, height: CGFloat) -> Path {
|
||||
var p = line
|
||||
p.addLine(to: CGPoint(x: width, y: height))
|
||||
p.addLine(to: CGPoint(x: 0, y: height))
|
||||
p.closeSubpath()
|
||||
return p
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue