feat: Styles views for macos

This commit is contained in:
2024-06-17 09:30:47 -04:00
parent 50e80d39eb
commit bd758fac90
7 changed files with 115 additions and 16 deletions

View File

@@ -0,0 +1,18 @@
import SharedModels
import SwiftUI
extension View {
public func dynamicBottomPadding(
iOS iOSPadding: CGFloat? = nil,
macOS macOSPadding: CGFloat? = 40
) -> some View {
self
#if os(macOS)
.padding(.bottom, macOSPadding)
#elseif os(iOS)
.padding(.bottom, iOSPadding)
#endif
}
}

View File

@@ -0,0 +1,50 @@
import SwiftUI
public struct SectionHeaderLabel<Content: View>: View {
@Environment(\.sectionHeaderLabelStyle) var style
let content: () -> Content
public init(@ViewBuilder _ content: @escaping () -> Content) {
self.content = content
}
public var body: some View {
style.makeBody(
configuration: TextLabelConfiguration(
label: TextLabelConfiguration.Label(content: content())
)
)
}
}
extension SectionHeaderLabel where Content == Text {
public init(_ text: LocalizedStringKey) {
self.init { Text(text) }
}
public init<S>(_ text: S) where S: StringProtocol {
self.init { Text(text) }
}
}
#Preview {
Form {
Section {
Text("Content")
} header: {
SectionHeaderLabel("One")
}
Section {
Text("Content")
} header: {
SectionHeaderLabel("Two")
}
}
.formStyle(.grouped)
#if os(macOS)
.frame(width: 400, height: 400)
#endif
}

View File

@@ -6,6 +6,7 @@ extension View {
public func applyFormStyle() -> some View {
self
.labelsHidden()
// .formStyle(.grouped)
.textLabelStyle(.boldSecondary)
.textFieldStyle(.roundedBorder)
#if os(macOS)

View File

@@ -67,6 +67,15 @@ public struct AutomaticTextLabelStyle: TextLabelStyle {
}
}
public struct DefaultSectionHeaderLabelStyle: TextLabelStyle {
public func makeBody(configuration: Configuration) -> some View {
configuration.label
#if os(macOS)
.font(.headline)
#endif
}
}
private struct TextLabelStyleKey: EnvironmentKey {
static let defaultValue = MainActor.assumeIsolated {
AnyTextLabelStyle(style: AutomaticTextLabelStyle())
@@ -75,7 +84,7 @@ private struct TextLabelStyleKey: EnvironmentKey {
private struct SectionHeaderLabelStyleKey: EnvironmentKey {
static let defaultValue = MainActor.assumeIsolated {
AnyTextLabelStyle(style: AutomaticTextLabelStyle())
AnyTextLabelStyle(style: DefaultSectionHeaderLabelStyle())
}
}