Use of Nspathcontrol to Represent Virtual Path

Customise NSPathControl

I found the solution and posted it on github.
I subclassed from NSPathControl. It's pretty simple actually.

NSPathControl and Auto Layout

Solution

Sorry, I guess I asked to fast.

I got it working like this:


NSPathControl subclass

- (NSSize)intrinsicContentSize {
return NSMakeSize(NSViewNoInstrinsicMetric, NSViewNoInstrinsicMetric);
}

SwiftUI NSPathControl open finder location when clicking icons

You can use the Coordinator pattern with 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)
}
}
}
}

Note: I'm not actually using any NSPathControlDelegate methods -- that's just to show that it could be extended to be a delegate as well

How to set the URL of a NSPathComponentCell?

  • If you want to display a valid URL, just set the URL property of the NSPathControl instance with an NSURL object
  • If you want to display relative paths or other invalid URLs, look at the Apple sample code NSPathControl Basics


Related Topics



Leave a reply



Submit