documentBrowserContextMenu
Adds to a DocumentLaunchView actions that accept a list of selected files as their parameter.
Declaration
compose().documentBrowserContextMenu(
menu: ReactNode | ((arg0: string[] | null) => ReactNode)
)@MainActor @preconcurrency func documentBrowserContextMenu(
@ContentBuilder _ menu: @escaping ([URL]?) -> some View
) -> some ViewParameters
menu*ReactNode | ((arg0: string[] | null) => ReactNode)Items representing the content of the menu.
Discussion
Add documentBrowserContextMenu modifier to DocumentLaunchView
to provide additional actions to the document browser items' context menu.
For example, a book editor application could have a "Favorite" button
that marks user-chosen books as their favorite. The button enables when the user
switches the browser into "Selection" mode.
import UniformTypeIdentifiers
struct BookEditor: View {
var body: some View {
DocumentLaunchView(for: [.book]) {
NewDocumentButton("Start New Book")
} onDocumentOpen: { url in
BookEditor(url)
}
.documentBrowserContextMenu { selectedURLs in
FavoriteBookButton(urls: selectedURLs)
}
}
}
struct FavoriteBookButton: View {
var urls: [URL]?
var body: some View {
Button {
updateFavorite(urls)
} label: {
Image(systemName: allFavorite(urls) ? "heart.fill" : "heart")
}
}
func allFavorite(_ urls: [URL]?) -> Bool { ... }
func updateFavorite(_ urls: [URL]?) { ... }
}
struct BookEditor: View {
init(_ url: URL) { ... }
}
extension UTType {
static let book = UTType(exportedAs: "com.example.bookEditor")
}In the example above, the application stores a list of URLs to favorite books,
and does not need to access the file on disk. In cases when an application wants to read
the contents of the URL from the disk or associated metadata, it should call
URL.startAccessingSecurityScopedResource() to gain access to the resource,
and URL.stopAccessingSecurityScopedResource() to relinquish access when it is not needed.
The actions are displayed in the document browser navigation bar when a document browser is in Select mode, and also added to context menu for the file items.
See also
More in Presentation.
- alertPresents an alert when a given condition is true, using a localized string resource for the title.
- confirmationDialogPresents a confirmation dialog when a given condition is true, using a localized string resource for the title.
- contextMenuAdds an item-based context menu to a view.
- dialogIconConfigures the icon used by dialogs within this view.
- dialogSuppressionToggleEnables user suppression of dialogs and alerts presented within self, with a custom suppression message on macOS. Unused on other platforms.
- dismissalConfirmationDialogPresents a confirmation dialog upon an attempt to dismiss the window for this view.
- fileDialogBrowserOptionsOn macOS, configures the fileExporter, fileImporter, or fileMover to provide a refined URL search experience: include or exclude hidden files, allow searching by tag, etc.
- fileDialogConfirmationLabelOn macOS, configures the fileExporter, fileImporter, or fileMover with a custom confirmation button label.
- fileDialogCustomizationIDOn macOS, configures the fileExporter, fileImporter, or fileMover to persist and restore the file dialog configuration.
- fileDialogDefaultDirectoryConfigures the fileExporter, fileImporter, or fileMover to open with the specified default directory.
- fileDialogImportsUnresolvedAliasesOn macOS, configures the fileExporter, fileImporter, or fileMover behavior when a user chooses an alias.
- fileDialogMessageOn macOS, configures the fileExporter, fileImporter, or fileMover with a custom message that is presented to the user, similar to a title.