AutoTestAdvanceSettingView.swift 7.6 KB

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