Files
museion/Museion/SettingsPaneView.swift
T

203 lines
5.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// SettingsPaneView.swift
// Museion
//
// #17 (S1.1): Settings — the Museion Home path, repoint-only (ADR-0004).
// #71 (map #20 / #70): the Typography section — three role slots (Body /
// Headings / Code), each {face, size}: a Picker over every installed font
// family (defaults are being auditioned; shrinks to a curated shortlist
// once they settle) + a size field. UserDefaults persistence; re-picking a
// face is reset. Chrome stays system.
//
import AppKit
import SwiftUI
/// Settings pane. Home control per #17; typography slots per #70.
struct SettingsPaneView: View {
@Binding var home: URL
/// Full family list once per pane lifetime (#70: full list while
/// auditioning; the picker's current selection must be listed even when
/// NSFontManager omits it — Iowan Old Style is such a case).
private var families: [String] {
let current = [
UserDefaults.standard.string(forKey: "proseBodyFace"),
UserDefaults.standard.string(forKey: "proseHeadingsFace"),
UserDefaults.standard.string(forKey: "proseCodeFace"),
].compactMap(\.self)
let all = NSFontManager.shared.availableFontFamilies
return Array((current + all).uniqued())
}
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 24) {
contentSection
typographySection
flowSection
}
.padding(24)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
}
.background(DesignTokens.background)
}
// MARK: Home (#17)
private var contentSection: some View {
VStack(alignment: .leading, spacing: 12) {
Text("Content")
.font(.system(size: 13, weight: .semibold))
HStack(spacing: 8) {
Text(home.path(percentEncoded: false))
.font(.system(size: 12, design: DesignTokens.mono))
.lineLimit(1)
.truncationMode(.middle)
.textSelection(.enabled)
Spacer(minLength: 12)
Button("Choose…") { chooseHome() }
}
Text(
"Repoint-only: Museion never moves content. The Posts list reads this folder's posts/ directory.",
)
.font(.callout)
.foregroundStyle(DesignTokens.muted)
}
}
// MARK: Text flow (#71 polish)
private var flowSection: some View {
VStack(alignment: .leading, spacing: 12) {
Text("Text Flow")
.font(.system(size: 13, weight: .semibold))
Text(
"Reading rhythm. Line height scales the leading everywhere; the two spacings shape reading mode only — in the editor, your blank lines are the separator."
)
.font(.callout)
.foregroundStyle(DesignTokens.muted)
FlowRow(label: "Line height", key: "proseLineHeight", range: 1 ... 2, step: 0.05)
FlowRow(label: "Space after ¶", key: "proseParagraphSpacingLines", range: 0 ... 4, step: 0.25)
FlowRow(label: "Space after headings", key: "proseHeadingSpacingLines", range: 0 ... 6, step: 0.25)
}
}
// MARK: Typography (#70)
private var typographySection: some View {
VStack(alignment: .leading, spacing: 12) {
Text("Typography")
.font(.system(size: 13, weight: .semibold))
Text(
"The three prose roles. Both the editor and reading mode use them; a missing face falls back to the system font.",
)
.font(.callout)
.foregroundStyle(DesignTokens.muted)
SlotRow(
label: "Body", faceKey: "proseBodyFace", sizeKey: "proseBodySize",
families: families,
)
SlotRow(
label: "Headings", faceKey: "proseHeadingsFace", sizeKey: "proseHeadingsSize",
families: families,
)
SlotRow(
label: "Code", faceKey: "proseCodeFace", sizeKey: "proseCodeSize",
families: families,
)
}
}
private func chooseHome() {
let panel = NSOpenPanel()
panel.canChooseDirectories = true
panel.canChooseFiles = false
panel.canCreateDirectories = true
panel.message = "Choose the folder holding your posts. Museion never moves content."
if panel.runModal() == .OK, let url = panel.url {
home = url
}
}
}
/// One role slot: face picker (full family list) + size stepper.
private struct SlotRow: View {
let label: String
let faceKey: String
let sizeKey: String
let families: [String]
var body: some View {
HStack(spacing: 10) {
Text(label)
.font(.system(size: 12, weight: .semibold))
.frame(width: 76, alignment: .leading)
Picker("", selection: faceBinding) {
ForEach(families, id: \.self) { family in
Text(family).tag(family)
}
}
.labelsHidden()
.frame(maxWidth: 320)
TextField("", value: sizeBinding, format: .number)
.textFieldStyle(.roundedBorder)
.frame(width: 56)
Text("pt")
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.muted)
}
}
private var faceBinding: Binding<String> {
Binding(
get: { UserDefaults.standard.string(forKey: faceKey) ?? "" },
set: { UserDefaults.standard.set($0, forKey: faceKey) },
)
}
private var sizeBinding: Binding<Double> {
Binding(
get: { UserDefaults.standard.double(forKey: sizeKey) },
set: { UserDefaults.standard.set(max(6, min(72, $0)), forKey: sizeKey) },
)
}
}
/// One flow knob: label + numeric field (UserDefaults-backed).
private struct FlowRow: View {
let label: String
let key: String
let range: ClosedRange<Double>
let step: Double
var body: some View {
HStack(spacing: 10) {
Text(label)
.font(.system(size: 12, weight: .semibold))
.frame(width: 150, alignment: .leading)
TextField("", value: binding, format: .number.precision(.fractionLength(2)))
.textFieldStyle(.roundedBorder)
.frame(width: 64)
Text("× body line")
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.muted)
}
}
private var binding: Binding<Double> {
Binding(
get: { UserDefaults.standard.double(forKey: key) },
set: { UserDefaults.standard.set(max(range.lowerBound, min(range.upperBound, $0)), forKey: key) },
)
}
}
private extension Array {
/// Order-preserving dedupe (face lists merge current + installed).
func uniqued() -> [Element] where Element: Hashable {
var seen = Set<Element>()
return filter { seen.insert($0).inserted }
}
}