Created
December 16, 2020 09:13
-
-
Save hishnash/f4a91153331068ae17d6d0ea74aa9651 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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