Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/PathControl/Internal/PathControlDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public final class PathControlDelegate: NSObject {
}

@objc func pathItemClicked(_ sender: NSPathControl) {
urlChanged(sender.clickedPathItem?.url)
urlChanged(sender.clickedPathItem?.url ?? sender.url)
}

private func createMenu(from definingMenuItems: [PathMenuItem], fileChooserItem: NSMenuItem) -> [NSMenuItem] {
Expand Down
9 changes: 8 additions & 1 deletion Sources/PathControl/PopUpPathControl.swift
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allowedTypes may should be based on UTType in the future and map to allowedTypes via UTType.identifier. UTType requires macOS 11.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import SwiftUI
public struct PopUpPathControl: NSViewRepresentable {

@Binding private var url: URL?
private var allowedTypes: [String]?
private var placeholderString: String?

private let transformMenuItems: ([PathMenuItem]) -> [PathMenuItem]

/// Initializes a `PopUpPathControl` with custom menu contents.
Expand All @@ -34,8 +37,10 @@ public struct PopUpPathControl: NSViewRepresentable {
/// - Parameters:
/// - url: A binding to the currently selected URL.
/// - includeFileChooser: Whether to include a standard file chooser item in the menu. Defaults to `true`.
public init(url: Binding<URL?>, includeFileChooser: Bool = true) {
public init(url: Binding<URL?>, includeFileChooser: Bool = true, allowedTypes: [String]? = nil, placeholderString: String? = nil) {
self._url = url
self.allowedTypes = allowedTypes
self.placeholderString = placeholderString
self.transformMenuItems = { currentPathItems in
if includeFileChooser {
let defaultItems = [
Expand All @@ -53,6 +58,8 @@ public struct PopUpPathControl: NSViewRepresentable {
let pathControl = NSPathControl()
pathControl.pathStyle = .popUp
pathControl.url = url
pathControl.allowedTypes = allowedTypes
pathControl.placeholderString = placeholderString
pathControl.target = context.coordinator
pathControl.action = #selector(context.coordinator.pathItemClicked)
pathControl.delegate = context.coordinator
Expand Down
8 changes: 7 additions & 1 deletion Sources/PathControl/StandardPathControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,24 @@ import SwiftUI
public struct StandardPathControl: NSViewRepresentable {

@Binding private var url: URL?
private var allowedTypes: [String]?
private var placeholderString: String?

/// Initializes a new `StandardPathControl` with a bindable URL.
///
/// - Parameter url: A two-way binding to the currently selected URL.
public init(url: Binding<URL?>) {
public init(url: Binding<URL?>, allowedTypes: [String]? = nil, placeholderString: String?) {
self._url = url
self.allowedTypes = allowedTypes
self.placeholderString = placeholderString
}

public func makeNSView(context: Context) -> NSPathControl {
let pathControl = NSPathControl()
pathControl.pathStyle = .standard
pathControl.url = url
pathControl.allowedTypes = allowedTypes
pathControl.placeholderString = placeholderString
pathControl.target = context.coordinator
pathControl.action = #selector(context.coordinator.pathItemClicked)
pathControl.delegate = context.coordinator
Expand Down