diff --git a/Sources/Core/View/DisabledWrapper.swift b/Sources/Core/View/DisabledWrapper.swift new file mode 100644 index 0000000..ece842c --- /dev/null +++ b/Sources/Core/View/DisabledWrapper.swift @@ -0,0 +1,40 @@ +// +// DisabledWrapper.swift +// MacBackend +// +// Created by david-swift on 18.01.2025. +// + +import SwiftUI + +/// Wrap a view to disable it. +public struct DisabledWrapper: SwiftUIWidget { + + /// Whether the view is disabled. + var disabled: Bool + /// The content. + var content: Body + + /// The wrapped views. + public var wrappedViews: [String: Meta.AnyView] { + [.mainContent: content] + } + + /// Initialize a disabled wrapper. + /// - Parameters: + /// - disabled: Whether the view is disabled. + /// - content: The view content. + public init(disabled: Bool, @Meta.ViewBuilder content: () -> Body) { + self.content = content() + self.disabled = disabled + } + + /// Get the SwiftUI view. + /// - Parameter properties: The widget data. + /// - Returns: The SwiftUI view. + public static func view(properties: Self) -> some SwiftUI.View { + MacBackendView(.mainContent) + .disabled(properties.disabled) + } + +} diff --git a/Sources/MacBackend/AnyView.swift b/Sources/MacBackend/AnyView.swift index 22984d0..6c54f77 100644 --- a/Sources/MacBackend/AnyView.swift +++ b/Sources/MacBackend/AnyView.swift @@ -110,4 +110,13 @@ extension AnyView { Card { self } } + /// Whether the view is disabled. + /// - Parameter disabled: Whether the view is disabled. + /// - Returns: The view. + public func disabled(_ disabled: Bool = true) -> AnyView { + DisabledWrapper(disabled: disabled) { + self + } + } + }