Add support for disabling controls
This commit is contained in:
parent
ebd3797d78
commit
82fb4982ec
40
Sources/Core/View/DisabledWrapper.swift
Normal file
40
Sources/Core/View/DisabledWrapper.swift
Normal 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -110,4 +110,13 @@ extension AnyView {
|
|||||||
Card { self }
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user