Files
plak/Sources/PlakUI/Theme/Theme.swift
T
2026-10-17 17:33:13 +02:00

94 lines
3.9 KiB
Swift

import AppKit
/// The colour tokens of the paper look.
public enum Theme {
/// Window and page background.
public static let paper = NSColor(hex: 0xD8D0C0)
/// Text.
public static let ink = NSColor(hex: 0x2B2621)
/// Dates, `image missing`.
public static let inkMuted = NSColor(hex: 0x6E655A)
/// The caret.
public static let accent = NSColor(hex: 0x8A4B2A)
/// Selected text, the save error message, and the lines of a table.
public static let selection = NSColor(hex: 0xB9AE98)
/// The tint behind a code block.
public static let codePaper = NSColor(hex: 0xCBC2B0)
/// Code keywords, and true, false and null.
public static let codeKeyword = NSColor(hex: 0x7A2E14)
/// Code strings and heredocs, XML attribute values and CDATA.
public static let codeString = NSColor(hex: 0x385218)
/// Code comments: muted, yet readable on the code paper.
public static let codeComment = NSColor(hex: 0x564E44)
/// Code numbers.
public static let codeNumber = NSColor(hex: 0x2E4A7A)
/// PHP `$variables`.
public static let codeVariable = NSColor(hex: 0x0E5064)
/// XML tags and the PHP open and close tags.
public static let codeTag = NSColor(hex: 0x7A3B12)
/// XML attribute names and JSON keys.
public static let codeAttribute = NSColor(hex: 0x5E4A12)
}
public extension NSColor {
convenience init(hex: UInt32, alpha: CGFloat = 1) {
self.init(
srgbRed: CGFloat((hex >> 16) & 0xFF) / 255,
green: CGFloat((hex >> 8) & 0xFF) / 255,
blue: CGFloat(hex & 0xFF) / 255,
alpha: alpha
)
}
}
/// The body text size that ⌘+ / ⌘− / ⌘0 change.
public struct TextSize: Equatable, Sendable {
public static let standard: CGFloat = 16
public static let range: ClosedRange<CGFloat> = 10...32
public static let step: CGFloat = 2
public private(set) var body: CGFloat
public init(body: CGFloat = TextSize.standard) {
self.body = min(max(body, TextSize.range.lowerBound), TextSize.range.upperBound)
}
public mutating func bigger() { self = TextSize(body: body + TextSize.step) }
public mutating func smaller() { self = TextSize(body: body - TextSize.step) }
public mutating func reset() { self = TextSize() }
}
/// Sizes of the page layout that follow the body size.
public enum PageMetrics {
public static let columnCharacters = 68
public static let lineHeightMultiple: CGFloat = 1.6
public static let dateScale: CGFloat = 0.75
/// Room under the transparent title bar.
public static let topInset: CGFloat = 52
public static let sideMargin: CGFloat = 28
/// The narrowest left margin that shows created dates.
public static let dateMarginMinimum: CGFloat = 120
/// Room between a margin date and the column.
public static let dateSpacing: CGFloat = 16
/// The size of code and tables against the body size.
public static let codeScale: CGFloat = 0.875
/// Room around the code inside a code block.
public static let codePadding: CGFloat = 8
public static let codeCornerRadius: CGFloat = 4
/// The widest a table column shows; a longer value is cut off with an ellipsis.
public static let tableColumnMaximum: CGFloat = 240
public static let tableCellPadding = NSSize(width: 6, height: 3)
/// The width of about 68 characters of body text.
public static func columnWidth(bodySize: CGFloat) -> CGFloat {
let sample = String(String(repeating: "the quick brown fox jumps over a lazy dog ", count: 2).prefix(columnCharacters))
let width = NSAttributedString(string: sample, attributes: [.font: PlakFonts.body(bodySize)]).size().width
return ceil(width)
}
/// The height of one text line: 1.6 times the size, or more when the font needs it.
public static func lineHeight(_ font: NSFont) -> CGFloat {
max(round(font.pointSize * lineHeightMultiple), PlakFonts.naturalLineHeight(font))
}
}