我已經包裝了 NSPathControl 以在我的 Mac OS SwiftUI 應用程式中使用,但我無法弄清楚如何縮小路徑的大小或如何在單擊時獲取單個路徑以在 Finder 中打開該位置。
這是包裝器:
struct PathView: NSViewRepresentable {
typealias pathViewNSView = NSPathControl
var configuration = { (view: pathViewNSView) in }
func makeNSView(context: NSViewRepresentableContext<PathView>) -> NSPathControl {
pathViewNSView()
}
func updateNSView(_ nsView: NSPathControl, context: NSViewRepresentableContext<PathView>) {
configuration(nsView)
}
}
我使用這樣的視圖:
PathView { view in
view.url = URL(fileURLWithPath: "/Volumes/Users/myfolder", isDirectory: true)
}
我知道使用 NSWorkspace.shared.selectFile 在 Finder 中打開路徑,但不確定如何使用包裝的 NSPathControl 執行此操作。
uj5u.com熱心網友回復:
您可以使用該Coordinator模式NSViewRepresentable:
struct PathView: NSViewRepresentable {
var configuration = { (view: NSPathControl) in }
func makeNSView(context: NSViewRepresentableContext<PathView>) -> NSPathControl {
let pathControl = NSPathControl()
pathControl.target = context.coordinator
pathControl.action = #selector(Coordinator.action)
return pathControl
}
func updateNSView(_ nsView: NSPathControl, context: NSViewRepresentableContext<PathView>) {
configuration(nsView)
}
func makeCoordinator() -> Coordinator {
return Coordinator()
}
class Coordinator : NSObject, NSPathControlDelegate {
@objc func action(sender: NSPathControl) {
if let url = sender.clickedPathItem?.url {
print(url)
NSWorkspace.shared.selectFile(url.path, inFileViewerRootedAtPath: url.path)
}
}
}
}
注意:我實際上并沒有使用任何NSPathControlDelegate方法——這只是為了表明它也可以擴展為委托
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/350208.html
標籤:苹果系统 迅捷 应用套件 nspathcontrol
