Skip to content

Instantly share code, notes, and snippets.

@hishnash

hishnash/main.swift

Created Dec 16, 2020
Embed
What would you like to do?
import SwiftUI
@main
struct ExampleCommandsApp: App {
var body: some Scene {
DocumentGroup(
newDocument: ExampleCommandsDocument()
) { file in
ContentView(
document: file.$document
).focusedValue(\.document, file.$document)
}.commands {
CommandMenu("Document") {
TestCommand()
}
}
}
}
struct DocumentFocusedValueKey: FocusedValueKey {
typealias Value = Binding<ExampleCommandsDocument>
}
extension FocusedValues {
var document: DocumentFocusedValueKey.Value? {
get {
return self[DocumentFocusedValueKey.self]
}
set {
self[DocumentFocusedValueKey.self] = newValue
}
}
}
struct TestCommand: View {
@FocusedBinding(\.document)
var document: ExampleCommandsDocument?
var body: some View {
Button("ALL CAPS") {
document?.text = document?.text.uppercased() ?? ""
}.disabled(document?.text.uppercased() == document?.text)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment