Files
plak/Sources/PlakUI/Theme/Theme.swift
T

132 lines
6.1 KiB
Swift

import AppKit
/// The colour tokens of the Kade dark slate look, the same as Helder.
public enum Theme {
/// Window and page background.
public static let paper = NSColor(hex: 0x101720)
/// Text, also on the save error banner.
public static let ink = NSColor(hex: 0xE4E7E6)
/// Bookmark labels, the bookmark index, `image missing`.
public static let inkMuted = NSColor(hex: 0x6C7783)
/// The caret, the drop caret and the frame of the selected table cell.
public static let accent = NSColor(hex: 0xE8913A)
/// The tint behind selected text.
public static let selection = NSColor(hex: 0xE8913A, alpha: 0.22)
/// The grid lines of a table.
public static let rule = NSColor(hex: 0x3A424C)
/// The hairline from a bookmark to its label.
public static let bookmarkRule = NSColor(hex: 0x6C7783)
/// The background of the save error banner.
public static let panel = NSColor(hex: 0x18202B)
/// The tint behind a code block.
public static let codePaper = NSColor(hex: 0x18202B)
/// Code keywords, and true, false and null.
public static let codeKeyword = NSColor(hex: 0xC3A6CE)
/// Code strings and heredocs, XML attribute values and CDATA.
public static let codeString = NSColor(hex: 0x8FBFB4)
/// Code comments.
public static let codeComment = NSColor(hex: 0x6C7783)
/// Code numbers.
public static let codeNumber = NSColor(hex: 0xF0B476)
/// PHP `$variables`.
public static let codeVariable = NSColor(hex: 0xBAC0C0)
/// XML tags and the PHP open and close tags.
public static let codeTag = NSColor(hex: 0xC3A6CE)
/// XML attribute names and JSON keys.
public static let codeAttribute = NSColor(hex: 0xBAC0C0)
}
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 = 13
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() }
}
/// The width of the reading column in characters, that ⌘. / ⌘, change.
public struct ColumnWidth: Equatable, Sendable {
public static let standard = 68
public static let range: ClosedRange<Int> = 40...160
public static let step = 8
public private(set) var characters: Int
public init(characters: Int = ColumnWidth.standard) {
self.characters = min(max(characters, ColumnWidth.range.lowerBound), ColumnWidth.range.upperBound)
}
public mutating func wider() { self = ColumnWidth(characters: characters + ColumnWidth.step) }
public mutating func narrower() { self = ColumnWidth(characters: characters - ColumnWidth.step) }
public mutating func reset() { self = ColumnWidth() }
}
/// Sizes of the page layout that follow the body size.
public enum PageMetrics {
public static let lineHeightMultiple: CGFloat = 20.0 / 13.0
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
/// Room between the left edge of the window and the bookmark index.
public static let bookmarkIndexInset: CGFloat = 8
/// The bookmark index shows only the last bookmarks of the page.
public static let bookmarkIndexCount = 10
/// Room between the column, a bookmark rule and a bookmark label.
public static let bookmarkGap: CGFloat = 8
/// The rule right of a bookmark label.
public static let bookmarkTail: CGFloat = 16
/// The shortest rule between the column and a bookmark label.
public static let bookmarkRuleMinimum: CGFloat = 24
/// Below this side space bookmarks are always narrow.
public static let bookmarkSideMinimum: CGFloat = 160
/// A label longer than this many characters never keeps bookmarks narrow; it is cut off with an ellipsis instead.
public static let bookmarkLabelCharacters = 40
/// The size of code and tables against the body size; the same as the page text.
public static let codeScale: CGFloat = 1
/// Room around the code inside a code block.
public static let codePadding: CGFloat = 8
public static let codeCornerRadius: CGFloat = 2
/// 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 side space from which bookmarks are wide: room for the widest label, at most `reasonableLabel`, with its rules.
public static func bookmarkWideSide(widestLabel: CGFloat, reasonableLabel: CGFloat) -> CGFloat {
let label = ceil(min(widestLabel, reasonableLabel))
return max(bookmarkGap + bookmarkRuleMinimum + bookmarkGap + label + bookmarkGap + bookmarkTail, bookmarkSideMinimum)
}
/// The width of about `characters` characters of body text.
public static func columnWidth(bodySize: CGFloat, characters: Int) -> CGFloat {
let sample = String(String(repeating: "the quick brown fox jumps over a lazy dog ", count: 4).prefix(characters))
let width = NSAttributedString(string: sample, attributes: [.font: PlakFonts.mono(bodySize)]).size().width
return ceil(width)
}
/// The height of one text line: 20/13 of the size, Helder's code line, or more when the font needs it.
public static func lineHeight(_ font: NSFont) -> CGFloat {
max(round(font.pointSize * lineHeightMultiple), PlakFonts.naturalLineHeight(font))
}
}