AutoTestAdvanceSettingView.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // AutoTestAdvanceSettingView.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/25.
  6. //
  7. import Foundation
  8. import AppKit
  9. let kItemCountPerRow = 3
  10. public protocol AutoTestAdvanceSettingViewDelegate : NSObjectProtocol {
  11. func advanceSettingDidUpdate(_ settingView:NSView?)
  12. }
  13. class AutoTestAdvanceSettingView : NSView, NSTableViewDataSource, NSTableViewDelegate {
  14. @IBOutlet var _titleLbl : NSTextField!
  15. @IBOutlet var _keyScrollView : NSScrollView!
  16. @IBOutlet var _keyContentView : NSView!
  17. @IBOutlet var _fileList : NSTableView!
  18. var _keyViews : NSMutableArray!
  19. var _autoTestObj : AutoTest?
  20. var _files : [String]! = []
  21. var delegate : AutoTestAdvanceSettingViewDelegate?
  22. override func awakeFromNib() {
  23. self.wantsLayer = true;
  24. self.layer?.backgroundColor = NSColor.init(red: 220/255.0, green: 221/255.0, blue: 220/255.0, alpha: 1).cgColor
  25. }
  26. // Setter
  27. let kCheckBtnDefaultHeight = 25.0
  28. public func setAutoTestObj(_ obj:AutoTest?) {
  29. _autoTestObj = obj;
  30. // Set Title
  31. // self.setAccessibilityEnabled((_autoTestObj?.isOriginFileExist() == true && _autoTestObj?.isCheckFileExist() == true))
  32. //
  33. if nil != _autoTestObj {
  34. _titleLbl.stringValue = String("[\(_autoTestObj?.fileType() as! String)]\(_autoTestObj?.name() as! String)")
  35. let checkKeys = _autoTestObj?.keys() as! NSArray
  36. let selectKeys = _autoTestObj?.selectedKeys() as! NSArray
  37. if _keyViews == nil {
  38. _keyViews = NSMutableArray()
  39. }
  40. while (_keyViews.count != checkKeys.count) {
  41. if (_keyViews.count > checkKeys.count) {
  42. (_keyViews.lastObject as! NSButton).removeFromSuperview()
  43. _keyViews.removeLastObject()
  44. }else {
  45. let checkBtn = NSButton(checkboxWithTitle: "", target: self, action: #selector(keyChecked))
  46. _keyContentView.addSubview(checkBtn);
  47. _keyViews.add(checkBtn)
  48. }
  49. }
  50. var height = CGFloat((_keyViews.count + 1)/kItemCountPerRow * (Int(kCheckBtnDefaultHeight) + 5));
  51. // _keyContentView.frame = NSRect(x: 0, y: 0, width: width, height: kTFDefaultHeight)
  52. if (height > _keyScrollView.frame.height) {
  53. _keyContentView.setFrameSize(NSSize.init(width: _keyScrollView.frame.width - 20, height: height))
  54. }else {
  55. height = _keyScrollView.frame.height
  56. _keyContentView.setFrameSize(NSSize.init(width: _keyScrollView.frame.width - 20, height: _keyScrollView.frame.height))
  57. }
  58. for btn in _keyViews {
  59. let i = _keyViews.index(of: btn)
  60. let checkBtn = btn as! NSButton
  61. checkBtn.title = checkKeys[i] as! String;
  62. // Update Check Status
  63. if selectKeys.contains(checkKeys[i]) {
  64. checkBtn.state = .on
  65. }else {
  66. checkBtn.state = .off
  67. }
  68. checkBtn.sizeToFit()
  69. checkBtn.frame = CGRect.init(x: CGFloat(i % kItemCountPerRow) * _keyContentView.frame.size.width/CGFloat(kItemCountPerRow), y: CGFloat(height - kCheckBtnDefaultHeight), width: checkBtn.frame.width, height: kCheckBtnDefaultHeight)
  70. checkBtn.autoresizingMask = .maxXMargin.union(.minYMargin)
  71. if i % kItemCountPerRow == (kItemCountPerRow - 1) {
  72. height = height - checkBtn.frame.height - 5
  73. }
  74. }
  75. _keyScrollView.documentView = _keyContentView;
  76. _files = DataModel.shared.originFilesFor(String(_autoTestObj?.fileType() ?? ""),
  77. type: String(_autoTestObj?.type() ?? ""))
  78. _fileList.reloadData()
  79. }else {
  80. _titleLbl.stringValue = ""
  81. if _keyViews == nil {
  82. _keyViews = NSMutableArray()
  83. }
  84. while (_keyViews.count != 0) {
  85. (_keyViews.lastObject as! NSView).removeFromSuperview()
  86. _keyViews.removeLastObject()
  87. }
  88. _files = []
  89. _fileList.reloadData()
  90. }
  91. }
  92. /// IBActionn
  93. @IBAction func keyChecked(_ sender:NSButton) {
  94. var currentSelectedKeys = NSMutableArray()
  95. let checkKeys = _autoTestObj?.keys() as! NSArray
  96. for btn in _keyViews {
  97. let cBtn = btn as! NSButton
  98. if cBtn.state == .on {
  99. let index = _keyViews.index(of: btn)
  100. currentSelectedKeys.add(checkKeys[index])
  101. }
  102. }
  103. _autoTestObj?.setSelectedKeys(currentSelectedKeys)
  104. if delegate != nil {
  105. delegate?.advanceSettingDidUpdate(self)
  106. }
  107. }
  108. // TableView Delegate
  109. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  110. let cellView = TestFileCellView.shared()
  111. if (_files.count >= row) {
  112. let title = _files[row]
  113. cellView?.setTitle(title)
  114. }
  115. return cellView
  116. }
  117. func selectionShouldChange(in tableView: NSTableView) -> Bool {
  118. return true
  119. }
  120. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  121. return false
  122. }
  123. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  124. return false
  125. }
  126. func tableView(_ tableView: NSTableView, mouseDownInHeaderOf tableColumn: NSTableColumn) {
  127. }
  128. func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) {
  129. }
  130. func tableView(_ tableView: NSTableView, didDrag tableColumn: NSTableColumn) {
  131. }
  132. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  133. return 30
  134. }
  135. func tableView(_ tableView: NSTableView, isGroupRow row: Int) -> Bool {
  136. return false
  137. }
  138. func tableView(_ tableView: NSTableView, sizeToFitWidthOfColumn column: Int) -> CGFloat {
  139. return tableView.frame.width
  140. }
  141. func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
  142. return []
  143. }
  144. func tableViewSelectionDidChange(_ notification: Notification) {
  145. }
  146. // TableView Data Source
  147. func numberOfRows(in tableView: NSTableView) -> Int {
  148. return _files.count
  149. }
  150. }