Skip to content

Instantly share code, notes, and snippets.

@hishnash
Created December 16, 2020 09:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hishnash/f4a91153331068ae17d6d0ea74aa9651 to your computer and use it in GitHub Desktop.
Save hishnash/f4a91153331068ae17d6d0ea74aa9651 to your computer and use it in GitHub Desktop.
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