Sources/OrgEditorAppKit/Theme.swift
96 lines · 4505 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 /// Markup hidden from view: no width, no color.
20 var hidden: [NSAttributedString.Key: Any] {
21 [.font: NSFont.monospacedSystemFont(ofSize: 0.01, weight: .regular), .foregroundColor: NSColor.clear]
22 }
23
24 func paragraphStyle(firstLine: Int, wrapped: Int) -> NSParagraphStyle {
25 let style = NSMutableParagraphStyle()
26 style.firstLineHeadIndent = CGFloat(firstLine) * column
27 style.headIndent = CGFloat(wrapped) * column
28 return style
29 }
30
31 func apply(_ role: StyleRole, to storage: NSTextStorage, range: NSRange) {
32 switch role {
33 case .heading(let level):
34 let size = font.pointSize + CGFloat(max(0, 4 - level))
35 storage.addAttribute(.font, value: NSFont.monospacedSystemFont(ofSize: size, weight: .semibold), range: range)
36 case .leadingStars:
37 storage.addAttribute(.foregroundColor, value: NSColor.clear, range: range)
38 case .todo:
39 storage.addAttributes([.foregroundColor: NSColor.systemRed, .font: bold(storage, range)], range: range)
40 case .done:
41 storage.addAttributes([.foregroundColor: NSColor.systemGreen, .font: bold(storage, range)], range: range)
42 case .priority:
43 storage.addAttribute(.foregroundColor, value: NSColor.systemOrange, range: range)
44 case .tags, .comment, .keyword, .metadata:
45 storage.addAttribute(.foregroundColor, value: NSColor.secondaryLabelColor, range: range)
46 case .bold:
47 storage.addAttribute(.font, value: bold(storage, range), range: range)
48 case .italic:
49 storage.addAttribute(.font, value: NSFontManager.shared.convert(currentFont(storage, range), toHaveTrait: .italicFontMask), range: range)
50 case .underline:
51 storage.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
52 case .strikeThrough:
53 storage.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: range)
54 case .verbatim, .code, .inlineSource:
55 storage.addAttribute(.backgroundColor, value: NSColor.quaternarySystemFill, range: range)
56 case .markup, .linkMarkup, .blockDelimiter:
57 storage.addAttribute(.foregroundColor, value: NSColor.tertiaryLabelColor, range: range)
58 case .link:
59 storage.addAttributes([.foregroundColor: NSColor.linkColor, .underlineStyle: NSUnderlineStyle.single.rawValue], range: range)
60 case .timestamp:
61 storage.addAttribute(.foregroundColor, value: NSColor.systemPurple, range: range)
62 case .footnote, .cookie, .target, .macro, .latex:
63 storage.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range)
64 case .block:
65 storage.addAttribute(.backgroundColor, value: NSColor.quaternarySystemFill, range: range)
66 case .table, .rule:
67 break
68 case .syntax(let category):
69 if let color = Self.color(category) { storage.addAttribute(.foregroundColor, value: color, range: range) }
70 }
71 }
72
73 /// Code colors, close to Xcode's default theme.
74 static func color(_ category: SyntaxCategory) -> NSColor? {
75 switch category {
76 case .keyword: .systemPink
77 case .string: .systemRed
78 case .comment: .secondaryLabelColor
79 case .function: .systemBlue
80 case .type: .systemTeal
81 case .number, .constant, .escape: .systemOrange
82 case .property, .attribute, .tag: .systemIndigo
83 case .label: .systemPurple
84 case .variable, .operator, .punctuation: nil
85 }
86 }
87
88 private func currentFont(_ storage: NSTextStorage, _ range: NSRange) -> NSFont {
89 storage.attribute(.font, at: range.location, effectiveRange: nil) as? NSFont ?? font
90 }
91
92 private func bold(_ storage: NSTextStorage, _ range: NSRange) -> NSFont {
93 NSFontManager.shared.convert(currentFont(storage, range), toHaveTrait: .boldFontMask)
94 }
95}
96#endif