123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- //
- // 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()
- }
- }
|