SPB Git

Repository Public

TemplateChooser.swift 6.8 KB 136 lines swift

1import SwiftUI2import AppKit3import AppIntents4import SheetCore5import SheetUI6import SheetTemplates7import FeatureAutomation89/// Lets Shortcuts discover the intents defined in the automation package.10struct ORIXSheetIntents: AppIntentsPackage {11    static var includedPackages: [any AppIntentsPackage.Type] { [AutomationIntentsPackage.self] }12}1314/// Template chooser shown at launch and from File ▸ New from Template.15struct TemplateChooser: View {16    let choose: (Workbook?, String?) -> Void17    let openExisting: () -> Void18    @State private var category: TemplateCategory?19    @State private var selected: String?20    @State private var thumbnails: [String: NSImage] = [:]21    private let locale = Locale.current2223    private var templates: [WorkbookTemplate] { category.map { TemplateLibrary.templates(in: $0) } ?? TemplateLibrary.all }2425    var body: some View {26        HStack(spacing: 0) {27            VStack(alignment: .leading, spacing: 4) {28                HStack(spacing: 10) {29                    Image(nsImage: NSApp.applicationIconImage).resizable().frame(width: 44, height: 44).accessibilityHidden(true)30                    VStack(alignment: .leading, spacing: 0) {31                        Text("ORIX Sheet").font(.system(size: 18, weight: .bold))32                        Text(NSLocalizedString("Beyond Spreadsheets.", comment: "")).font(.system(size: 11.5)).foregroundStyle(ORIX.Palette.accentText)33                    }34                }35                .padding(.bottom, ORIX.Space.md)36                categoryRow(nil, NSLocalizedString("All Templates", comment: ""), "square.grid.2x2")37                ForEach(TemplateCategory.allCases, id: \.self) { c in categoryRow(c, c.title(locale: locale), icon(c)) }38                Spacer()39                Button { openExisting() } label: { Label(NSLocalizedString("Open a File…", comment: ""), systemImage: "folder") }40                    .buttonStyle(.plain).font(.system(size: 12.5)).padding(.vertical, 4)41                Text(NSLocalizedString("Everything stays on this Mac.", comment: "")).font(.system(size: 10.5)).foregroundStyle(ORIX.Palette.textTertiary)42            }43            .padding(ORIX.Space.md)44            .frame(width: 210)45            .background(ORIX.Palette.canvas)4647            VStack(spacing: 0) {48                ScrollView {49                    LazyVGrid(columns: [GridItem(.adaptive(minimum: 200, maximum: 240), spacing: ORIX.Space.md)], spacing: ORIX.Space.md) {50                        card(id: "blank", title: NSLocalizedString("Blank Workbook", comment: ""), summary: NSLocalizedString("An empty sheet.", comment: ""), image: nil, systemImage: "plus")51                        ForEach(templates) { t in52                            card(id: t.id, title: t.title(locale: locale), summary: t.summary(locale: locale), image: thumbnails[t.id], systemImage: t.systemImage)53                        }54                    }55                    .padding(ORIX.Space.lg)56                }57                Divider()58                HStack {59                    Spacer()60                    Button(NSLocalizedString("Cancel", comment: "")) { NSApp.keyWindow?.close() }.keyboardShortcut(.cancelAction)61                    Button(NSLocalizedString("Create", comment: "")) { create(selected ?? "blank") }62                        .keyboardShortcut(.defaultAction).buttonStyle(.borderedProminent).tint(ORIX.Palette.accentFill)63                        .accessibilityIdentifier("template-create")64                }65                .padding(ORIX.Space.sm)66            }67            .background(ORIX.Palette.surface)68        }69        .frame(width: 940, height: 640)70        .task { await loadThumbnails() }71    }7273    private func icon(_ c: TemplateCategory) -> String {74        switch c {75        case .personal: "person"76        case .business: "building.2"77        case .finance: "chart.line.uptrend.xyaxis"78        case .planning: "calendar"79        case .research: "flask"80        case .education: "graduationcap"81        }82    }8384    private func categoryRow(_ c: TemplateCategory?, _ title: String, _ image: String) -> some View {85        Button { category = c } label: {86            HStack(spacing: 8) {87                Image(systemName: image).frame(width: 18).foregroundStyle(category == c ? ORIX.Palette.accentText : ORIX.Palette.textSecondary)88                Text(title).font(.system(size: 12.5, weight: category == c ? .semibold : .regular))89                Spacer()90            }91            .padding(.horizontal, 8).padding(.vertical, 5)92            .background(RoundedRectangle(cornerRadius: ORIX.Radius.chip).fill(category == c ? ORIX.Palette.surfaceRaised : .clear))93            .contentShape(Rectangle())94        }95        .buttonStyle(.plain)96        .accessibilityAddTraits(category == c ? .isSelected : [])97    }9899    private func card(id: String, title: String, summary: String, image: NSImage?, systemImage: String) -> some View {100        let isSelected = (selected ?? "blank") == id101        return VStack(alignment: .leading, spacing: 6) {102            ZStack {103                RoundedRectangle(cornerRadius: ORIX.Radius.control).fill(Color.white)104                if let image { Image(nsImage: image).resizable().aspectRatio(contentMode: .fill) }105                else { Image(systemName: systemImage).font(.system(size: 30, weight: .light)).foregroundStyle(ORIX.Palette.accent) }106            }107            .frame(height: 132)108            .clipShape(RoundedRectangle(cornerRadius: ORIX.Radius.control))109            .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.control).strokeBorder(isSelected ? ORIX.Palette.accent : ORIX.Palette.hairline, lineWidth: isSelected ? 2.5 : 1))110            Text(title).font(.system(size: 12.5, weight: .semibold)).lineLimit(1)111            Text(summary).font(.system(size: 11)).foregroundStyle(ORIX.Palette.textSecondary).lineLimit(2).frame(height: 28, alignment: .top)112        }113        .contentShape(Rectangle())114        .onTapGesture(count: 2) { create(id) }115        .onTapGesture { selected = id }116        .accessibilityElement(children: .combine)117        .accessibilityLabel("\(title). \(summary)")118        .accessibilityAddTraits(isSelected ? [.isButton, .isSelected] : .isButton)119        .accessibilityIdentifier("template-\(id)")120    }121122    private func create(_ id: String) {123        if id == "blank" { choose(nil, nil); return }124        guard let t = TemplateLibrary.template(id: id) else { return }125        choose(t.build(locale: locale), t.title(locale: locale))126    }127128    private func loadThumbnails() async {129        for t in TemplateLibrary.all where thumbnails[t.id] == nil {130            let locale = self.locale131            let cg = await Task.detached(priority: .userInitiated) { UncheckedSendableBox(TemplateThumbnail.image(of: t, locale: locale, size: CGSize(width: 240, height: 132))) }.value.value132            if let cg { thumbnails[t.id] = NSImage(cgImage: cg, size: NSSize(width: 240, height: 132)) }133        }134    }135}136
Files main
Menu

Theme