Update AppStoreButtonStyle with secondary state

This commit is contained in:
Brandon Evans 2020-12-30 09:02:50 -07:00
parent d122014b74
commit 69c083ca35
No known key found for this signature in database
GPG key ID: D58A4B8DB64F8E93
2 changed files with 97 additions and 35 deletions

View file

@ -1,75 +1,137 @@
import SwiftUI
struct AppStoreButtonStyle: ButtonStyle {
var installed: Bool
var primary: Bool
var highlighted: Bool
private struct AppStoreButton: View {
@SwiftUI.Environment(\.isEnabled) var isEnabled
var configuration: ButtonStyle.Configuration
var installed: Bool
var primary: Bool
var highlighted: Bool
// This seems to magically help the highlight colors update on time
@SwiftUI.Environment(\.isFocused) var isFocused
var textColor: Color {
if installed {
if highlighted {
return Color.white
if isEnabled {
if primary {
if highlighted {
return Color.accentColor
}
else {
return Color.white
}
}
else {
return Color.secondary
}
}
else {
if highlighted {
return Color.accentColor
}
} else {
if primary {
if highlighted {
return Color(.disabledControlTextColor)
}
else {
return Color.white
}
}
else {
return Color.white
if highlighted {
return Color.white
}
else {
return Color(.disabledControlTextColor)
}
}
}
}
func background(isPressed: Bool) -> some View {
Group {
if installed {
EmptyView()
if isEnabled {
if primary {
Capsule()
.fill(
highlighted ?
Color.white :
Color.accentColor
)
.brightness(isPressed ? -0.25 : 0)
} else {
Capsule()
.fill(
Color(NSColor(red: 0.95, green: 0.95, blue: 0.97, alpha: 1))
)
.brightness(isPressed ? -0.25 : 0)
}
} else {
Capsule()
.fill(
highlighted ?
Color.white :
Color.accentColor
)
.brightness(isPressed ? -0.25 : 0)
if primary {
Capsule()
.fill(
highlighted ?
Color.white :
Color(.disabledControlTextColor)
)
.brightness(isPressed ? -0.25 : 0)
} else {
EmptyView()
}
}
}
}
var body: some View {
configuration.label
.font(Font.caption.weight(.medium))
.font(Font.caption.weight(.bold))
.foregroundColor(textColor)
.padding(EdgeInsets(top: 2, leading: 8, bottom: 2, trailing: 8))
.frame(minWidth: 80)
.padding(EdgeInsets(top: 4, leading: 8, bottom: 4, trailing: 8))
.frame(minWidth: 65)
.background(background(isPressed: configuration.isPressed))
.padding(1)
}
}
func makeBody(configuration: ButtonStyle.Configuration) -> some View {
AppStoreButton(configuration: configuration, installed: installed, highlighted: highlighted)
AppStoreButton(configuration: configuration, primary: primary, highlighted: highlighted)
}
}
struct AppStoreButtonStyle_Previews: PreviewProvider {
static var previews: some View {
Group {
Button("INSTALLED", action: {})
.buttonStyle(AppStoreButtonStyle(installed: true, highlighted: false))
.padding()
Button("INSTALL", action: {})
.buttonStyle(AppStoreButtonStyle(installed: false, highlighted: false))
.padding()
ForEach([ColorScheme.light, .dark], id: \.self) { colorScheme in
Group {
Button("OPEN", action: {})
.buttonStyle(AppStoreButtonStyle(primary: true, highlighted: false))
.padding()
.background(Color(.textBackgroundColor))
.previewDisplayName("Primary")
Button("OPEN", action: {})
.buttonStyle(AppStoreButtonStyle(primary: true, highlighted: true))
.padding()
.background(Color(.controlAccentColor))
.previewDisplayName("Primary, Highlighted")
Button("OPEN", action: {})
.buttonStyle(AppStoreButtonStyle(primary: true, highlighted: false))
.padding()
.disabled(true)
.background(Color(.textBackgroundColor))
.previewDisplayName("Primary, Disabled")
Button("INSTALL", action: {})
.buttonStyle(AppStoreButtonStyle(primary: false, highlighted: false))
.padding()
.background(Color(.textBackgroundColor))
.previewDisplayName("Secondary")
Button("INSTALL", action: {})
.buttonStyle(AppStoreButtonStyle(primary: false, highlighted: true))
.padding()
.background(Color(.controlAccentColor))
.previewDisplayName("Secondary, Highlighted")
Button("INSTALL", action: {})
.buttonStyle(AppStoreButtonStyle(primary: false, highlighted: false))
.padding()
.disabled(true)
.background(Color(.textBackgroundColor))
.previewDisplayName("Secondary, Disabled")
}
.environment(\.colorScheme, colorScheme)
}
}
}
}

View file

@ -88,16 +88,16 @@ struct XcodeListView: View {
private func installControl(for xcode: Xcode) -> some View {
if xcode.selected {
Button("DEFAULT") { appState.select(id: xcode.id) }
.buttonStyle(AppStoreButtonStyle(installed: false, highlighted: selectedXcodeID == xcode.id))
.buttonStyle(AppStoreButtonStyle(primary: false, highlighted: selectedXcodeID == xcode.id))
.disabled(true)
.help("This version is selected as the default")
} else if xcode.installed {
Button("SELECT") { appState.select(id: xcode.id) }
.buttonStyle(AppStoreButtonStyle(installed: false, highlighted: selectedXcodeID == xcode.id))
.buttonStyle(AppStoreButtonStyle(primary: false, highlighted: selectedXcodeID == xcode.id))
.help("Select this version as the default")
} else {
Button("INSTALL") { print("Installing...") }
.buttonStyle(AppStoreButtonStyle(installed: true, highlighted: selectedXcodeID == xcode.id))
.buttonStyle(AppStoreButtonStyle(primary: true, highlighted: selectedXcodeID == xcode.id))
.help("Install this version")
}
}