macbackend/Sources/Core/View/FrameWrapper.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

96 lines
2.7 KiB
Swift

//
// FrameWrapper.swift
// MacBackend
//
// Created by david-swift on 05.01.2025.
//
import SwiftUI
/// Wrap a view with a view that defines the wrapped view's frame.
public struct FrameWrapper: SwiftUIWidget {
/// The wrapped view.
var content: Body
/// The minimum width.
var minWidth: Double?
/// The ideal width.
var idealWidth: Double?
/// The maximum width.
var maxWidth: Double?
/// The minimum height.
var minHeight: Double?
/// The ideal height.
var idealHeight: Double?
/// The maximum height.
var maxHeight: Double?
/// The alignment inside the frame.
var alignment: Alignment
/// The wrapped views.
public var wrappedViews: [String: Meta.AnyView] {
[.mainContent: content]
}
/// Initialize a wrapper view defining 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.
/// - content: The wrapped view.
public init(
minWidth: Double? = nil,
idealWidth: Double? = nil,
maxWidth: Double? = nil,
minHeight: Double? = nil,
idealHeight: Double? = nil,
maxHeight: Double? = nil,
alignment: Alignment = .center,
@Meta.ViewBuilder content: () -> Body
) {
self.content = content()
self.minWidth = minWidth
self.idealWidth = idealWidth
self.maxWidth = maxWidth
self.minHeight = minHeight
self.idealHeight = idealHeight
self.maxHeight = maxHeight
self.alignment = alignment
}
/// Get the SwiftUI view.
/// - Parameter properties: The widget data.
/// - Returns: The SwiftUI view.
public static func view(properties: Self) -> some SwiftUI.View {
MacBackendView(.mainContent)
.frame(
minWidth: .init(properties.minWidth),
idealWidth: .init(properties.idealWidth),
maxWidth: .init(properties.maxWidth),
minHeight: .init(properties.minHeight),
idealHeight: .init(properties.idealHeight),
maxHeight: .init(properties.maxHeight),
alignment: properties.alignment.swiftUI
)
}
}
extension CGFloat {
/// Initialize a `CGFloat` from a `Double`.
/// - Parameter value: The double value.
public init?(_ value: Double?) {
if let value {
self = .init(value)
} else {
return nil
}
}
}