Meshtastic-Apple/Meshtastic/Extensions/View.swift
copilot-swe-agent[bot] b1cd2d0579
Merge remote-tracking branch 'origin/2.7.10' into copilot/feature-user-onboarding-flow
# Conflicts:
#	Localizable.xcstrings
#	Meshtastic.xcodeproj/project.pbxproj
#	Meshtastic/Extensions/View.swift
#	Meshtastic/Meshtastic.entitlements

Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com>
2026-04-18 17:37:10 +00:00

97 lines
2.2 KiB
Swift

//
// View.swift
// Meshtastic
//
// Copyright(c) Garth Vander Houwen 8/14/24.
//
import SwiftUI
extension View {
func onFirstAppear(_ action: @escaping () -> Void) -> some View {
modifier(FirstAppear(action: action))
}
@ViewBuilder func olderThanOS26( _ contentBuilder: (@escaping (Self) -> some View) ) -> some View {
if #available(iOS 26.0, macOS 26.0, *) {
self
} else {
contentBuilder(self)
}
}
/// Conditionally applies `defaultScrollAnchor` only on iOS 18+.
@ViewBuilder
func defaultScrollAnchorTopAlignment() -> some View {
if #available(iOS 18, macOS 15, *) {
AnyView(self.defaultScrollAnchor(.top, for: .alignment))
} else {
AnyView(self)
}
}
/// Conditionally applies `defaultScrollAnchor` only on iOS 18+.
@ViewBuilder
func defaultScrollAnchorBottomSizeChanges() -> some View {
if #available(iOS 18, macOS 15, *) {
AnyView(self.defaultScrollAnchor(.bottom, for: .sizeChanges))
} else {
AnyView(self)
}
}
@ViewBuilder func `if`<Content: View>(_ condition: @autoclosure () -> Bool, transform: (Self) -> Content) -> some View {
if condition() {
transform(self)
} else {
self
}
}
/// Standard capsule-shaped prominent button styling.
/// On iOS 26+ the button also receives a glass background effect.
@ViewBuilder
func capsuleButtonStyle() -> some View {
if #available(iOS 26.0, macOS 26.0, *) {
self
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding()
.buttonStyle(.borderedProminent)
.glassEffect(in: .capsule)
} else {
self
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding()
.buttonStyle(.borderedProminent)
}
}
@ViewBuilder
func glassButtonStyle() -> some View {
if #available(iOS 26.0, macOS 26.0, *) {
self.buttonStyle(.glass)
} else {
self
.tint(Color(UIColor.secondarySystemBackground))
.foregroundColor(.accentColor)
.buttonStyle(.borderedProminent)
}
}
}
private struct FirstAppear: ViewModifier {
let action: () -> Void
// Use this to only fire your block one time
@State private var hasAppeared = false
func body(content: Content) -> some View {
// And then, track it here
content.onAppear {
guard !hasAppeared else { return }
hasAppeared = true
action()
}
}
}