diff --git a/Sources/Core/View/ToolbarView.swift b/Sources/Core/View/ToolbarView.swift new file mode 100644 index 0000000..cc7d5fa --- /dev/null +++ b/Sources/Core/View/ToolbarView.swift @@ -0,0 +1,46 @@ +// +// 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 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. + /// - toolbarViews: The views in the toolbar. + public init(child: Meta.AnyView, @Meta.ViewBuilder toolbarViews: () -> Body) { + self.child = child + self.toolbarViews = toolbarViews() + } + + /// Get the SwiftUI view. + /// - Parameter properties: The widget data. + /// - Returns: The SwiftUI view. + public static func view(properties: Self) -> some SwiftUI.View { + MacBackendView(.mainContent) + .toolbar { + ForEach(properties.toolbarViews.indices, id: \.self) { index in + MacBackendView("\(index)") + } + } + } + +} diff --git a/Sources/Demo/Demo.swift b/Sources/Demo/Demo.swift index 64df973..7b0c07f 100644 --- a/Sources/Demo/Demo.swift +++ b/Sources/Demo/Demo.swift @@ -55,6 +55,11 @@ struct Demo: App { .destructiveButton("Destructive", default: true) { print("Destructive") } + .toolbar { + Button("World", icon: .system(name: "globe")) { + print("World") + } + } } } .frame(width: $width, height: $height) diff --git a/Sources/MacBackend/AnyView.swift b/Sources/MacBackend/AnyView.swift index 4e9fe3f..addf3a3 100644 --- a/Sources/MacBackend/AnyView.swift +++ b/Sources/MacBackend/AnyView.swift @@ -26,7 +26,7 @@ extension AnyView { /// - padding: The padding. /// - edges: The edges. /// - Returns: The view. - public func padding(_ padding: Double, edges: Set = .all) -> Meta.AnyView { + public func padding(_ padding: Double, edges: Set = .all) -> AnyView { PaddingView(padding: padding, edges: edges, child: self) } @@ -35,8 +35,15 @@ extension AnyView { /// - isPresented: Whether the menu is currently visible. /// - menu: The menu. /// - Returns: The view. - public func menu(isPresented: Binding, @ViewBuilder menu: @escaping () -> Body) -> Meta.AnyView { + public func menu(isPresented: Binding, @ViewBuilder menu: @escaping () -> Body) -> AnyView { MenuWrapper(content: { self }, isPresented: isPresented, menu: menu) } + /// Set the toolbar. + /// - Parameter content: The toolbar's content. + /// - Returns: The view. + public func toolbar(@ViewBuilder content: () -> Body) -> AnyView { + ToolbarView(child: self, toolbarViews: content) + } + }