import SwiftUI struct NotationView: View { let game: GameData let current: Int let onMove: (Int) -> Void var body: some View { let notation = NotationModel(game: game) ScrollViewReader { proxy in ScrollView { LazyVStack(alignment: .leading, spacing: 0) { Color.clear.frame(height: 1).id("move-0") ForEach(notation.entries) { entry in switch entry { case .comment(let comment): CommentBlock(text: comment.text, indent: comment.indent) case .moves(let row): MoveRowView(row: row, current: current, onMove: onMove) case .variation(let variation): VariationView(variation: variation, current: current, onMove: onMove) case .result(let result): Text(result.text) .font(.subheadline.weight(.medium)) .padding(.horizontal, 16) .padding(.vertical, 8) .background(Theme.containerHigh) .clipShape(RoundedRectangle(cornerRadius: 8)) .frame(maxWidth: .infinity) .padding(16) } } } .padding(.bottom, 8) } .onAppear { scroll(to: current, proxy: proxy) } .onChange(of: current) { id in scroll(to: id, proxy: proxy) } } .background(Theme.background) } private func scroll(to id: Int, proxy: ScrollViewProxy) { withAnimation(.easeInOut(duration: 0.2)) { proxy.scrollTo("move-\(id)", anchor: .center) } } } private struct MoveRowView: View { let row: NotationMoveRow let current: Int let onMove: (Int) -> Void var body: some View { HStack(spacing: 0) { Text("\(row.number)") .font(.subheadline.monospacedDigit()) .foregroundStyle(Theme.onSurfaceVariant) .frame(width: 40, alignment: .trailing) .padding(.trailing, 8) MoveCell(move: row.white, emptyPlaceholder: "…", current: current, onMove: onMove) MoveCell(move: row.black, emptyPlaceholder: "", current: current, onMove: onMove) } .frame(minHeight: 48) .padding(.leading, CGFloat(row.indent * 16)) } } private struct MoveCell: View { let move: NotationMove? let emptyPlaceholder: String let current: Int let onMove: (Int) -> Void var body: some View { Group { if let move { NotationMoveButton(move: move, current: current, onMove: onMove) .frame(maxWidth: .infinity, minHeight: 44, alignment: .leading) } else { Text(emptyPlaceholder) .foregroundStyle(Theme.onSurfaceVariant) .padding(.horizontal, 12) .frame(maxWidth: .infinity, minHeight: 44, alignment: .leading) } } } } private struct NotationMoveButton: View { let move: NotationMove let current: Int let onMove: (Int) -> Void var body: some View { Button { onMove(move.id) } label: { Text(move.label) .font(.body.weight(.bold)) .foregroundStyle(Theme.onSurface) .padding(.horizontal, 8) .padding(.vertical, 6) .background(current == move.id ? Theme.currentMove : Color.clear) .clipShape(RoundedRectangle(cornerRadius: 6)) } .buttonStyle(.plain) .frame(minHeight: 44) .id("move-\(move.id)") .accessibilityValue(current == move.id ? L10n.currentMove : "") .accessibilityAddTraits(current == move.id ? .isSelected : []) } } private struct VariationView: View { let variation: NotationVariation let current: Int let onMove: (Int) -> Void var body: some View { HStack(alignment: .top, spacing: 12) { Rectangle() .fill(Theme.outlineVariant) .frame(width: 2) .frame(maxHeight: .infinity) FlowLayout(spacing: 4) { ForEach(variation.tokens) { token in switch token { case .move(let move): NotationMoveButton(move: move, current: current, onMove: onMove) case .comment(_, let text): Text(text) .font(.subheadline.italic()) .foregroundStyle(Theme.comment) .padding(.vertical, 6) case .punctuation(_, let text): Text(text) .font(.body) .foregroundStyle(Theme.onSurfaceVariant) .padding(.vertical, 6) } } } .frame(maxWidth: .infinity, alignment: .leading) } .padding(.leading, CGFloat(16 + variation.indent * 16)) .padding(.trailing, 16) .padding(.vertical, 4) } } private struct CommentBlock: View { let text: String let indent: Int var body: some View { Text(text) .font(.subheadline) .foregroundStyle(Theme.comment) .padding(.horizontal, 12) .padding(.vertical, 8) .frame(maxWidth: .infinity, alignment: .leading) .background(Theme.containerLow) .clipShape(RoundedRectangle(cornerRadius: 12)) .padding(.leading, CGFloat(16 + indent * 16)) .padding(.trailing, 16) .padding(.vertical, 4) } } /// iOS 16 token flow: every SAN remains its own Button even across lines and nested RAV. private struct FlowLayout: Layout { let spacing: CGFloat func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout Void) -> CGSize { let available = max(0, proposal.width ?? 0) var x: CGFloat = 0 var y: CGFloat = 0 var lineHeight: CGFloat = 0 var contentWidth: CGFloat = 0 for subview in subviews { let size = fittingSize(of: subview, available: available) if x > 0, x + spacing + size.width > available { x = 0 y += lineHeight + spacing lineHeight = 0 } if x > 0 { x += spacing } x += size.width lineHeight = max(lineHeight, size.height) contentWidth = max(contentWidth, x) } return CGSize(width: proposal.width ?? contentWidth, height: y + lineHeight) } func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout Void) { var x = bounds.minX var y = bounds.minY var lineHeight: CGFloat = 0 for subview in subviews { let size = fittingSize(of: subview, available: bounds.width) if x > bounds.minX, x + spacing + size.width > bounds.maxX { x = bounds.minX y += lineHeight + spacing lineHeight = 0 } if x > bounds.minX { x += spacing } subview.place(at: CGPoint(x: x, y: y), proposal: ProposedViewSize(size)) x += size.width lineHeight = max(lineHeight, size.height) } } private func fittingSize(of subview: LayoutSubview, available: CGFloat) -> CGSize { let intrinsic = subview.sizeThatFits(ProposedViewSize(width: nil, height: nil)) guard available > 0, intrinsic.width > available else { return intrinsic } return subview.sizeThatFits(ProposedViewSize(width: available, height: nil)) } }