// // KMCloudPathControl.swift // PDF Master // // Created by tangchao on 2023/2/14. // import Cocoa typealias KMCloudPathControlItemClick = (Int)->() class KMCloudPathControl: NSView, NSTableViewDelegate, NSTableViewDataSource { var scrollView = NSScrollView() var tableView = NSTableView() var enabled: Bool = true private var paths: [String] = [] private var myUrl: String? var urlString: String? { get { return self.myUrl } set { self.myUrl = newValue self.paths.removeAll() let tableColumns = self.tableView.tableColumns for tableColumn in tableColumns { self.tableView.removeTableColumn(tableColumn) } if (self.myUrl == nil) { return } let paths: [String] = (self.myUrl?.components(separatedBy: "\\"))! for path in paths { self.addPath(path) } self.tableView.reloadData() } } var itemClick: KMCloudPathControlItemClick? typealias KMCloudPathControlCellItemClick = ()->() private class KMCellView: NSTableCellView { var button = NSButton() var cellItemClick: KMCloudPathControlCellItemClick? override init(frame frameRect: NSRect) { super.init(frame: frameRect) initSubViews() } required init?(coder: NSCoder) { super.init(coder: coder) initSubViews() } public func initSubViews() { self.addSubview(self.button) // self.label.lineBreakMode = .byTruncatingTail self.button.isBordered = false self.button.target = self self.button.action = #selector(buttonAction) } override func layout() { super.layout() self.button.frame = NSMakeRect(5, 10, NSWidth(self.bounds)-10, 20) } @objc func buttonAction() { guard let callback = self.cellItemClick else { return } callback() } } override init(frame frameRect: NSRect) { super.init(frame: frameRect) initSubViews() } required init?(coder: NSCoder) { super.init(coder: coder) initSubViews() } public func initSubViews() { self.addSubview(self.scrollView) self.scrollView.documentView = self.tableView self.tableView.rowHeight = 30 self.tableView.delegate = self self.tableView.dataSource = self self.tableView.headerView = nil } override func layout() { super.layout() self.scrollView.frame = self.bounds self.tableView.frame = self.scrollView.bounds } private func addPath(_ path: String) { let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "column\(self.paths.count)")) column.title = "column1" column.width = 120+20 self.tableView.addTableColumn(column) self.paths.append(path) } func numberOfRows(in tableView: NSTableView) -> Int { return 1 } func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { let view = KMCellView() var i: Int = 0 for path in self.paths { if (tableColumn?.identifier.rawValue == "column\(i)") { view.button.title = path break } i += 1 } view.cellItemClick = { [weak self] in guard let callback = self?.itemClick else { return } if (self!.enabled == false) { return } callback(i) } return view } func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? { return NSTableRowView() } }