Repository Public
1import SwiftUI2import SheetCore3import SheetUI4import AnalysisKit56/// "Mark as Input / Output" commands, the colour convention and the list of assumption cells of the active sheet.7enum ModelConventionsTool {8 static let descriptor: ToolDescriptor = {9 var t = ToolDescriptor(10 id: "finance.conventions", title: L("Inputs & Outputs"), group: FinGroup.conventions, systemImage: "paintpalette",11 summary: L("Applies the financial-modelling colour convention to the selection and lists the assumption cells of the active sheet."),12 fields: [], run: { _ in ToolResult(title: L("Inputs & Outputs"), blocks: [], canWriteReport: false) })13 t.customPanel = { AnyView(ModelConventionsPanel(session: $0)) }14 t.demo = { session, _ in15 guard let si = session.workbook.sheetIndex(named: "Model"), let inputs = RangeRef(a1: "B4:B9"), let outputs = RangeRef(a1: "B17:B18") else { return }16 session.activateSheet(si)17 session.select(inputs)18 ModelStyle.markSelection(session, asInput: true)19 session.select(outputs)20 ModelStyle.markSelection(session, asInput: false)21 session.select(RangeRef(CellRef(4, 1)))22 }23 return t24 }()25}2627struct ModelConventionsPanel: View {28 let session: WorkbookSession2930 var body: some View {31 let _ = session.contentRevision32 let _ = session.revision33 let cells = ModelStyle.assumptionCells(workbook: session.workbook, sheet: session.sheetIndex)34 VStack(alignment: .leading, spacing: 14) {35 card {36 VStack(alignment: .leading, spacing: 10) {37 Text(L("Selection: %@ on “%@”", session.selection.ranges.map(\.a1).joined(separator: ", "), session.sheet.name))38 .font(.system(size: 12)).foregroundStyle(ORIX.Palette.textSecondary)39 HStack(spacing: 10) {40 Button { ModelStyle.markSelection(session, asInput: true) } label: { Label(L("Mark as Input"), systemImage: "pencil.line") }41 .buttonStyle(PrimaryButtonStyle())42 .help(L("Blue text on a light fill: a value the user may change"))43 .accessibilityIdentifier("finance.conventions.mark-input")44 Button { ModelStyle.markSelection(session, asInput: false) } label: { Label(L("Mark as Output"), systemImage: "equal.square") }45 .help(L("Bold: a result of the model"))46 .accessibilityIdentifier("finance.conventions.mark-output")47 }48 Rectangle().fill(ORIX.Palette.hairline).frame(height: 1)49 Text(L("Colour convention").uppercased()).font(.system(size: 10.5, weight: .semibold)).kerning(0.6).foregroundStyle(ORIX.Palette.textTertiary)50 .accessibilityAddTraits(.isHeader)51 legend(sample: "1,250.00", text: L("Input (assumption): blue text on a light fill. Typed by the user."), input: true, bold: false)52 legend(sample: "1,362.50", text: L("Calculation: default black text. A formula; do not overwrite."), input: false, bold: false)53 legend(sample: "2,855.88", text: L("Output: bold. A result read by the user of the model."), input: false, bold: true)54 }55 }56 card {57 VStack(alignment: .leading, spacing: 8) {58 HStack {59 Text(L("Assumption cells on “%@”", session.sheet.name)).font(.system(size: 12.5, weight: .semibold))60 .accessibilityAddTraits(.isHeader)61 Spacer()62 Text(L("%d cells", cells.count)).font(ORIX.Typeface.caption).foregroundStyle(ORIX.Palette.textTertiary)63 }64 if cells.isEmpty {65 EmptyStateView(systemImage: "pencil.line", title: L("No assumption cells yet"),66 message: L("Select the input cells of your model in the sheet, then choose Mark as Input. They are listed here."))67 .frame(minHeight: 150)68 } else {69 VStack(spacing: 0) {70 ForEach(cells.prefix(400)) { c in row(c) }71 }72 .clipShape(RoundedRectangle(cornerRadius: ORIX.Radius.control))73 .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.control).strokeBorder(ORIX.Palette.hairline))74 if cells.count > 400 {75 Text(L("Showing the first %d cells.", 400)).font(.system(size: 11)).foregroundStyle(ORIX.Palette.textTertiary)76 }77 }78 }79 }80 }81 .accessibilityIdentifier("finance.conventions.panel")82 }8384 private func card<C: View>(@ViewBuilder _ content: () -> C) -> some View {85 content()86 .padding(ORIX.Space.md)87 .frame(maxWidth: .infinity, alignment: .leading)88 .background(RoundedRectangle(cornerRadius: ORIX.Radius.card).fill(ORIX.Palette.surface))89 .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.card).strokeBorder(ORIX.Palette.hairline))90 }9192 private func legend(sample: String, text: String, input: Bool, bold: Bool) -> some View {93 HStack(spacing: 10) {94 Text(sample)95 .font(.system(size: 12, weight: bold ? .bold : .regular).monospacedDigit())96 .foregroundStyle(input ? Color(ModelStyle.inputText) : ORIX.Palette.textPrimary)97 .frame(width: 84, alignment: .trailing)98 .padding(.horizontal, 6).padding(.vertical, 3)99 .background(input ? Color(ModelStyle.inputFill) : Color.clear)100 .overlay(Rectangle().strokeBorder(ORIX.Palette.hairline))101 .accessibilityHidden(true)102 Text(text).font(.system(size: 12)).foregroundStyle(ORIX.Palette.textSecondary)103 }104 }105106 private func row(_ c: ModelStyle.AssumptionCell) -> some View {107 let display = Workspace.shared.displayProvider.display(c.value, format: c.numberFormat, maxChars: nil, date1904: session.workbook.date1904).text108 return Button {109 session.select(RangeRef(c.cell))110 session.scrollTarget = c.cell111 } label: {112 HStack(spacing: 10) {113 Text(c.cell.a1).font(ORIX.Typeface.mono).foregroundStyle(ORIX.Palette.accent).frame(width: 56, alignment: .leading)114 Text(c.label.isEmpty ? L("(no label)") : c.label).font(.system(size: 12)).foregroundStyle(c.label.isEmpty ? ORIX.Palette.textTertiary : ORIX.Palette.textPrimary).lineLimit(1)115 Spacer(minLength: 8)116 if c.isFormula { Image(systemName: "function").font(.system(size: 10)).foregroundStyle(ORIX.Palette.warning).help(L("This input cell contains a formula")).accessibilityLabel(L("This input cell contains a formula")) }117 Text(display).font(.system(size: 12).monospacedDigit()).foregroundStyle(ORIX.Palette.accentSecondary).lineLimit(1)118 }119 .padding(.horizontal, 10).padding(.vertical, 5)120 .background(session.selection.active == c.cell ? ORIX.Palette.accent.opacity(0.10) : ORIX.Palette.surface)121 .overlay(alignment: .bottom) { Rectangle().fill(ORIX.Palette.hairline.opacity(0.6)).frame(height: 0.5) }122 .contentShape(Rectangle())123 }124 .buttonStyle(.plain)125 .help(L("Select this cell"))126 .accessibilityLabel(L("%@, %@, value %@", c.cell.a1, c.label, display))127 .accessibilityIdentifier("finance.conventions.cell.\(c.cell.a1)")128 }129}130