Files
StickerSlack/StickerSlack/Extensions/ViewModifiers.swift
T
Nihaal Sharma a1d231054e added emojipackdetailview uikit
bcak to observableobject
ios 15 minimum
context menu to fix multi delete
added duplicate button to the pack menu
consolidated edit mode setting on collecitoview
notifications
emojipack notifications take id instead of pack
2026-07-03 13:47:16 +01:00

191 lines
3.7 KiB
Swift

//
// ViewModifiers.swift
// StickerSlack
//
// Created by neon443 on 03/11/2025.
//
import Foundation
import SwiftUI
struct numericTextCompat: ViewModifier {
func body(content: Content) -> some View {
if #available(iOS 16, *) {
content.contentTransition(.numericText())
} else {
content.transition(.opacity)
}
}
}
struct glassButtonIfAv: ViewModifier {
var enabled: Bool
init(_ enabled: Bool = true) {
self.enabled = enabled
}
func body(content: Content) -> some View {
#if compiler(>=6.2)
if enabled {
if #available(iOS 19, *) {
content.buttonStyle(.glassProminent)
} else {
content.buttonStyle(.borderedProminent)
}
} else {
content
}
#else
if enabled {
content.buttonStyle(.borderedProminent)
} else {
content
}
#endif
}
}
struct NavigationView2<T: View>: View {
@ViewBuilder var contents: T
var body: some View {
if #available(iOS 16, *) {
NavigationStack {
contents
}
} else {
NavigationView {
contents
}
}
}
}
struct presentationHalfAndFullIfAv: ViewModifier {
func body(content: Content) -> some View {
if #available(iOS 16, *) {
content
.presentationDetents([.medium, .large])
.presentationDragIndicator(.visible)
} else {
content
}
}
}
struct BoldSafe: ViewModifier {
var isEnabled: Bool = true
var style: Font.TextStyle = .body
func body(content: Content) -> some View {
if #available(iOS 16, *) {
content.bold(isEnabled)
} else {
if isEnabled {
content.font(.system(style).bold())
} else {
content.font(.system(style))
}
}
}
}
struct MonospacedSafe: ViewModifier {
var isEnabled: Bool = true
var style: Font.TextStyle = .body
func body(content: Content) -> some View {
if #available(iOS 16, *) {
content.monospaced(isEnabled)
} else {
if isEnabled {
content.font(.system(style).monospaced())
} else {
content.font(.system(style))
}
}
}
}
struct ScrollContentBackgroundHidden: ViewModifier {
func body(content: Content) -> some View {
if #available(iOS 16, *) {
content.scrollContentBackground(.hidden)
} else {
content
}
}
}
struct TabViewBottomAcceessorySafe<T: View>: ViewModifier {
var isEnabled: Bool
@ViewBuilder var contents: () -> T
func body(content: Content) -> some View where Content: View {
#if compiler(>=6.2.1)
if #available(iOS 26.1, *) {
content
.tabViewBottomAccessory(isEnabled: isEnabled, content: contents)
} else {
content
}
#else
content
#endif
}
}
extension View {
func tabViewBottomAccessorySafe<Content: View>(
isEnabled: Bool = true,
@ViewBuilder content: @escaping () -> Content
) -> some View {
modifier(TabViewBottomAcceessorySafe(isEnabled: isEnabled, contents: content))
}
}
struct fadeIn: ViewModifier {
@State private var enabled: Bool
@State private var isVisible: Bool = false
init(enabled: Bool = true) {
self.enabled = enabled
}
func body(content: Content) -> some View {
if enabled {
content
.opacity(isVisible ? 1 : 0)
.scaleEffect(isVisible ? 1 : 0)
.onAppear {
withAnimation { isVisible = true }
}
.onDisappear {
withAnimation { isVisible = false }
}
} else {
content
}
}
}
//nonisolated func contextMenu<I, M>(forSelectionType itemType: I.Type = I.self, @ViewBuilder menu: @escaping (Set<I>) -> M, primaryAction: ((Set<I>) -> Void)? = nil) -> some View where I : Hashable, M : View
struct ContextMenuSafe<I, M>: ViewModifier where I : Hashable, M : View {
var itemType: I.Type = I.self
@ViewBuilder var menu: (Set<I>) -> M
var primaryAction: ((Set<I>) -> Void)? = nil
func body(content: Content) -> some View {
if #available(iOS 16, *) {
content
.contextMenu(forSelectionType: itemType) { items in
menu(items)
}
} else {
content
}
}
}