import SwiftUI struct MonthGrid: View { @Environment(\.theme) private var theme let monthKey: String let selectedKey: String let dotCounts: [String: Int] let now: Date let onSelectDay: (String) -> Void @Environment(\.calendar) private var calendar private static let weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] var body: some View { let weeks = CalendarDates.buildMonthGrid(monthKey, calendar: calendar) VStack(spacing: MonthGridKnobs.faceMonthGridGap) { HStack(spacing: 0) { ForEach(Self.weekdays, id: \.self) { name in Text(name) .font(theme.type.body(theme.scale.bodySize - 1.5)) .foregroundStyle(theme.print(.pencil)) .frame(maxWidth: .infinity) } } .padding(.bottom, MonthGridKnobs.faceMonthGridHeaderBottomPadding) .accessibilityHidden(true) ForEach(Array(weeks.enumerated()), id: \.offset) { _, week in HStack(spacing: 0) { ForEach(week) { cell in dayCell(cell) } } } } .accessibilityIdentifier("month-grid") } private func dayCell(_ cell: MonthGridCell) -> some View { let today = CalendarDates.isTodayKey(cell.key, now: now, calendar: calendar) let selected = cell.key == selectedKey let dots = min(MonthGridKnobs.faceMonthGridMaxDots, max(0, dotCounts[cell.key] ?? 0)) return Button { onSelectDay(cell.key) } label: { VStack(spacing: MonthGridKnobs.faceMonthGridCellGap) { ZStack { if selected { PencilCircle(seed: "selected-\(cell.key)", theme: theme) .stroke(theme.print(.red), style: StrokeStyle(lineWidth: MonthGridKnobs.faceMonthGridSelectedStroke, lineCap: .round)) .frame(width: MonthGridKnobs.faceMonthGridDayCellWidth, height: MonthGridKnobs.faceMonthGridDayCellHeight) .accessibilityIdentifier("month-grid-day-\(cell.key)-selected") } Text("\(cell.dayOfMonth)") .font(theme.type.body(theme.scale.bodySize + 3)) .foregroundStyle( cell.inMonth ? theme.colors.ink.color : theme.colors.ink.color.opacity(MonthGridKnobs.faceMonthGridOutsideMonthOpacity) ) .monospacedDigit() .overlay(alignment: .bottom) { if today { PencilUnderline(seed: "today", theme: theme) .stroke(theme.print(.pencil), lineWidth: 1) .frame(width: MonthGridKnobs.faceMonthGridMarkerWidth, height: MonthGridKnobs.faceMonthGridMarkerHeight) .offset(y: MonthGridKnobs.faceMonthGridMarkerOffsetY) .accessibilityIdentifier("month-grid-day-\(cell.key)-today") } } } .frame(height: MonthGridKnobs.faceMonthGridDayCellHeight) HStack(spacing: MonthGridKnobs.faceMonthGridDotGap) { ForEach(0.. String { let date = CalendarDates.formatFullDate(cell.key, calendar: calendar) var parts = [date] if today { parts.append("today") } if count > 0 { parts.append(count == 1 ? "1 thing" : "\(count) things") } return parts.joined(separator: ", ") } } /// The sizes, counts and timings MonthGrid draws with: Swift's own, beside /// their reader — a theme sets only its tokens (`scale.tokens`). enum MonthGridKnobs { static let faceMonthGridCellGap: CGFloat = 5 static let faceMonthGridDayCellHeight: CGFloat = 34 static let faceMonthGridDayCellWidth: CGFloat = 38 static let faceMonthGridDotGap: CGFloat = 3 static let faceMonthGridDotRowHeight: CGFloat = 5 static let faceMonthGridDotSize: CGFloat = 4.5 static let faceMonthGridGap: CGFloat = 6 static let faceMonthGridHeaderBottomPadding: CGFloat = 4 static let faceMonthGridMarkerHeight: CGFloat = 4 static let faceMonthGridMarkerOffsetY: CGFloat = 4 static let faceMonthGridMarkerOpacity: CGFloat = 0.72 static let faceMonthGridMarkerOutsideOpacity: CGFloat = 0.25 static let faceMonthGridMarkerWidth: CGFloat = 20 static let faceMonthGridMaxDots = 3 static let faceMonthGridOutsideMonthOpacity: CGFloat = 0.28 static let faceMonthGridMinHeight: CGFloat = 50 static let faceMonthGridSelectedStroke: CGFloat = 1.4 }