Ferrite-backup/Ferrite/Views/CommonViews/Modifiers/ConditionalContextMenu.swift
kingbri 4a87d86e76 Ferrite: Add fixes for Introspect v0.2.2
Introspect now properly updates on every view lifecycle change, so
add a check to a reference on the UIHostingController and see if it
has been instantiated already.

Also clean up view struct names to reflect what is a modifier.

Signed-off-by: kingbri <bdashore3@proton.me>
2023-02-18 11:39:03 -05:00

39 lines
1 KiB
Swift

//
// ConditionalContextMenu.swift
// Ferrite
//
// Created by Brian Dashore on 9/3/22.
//
// Used as a workaround for iOS 15 not updating context views with conditional variables
// A stateful ID is required for the contextMenu to update itself.
//
import SwiftUI
struct ConditionalContextMenuModifier<InternalContent: View, ID: Hashable>: ViewModifier {
let internalContent: () -> InternalContent
let id: ID
init(@ViewBuilder _ internalContent: @escaping () -> InternalContent, id: ID) {
self.internalContent = internalContent
self.id = id
}
func body(content: Content) -> some View {
if #available(iOS 16, *) {
content
.contextMenu {
internalContent()
}
} else {
content
.background {
Color.clear
.contextMenu {
internalContent()
}
.id(id)
}
}
}
}