A simple view class for dropping files onto. Supporting Swift, SwiftUI and Objective-C.
I've need something like this many times. So here's a module to make my future life easier maybe.
Add https://github.com/dagronf/DSFDropFilesView
to your project.
Add a new NSView
instance using Interface Builder, then change the class type to DSFDropFilesView
let dropView = DSFDropFilesView(frame: .zero)
See below.
Add https://github.com/dagronf/DSFDropFilesView
to your project.
Note that if you need to use DSFDropFilesView
in Objective-C projects supporting macOS earlier than 10.14, you will also need to make sure to set Always Embed Swift Standard Libraries
to Yes
in the project settings. Before macOS 10.14 the Swift standard libraries are not distributed as part of the macOS installation and you'll need to provide your own in your application.
To receive callbacks from the control, set the dropDelegate
to your view controller.
@objc optional func dropFilesViewWantsSelectFiles(_ sender: DSFDropFilesView)
An optional method that gets called if the user clicks the 'Select files...' button on the control.
func dropFilesView(_ sender: DSFDropFilesView, validateFiles: [URL]) -> NSDragOperation
Called when the drag enters the view. Return the drag operation (or an empty array) to indicate how the drag is to proceed. Use this function to filter out if the 'wrong' types of files are dropped. (for example, if you only want to receive PDFs).
func dropFilesView(_ sender: DSFDropFilesView, didDropFiles files: [URL]) -> Bool
Called with the files when the user actually drops the files.
These properties can all be configured via Interface Builder or programatically.
allowsMultipleDrop
: Allow multiple files/folders to dropped
selectFilesButtonLabel
: Embed a clickable button for accessibility with the specified label. If empty, doesn't display the button (default)selectFilesButtonIsLink
: If the select files button is display, show the button as a hyperlink (blue underlined text) instead of a button.
showIcon
: Should we display an iconicon
: The icon to display. A default icon is supplied
label
: The text of the label. If empty, the label is hidden.lineWidth
: The line width for the dotted bordercornerWidth
: The radius for the corners
There is a basic SwiftUI View that embeds the DSFDropFilesView
control.
DSFDropFilesView.SwiftUI(
isEnabled: true,
allowsMultipleDrop: true,
iconLabel: "Drop files here",
selectFilesLabel: "Select Files…",
selectFilesButtonIsLink: true,
validateFiles: { urls in
// Check the urls, and return the appropriate drop mode
return .copy
},
dropFiles: { urls in
Swift.print("\(urls)")
return true
},
selectFiles: {
Swift.print("User clicked select files")
}
)
- Updated to use DSFAppearanceManager for handling display changes
- Removed warnings for newer versions of Xcode.
- Added separate dynamic/static versions of the library
Added a SwiftUI Wrapper
- Added
selectFilesButtonLabel
to allow the user to specify the 'select files' text. Removes the need for the package to provide its own localizations -- the user can provide them if needed. - Removed the
selectFiles
property, as it can now be inferred from theselectFilesButtonLabel
content. - Added
selectFilesButtonIsLink
to display the 'select files' button in a hyperlink style (blue underlined) or a standard button.
- Renamed
multipleSelect
toallowsMultipleDrop
- Renamed
cornerWidth
tocornerRadius
- Renamed
animated
toisAnimated
- Removed localizations file (no longer needed)
- Initial release
MIT License
Copyright (c) 2020 Darren Ford
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.