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

76 lines
2.8 KiB
Swift

import SwiftUI
/// A title-bar toggle: Sans 12 label plus keycap. Off has no fill; on gets the amber tint and an amber keycap border;
/// `muted` is for a toggle that is off by meaning (hidden files), where label and keycap step back to text-4.
public struct ToolbarToggle: View {
var label: String
var keys: String
var isOn: Bool
var isMuted: Bool
var action: () -> Void
@State private var isHovered = false
public init(_ label: String, keys: String, isOn: Bool, muted: Bool = false, action: @escaping () -> Void) {
self.label = label
self.keys = keys
self.isOn = isOn
isMuted = muted
self.action = action
}
public var body: some View {
Button(action: action) {
HStack(spacing: 8) {
Text(label).typeRole(isOn ? Typography.toolbarLabelOn : Typography.toolbarLabel)
Keycap(keys, style: isOn ? .active : isMuted ? .muted : .normal)
}
.foregroundStyle(labelColor)
.padding(Metrics.toolbarPadding)
.background(fill, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.onHover { isHovered = $0 }
.animation(Metrics.smallControlTransition, value: isHovered)
.animation(Metrics.smallControlTransition, value: isOn)
}
private var labelColor: Color {
if isOn { return Palette.text1 }
if isMuted { return isHovered ? Palette.text3 : Palette.text4 }
return isHovered ? Palette.text1 : Palette.text3
}
private var fill: Color {
if isOn { return Palette.amberTint }
return isHovered && !isMuted ? Palette.hover : .clear
}
}
/// The icon-only title-bar control (the `?`): a 26pt square with a hairline border.
public struct ToolbarIconButton: View {
var glyph: String
var action: () -> Void
@State private var isHovered = false
public init(_ glyph: String, action: @escaping () -> Void) {
self.glyph = glyph
self.action = action
}
public var body: some View {
Button(action: action) {
Text(glyph)
.typeRole(Typography.keycap)
.foregroundStyle(isHovered ? Palette.text1 : Palette.text4)
.frame(width: Metrics.toolbarIconButton, height: Metrics.toolbarIconButton)
.background(isHovered ? Palette.hover : .clear, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.raised, lineWidth: Metrics.hairline))
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.onHover { isHovered = $0 }
.animation(Metrics.smallControlTransition, value: isHovered)
}
}