2
0

BigButtonStyle.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // Copyright © 2021 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. import SwiftUI
  17. struct BigButtonStyle: ButtonStyle {
  18. let width: CGFloat?
  19. let height: CGFloat?
  20. fileprivate struct BigButtonView: View {
  21. let width: CGFloat?
  22. let height: CGFloat?
  23. let configuration: BigButtonStyle.Configuration
  24. @Environment(\.isEnabled) private var isEnabled: Bool
  25. #if os(macOS)
  26. let defaultColor = Color(NSColor.controlColor)
  27. let pressedColor = Color(NSColor.controlAccentColor)
  28. let foregroundColor = Color(NSColor.controlTextColor)
  29. let foregroundDisabledColor = Color(NSColor.disabledControlTextColor)
  30. let foregroundPressedColor = Color(NSColor.selectedControlTextColor)
  31. #else
  32. let defaultColor = Color(UIColor.tertiarySystemFill)
  33. let pressedColor = Color(UIColor.systemFill)
  34. let foregroundColor = Color(UIColor.label)
  35. let foregroundDisabledColor = Color(UIColor.systemGray)
  36. let foregroundPressedColor = Color(UIColor.secondaryLabel)
  37. #endif
  38. var body: some View {
  39. ZStack {
  40. RoundedRectangle(cornerRadius: 10.0)
  41. .fill(configuration.isPressed ? pressedColor : defaultColor)
  42. #if os(iOS) || os(visionOS)
  43. .hoverEffect()
  44. .scaleEffect(configuration.isPressed ? 0.95 : 1)
  45. #endif
  46. configuration.label
  47. .foregroundColor(isEnabled ? (configuration.isPressed ? foregroundPressedColor : foregroundColor) : foregroundDisabledColor)
  48. }.frame(width: width, height: height)
  49. }
  50. }
  51. func makeBody(configuration: Configuration) -> some View {
  52. BigButtonView(width: width, height: height, configuration: configuration)
  53. }
  54. }