1
0

PreferenceValues.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import Foundation
  2. public struct PreferenceValues: Sendable {
  3. /// The default preferences.
  4. public static let `default` = PreferenceValues(
  5. onOpenURL: nil,
  6. presentationDetents: nil,
  7. presentationCornerRadius: nil,
  8. presentationDragIndicatorVisibility: nil,
  9. presentationBackground: nil,
  10. interactiveDismissDisabled: nil,
  11. windowDismissBehavior: nil,
  12. preferredWindowMinimizeBehavior: nil,
  13. windowResizeBehavior: nil,
  14. layoutPriority: defaultLayoutPriority
  15. )
  16. static let defaultLayoutPriority = 0.0
  17. public var onOpenURL: (@Sendable @MainActor (URL) -> Void)?
  18. /// The available detents for a sheet presentation. Applies to enclosing sheets.
  19. public var presentationDetents: [PresentationDetent]?
  20. /// The corner radius for a sheet presentation. Applies to enclosing sheets.
  21. public var presentationCornerRadius: Double?
  22. /// The drag indicator visibility for a sheet presentation. Applies to enclosing sheets.
  23. public var presentationDragIndicatorVisibility: Visibility?
  24. /// The background color for enclosing sheets.
  25. public var presentationBackground: Color?
  26. /// Sets the preferred color scheme for the nearest enclosing presentation.
  27. public var preferredColorScheme: ColorScheme?
  28. /// Controls whether the user can interactively dismiss enclosing sheets.
  29. public var interactiveDismissDisabled: Bool?
  30. /// Controls whether the user can close the enclosing window.
  31. public var windowDismissBehavior: WindowInteractionBehavior?
  32. /// Controls whether the user can minimize the enclosing window.
  33. public var preferredWindowMinimizeBehavior: WindowInteractionBehavior?
  34. /// Controls whether the user can resize the enclosing window.
  35. public var windowResizeBehavior: WindowInteractionBehavior?
  36. /// The layout priority of the view.
  37. var layoutPriority: Double
  38. /// Returns a copy of the preferences with the specified property set to the
  39. /// provided new value.
  40. ///
  41. /// - Parameters:
  42. /// - keyPath: A key path to the property to set.
  43. /// - newValue: The new value of the property.
  44. /// - Returns: A copy of the preferences with the specified property set to
  45. /// `newValue`.
  46. public func with<T>(_ keyPath: WritableKeyPath<Self, T>, _ newValue: T) -> Self {
  47. var preferences = self
  48. preferences[keyPath: keyPath] = newValue
  49. return preferences
  50. }
  51. }
  52. extension PreferenceValues {
  53. init(merging children: [PreferenceValues]) {
  54. let handlers = children.compactMap(\.onOpenURL)
  55. if !handlers.isEmpty {
  56. onOpenURL = { url in
  57. for handler in handlers {
  58. handler(url)
  59. }
  60. }
  61. }
  62. // For presentation modifiers, take the outer-most value (using child ordering to break ties).
  63. presentationDetents = children.compactMap(\.presentationDetents).first
  64. presentationCornerRadius = children.compactMap(\.presentationCornerRadius).first
  65. presentationDragIndicatorVisibility =
  66. children.compactMap(\.presentationDragIndicatorVisibility).first
  67. presentationBackground = children.compactMap(\.presentationBackground).first
  68. preferredColorScheme = children.compactMap(\.preferredColorScheme).first
  69. interactiveDismissDisabled = children.compactMap(\.interactiveDismissDisabled).first
  70. windowDismissBehavior = children.compactMap(\.windowDismissBehavior).first
  71. preferredWindowMinimizeBehavior =
  72. children.compactMap(\.preferredWindowMinimizeBehavior).first
  73. windowResizeBehavior = children.compactMap(\.windowResizeBehavior).first
  74. if let firstChild = children.first, children.count == 1 {
  75. layoutPriority = firstChild.layoutPriority
  76. } else {
  77. layoutPriority = Self.defaultLayoutPriority
  78. }
  79. }
  80. }