feat: Reimplements reset button style.
This commit is contained in:
@@ -14,16 +14,14 @@ public struct InfoButton: View {
|
||||
Button(action: action) {
|
||||
Label("Info", systemImage: "info.circle")
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.labelStyle(.iconOnly)
|
||||
.font(.title2)
|
||||
.foregroundStyle(Color.accentColor)
|
||||
// .buttonStyle(infoButtonStyle)
|
||||
.buttonStyle(infoButtonStyle)
|
||||
}
|
||||
}
|
||||
|
||||
public struct ResetButton: View {
|
||||
|
||||
@Environment(\.resetButtonStyle) private var resetButtonStyle
|
||||
|
||||
let action: () -> Void
|
||||
|
||||
public init(action: @escaping () -> Void) {
|
||||
@@ -34,13 +32,30 @@ public struct ResetButton: View {
|
||||
Button("Reset", role: .destructive) {
|
||||
action()
|
||||
}
|
||||
.buttonStyle(.borderedProminent)
|
||||
.buttonStyle(resetButtonStyle)
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
struct ButtonPreview: View {
|
||||
|
||||
@State private var lastButtonPressed: String = ""
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Text(lastButtonPressed)
|
||||
|
||||
InfoButton {
|
||||
lastButtonPressed = "Info button pressed."
|
||||
}
|
||||
ResetButton {
|
||||
lastButtonPressed = "Reset button pressed."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
VStack {
|
||||
InfoButton { }
|
||||
ResetButton { }
|
||||
}
|
||||
ButtonPreview()
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -3,7 +3,24 @@ import SwiftUI
|
||||
/// A name space for info button styles.
|
||||
public enum InfoButtonType { }
|
||||
|
||||
public struct AnyButtonStyle<ButtonType>: PrimitiveButtonStyle {
|
||||
/// A name space for info button styles.
|
||||
public enum ResetButtonType { }
|
||||
|
||||
public struct AnyButtonStyle<ButtonType>: ButtonStyle {
|
||||
private let _makeBody: (Configuration) -> AnyView
|
||||
|
||||
public init<S: ButtonStyle>(_ style: S) {
|
||||
self._makeBody = { configuration in
|
||||
AnyView(style.makeBody(configuration: configuration))
|
||||
}
|
||||
}
|
||||
|
||||
public func makeBody(configuration: Configuration) -> some View {
|
||||
self._makeBody(configuration)
|
||||
}
|
||||
}
|
||||
|
||||
public struct AnyPrimitiveButtonStyle<ButtonType>: PrimitiveButtonStyle {
|
||||
private let _makeBody: (Configuration) -> AnyView
|
||||
|
||||
public init<S: PrimitiveButtonStyle>(_ style: S) {
|
||||
@@ -18,26 +35,35 @@ public struct AnyButtonStyle<ButtonType>: PrimitiveButtonStyle {
|
||||
}
|
||||
|
||||
|
||||
public struct DefaultInfoButtonStyle: PrimitiveButtonStyle {
|
||||
|
||||
public struct DefaultInfoButtonStyle<Style: LabelStyle>: ButtonStyle {
|
||||
let color: Color
|
||||
let labelStyle: Style
|
||||
|
||||
init(
|
||||
color: Color = .accentColor,
|
||||
labelStyle: Style
|
||||
) {
|
||||
self.color = color
|
||||
self.labelStyle = labelStyle
|
||||
}
|
||||
|
||||
public func makeBody(configuration: Configuration) -> some View {
|
||||
configuration.label
|
||||
.buttonStyle(.plain)
|
||||
.font(.title2)
|
||||
.foregroundStyle(Color.accentColor)
|
||||
.labelStyle(.iconOnly)
|
||||
.foregroundStyle(color.opacity(configuration.isPressed ? 0.5 : 1))
|
||||
.labelStyle(labelStyle)
|
||||
.scaleEffect(configuration.isPressed ? 0.8 : 1)
|
||||
}
|
||||
}
|
||||
|
||||
extension AnyButtonStyle where ButtonType == InfoButtonType {
|
||||
public static var `default`: Self {
|
||||
.init(DefaultInfoButtonStyle())
|
||||
.init(DefaultInfoButtonStyle<IconOnlyLabelStyle>(labelStyle: .iconOnly))
|
||||
}
|
||||
}
|
||||
|
||||
public struct DefaultResetButtonStyle: PrimitiveButtonStyle {
|
||||
public func makeBody(configuration: Configuration) -> some View {
|
||||
configuration.label
|
||||
.buttonStyle(.borderedProminent)
|
||||
public static func `default`<S: LabelStyle>(color: Color, labelStyle: S) -> Self {
|
||||
.init(DefaultInfoButtonStyle<S>(color: color, labelStyle: labelStyle))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,12 +71,20 @@ private struct InfoButtonStyleKey: EnvironmentKey {
|
||||
static var defaultValue = AnyButtonStyle<InfoButtonType>.default
|
||||
}
|
||||
|
||||
private struct ResetButtonStyleKey: EnvironmentKey {
|
||||
static var defaultValue = AnyPrimitiveButtonStyle<ResetButtonType>(BorderedProminentButtonStyle())
|
||||
}
|
||||
|
||||
extension EnvironmentValues {
|
||||
public var infoButtonStyle: AnyButtonStyle<InfoButtonType> {
|
||||
get { self[InfoButtonStyleKey.self] }
|
||||
set { self[InfoButtonStyleKey.self] = newValue }
|
||||
}
|
||||
|
||||
public var resetButtonStyle: AnyPrimitiveButtonStyle<ResetButtonType> {
|
||||
get { self[ResetButtonStyleKey.self] }
|
||||
set { self[ResetButtonStyleKey.self] = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
extension View {
|
||||
@@ -59,8 +93,15 @@ extension View {
|
||||
environment(\.infoButtonStyle, style)
|
||||
}
|
||||
|
||||
public func infoButtonStyle<S: PrimitiveButtonStyle>(_ style: S) -> some View {
|
||||
public func infoButtonStyle<S: ButtonStyle>(_ style: S) -> some View {
|
||||
infoButtonStyle(AnyButtonStyle(style))
|
||||
}
|
||||
|
||||
public func resetButtonStyle(_ style: AnyPrimitiveButtonStyle<ResetButtonType>) -> some View {
|
||||
environment(\.resetButtonStyle, style)
|
||||
}
|
||||
|
||||
public func resetButtonStyle<S: PrimitiveButtonStyle>(_ style: S) -> some View {
|
||||
resetButtonStyle(AnyPrimitiveButtonStyle(style))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user