Sources/OrgEditorAppKit/Theme.swift

b5449dec59a00fac6867954166c6c612ffd6aee4
orgstar/Sources/OrgEditorAppKit/Theme.swift history · blame · raw

99 lines · 4652 bytes

 1#if os(macOS)
 2import AppKit
 3import OrgPresentation
 4
 5/// Attributes for each style role. A fixed theme for now; the app's theme settings replace it.
 6@MainActor
 7struct Theme {
 8    let font = NSFont.monospacedSystemFont(ofSize: 13, weight: .regular)
 9
10    var base: [NSAttributedString.Key: Any] {
11        [.font: font, .foregroundColor: NSColor.textColor]
12    }
13
14    /// Width of one column of the monospaced font.
15    var column: CGFloat {
16        ("0" as NSString).size(withAttributes: [.font: font]).width
17    }
18
19    /// The band behind blocks.
20    nonisolated(unsafe) static let blockBackground = NSColor(name: nil) { appearance in
21        appearance.bestMatch(from: [.aqua, .darkAqua]) == .darkAqua ? NSColor(white: 1, alpha: 0.06) : NSColor(white: 0, alpha: 0.045)
22    }
23
24    /// Markup hidden from view: no width, no color.
25    var hidden: [NSAttributedString.Key: Any] {
26        [.font: NSFont.monospacedSystemFont(ofSize: 0.01, weight: .regular), .foregroundColor: NSColor.clear]
27    }
28
29    func paragraphStyle(firstLine: Int, wrapped: Int) -> NSParagraphStyle {
30        let style = NSMutableParagraphStyle()
31        style.firstLineHeadIndent = CGFloat(firstLine) * column
32        style.headIndent = CGFloat(wrapped) * column
33        return style
34    }
35
36    func apply(_ role: StyleRole, to storage: NSTextStorage, range: NSRange) {
37        switch role {
38        case .heading(let level):
39            let size = font.pointSize + CGFloat(max(0, 4 - level))
40            storage.addAttribute(.font, value: NSFont.monospacedSystemFont(ofSize: size, weight: .semibold), range: range)
41        case .leadingStars:
42            storage.addAttribute(.foregroundColor, value: NSColor.clear, range: range)
43        case .todo:
44            storage.addAttributes([.foregroundColor: NSColor.systemRed, .font: bold(storage, range)], range: range)
45        case .done:
46            storage.addAttributes([.foregroundColor: NSColor.systemGreen, .font: bold(storage, range)], range: range)
47        case .priority:
48            storage.addAttribute(.foregroundColor, value: NSColor.systemOrange, range: range)
49        case .tags, .comment, .keyword, .metadata:
50            storage.addAttribute(.foregroundColor, value: NSColor.secondaryLabelColor, range: range)
51        case .bold:
52            storage.addAttribute(.font, value: bold(storage, range), range: range)
53        case .italic:
54            storage.addAttribute(.font, value: NSFontManager.shared.convert(currentFont(storage, range), toHaveTrait: .italicFontMask), range: range)
55        case .underline:
56            storage.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
57        case .strikeThrough:
58            storage.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: range)
59        case .verbatim, .code, .inlineSource:
60            storage.addAttribute(.backgroundColor, value: NSColor.quaternarySystemFill, range: range)
61        case .markup, .linkMarkup, .blockDelimiter:
62            storage.addAttribute(.foregroundColor, value: NSColor.tertiaryLabelColor, range: range)
63        case .link:
64            storage.addAttributes([.foregroundColor: NSColor.linkColor, .underlineStyle: NSUnderlineStyle.single.rawValue], range: range)
65        case .timestamp:
66            storage.addAttribute(.foregroundColor, value: NSColor.systemPurple, range: range)
67        case .footnote, .cookie, .target, .macro, .latex:
68            storage.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range)
69        case .block, .table, .rule:
70            break
71        case .syntax(let category):
72            if let color = Self.color(category) { storage.addAttribute(.foregroundColor, value: color, range: range) }
73        }
74    }
75
76    /// Code colors, close to Xcode's default theme.
77    static func color(_ category: SyntaxCategory) -> NSColor? {
78        switch category {
79        case .keyword: .systemPink
80        case .string: .systemRed
81        case .comment: .secondaryLabelColor
82        case .function: .systemBlue
83        case .type: .systemTeal
84        case .number, .constant, .escape: .systemOrange
85        case .property, .attribute, .tag: .systemIndigo
86        case .label: .systemPurple
87        case .variable, .operator, .punctuation: nil
88        }
89    }
90
91    private func currentFont(_ storage: NSTextStorage, _ range: NSRange) -> NSFont {
92        storage.attribute(.font, at: range.location, effectiveRange: nil) as? NSFont ?? font
93    }
94
95    private func bold(_ storage: NSTextStorage, _ range: NSRange) -> NSFont {
96        NSFontManager.shared.convert(currentFont(storage, range), toHaveTrait: .boldFontMask)
97    }
98}
99#endif