Add support for disabling controls
All checks were successful
Deploy Docs / publish (push) Successful in 4m0s
SwiftLint / SwiftLint (push) Successful in 3s

This commit is contained in:
david-swift 2025-01-18 12:09:19 +01:00
parent ebd3797d78
commit 82fb4982ec
2 changed files with 49 additions and 0 deletions

View File

@ -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)
}
}

View File

@ -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
}
}
}