macbackend/Sources/MacBackend/View/EitherView.swift
david-swift e989bea14e
All checks were successful
Deploy Docs / publish (push) Successful in 2m38s
Initial commit
2024-12-02 22:14:13 +01:00

49 lines
1.2 KiB
Swift

//
// EitherView.swift
// MacBackend
//
// Created by david-swift on 02.12.24.
//
import SwiftUI
/// A widget showing one of two widgets based on a condition.
public struct EitherView: SwiftUIWidget, Meta.EitherView {
/// The condition.
var condition: Bool
/// The first view.
var view1: Body
/// The second view.
var view2: Body
/// The wrapped views.
public var wrappedViews: [String: Meta.AnyView] {
condition ? ["1": view1] : ["2": view2]
}
/// Initialize an either view.
/// - Parameters:
/// - condition: The condition.
/// - view1: The first view.
/// - view2: The second view.
public init(_ condition: Bool, view1: () -> Body, else view2: () -> Body) {
self.condition = condition
self.view1 = view1()
self.view2 = view2()
}
/// Get the SwiftUI view.
/// - Parameter properties: The widget data.
/// - Returns: The SwiftUI view.
@SwiftUI.ViewBuilder
public static func view(properties: Self) -> some SwiftUI.View {
if properties.condition {
MacBackendView("1")
} else {
MacBackendView("2")
}
}
}