Skip to content

Commit 3ee10ee

Browse files
authored
Add StandardPathControl, add fileChooser bool argument to explicitly ask or not to have a menu (#2)
* add a StandardPathControl View that use NSPathControl .standard pathStyle * PopUpPathControl initializer: add fileChooser bool argument to explicitely ask or not to have a menu * replace @_functionBuilder by @resultBuilder * StandardPathControl require a non-nil URL * PopUpPathControl: default fileChooser argument is true to keep the previous behavior
1 parent 6c61839 commit 3ee10ee

File tree

4 files changed

+79
-9
lines changed

4 files changed

+79
-9
lines changed

Sources/PathControl/Internal/PathControlDelegate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public final class PathControlDelegate: NSObject {
1212

1313
private let transformMenuItems: ([PathMenuItem]) -> [PathMenuItem]
1414
private let urlChanged: (URL?) -> Void
15-
var actions = [ActionWrapper]()
15+
private var actions = [ActionWrapper]()
1616

1717
init(
1818
transformMenuItems: @escaping ([PathMenuItem]) -> [PathMenuItem],
@@ -79,7 +79,7 @@ extension PathControlDelegate: NSPathControlDelegate {
7979

8080
extension PathMenuItem {
8181

82-
func build(basedOn menuItem: NSMenuItem) -> NSMenuItem {
82+
fileprivate func build(basedOn menuItem: NSMenuItem) -> NSMenuItem {
8383
let menuItemCopy = menuItem.copy() as! NSMenuItem
8484
menuItemCopy.title = title
8585
return menuItemCopy

Sources/PathControl/PathMenuBuilder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import SwiftUI
99

1010
/// A function builder used to build a path control menu.
11-
@_functionBuilder
11+
@resultBuilder
1212
public struct PathMenuBuilder {
1313

1414
public static func buildExpression(_ menuItem: PathMenuItemConvertible) -> [PathMenuItem] {

Sources/PathControl/PopUpPathControl.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ public struct PopUpPathControl: NSViewRepresentable {
2525
/// Creates a pop-up path control with default contents.
2626
///
2727
/// - Parameter url: A binding to property that defines the currently-selected url.
28-
public init(url: Binding<URL?>) {
28+
public init(url: Binding<URL?>, fileChooser: Bool = true) {
2929
self._url = url
3030
self.transformMenuItems = { currentPathItems in
31-
let defaultItems = [
32-
PathMenuItem.fileChooser(),
33-
PathMenuItem(type: .divider, title: "")
34-
]
35-
return defaultItems + currentPathItems
31+
if fileChooser {
32+
let defaultItems = [
33+
PathMenuItem.fileChooser(),
34+
PathMenuItem(type: .divider, title: "")
35+
]
36+
return defaultItems + currentPathItems
37+
} else {
38+
return currentPathItems
39+
}
3640
}
3741
}
3842

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// StandardPathControl.swift
3+
// PathControl
4+
//
5+
// Created by Vincent LIEGEOIS on 25/10/2024.
6+
//
7+
8+
import SwiftUI
9+
10+
/// A control for display of a file system path or virtual path information.
11+
public struct StandardPathControl: NSViewRepresentable {
12+
13+
@Binding private var url: URL?
14+
15+
/// Creates a standard path control.
16+
///
17+
/// - Parameter url: A binding to property that defines the currently-selected url.
18+
public init(url: Binding<URL?>) {
19+
self._url = url
20+
}
21+
22+
public func makeNSView(context: Context) -> NSPathControl {
23+
let pathControl = NSPathControl()
24+
pathControl.pathStyle = .standard
25+
pathControl.url = url
26+
pathControl.target = context.coordinator
27+
pathControl.action = #selector(context.coordinator.pathItemClicked)
28+
pathControl.delegate = context.coordinator
29+
return pathControl
30+
}
31+
32+
public func updateNSView(_ nsView: NSPathControl, context: Context) {
33+
nsView.url = url
34+
}
35+
36+
public func makeCoordinator() -> PathControlDelegate {
37+
return PathControlDelegate(transformMenuItems: {_ in []}) { newUrl in
38+
self.url = newUrl
39+
}
40+
}
41+
}
42+
43+
/// A control for display of a file system path or virtual path information.
44+
public struct StandardStaticPathControl: NSViewRepresentable {
45+
46+
private var url: URL
47+
48+
/// Creates a standard path control.
49+
///
50+
/// - Parameter url: A binding to property that defines the currently-selected url.
51+
public init(url: URL) {
52+
self.url = url
53+
}
54+
55+
public func makeNSView(context: Context) -> NSPathControl {
56+
let pathControl = NSPathControl()
57+
pathControl.pathStyle = .standard
58+
pathControl.url = url
59+
return pathControl
60+
}
61+
62+
public func updateNSView(_ nsView: NSPathControl, context: Context) {
63+
nsView.url = url
64+
}
65+
66+
}

0 commit comments

Comments
 (0)