SPB Git

Repository Public

FeatureLayout.swift 3.3 KB 75 lines swift

1// SP Notes Core2// File: Sources/SPNotesFeatures/Support/FeatureLayout.swift3// Description: The two layouts of every feature screen: compact (a real phone design) and regular (Mac / iPad multi-column).4//              iOS follows the horizontal size class; macOS (which has no size classes) follows the width of the screen's5//              container. An explicit override exists for the host app, the catalogue and the snapshot tests.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import SwiftUI1011public enum FeatureSizeClass: String, Sendable, Hashable, CaseIterable {12    case compact, regular13    public var isCompact: Bool { self == .compact }14}1516private struct FeatureSizeClassOverrideKey: EnvironmentKey { static let defaultValue: FeatureSizeClass? = nil }17private struct FeatureSizeClassKey: EnvironmentKey { static let defaultValue: FeatureSizeClass = .regular }18private struct FeatureNowKey: EnvironmentKey { static let defaultValue: Date? = nil }1920public extension EnvironmentValues {21    /// Forces the layout (nil = automatic).22    var featureSizeClassOverride: FeatureSizeClass? {23        get { self[FeatureSizeClassOverrideKey.self] }24        set { self[FeatureSizeClassOverrideKey.self] = newValue }25    }26    /// The layout the screen must use. Valid inside `FeatureLayoutReader` / `FeatureScaffold`.27    var featureSizeClass: FeatureSizeClass {28        get { self[FeatureSizeClassKey.self] }29        set { self[FeatureSizeClassKey.self] = newValue }30    }31    /// A fixed "now" for relative dates (snapshot tests); nil = the real clock.32    var featureNow: Date? {33        get { self[FeatureNowKey.self] }34        set { self[FeatureNowKey.self] = newValue }35    }36}3738public extension View {39    /// Forces the compact or regular layout of the feature screens below.40    func featureSizeClass(_ sizeClass: FeatureSizeClass?) -> some View { environment(\.featureSizeClassOverride, sizeClass) }41}4243public enum FeatureLayout {44    /// Under this width a macOS container gets the compact layout.45    public static let compactWidth: CGFloat = 5604647    public static func resolve(override: FeatureSizeClass?, horizontal: UserInterfaceSizeClass?, width: CGFloat) -> FeatureSizeClass {48        if let override { return override }49        #if os(macOS)50        return width > 0 && width < compactWidth ? .compact : .regular51        #else52        if let horizontal { return horizontal == .compact ? .compact : .regular }53        return width > 0 && width < compactWidth ? .compact : .regular54        #endif55    }56}5758/// Resolves the layout once at the root of a screen and publishes it in `\.featureSizeClass`.59public struct FeatureLayoutReader<Content: View>: View {60    @Environment(\.featureSizeClassOverride) private var override61    @Environment(\.horizontalSizeClass) private var horizontal62    private let content: (FeatureSizeClass) -> Content6364    public init(@ViewBuilder content: @escaping (FeatureSizeClass) -> Content) { self.content = content }6566    public var body: some View {67        GeometryReader { proxy in68            let sizeClass = FeatureLayout.resolve(override: override, horizontal: horizontal, width: proxy.size.width)69            content(sizeClass)70                .environment(\.featureSizeClass, sizeClass)71                .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)72        }73    }74}75
Files main
Menu

Theme