macbackend/Sources/MacBackend/AnyView.swift
david-swift faffc6da0f
All checks were successful
Deploy Docs / publish (push) Successful in 3m50s
SwiftLint / SwiftLint (push) Successful in 3s
Add support for frame
2025-01-05 11:07:46 +01:00

108 lines
3.5 KiB
Swift

//
// AnyView.swift
// MacBackend
//
// Created by david-swift on 04.12.2024.
//
@_exported import Core
extension AnyView {
// swiftlint:disable function_default_parameter_at_end
/// Add an alert to a view.
/// - Parameters:
/// - title: The title.
/// - description: The description.
/// - isPresented: Whether the alert is visible.
/// - Returns: The alert.
public func alert(_ title: String, description: String = "", isPresented: Meta.Binding<Bool>) -> Alert {
.init(title: title, description: description, isPresented: isPresented, child: self)
}
// swiftlint:enable function_default_parameter_at_end
/// Set the padding.
/// - Parameters:
/// - padding: The padding.
/// - edges: The edges.
/// - Returns: The view.
public func padding(_ padding: Double, edges: Set<Edge> = .all) -> AnyView {
PaddingView(padding: padding, edges: edges, child: self)
}
/// A menu over the view.
/// - Parameters:
/// - isPresented: Whether the menu is currently visible.
/// - menu: The menu.
/// - Returns: The view.
public func menu(present: Binding<Signal>, @ViewBuilder menu: @escaping () -> Body) -> AnyView {
MenuWrapper(content: { self }, present: present, menu: menu)
}
/// Add toolbar items to the end.
/// - Parameter content: The toolbar's content.
/// - Returns: The view.
public func toolbar(@ViewBuilder content: () -> Body) -> AnyView {
ToolbarView(child: self, type: .end, toolbarViews: content)
}
/// Add toolbar items to the start.
/// - Parameter content: The toolbar's content.
/// - Returns: The view.
public func leadingToolbar(@ViewBuilder content: () -> Body) -> AnyView {
ToolbarView(child: self, type: .start, toolbarViews: content)
}
/// Add centered toolbar items.
/// - Parameter content: The toolbar's content.
/// - Returns: The view.
public func centeredToolbar(@ViewBuilder content: () -> Body) -> 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)
}
/// Define a view's frame.
/// - Parameters:
/// - minWidth: The minimum width.
/// - idealWidth: The ideal width.
/// - maxWidth: The maximum width.
/// - minHeight: The minimum height.
/// - idealHeight: The ideal height.
/// - maxHeight: The maximum height.
/// - alignment: The alignment inside the view's frame.
/// - Returns: The view.
public func frame(
minWidth: Double? = nil,
idealWidth: Double? = nil,
maxWidth: Double? = nil,
minHeight: Double? = nil,
idealHeight: Double? = nil,
maxHeight: Double? = nil,
alignment: Alignment = .center
) -> AnyView {
FrameWrapper(
minWidth: minWidth,
idealWidth: idealWidth,
maxWidth: maxWidth,
minHeight: minHeight,
idealHeight: idealHeight,
maxHeight: maxHeight,
alignment: alignment
) {
self
}
}
}