Add support for toolbars
This commit is contained in:
parent
51d1a9137f
commit
43f53e1681
46
Sources/Core/View/ToolbarView.swift
Normal file
46
Sources/Core/View/ToolbarView.swift
Normal 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)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -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)
|
||||
|
||||
@ -26,7 +26,7 @@ extension AnyView {
|
||||
/// - padding: The padding.
|
||||
/// - edges: The edges.
|
||||
/// - 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)
|
||||
}
|
||||
|
||||
@ -35,8 +35,15 @@ extension AnyView {
|
||||
/// - isPresented: Whether the menu is currently visible.
|
||||
/// - menu: The menu.
|
||||
/// - 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)
|
||||
}
|
||||
|
||||
/// Set the toolbar.
|
||||
/// - Parameter content: The toolbar's content.
|
||||
/// - Returns: The view.
|
||||
public func toolbar(@ViewBuilder content: () -> Body) -> AnyView {
|
||||
ToolbarView(child: self, toolbarViews: content)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user