AutoTestAdvanceSettingView.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. @IBOutlet var _addBtn : NSButton!
  20. var _keyViews : NSMutableArray!
  21. var _autoTestObj : AutoTest?
  22. var _files : [String]! = []
  23. var _appleInterface : Any?
  24. var delegate : AutoTestAdvanceSettingViewDelegate?
  25. override func awakeFromNib() {
  26. self.wantsLayer = true;
  27. updateBGColor()
  28. _addBtn.isHidden = true;
  29. _appleInterface = DistributedNotificationCenter.default().addObserver(forName: Notification.Name("AppleInterfaceThemeChangedNotification"), object: nil, queue: nil) { notification in
  30. DispatchQueue.main.async {
  31. self.updateBGColor()
  32. }
  33. }
  34. }
  35. func isDarkMode() -> Bool {
  36. let apperace = NSApplication.shared.effectiveAppearance
  37. return apperace.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua
  38. }
  39. func updateBGColor() {
  40. self.wantsLayer = true;
  41. if (isDarkMode()) {
  42. self.layer?.backgroundColor = NSColor(white: 0.196, alpha: 1).cgColor
  43. }else {
  44. self.layer?.backgroundColor = NSColor(white: 0.925, alpha: 1).cgColor
  45. }
  46. // self.layer?.backgroundColor = NSColor(named: "AdvanceBackgrougColor")?.cgColor
  47. }
  48. // Setter
  49. let kCheckBtnDefaultHeight = 25.0
  50. public func getAutoTestObj() -> AutoTest? {
  51. return _autoTestObj
  52. }
  53. public func setAutoTestObj(_ obj:AutoTest?) {
  54. _autoTestObj = obj;
  55. // Set Title
  56. // self.setAccessibilityEnabled((_autoTestObj?.isOriginFileExist() == true && _autoTestObj?.isCheckFileExist() == true))
  57. //
  58. _addBtn.isHidden = nil == _autoTestObj;
  59. if nil != _autoTestObj {
  60. _titleLbl.stringValue = "【"+(_autoTestObj?.fileType() ?? "")+"】"+(_autoTestObj?.name() ?? "")
  61. let checkKeys = NSArray(array: _autoTestObj?.keys() ?? [])
  62. let selectKeys = NSArray(array: _autoTestObj?.selectedKeys() ?? [])
  63. if _keyViews == nil {
  64. _keyViews = NSMutableArray()
  65. }
  66. while (_keyViews.count != checkKeys.count) {
  67. if (_keyViews.count > checkKeys.count) {
  68. (_keyViews.lastObject as! NSButton).removeFromSuperview()
  69. _keyViews.removeLastObject()
  70. }else {
  71. let checkBtn = NSButton(checkboxWithTitle: "", target: self, action: #selector(keyChecked))
  72. _keyContentView.addSubview(checkBtn);
  73. _keyViews.add(checkBtn)
  74. }
  75. }
  76. var height = CGFloat((_keyViews.count + 1)/kItemCountPerRow * (Int(kCheckBtnDefaultHeight) + 5));
  77. // _keyContentView.frame = NSRect(x: 0, y: 0, width: width, height: kTFDefaultHeight)
  78. if ((height+28) > _keyScrollView.frame.height) {
  79. _keyContentView.setFrameSize(NSSize.init(width: _keyScrollView.frame.width - 28, height: height))
  80. }else {
  81. height = _keyScrollView.frame.height - 28
  82. _keyContentView.setFrameSize(NSSize.init(width: _keyScrollView.frame.width - 28, height: height))
  83. }
  84. for btn in NSArray(array: _keyViews) {
  85. let i = _keyViews.index(of: btn)
  86. let checkBtn = btn as! NSButton
  87. checkBtn.title = checkKeys[i] as! String;
  88. // Update Check Status
  89. if selectKeys.contains(checkKeys[i]) {
  90. checkBtn.state = .on
  91. }else {
  92. checkBtn.state = .off
  93. }
  94. checkBtn.sizeToFit()
  95. checkBtn.frame = CGRect.init(x: CGFloat(i % kItemCountPerRow) * _keyContentView.frame.size.width/CGFloat(kItemCountPerRow), y: CGFloat(height - kCheckBtnDefaultHeight), width: checkBtn.frame.width, height: kCheckBtnDefaultHeight)
  96. checkBtn.autoresizingMask = .maxXMargin.union(.minYMargin)
  97. if i % kItemCountPerRow == (kItemCountPerRow - 1) {
  98. height = height - checkBtn.frame.height - 5
  99. }
  100. }
  101. _keyScrollView.documentView = _keyContentView;
  102. _files = DataModel.shared.originFilesFor(String(_autoTestObj?.fileType() ?? ""),
  103. type: String(_autoTestObj?.type() ?? ""))
  104. _fileList.reloadData()
  105. _replaceAllBtn.isEnabled = _autoTestObj!.canUpdateRefImage()
  106. }else {
  107. _titleLbl.stringValue = ""
  108. if _keyViews == nil {
  109. _keyViews = NSMutableArray()
  110. }
  111. while (_keyViews.count != 0) {
  112. (_keyViews.lastObject as! NSView).removeFromSuperview()
  113. _keyViews.removeLastObject()
  114. }
  115. _files = []
  116. _fileList.reloadData()
  117. _replaceAllBtn.isEnabled = false
  118. }
  119. }
  120. /// IBActionn
  121. @IBAction func keyChecked(_ sender:NSButton) {
  122. let currentSelectedKeys = NSMutableArray()
  123. let checkKeys = NSArray(array: _autoTestObj?.keys() ?? [])
  124. for btn in NSArray(array: _keyViews) {
  125. let cBtn = btn as! NSButton
  126. if cBtn.state == .on {
  127. let index = _keyViews.index(of: btn)
  128. currentSelectedKeys.add(checkKeys[index])
  129. }
  130. }
  131. _autoTestObj?.setSelectedKeys(currentSelectedKeys)
  132. if delegate != nil {
  133. delegate?.advanceSettingDidUpdate(self)
  134. }
  135. }
  136. @IBAction func replaceAllAction(_ sender:NSButton) {
  137. if nil != _autoTestObj {
  138. _autoTestObj?.updateRefImage()
  139. }
  140. _fileList.reloadData();
  141. _replaceAllBtn.isEnabled = _autoTestObj!.canUpdateRefImage()
  142. }
  143. @IBAction func addFileAction(_ sender:NSButton) {
  144. // let files = _autoTestObj?.compareFiles() ?? []
  145. //
  146. // if nil != files && files.count > 0 {
  147. // let compareVC = CompareViewController.shared()
  148. // compareVC.setFiles(files)
  149. // compareVC.showIn(sender)
  150. // }
  151. //
  152. // return
  153. if nil == _autoTestObj {
  154. return
  155. }
  156. let originDirectory = _autoTestObj?.originFileDirectory()
  157. let openPanel = NSOpenPanel()
  158. openPanel.canChooseFiles = true
  159. openPanel.allowsMultipleSelection = true
  160. openPanel.allowedContentTypes = [.pdf]
  161. openPanel.canChooseDirectories = true
  162. let comboBox = NSComboBox.init(frame: NSRect.init(x: 0, y: 0, width: 100, height: 25))
  163. comboBox.removeAllItems()
  164. comboBox.addItems(withObjectValues: ["添加超链接", "导入文件"])
  165. comboBox.selectItem(at: 0)
  166. openPanel.accessoryView = comboBox;
  167. if openPanel.runModal() == NSApplication.ModalResponse.OK {
  168. let paths = NSMutableArray()
  169. if openPanel.urls.count != 0 {
  170. for url in openPanel.urls {
  171. var path = url.relativePath
  172. if path.lengthOfBytes(using: .utf8) == 0 {
  173. path = url.absoluteString
  174. }
  175. if path.lengthOfBytes(using: .utf8) != 0 {
  176. paths.add(path)
  177. }
  178. }
  179. }else {
  180. var path = openPanel.url?.relativePath
  181. if nil == path {
  182. path = openPanel.url?.absoluteString
  183. }
  184. if nil != path {
  185. paths.add(path!)
  186. }
  187. }
  188. for item in NSArray(array: paths) {
  189. let path = item as! String
  190. if comboBox.indexOfSelectedItem == 0 {
  191. createSymbolicLink(path, toDirectory: originDirectory!)
  192. }else {
  193. copyFile(path, toDirectory: originDirectory!)
  194. }
  195. }
  196. _files = DataModel.shared.originFilesFor(String(_autoTestObj?.fileType() ?? ""),
  197. type: String(_autoTestObj?.type() ?? ""))
  198. _fileList.reloadData()
  199. }
  200. }
  201. func createSymbolicLink(_ path:String, toDirectory:String) {
  202. var isDirectory = ObjCBool(false)
  203. if FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory) {
  204. if isDirectory.boolValue {
  205. // 文件夹
  206. let subpaths = try! FileManager.default.subpathsOfDirectory(atPath: path)
  207. for subpath in subpaths {
  208. if NSString(string: subpath).contains(".DS_Store") {
  209. continue
  210. }
  211. let nPath = NSString(string: path).appendingPathComponent(subpath)
  212. var isDirectory = ObjCBool(false)
  213. if FileManager.default.fileExists(atPath: nPath, isDirectory: &isDirectory) && isDirectory.boolValue {
  214. continue
  215. }
  216. let resultPath = NSString(string: toDirectory).appendingPathComponent(NSString(string: path).lastPathComponent+"/"+subpath)
  217. createSymbolicLink(nPath, toDirectory: NSString(string: resultPath).deletingLastPathComponent)
  218. }
  219. }else {
  220. let resultPath = NSString(string: toDirectory).appendingPathComponent(NSString(string: path).lastPathComponent)
  221. let directory = NSString(string: resultPath).deletingLastPathComponent
  222. // Create directory for
  223. var isDirectory = ObjCBool(false)
  224. if !FileManager.default.fileExists(atPath: directory, isDirectory: &isDirectory) || !isDirectory.boolValue {
  225. try? FileManager.default.createDirectory(atPath: directory, withIntermediateDirectories: true)
  226. }
  227. if FileManager.default.fileExists(atPath: resultPath) {
  228. try? FileManager.default.removeItem(atPath: resultPath)
  229. }
  230. try? FileManager.default.createSymbolicLink(atPath: resultPath, withDestinationPath: path)
  231. }
  232. }
  233. }
  234. func copyFile(_ path:String, toDirectory:String) {
  235. let resultPath = NSString(string: toDirectory).appendingPathComponent(NSString(string: path).lastPathComponent)
  236. if FileManager.default.fileExists(atPath: path) {
  237. let directory = NSString(string: resultPath).deletingLastPathComponent
  238. // Create directory for
  239. var isDirectory = ObjCBool(false)
  240. if !FileManager.default.fileExists(atPath: directory, isDirectory: &isDirectory) || !isDirectory.boolValue {
  241. try? FileManager.default.createDirectory(atPath: directory, withIntermediateDirectories: true)
  242. }
  243. if FileManager.default.fileExists(atPath: resultPath) {
  244. try? FileManager.default.removeItem(atPath: resultPath)
  245. }
  246. try? FileManager.default.copyItem(atPath: path, toPath: resultPath)
  247. }
  248. }
  249. // TableView Delegate
  250. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  251. let cellView = TestFileCellView.shared()
  252. if (_files.count >= row) {
  253. let title = _files[row]
  254. cellView?.setTitle(title)
  255. cellView?._delegate = self
  256. if _autoTestObj != nil {
  257. cellView?.setNeedReplaceBtn(_autoTestObj!.canUpdateRefImage(title))
  258. }else {
  259. cellView?.setNeedReplaceBtn(false)
  260. }
  261. if (_autoTestObj != nil) {
  262. cellView?.set((_autoTestObj!.status() == .Process &&
  263. _autoTestObj!.convertFiles.contains(title) &&
  264. !_autoTestObj!.compareFinishedFiles.contains(title)))
  265. }else {
  266. cellView?.set(false)
  267. }
  268. if _autoTestObj != nil &&
  269. (_autoTestObj?.status() == .Finished ||
  270. _autoTestObj!.compareFinishedFiles.contains(title)) {
  271. cellView?.setNeedDegreeBtn(true)
  272. cellView?.setDegree((_autoTestObj?.degreeOfFile(title))!)
  273. }else {
  274. cellView?.setNeedDegreeBtn(false)
  275. }
  276. }
  277. return cellView
  278. }
  279. func selectionShouldChange(in tableView: NSTableView) -> Bool {
  280. return true
  281. }
  282. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  283. return false
  284. }
  285. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  286. return false
  287. }
  288. func tableView(_ tableView: NSTableView, mouseDownInHeaderOf tableColumn: NSTableColumn) {
  289. }
  290. func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) {
  291. }
  292. func tableView(_ tableView: NSTableView, didDrag tableColumn: NSTableColumn) {
  293. }
  294. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  295. return 30
  296. }
  297. func tableView(_ tableView: NSTableView, isGroupRow row: Int) -> Bool {
  298. return false
  299. }
  300. func tableView(_ tableView: NSTableView, sizeToFitWidthOfColumn column: Int) -> CGFloat {
  301. return tableView.frame.width
  302. }
  303. func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
  304. return []
  305. }
  306. func tableViewSelectionDidChange(_ notification: Notification) {
  307. }
  308. // TableView Data Source
  309. func numberOfRows(in tableView: NSTableView) -> Int {
  310. return _files.count
  311. }
  312. // TestFileCellViewDelegate
  313. func fileCellNeedReplace(_ cell: TestFileCellView, fileName: String) {
  314. if (_autoTestObj != nil) {
  315. _autoTestObj?.updateRefImage(fileName)
  316. }
  317. _replaceAllBtn.isEnabled = _autoTestObj!.canUpdateRefImage()
  318. _fileList.reloadData()
  319. }
  320. func fileCellNeedShowReport(_ cell: TestFileCellView, fileName: String, sender:NSButton) {
  321. let files = _autoTestObj?.compareFiles(fileName) ?? []
  322. if files.count > 0 {
  323. let compareVC = CompareViewController.shared()
  324. compareVC.setFiles(files)
  325. let point = sender.convert(CGPoint(x: 0, y: 0), to: self)
  326. compareVC.showIn(self, rect: NSRect.init(origin: point, size: sender.frame.size))
  327. }
  328. }
  329. func fileCellNeedShowInFinder(_ cell:TestFileCellView, fileName:String) {
  330. let originDirectory = _autoTestObj?.originFileDirectory()
  331. let path = NSString(string: originDirectory!).appendingPathComponent(fileName);
  332. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
  333. }
  334. func fileCellNeedDelete(_ cell:TestFileCellView, fileName:String) {
  335. let originDirectory = _autoTestObj?.originFileDirectory()
  336. let path = NSString(string: originDirectory!).appendingPathComponent(fileName);
  337. try? FileManager.default.removeItem(atPath: path);
  338. let index = _files.firstIndex(of: fileName)
  339. if (index != NSNotFound) {
  340. _files.remove(at: index!)
  341. _fileList.reloadData()
  342. }
  343. }
  344. }