feat(editor): configurable text flow — line height + spacing knobs (#71 follow-up 7)
Three UserDefaults-backed knobs in ProseTypography (line-height multiple, space-after-¶ and space-after-heading in body lines, clamped), surfaced in Settings → Text Flow. Also fixes two real bugs from the last commit: paragraphSpacing was set to 1.25 POINTS (now lines × linePoints), and reading-mode body paragraphs never received the rhythm at all (they rode the editing surface's fixed 10pt) — both surfaces now share one rhythm.
This commit is contained in:
1 parent
2d4e2563f7
commit
6d7d1411a2
4 files changed
+98
-5
No files matched your search
@@ -42,10 +42,12 @@ nonisolated enum ProsePresenter {
|
||||
/// as body anyway; dedicated quote spacing waits for #20's polish pass.
|
||||
static func paragraph(_ kind: LineKind) -> NSParagraphStyle {
|
||||
let style = NSMutableParagraphStyle()
|
||||
style.lineHeightMultiple = ProseTypography.lineHeightMultiple
|
||||
let line = ProseTypography.linePoints
|
||||
switch kind {
|
||||
case .heading1: style.paragraphSpacing = 18; style.paragraphSpacingBefore = 26
|
||||
case .heading2: style.paragraphSpacing = 12; style.paragraphSpacingBefore = 20
|
||||
case .quote, .syntaxOnly, .body: style.paragraphSpacing = 10
|
||||
case .heading1: style.paragraphSpacing = 2 * line; style.paragraphSpacingBefore = 26
|
||||
case .heading2: style.paragraphSpacing = 2 * line; style.paragraphSpacingBefore = 1 * line
|
||||
case .quote, .syntaxOnly, .body: style.paragraphSpacing = 1 * line
|
||||
}
|
||||
return style
|
||||
}
|
||||
|
||||
@@ -28,6 +28,11 @@ nonisolated enum ProseTypography {
|
||||
static let defaultBodySize: Double = 17
|
||||
static let defaultHeadingsSize: Double = 26
|
||||
static let defaultCodeSize: Double = 13
|
||||
/// Text flow (#71 polish): line-height multiple + paragraph/heading
|
||||
/// spacing measured in body lines — different faces need different flow.
|
||||
static let defaultLineHeight: Double = 1.25
|
||||
static let defaultParagraphSpacingLines: Double = 1
|
||||
static let defaultHeadingSpacingLines: Double = 2
|
||||
|
||||
/// Register the six defaults once at app start.
|
||||
static func registerDefaults() {
|
||||
@@ -38,6 +43,9 @@ nonisolated enum ProseTypography {
|
||||
"proseHeadingsSize": defaultHeadingsSize,
|
||||
"proseCodeFace": defaultCodeFace,
|
||||
"proseCodeSize": defaultCodeSize,
|
||||
"proseLineHeight": defaultLineHeight,
|
||||
"proseParagraphSpacingLines": defaultParagraphSpacingLines,
|
||||
"proseHeadingSpacingLines": defaultHeadingSpacingLines,
|
||||
])
|
||||
}
|
||||
|
||||
@@ -83,6 +91,35 @@ nonisolated enum ProseTypography {
|
||||
bolded(bodyFont)
|
||||
}
|
||||
|
||||
// MARK: Text flow (#71 polish)
|
||||
|
||||
/// Line-height multiple (clamped 1…2; 0/absent → default).
|
||||
static var lineHeightMultiple: CGFloat {
|
||||
clamped("proseLineHeight", default: defaultLineHeight, 1 ... 2)
|
||||
}
|
||||
|
||||
/// Spacing after body paragraphs, in body lines.
|
||||
static var paragraphSpacingLines: CGFloat {
|
||||
clamped("proseParagraphSpacingLines", default: defaultParagraphSpacingLines, 0 ... 4)
|
||||
}
|
||||
|
||||
/// Spacing after headings, in body lines.
|
||||
static var headingSpacingLines: CGFloat {
|
||||
clamped("proseHeadingSpacingLines", default: defaultHeadingSpacingLines, 0 ... 6)
|
||||
}
|
||||
|
||||
/// One body line at the configured flow, in points.
|
||||
static var linePoints: CGFloat {
|
||||
let f = bodyFont
|
||||
return (f.ascender - f.descender + f.leading) * lineHeightMultiple
|
||||
}
|
||||
|
||||
private static func clamped(_ key: String, default def: Double, _ range: ClosedRange<Double>) -> CGFloat {
|
||||
let stored = UserDefaults.standard.double(forKey: key)
|
||||
let value = stored == 0 ? def : stored
|
||||
return CGFloat(min(max(value, range.lowerBound), range.upperBound))
|
||||
}
|
||||
|
||||
// MARK: Attribute stacks (reading + editing share them)
|
||||
|
||||
static var foregroundColor: NSColor {
|
||||
|
||||
@@ -146,7 +146,8 @@ enum ReadingPresenter {
|
||||
/// paragraph when the file can't load.
|
||||
private static func imageBlockContent(url: URL, alt: String) -> NSAttributedString {
|
||||
let ps = NSMutableParagraphStyle()
|
||||
ps.paragraphSpacing = 10
|
||||
ps.lineHeightMultiple = lineHeightMultiple
|
||||
ps.paragraphSpacing = paragraphSpacingAfter
|
||||
ps.paragraphSpacingBefore = 8
|
||||
guard let image = NSImage(contentsOf: url), image.size.width > 0 else {
|
||||
return inlineParse(alt.isEmpty ? ")" : alt)
|
||||
@@ -201,7 +202,13 @@ enum ReadingPresenter {
|
||||
.map { $0.trimmingCharacters(in: .whitespaces) }
|
||||
.filter { !$0.isEmpty }
|
||||
.joined(separator: " ")
|
||||
return inlineParse(joined)
|
||||
let para = inlineParse(joined)
|
||||
let ps = NSMutableParagraphStyle()
|
||||
ps.lineHeightMultiple = lineHeightMultiple
|
||||
ps.paragraphSpacing = paragraphSpacingAfter
|
||||
let styled = NSMutableAttributedString(attributedString: para)
|
||||
styled.addAttribute(.paragraphStyle, value: ps, range: NSRange(location: 0, length: styled.length))
|
||||
return styled
|
||||
}
|
||||
|
||||
private static func headingContent(_ text: String, level: Int) -> NSAttributedString {
|
||||
|
||||
@@ -35,6 +35,7 @@ struct SettingsPaneView: View {
|
||||
VStack(alignment: .leading, spacing: 24) {
|
||||
contentSection
|
||||
typographySection
|
||||
flowSection
|
||||
}
|
||||
.padding(24)
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
||||
@@ -65,6 +66,23 @@ struct SettingsPaneView: View {
|
||||
}
|
||||
}
|
||||
|
||||
// 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; the spacings are measured in body lines — tune them when a face change tightens or loosens the page.",
|
||||
)
|
||||
.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 {
|
||||
@@ -146,6 +164,35 @@ private struct SlotRow: View {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
|
||||
Reference in new issue
Block a user