Files
helder/Sources/HelderUI/Components/Keycap.swift
T

79 lines
2.4 KiB
Swift

import SwiftUI
/// A key hint: 12pt mono 600, amber glyph, 1pt border, radius 2. Use it for every shortcut shown in the UI.
public struct Keycap: View {
public enum Style {
/// Amber glyph, raised border.
case normal
/// On an active toolbar toggle: the border turns amber.
case active
/// On a toggle that is off by meaning: glyph and border step back to text-4.
case muted
/// On an amber fill (primary button): ink glyph, ink border.
case onAmber
/// On an amber-deep fill (destructive button): light glyph, light border.
case onDeep
}
var keys: String
var style: Style
var role: TypeRole
public init(_ keys: String, style: Style = .normal, small: Bool = false) {
self.keys = keys
self.style = style
role = small ? Typography.keycapSmall : Typography.keycap
}
public init(_ keys: String, highlighted: Bool) {
self.init(keys, style: highlighted ? .active : .normal)
}
public var body: some View {
Text(keys)
.typeRole(role)
.foregroundStyle(glyph)
.padding(.horizontal, 6)
.padding(.vertical, 4)
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(border, lineWidth: Metrics.hairline))
.fixedSize()
}
private var glyph: Color {
switch style {
case .normal, .active: Palette.amber
case .muted: Palette.text4
case .onAmber: Palette.ink
case .onDeep: Palette.onDeep
}
}
private var border: Color {
switch style {
case .normal, .muted: Palette.raised
case .active: Palette.amberKeycapBorder
case .onAmber: Palette.inkKeycapBorder
case .onDeep: Palette.onDeep.opacity(0.4)
}
}
}
/// The `esc` (or `↵`) chip at the right of an overlay header or footer: mono 11, text-4, hairline border.
public struct EscChip: View {
var label: String
public init(_ label: String = "esc") {
self.label = label
}
public var body: some View {
Text(label)
.typeRole(Typography.chip)
.foregroundStyle(Palette.text4)
.padding(.horizontal, 6)
.padding(.vertical, 4)
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.raised, lineWidth: Metrics.hairline))
.fixedSize()
}
}