SPB Git

Repository Public

PageSetupView.swift 6.3 KB 110 lines swift

1import SwiftUI2import SheetCore3import SheetUI45/// Page layout of the active sheet: orientation, paper, margins, scaling, print area, titles, header and footer.6struct PageSetupView: View {7    let session: WorkbookSession8    let close: () -> Void9    @State private var p = PrintSettings()10    @State private var fitWidth = false11    @State private var fitHeight = false12    @State private var printAreaText = ""13    @State private var repeatRowsText = ""1415    /// The first element is both the stored paper name and the catalog key of its display name.16    static let papers: [(String, Double, Double)] = [(LK("Letter"), 612, 792), (LK("Legal"), 612, 1008), (LK("Tabloid"), 792, 1224), (LK("A3"), 841.89, 1190.55), (LK("A4"), 595.28, 841.89), (LK("A5"), 419.53, 595.28)]1718    var body: some View {19        VStack(alignment: .leading, spacing: ORIX.Space.sm) {20            Text(String(format: NSLocalizedString("Page Setup — %@", comment: ""), session.sheet.name)).font(.system(size: 16, weight: .semibold)).accessibilityAddTraits(.isHeader)21            Form {22                Section {23                    Picker(NSLocalizedString("Paper", comment: ""), selection: $p.paperName) {24                        ForEach(Self.papers, id: \.0) { Text(NSLocalizedString($0.0, comment: "")).tag($0.0) }25                    }26                    Picker(NSLocalizedString("Orientation", comment: ""), selection: $p.landscape) {27                        Text(NSLocalizedString("Portrait", comment: "")).tag(false)28                        Text(NSLocalizedString("Landscape", comment: "")).tag(true)29                    }30                    .pickerStyle(.segmented)31                }32                Section(NSLocalizedString("Scaling", comment: "")) {33                    Stepper(value: $p.scalePercent, in: 10...400, step: 5) { Text(String(format: NSLocalizedString("Scale: %d%%", comment: ""), p.scalePercent)) }.disabled(fitWidth || fitHeight)34                    Toggle(NSLocalizedString("Fit all columns on one page wide", comment: ""), isOn: $fitWidth)35                    Toggle(NSLocalizedString("Fit all rows on one page tall", comment: ""), isOn: $fitHeight)36                }37                Section(NSLocalizedString("Margins (points)", comment: "")) {38                    HStack {39                        margin(LK("Top"), $p.marginTop); margin(LK("Bottom"), $p.marginBottom); margin(LK("Left"), $p.marginLeft); margin(LK("Right"), $p.marginRight)40                    }41                    Toggle(NSLocalizedString("Centre horizontally", comment: ""), isOn: $p.centerHorizontally)42                    Toggle(NSLocalizedString("Centre vertically", comment: ""), isOn: $p.centerVertically)43                }44                Section(NSLocalizedString("Sheet", comment: "")) {45                    HStack {46                        TextField(NSLocalizedString("Print area (e.g. A1:H40, empty = used cells)", comment: ""), text: $printAreaText)47                        Button(NSLocalizedString("Use Selection", comment: "")) { printAreaText = session.selection.primary.a1 }48                    }49                    TextField(NSLocalizedString("Rows to repeat at top (e.g. 1:2)", comment: ""), text: $repeatRowsText)50                    Toggle(NSLocalizedString("Print gridlines", comment: ""), isOn: $p.showGridlines)51                    Toggle(NSLocalizedString("Print row and column headings", comment: ""), isOn: $p.showHeadings)52                }53                Section(NSLocalizedString("Header and footer", comment: "")) {54                    TextField(NSLocalizedString("Header", comment: ""), text: $p.header)55                    TextField(NSLocalizedString("Footer", comment: ""), text: $p.footer)56                    Text(NSLocalizedString("Codes: &L &C &R sections · &P page · &N pages · &D date · &T time · &F file · &A sheet", comment: ""))57                        .font(.caption).foregroundStyle(.secondary)58                }59            }60            .formStyle(.grouped)61            HStack {62                Text(String(format: NSLocalizedString("%d page(s) with these settings", comment: ""), previewPages)).font(.caption).foregroundStyle(.secondary)63                Spacer()64                Button(NSLocalizedString("Cancel", comment: ""), action: close).keyboardShortcut(.cancelAction)65                Button(NSLocalizedString("Apply", comment: "")) { apply(); close() }.keyboardShortcut(.defaultAction)66            }67        }68        .padding(ORIX.Space.lg)69        .frame(width: 560, height: 640)70        .onAppear {71            p = session.sheet.meta.print72            fitWidth = p.fitToWidth != nil73            fitHeight = p.fitToHeight != nil74            printAreaText = p.printArea?.a1 ?? ""75            repeatRowsText = p.repeatRows.map { "\($0.lowerBound + 1):\($0.upperBound + 1)" } ?? ""76        }77    }7879    private func margin(_ label: String, _ value: Binding<Double>) -> some View {80        VStack(alignment: .leading, spacing: 2) {81            Text(NSLocalizedString(label, comment: "")).font(.caption).foregroundStyle(.secondary)82            TextField("", value: value, format: .number.precision(.fractionLength(0...1))).frame(width: 64)83                .accessibilityLabel(String(format: NSLocalizedString("%@ margin in points", comment: "Top/Bottom/Left/Right margin"), NSLocalizedString(label, comment: "")))84        }85    }8687    private func resolved() -> PrintSettings {88        var out = p89        if let paper = Self.papers.first(where: { $0.0 == p.paperName }) { out.paperWidthPt = paper.1; out.paperHeightPt = paper.2 }90        out.fitToWidth = fitWidth ? 1 : nil91        out.fitToHeight = fitHeight ? 1 : nil92        out.printArea = RangeRef(a1: printAreaText.trimmingCharacters(in: .whitespaces))93        if let r = RangeRef(a1: repeatRowsText.trimmingCharacters(in: .whitespaces)), r.isWholeRows { out.repeatRows = r.r0...r.r1 } else { out.repeatRows = nil }94        return out95    }9697    private var previewPages: Int {98        let saved = session.sheet.meta.print99        session.sheet.meta.print = resolved()100        defer { session.sheet.meta.print = saved }101        return PrintLayout(workbook: session.workbook, sheetIndex: session.sheetIndex).pages.count102    }103104    private func apply() {105        let new = resolved()106        let si = session.sheetIndex107        session.perform("Page Setup") { $0.editMeta(sheet: si) { $0.print = new } }108    }109}110
Files main
Menu

Theme