Add support for toolbars
All checks were successful
Deploy Docs / publish (push) Successful in 2m59s
SwiftLint / SwiftLint (push) Successful in 2s

This commit is contained in:
david-swift 2024-12-08 20:39:50 +01:00
parent 51d1a9137f
commit 43f53e1681
3 changed files with 60 additions and 2 deletions

View File

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

View File

@ -55,6 +55,11 @@ struct Demo: App {
.destructiveButton("Destructive", default: true) { .destructiveButton("Destructive", default: true) {
print("Destructive") print("Destructive")
} }
.toolbar {
Button("World", icon: .system(name: "globe")) {
print("World")
}
}
} }
} }
.frame(width: $width, height: $height) .frame(width: $width, height: $height)

View File

@ -26,7 +26,7 @@ extension AnyView {
/// - padding: The padding. /// - padding: The padding.
/// - edges: The edges. /// - edges: The edges.
/// - Returns: The view. /// - Returns: The view.
public func padding(_ padding: Double, edges: Set<Edge> = .all) -> Meta.AnyView { public func padding(_ padding: Double, edges: Set<Edge> = .all) -> AnyView {
PaddingView(padding: padding, edges: edges, child: self) PaddingView(padding: padding, edges: edges, child: self)
} }
@ -35,8 +35,15 @@ extension AnyView {
/// - isPresented: Whether the menu is currently visible. /// - isPresented: Whether the menu is currently visible.
/// - menu: The menu. /// - menu: The menu.
/// - Returns: The view. /// - Returns: The view.
public func menu(isPresented: Binding<Bool>, @ViewBuilder menu: @escaping () -> Body) -> Meta.AnyView { public func menu(isPresented: Binding<Bool>, @ViewBuilder menu: @escaping () -> Body) -> AnyView {
MenuWrapper(content: { self }, isPresented: isPresented, menu: menu) 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)
}
} }