67 lines
1.7 KiB
Swift
67 lines
1.7 KiB
Swift
import SwiftUI
|
|
|
|
/// A name space for info button styles.
|
|
public enum InfoButtonType { }
|
|
|
|
public struct AnyButtonStyle<ButtonType>: PrimitiveButtonStyle {
|
|
private let _makeBody: (Configuration) -> AnyView
|
|
|
|
public init<S: PrimitiveButtonStyle>(_ style: S) {
|
|
self._makeBody = { configuration in
|
|
AnyView(style.makeBody(configuration: configuration))
|
|
}
|
|
}
|
|
|
|
public func makeBody(configuration: Configuration) -> some View {
|
|
self._makeBody(configuration)
|
|
}
|
|
}
|
|
|
|
|
|
public struct DefaultInfoButtonStyle: PrimitiveButtonStyle {
|
|
public func makeBody(configuration: Configuration) -> some View {
|
|
configuration.label
|
|
.buttonStyle(.plain)
|
|
.font(.title2)
|
|
.foregroundStyle(Color.accentColor)
|
|
.labelStyle(.iconOnly)
|
|
}
|
|
}
|
|
|
|
extension AnyButtonStyle where ButtonType == InfoButtonType {
|
|
public static var `default`: Self {
|
|
.init(DefaultInfoButtonStyle())
|
|
}
|
|
}
|
|
|
|
public struct DefaultResetButtonStyle: PrimitiveButtonStyle {
|
|
public func makeBody(configuration: Configuration) -> some View {
|
|
configuration.label
|
|
.buttonStyle(.borderedProminent)
|
|
}
|
|
}
|
|
|
|
private struct InfoButtonStyleKey: EnvironmentKey {
|
|
static var defaultValue = AnyButtonStyle<InfoButtonType>.default
|
|
}
|
|
|
|
extension EnvironmentValues {
|
|
public var infoButtonStyle: AnyButtonStyle<InfoButtonType> {
|
|
get { self[InfoButtonStyleKey.self] }
|
|
set { self[InfoButtonStyleKey.self] = newValue }
|
|
}
|
|
|
|
}
|
|
|
|
extension View {
|
|
|
|
public func infoButtonStyle(_ style: AnyButtonStyle<InfoButtonType>) -> some View {
|
|
environment(\.infoButtonStyle, style)
|
|
}
|
|
|
|
public func infoButtonStyle<S: PrimitiveButtonStyle>(_ style: S) -> some View {
|
|
infoButtonStyle(AnyButtonStyle(style))
|
|
}
|
|
|
|
}
|