2021-11-01 14:52:00 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 New Vector Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
@available(iOS 14.0, *)
|
|
|
|
struct PrimaryActionButtonStyle: ButtonStyle {
|
2021-12-06 12:52:33 +00:00
|
|
|
@Environment(\.theme) private var theme
|
|
|
|
@Environment(\.isEnabled) private var isEnabled
|
2021-11-01 14:52:00 +00:00
|
|
|
|
2021-12-06 12:52:33 +00:00
|
|
|
var customColor: Color? = nil
|
2021-11-01 14:52:00 +00:00
|
|
|
|
|
|
|
func makeBody(configuration: Self.Configuration) -> some View {
|
|
|
|
configuration.label
|
|
|
|
.padding(12.0)
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
.foregroundColor(.white)
|
|
|
|
.font(theme.fonts.body)
|
2021-12-06 12:52:33 +00:00
|
|
|
.background(backgroundColor(configuration.isPressed))
|
|
|
|
.opacity(isEnabled ? 1.0 : 0.6)
|
2021-11-01 14:52:00 +00:00
|
|
|
.cornerRadius(8.0)
|
|
|
|
}
|
2021-12-06 12:52:33 +00:00
|
|
|
|
|
|
|
func backgroundColor(_ isPressed: Bool) -> Color {
|
|
|
|
if let customColor = customColor {
|
|
|
|
return customColor
|
|
|
|
}
|
2021-12-07 12:09:26 +00:00
|
|
|
|
|
|
|
return isPressed ? theme.colors.accent.opacity(0.6) : theme.colors.accent
|
2021-12-06 12:52:33 +00:00
|
|
|
}
|
2021-11-01 14:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@available(iOS 14.0, *)
|
|
|
|
struct PrimaryActionButtonStyle_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
Group {
|
|
|
|
VStack {
|
|
|
|
Button("Enabled") { }
|
2021-12-06 12:52:33 +00:00
|
|
|
.buttonStyle(PrimaryActionButtonStyle())
|
2021-11-01 14:52:00 +00:00
|
|
|
|
|
|
|
Button("Disabled") { }
|
2021-12-06 12:52:33 +00:00
|
|
|
.buttonStyle(PrimaryActionButtonStyle())
|
2021-11-01 14:52:00 +00:00
|
|
|
.disabled(true)
|
2021-12-06 12:52:33 +00:00
|
|
|
|
|
|
|
Button { } label: {
|
|
|
|
Text("Clear BG")
|
|
|
|
.foregroundColor(.red)
|
|
|
|
}
|
|
|
|
.buttonStyle(PrimaryActionButtonStyle(customColor: .clear))
|
|
|
|
|
|
|
|
Button("Red BG") { }
|
|
|
|
.buttonStyle(PrimaryActionButtonStyle(customColor: .red))
|
2021-11-01 14:52:00 +00:00
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|