123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- //
- // SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
- // SPDX-License-Identifier: GPL-3.0-or-later
- //
- import Foundation
- import UIKit
- import SwiftUI
- struct NCButtonSwiftUI: View {
- @State var title: String
- @State var action: () -> Void
- @State var style: NCButtonStyle
- @State var height: CGFloat
- @Binding var disabled: Bool
- enum NCButtonStyle: Int {
- case primary = 0
- case secondary
- case tertiary
- case destructive
- }
- var body: some View {
- Button(action: action, label: {
- Text(title)
- .bold()
- .foregroundColor(titleColorForStyle(style: style).opacity(disabled ? 0.5 : 1))
- .padding(.vertical, 4)
- .padding(.horizontal, 40)
- .frame(height: height)
- .background(backgroundColorForStyle(style: style).opacity(disabled ? 0.5 : 1))
- .cornerRadius(height / 2)
- .overlay(
- RoundedRectangle(cornerRadius: 20)
- .stroke(borderColorForStyle(style: style).opacity(disabled ? 0.5 : 1), lineWidth: borderWidthForStyle(style: style))
- )
- })
- .disabled(disabled)
- }
- func backgroundColorForStyle(style: NCButtonStyle) -> Color {
- switch style {
- case .primary:
- return Color(NCAppBranding.themeColor())
- case .secondary:
- return .clear
- case .tertiary:
- return .clear
- case .destructive:
- return .red
- }
- }
- func titleColorForStyle(style: NCButtonStyle) -> Color {
- switch style {
- case .primary:
- return Color(NCAppBranding.themeTextColor())
- case .secondary:
- return Color(NCAppBranding.elementColor())
- case .tertiary:
- return .primary
- case .destructive:
- return .white
- }
- }
- func borderColorForStyle(style: NCButtonStyle) -> Color {
- switch style {
- case .primary:
- return .clear
- case .secondary:
- return Color(NCAppBranding.elementColor())
- case .tertiary:
- return Color(NCAppBranding.placeholderColor())
- case .destructive:
- return .clear
- }
- }
- func borderWidthForStyle(style: NCButtonStyle) -> CGFloat {
- switch style {
- case .primary:
- return 0.0
- case .secondary:
- return 1.0
- case .tertiary:
- return 1.0
- case .destructive:
- return 0.0
- }
- }
- }
- @objc class NCButton: UIButton {
- enum NCButtonStyle: Int {
- case primary = 0
- case secondary
- case tertiary
- case destructive
- }
- var style: NCButtonStyle?
- var buttonAction: Selector?
- override init(frame: CGRect) {
- super.init(frame: frame)
- commonInit()
- }
- required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- commonInit()
- }
- func commonInit() {
- self.layer.cornerRadius = self.frame.height / 2
- self.layer.masksToBounds = true
- self.setButtonStyle(style: .primary)
- }
- override func layoutSubviews() {
- super.layoutSubviews()
- self.layer.cornerRadius = self.frame.height / 2
- }
- func backgroundColorForStyle(style: NCButtonStyle) -> UIColor {
- switch style {
- case .primary:
- return NCAppBranding.themeColor()
- case .secondary:
- return .clear
- case .tertiary:
- return .clear
- case .destructive:
- return .systemRed
- }
- }
- func setButtonStyle(style: NCButtonStyle) {
- self.style = style
- switch style {
- case .primary:
- self.setTitleColor(NCAppBranding.themeTextColor(), for: .normal)
- self.setTitleColor(NCAppBranding.themeTextColor().withAlphaComponent(0.5), for: .disabled)
- self.backgroundColor = backgroundColorForStyle(style: .primary)
- self.layer.borderWidth = 0.0
- case .secondary:
- self.setTitleColor(NCAppBranding.elementColor(), for: .normal)
- self.setTitleColor(NCAppBranding.elementColor().withAlphaComponent(0.5), for: .disabled)
- self.backgroundColor = backgroundColorForStyle(style: .secondary)
- self.layer.borderColor = NCAppBranding.elementColor().cgColor
- self.layer.borderWidth = 1.0
- case .tertiary:
- self.setTitleColor(.label, for: .normal)
- self.setTitleColor(.label.withAlphaComponent(0.5), for: .disabled)
- self.backgroundColor = backgroundColorForStyle(style: .tertiary)
- self.layer.borderColor = NCAppBranding.placeholderColor().cgColor
- self.layer.borderWidth = 1.0
- case .destructive:
- self.setTitleColor(.white, for: .normal)
- self.setTitleColor(.white.withAlphaComponent(0.5), for: .disabled)
- self.backgroundColor = backgroundColorForStyle(style: .destructive)
- self.layer.borderWidth = 0.0
- }
- }
- func setButtonEnabled(enabled: Bool) {
- if let style = self.style, backgroundColorForStyle(style: style) != .clear {
- self.backgroundColor = enabled ? backgroundColorForStyle(style: style) : backgroundColorForStyle(style: style).withAlphaComponent(0.5)
- }
- if let borderColor = self.layer.borderColor {
- self.layer.borderColor = UIColor(cgColor: borderColor).withAlphaComponent(enabled ? 1 : 0.5).cgColor
- }
- self.isEnabled = enabled
- }
- func setButtonAction(target: Any?, selector: Selector) {
- self.removeTarget(target, action: buttonAction, for: .touchUpInside)
- self.addTarget(target, action: selector, for: .touchUpInside)
- buttonAction = selector
- }
- }
|