Skip to content

Commit 28e46c5

Browse files
authored
Supporting dropped files and folders and more parameters (#3)
* Support for dropping folders/files/paths onto the control by reading the NSPathControl's url instead of the clickedPathItem URL * adding more options like allowedTypes and placeholderString to the initialization. allowedTypes can be used to limit the PathControl to folders via UTType.folder.identifier or just image files and so on.
1 parent f37edb2 commit 28e46c5

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

Sources/PathControl/Internal/PathControlDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public final class PathControlDelegate: NSObject {
2323
}
2424

2525
@objc func pathItemClicked(_ sender: NSPathControl) {
26-
urlChanged(sender.clickedPathItem?.url)
26+
urlChanged(sender.clickedPathItem?.url ?? sender.url)
2727
}
2828

2929
private func createMenu(from definingMenuItems: [PathMenuItem], fileChooserItem: NSMenuItem) -> [NSMenuItem] {

Sources/PathControl/PopUpPathControl.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import SwiftUI
1414
public struct PopUpPathControl: NSViewRepresentable {
1515

1616
@Binding private var url: URL?
17+
private var allowedTypes: [String]?
18+
private var placeholderString: String?
19+
1720
private let transformMenuItems: ([PathMenuItem]) -> [PathMenuItem]
1821

1922
/// Initializes a `PopUpPathControl` with custom menu contents.
@@ -34,8 +37,10 @@ public struct PopUpPathControl: NSViewRepresentable {
3437
/// - Parameters:
3538
/// - url: A binding to the currently selected URL.
3639
/// - includeFileChooser: Whether to include a standard file chooser item in the menu. Defaults to `true`.
37-
public init(url: Binding<URL?>, includeFileChooser: Bool = true) {
40+
public init(url: Binding<URL?>, includeFileChooser: Bool = true, allowedTypes: [String]? = nil, placeholderString: String? = nil) {
3841
self._url = url
42+
self.allowedTypes = allowedTypes
43+
self.placeholderString = placeholderString
3944
self.transformMenuItems = { currentPathItems in
4045
if includeFileChooser {
4146
let defaultItems = [
@@ -53,6 +58,8 @@ public struct PopUpPathControl: NSViewRepresentable {
5358
let pathControl = NSPathControl()
5459
pathControl.pathStyle = .popUp
5560
pathControl.url = url
61+
pathControl.allowedTypes = allowedTypes
62+
pathControl.placeholderString = placeholderString
5663
pathControl.target = context.coordinator
5764
pathControl.action = #selector(context.coordinator.pathItemClicked)
5865
pathControl.delegate = context.coordinator

Sources/PathControl/StandardPathControl.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,24 @@ import SwiftUI
1414
public struct StandardPathControl: NSViewRepresentable {
1515

1616
@Binding private var url: URL?
17+
private var allowedTypes: [String]?
18+
private var placeholderString: String?
1719

1820
/// Initializes a new `StandardPathControl` with a bindable URL.
1921
///
2022
/// - Parameter url: A two-way binding to the currently selected URL.
21-
public init(url: Binding<URL?>) {
23+
public init(url: Binding<URL?>, allowedTypes: [String]? = nil, placeholderString: String?) {
2224
self._url = url
25+
self.allowedTypes = allowedTypes
26+
self.placeholderString = placeholderString
2327
}
2428

2529
public func makeNSView(context: Context) -> NSPathControl {
2630
let pathControl = NSPathControl()
2731
pathControl.pathStyle = .standard
2832
pathControl.url = url
33+
pathControl.allowedTypes = allowedTypes
34+
pathControl.placeholderString = placeholderString
2935
pathControl.target = context.coordinator
3036
pathControl.action = #selector(context.coordinator.pathItemClicked)
3137
pathControl.delegate = context.coordinator

0 commit comments

Comments
 (0)