macbackend/Sources/Core/View/ToolbarView.swift
david-swift b17f05a6a0
All checks were successful
Deploy Docs / publish (push) Successful in 3m39s
SwiftLint / SwiftLint (push) Successful in 5s
Add support for ForEach
2024-12-31 09:55:02 +01:00

88 lines
2.6 KiB
Swift

//
// ToolbarView.swift
// MacBackend
//
// Created by david-swift on 08.12.2024.
//
import SwiftUI
/// The padding view.
public struct ToolbarView: SwiftUIWidget {
/// The wrapped view.
var child: Meta.AnyView
/// The toolbar views.
var toolbarViews: Body
/// The toolbar type.
var toolbarType: ToolbarType = .end
/// The wrapped views.
public var wrappedViews: [String: Meta.AnyView] {
[.mainContent: child].merging(toolbarViews.enumerated().reduce(into: [:]) { partialResult, view in
partialResult["\(view.offset)"] = view.element
}) { $1 }
}
/// Initialize the padding view.
/// - Parameters:
/// - child: The wrapped view.
/// - type: The position.
/// - toolbarViews: The views in the toolbar.
public init(child: Meta.AnyView, type: ToolbarType, @Meta.ViewBuilder toolbarViews: () -> Body) {
self.child = child
self.toolbarViews = toolbarViews()
self.toolbarType = type
}
/// The type of toolbar.
public enum ToolbarType {
/// A leading toolbar.
case start
/// A trailing toolbar.
case end
/// A centered toolbar.
case center
}
/// Get the SwiftUI view.
/// - Parameter properties: The widget data.
/// - Returns: The SwiftUI view.
public static func view(properties: Self) -> some SwiftUI.View {
switch properties.toolbarType {
case .end:
MacBackendView(.mainContent)
.toolbar {
SwiftUI.ForEach(properties.toolbarViews.indices, id: \.self) { index in
MacBackendView("\(index)")
}
}
case .start:
MacBackendView(.mainContent)
.toolbar {
ToolbarItem(placement: .navigation) {
SwiftUI.HStack {
SwiftUI.ForEach(properties.toolbarViews.indices, id: \.self) { index in
MacBackendView("\(index)")
}
}
}
}
case .center:
MacBackendView(.mainContent)
.toolbar {
ToolbarItem(placement: .principal) {
SwiftUI.HStack {
SwiftUI.ForEach(properties.toolbarViews.indices, id: \.self) { index in
MacBackendView("\(index)")
}
}
}
}
}
}
}