Sources/OrgstarMobile/MobileTheme.swift
88 lines · 4035 bytes
1#if os(iOS)
2import OrgPresentation
3import UIKit
4
5/// Attributes for each style role, from a `ThemeSpec`, as the Mac editor's theme sets them.
6@MainActor
7struct MobileTheme {
8 let spec: ThemeSpec
9 let font: UIFont
10
11 init(_ spec: ThemeSpec = .default) {
12 self.spec = spec
13 // Dynamic Type, monospaced as on the Mac.
14 font = UIFontMetrics(forTextStyle: .body).scaledFont(for: .monospacedSystemFont(ofSize: 15, weight: .regular))
15 }
16
17 /// A color that follows the appearance, or nil when the theme has none for `key`.
18 func color(_ key: String) -> UIColor? {
19 let light = spec.light[key] ?? ThemeSpec.default.light[key] ?? spec.dark[key]
20 let dark = spec.dark[key] ?? ThemeSpec.default.dark[key] ?? spec.light[key]
21 guard let light, let dark else { return nil }
22 return Self.dynamic(light, dark)
23 }
24
25 static func dynamic(_ light: RGBA, _ dark: RGBA) -> UIColor {
26 UIColor { traits in
27 let c = traits.userInterfaceStyle == .dark ? dark : light
28 return UIColor(red: c.red, green: c.green, blue: c.blue, alpha: c.alpha)
29 }
30 }
31
32 var base: [NSAttributedString.Key: Any] {
33 let style = NSMutableParagraphStyle()
34 style.lineSpacing = CGFloat(max(0, spec.lineSpacing))
35 return [.font: font, .foregroundColor: color("foreground") ?? .label, .paragraphStyle: style]
36 }
37
38 func apply(_ role: StyleRole, to storage: NSTextStorage, range: NSRange) {
39 func fg(_ key: String) {
40 if let color = color(key) { storage.addAttribute(.foregroundColor, value: color, range: range) }
41 }
42 func traits(_ add: UIFontDescriptor.SymbolicTraits) {
43 let current = storage.attribute(.font, at: range.location, effectiveRange: nil) as? UIFont ?? font
44 if let descriptor = current.fontDescriptor.withSymbolicTraits(current.fontDescriptor.symbolicTraits.union(add)) {
45 storage.addAttribute(.font, value: UIFont(descriptor: descriptor, size: current.pointSize), range: range)
46 }
47 }
48 switch role {
49 case .heading(let level):
50 let size = font.pointSize + CGFloat(max(0, spec.headingSizeStep) * max(0, 4 - level))
51 storage.addAttribute(.font, value: font.withSize(size), range: range)
52 traits(.traitBold)
53 fg("heading-\(min(max(level, 1), 8))")
54 case .leadingStars: fg("markup")
55 case .todo, .done:
56 traits(.traitBold)
57 let keyword = (storage.string as NSString).substring(with: range)
58 if let custom = spec.todo[keyword] {
59 storage.addAttribute(.foregroundColor, value: Self.dynamic(custom, custom), range: range)
60 } else {
61 fg(role == .todo ? "todo" : "done")
62 }
63 case .priority: fg("priority")
64 case .tags: fg("tags")
65 case .comment: fg("comment")
66 case .keyword: fg("keyword")
67 case .metadata: fg("metadata")
68 case .bold: traits(.traitBold)
69 case .italic: traits(.traitItalic)
70 case .underline: storage.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
71 case .strikeThrough: storage.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: range)
72 case .verbatim, .code, .inlineSource:
73 if let background = color("inline-background") { storage.addAttribute(.backgroundColor, value: background, range: range) }
74 fg(role == .verbatim ? "verbatim" : "code")
75 case .markup, .linkMarkup: fg("markup")
76 case .blockDelimiter: fg("block-delimiter")
77 case .link:
78 storage.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
79 fg("link")
80 case .timestamp: fg("timestamp")
81 case .footnote, .cookie, .target, .macro, .latex: fg("special")
82 case .table, .rule: fg("table")
83 case .block: break
84 case .syntax: break
85 }
86 }
87}
88#endif