KMCloudPathControl.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // KMCloudPathControl.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/2/14.
  6. //
  7. import Cocoa
  8. typealias KMCloudPathControlItemClick = (Int)->()
  9. class KMCloudPathControl: NSView, NSTableViewDelegate, NSTableViewDataSource {
  10. var scrollView = NSScrollView()
  11. var tableView = NSTableView()
  12. var enabled: Bool = true
  13. private var paths: [String] = []
  14. private var myUrl: String?
  15. var urlString: String? {
  16. get {
  17. return self.myUrl
  18. }
  19. set {
  20. self.myUrl = newValue
  21. self.paths.removeAll()
  22. let tableColumns = self.tableView.tableColumns
  23. for tableColumn in tableColumns {
  24. self.tableView.removeTableColumn(tableColumn)
  25. }
  26. if (self.myUrl == nil) {
  27. return
  28. }
  29. let paths: [String] = (self.myUrl?.components(separatedBy: "\\"))!
  30. for path in paths {
  31. self.addPath(path)
  32. }
  33. self.tableView.reloadData()
  34. }
  35. }
  36. var itemClick: KMCloudPathControlItemClick?
  37. typealias KMCloudPathControlCellItemClick = ()->()
  38. private class KMCellView: NSTableCellView {
  39. var button = NSButton()
  40. var cellItemClick: KMCloudPathControlCellItemClick?
  41. override init(frame frameRect: NSRect) {
  42. super.init(frame: frameRect)
  43. initSubViews()
  44. }
  45. required init?(coder: NSCoder) {
  46. super.init(coder: coder)
  47. initSubViews()
  48. }
  49. public func initSubViews() {
  50. self.addSubview(self.button)
  51. // self.label.lineBreakMode = .byTruncatingTail
  52. self.button.isBordered = false
  53. self.button.target = self
  54. self.button.action = #selector(buttonAction)
  55. }
  56. override func layout() {
  57. super.layout()
  58. self.button.frame = NSMakeRect(5, 10, NSWidth(self.bounds)-10, 20)
  59. }
  60. @objc func buttonAction() {
  61. guard let callback = self.cellItemClick else {
  62. return
  63. }
  64. callback()
  65. }
  66. }
  67. override init(frame frameRect: NSRect) {
  68. super.init(frame: frameRect)
  69. initSubViews()
  70. }
  71. required init?(coder: NSCoder) {
  72. super.init(coder: coder)
  73. initSubViews()
  74. }
  75. public func initSubViews() {
  76. self.addSubview(self.scrollView)
  77. self.scrollView.documentView = self.tableView
  78. self.tableView.rowHeight = 30
  79. self.tableView.delegate = self
  80. self.tableView.dataSource = self
  81. self.tableView.headerView = nil
  82. }
  83. override func layout() {
  84. super.layout()
  85. self.scrollView.frame = self.bounds
  86. self.tableView.frame = self.scrollView.bounds
  87. }
  88. private func addPath(_ path: String) {
  89. let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "column\(self.paths.count)"))
  90. column.title = "column1"
  91. column.width = 120+20
  92. self.tableView.addTableColumn(column)
  93. self.paths.append(path)
  94. }
  95. func numberOfRows(in tableView: NSTableView) -> Int {
  96. return 1
  97. }
  98. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  99. let view = KMCellView()
  100. var i: Int = 0
  101. for path in self.paths {
  102. if (tableColumn?.identifier.rawValue == "column\(i)") {
  103. view.button.title = path
  104. break
  105. }
  106. i += 1
  107. }
  108. view.cellItemClick = { [weak self] in
  109. guard let callback = self?.itemClick else {
  110. return
  111. }
  112. if (self!.enabled == false) {
  113. return
  114. }
  115. callback(i)
  116. }
  117. return view
  118. }
  119. func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
  120. return NSTableRowView()
  121. }
  122. }