Add support for sheets
This commit is contained in:
parent
b17f05a6a0
commit
caa8e6bb6e
100
Sources/Core/View/SheetWrapper.swift
Normal file
100
Sources/Core/View/SheetWrapper.swift
Normal file
@ -0,0 +1,100 @@
|
||||
//
|
||||
// SheetWrapper.swift
|
||||
// MacBackend
|
||||
//
|
||||
// Created by david-swift on 31.12.2024.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
/// A sheet on top of a wrapped view.
|
||||
public struct SheetWrapper: SwiftUIWidget {
|
||||
|
||||
/// Whether the sheet is presented.
|
||||
@Meta.Binding var isPresented: Bool
|
||||
/// The wrapped view.
|
||||
var content: Body
|
||||
/// The dialog's content.
|
||||
var dialog: Body
|
||||
/// The cancellation action.
|
||||
var cancellationAction: Body = []
|
||||
/// The confirmation action.
|
||||
var confirmationAction: Body = []
|
||||
/// The toolbar's content.
|
||||
var toolbar: Body = []
|
||||
|
||||
/// The wrapped views.
|
||||
public var wrappedViews: [String: Meta.AnyView] {
|
||||
[
|
||||
.mainContent: content,
|
||||
"dialog": dialog,
|
||||
"cancellationAction": cancellationAction,
|
||||
"confirmationAction": confirmationAction,
|
||||
"toolbar": toolbar
|
||||
]
|
||||
}
|
||||
|
||||
/// Initialize a sheet wrapper.
|
||||
/// - Parameters:
|
||||
/// - isPresented: Whether the sheet is presented.
|
||||
/// - content: The wrapped view.
|
||||
/// - dialog: The dialog's content.
|
||||
public init(
|
||||
isPresented: Meta.Binding<Bool>,
|
||||
@Meta.ViewBuilder content: () -> Body,
|
||||
@Meta.ViewBuilder dialog: () -> Body
|
||||
) {
|
||||
self._isPresented = isPresented
|
||||
self.content = content()
|
||||
self.dialog = dialog()
|
||||
}
|
||||
|
||||
/// Get the SwiftUI view.
|
||||
/// - Parameter properties: The widget data.
|
||||
/// - Returns: The SwiftUI view.
|
||||
public static func view(properties: Self) -> some SwiftUI.View {
|
||||
MacBackendView(.mainContent)
|
||||
.sheet(isPresented: properties._isPresented.swiftUI) {
|
||||
MacBackendView("dialog")
|
||||
.toolbar {
|
||||
if !properties.cancellationAction.isEmpty {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
MacBackendView("cancellationAction")
|
||||
}
|
||||
}
|
||||
if !properties.confirmationAction.isEmpty {
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
MacBackendView("confirmationAction")
|
||||
}
|
||||
}
|
||||
if !properties.toolbar.isEmpty {
|
||||
ToolbarItem {
|
||||
MacBackendView("toolbar")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the cancellation action in the toolbar.
|
||||
/// - Parameter action: The action view.
|
||||
/// - Returns: The sheet wrapper.
|
||||
public func cancellationAction(@Meta.ViewBuilder _ action: () -> Body) -> Self {
|
||||
modify { $0.cancellationAction = action() }
|
||||
}
|
||||
|
||||
/// Set the confirmation action in the toolbar.
|
||||
/// - Parameter action: The action view.
|
||||
/// - Returns: The sheet wrapper.
|
||||
public func confirmationAction(@Meta.ViewBuilder _ action: () -> Body) -> Self {
|
||||
modify { $0.confirmationAction = action() }
|
||||
}
|
||||
|
||||
/// Set the sheet's toolbar.
|
||||
/// - Parameter action: The toolbar view.
|
||||
/// - Returns: The sheet wrapper.
|
||||
public func sheetToolbar(@Meta.ViewBuilder _ toolbar: () -> Body) -> Self {
|
||||
modify { $0.toolbar = toolbar() }
|
||||
}
|
||||
|
||||
}
|
||||
@ -60,4 +60,16 @@ extension AnyView {
|
||||
ToolbarView(child: self, type: .center, toolbarViews: content)
|
||||
}
|
||||
|
||||
/// Present a sheet on top of the window.
|
||||
/// - Parameters:
|
||||
/// - isPresented: Whether the sheet is presented.
|
||||
/// - dialog: The dialog's content.
|
||||
/// - Returns: The wrapped view.
|
||||
public func sheet(
|
||||
isPresented: Meta.Binding<Bool>,
|
||||
@Meta.ViewBuilder dialog: () -> Body
|
||||
) -> SheetWrapper {
|
||||
.init(isPresented: isPresented, content: { self }, dialog: dialog)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user