AutoTestAdvanceSettingView.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. if #available(macOS 11.0, *) {
  161. openPanel.allowedContentTypes = [.pdf]
  162. } else {
  163. openPanel.allowedFileTypes = ["pdf", "PDF"]
  164. }
  165. openPanel.canChooseDirectories = true
  166. let comboBox = NSComboBox.init(frame: NSRect.init(x: 0, y: 0, width: 100, height: 25))
  167. comboBox.removeAllItems()
  168. comboBox.addItems(withObjectValues: ["添加超链接", "导入文件"])
  169. comboBox.selectItem(at: 0)
  170. openPanel.accessoryView = comboBox;
  171. if openPanel.runModal() == NSApplication.ModalResponse.OK {
  172. let paths = NSMutableArray()
  173. if openPanel.urls.count != 0 {
  174. for url in openPanel.urls {
  175. var path = url.relativePath
  176. if path.lengthOfBytes(using: .utf8) == 0 {
  177. path = url.absoluteString
  178. }
  179. if path.lengthOfBytes(using: .utf8) != 0 {
  180. paths.add(path)
  181. }
  182. }
  183. }else {
  184. var path = openPanel.url?.relativePath
  185. if nil == path {
  186. path = openPanel.url?.absoluteString
  187. }
  188. if nil != path {
  189. paths.add(path!)
  190. }
  191. }
  192. for item in NSArray(array: paths) {
  193. let path = item as! String
  194. if comboBox.indexOfSelectedItem == 0 {
  195. createSymbolicLink(path, toDirectory: originDirectory!)
  196. }else {
  197. copyFile(path, toDirectory: originDirectory!)
  198. }
  199. }
  200. _files = DataModel.shared.originFilesFor(String(_autoTestObj?.fileType() ?? ""),
  201. type: String(_autoTestObj?.type() ?? ""))
  202. _fileList.reloadData()
  203. }
  204. }
  205. func createSymbolicLink(_ path:String, toDirectory:String) {
  206. var isDirectory = ObjCBool(false)
  207. if FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory) {
  208. if isDirectory.boolValue {
  209. // 文件夹
  210. let subpaths = try! FileManager.default.subpathsOfDirectory(atPath: path)
  211. for subpath in subpaths {
  212. if (NSString(string: subpath).contains(".DS_Store") ||
  213. !NSArray(array: ["PDF", "pdf"]).contains(NSString(string: subpath).pathExtension)) {
  214. continue
  215. }
  216. let nPath = NSString(string: path).appendingPathComponent(subpath)
  217. var isDirectory = ObjCBool(false)
  218. if FileManager.default.fileExists(atPath: nPath, isDirectory: &isDirectory) && isDirectory.boolValue {
  219. continue
  220. }
  221. let resultPath = NSString(string: toDirectory).appendingPathComponent(NSString(string: path).lastPathComponent+"/"+subpath)
  222. createSymbolicLink(nPath, toDirectory: NSString(string: resultPath).deletingLastPathComponent)
  223. }
  224. }else {
  225. if !NSArray(array: ["PDF", "pdf"]).contains(NSString(string: path).pathExtension) {
  226. return
  227. }
  228. let resultPath = NSString(string: toDirectory).appendingPathComponent(NSString(string: path).lastPathComponent)
  229. let directory = NSString(string: resultPath).deletingLastPathComponent
  230. // Create directory for
  231. var isDirectory = ObjCBool(false)
  232. if !FileManager.default.fileExists(atPath: directory, isDirectory: &isDirectory) || !isDirectory.boolValue {
  233. try? FileManager.default.createDirectory(atPath: directory, withIntermediateDirectories: true)
  234. }
  235. if FileManager.default.fileExists(atPath: resultPath) {
  236. try? FileManager.default.removeItem(atPath: resultPath)
  237. }
  238. try? FileManager.default.createSymbolicLink(atPath: resultPath, withDestinationPath: path)
  239. }
  240. }
  241. }
  242. func copyFile(_ path:String, toDirectory:String) {
  243. let resultPath = NSString(string: toDirectory).appendingPathComponent(NSString(string: path).lastPathComponent)
  244. if FileManager.default.fileExists(atPath: path) {
  245. let directory = NSString(string: resultPath).deletingLastPathComponent
  246. // Create directory for
  247. var isDirectory = ObjCBool(false)
  248. if !FileManager.default.fileExists(atPath: directory, isDirectory: &isDirectory) || !isDirectory.boolValue {
  249. try? FileManager.default.createDirectory(atPath: directory, withIntermediateDirectories: true)
  250. }
  251. if FileManager.default.fileExists(atPath: resultPath) {
  252. try? FileManager.default.removeItem(atPath: resultPath)
  253. }
  254. try? FileManager.default.copyItem(atPath: path, toPath: resultPath)
  255. }
  256. }
  257. // TableView Delegate
  258. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  259. let cellView = TestFileCellView.shared()
  260. if (_files.count >= row) {
  261. let title = _files[row]
  262. cellView?.setTitle(title)
  263. cellView?._delegate = self
  264. if _autoTestObj != nil {
  265. cellView?.setNeedReplaceBtn(_autoTestObj!.canUpdateRefImage(title))
  266. }else {
  267. cellView?.setNeedReplaceBtn(false)
  268. }
  269. if (_autoTestObj != nil) {
  270. cellView?.set((_autoTestObj!.status() == .Process &&
  271. _autoTestObj!.convertFiles.contains(title) &&
  272. !_autoTestObj!.compareFinishedFiles.contains(title)))
  273. }else {
  274. cellView?.set(false)
  275. }
  276. if _autoTestObj != nil &&
  277. (_autoTestObj?.status() == .Finished ||
  278. _autoTestObj!.compareFinishedFiles.contains(title)) {
  279. cellView?.setNeedDegreeBtn(true)
  280. cellView?.setDegree((_autoTestObj?.degreeOfFile(title))!)
  281. }else {
  282. cellView?.setNeedDegreeBtn(false)
  283. }
  284. }
  285. return cellView
  286. }
  287. func selectionShouldChange(in tableView: NSTableView) -> Bool {
  288. return true
  289. }
  290. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  291. return false
  292. }
  293. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  294. return false
  295. }
  296. func tableView(_ tableView: NSTableView, mouseDownInHeaderOf tableColumn: NSTableColumn) {
  297. }
  298. func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) {
  299. }
  300. func tableView(_ tableView: NSTableView, didDrag tableColumn: NSTableColumn) {
  301. }
  302. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  303. return 30
  304. }
  305. func tableView(_ tableView: NSTableView, isGroupRow row: Int) -> Bool {
  306. return false
  307. }
  308. func tableView(_ tableView: NSTableView, sizeToFitWidthOfColumn column: Int) -> CGFloat {
  309. return tableView.frame.width
  310. }
  311. func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
  312. return []
  313. }
  314. func tableViewSelectionDidChange(_ notification: Notification) {
  315. }
  316. // TableView Data Source
  317. func numberOfRows(in tableView: NSTableView) -> Int {
  318. return _files.count
  319. }
  320. // TestFileCellViewDelegate
  321. func fileCellNeedReplace(_ cell: TestFileCellView, fileName: String) {
  322. if (_autoTestObj != nil) {
  323. _autoTestObj?.updateRefImage(fileName)
  324. }
  325. _replaceAllBtn.isEnabled = _autoTestObj!.canUpdateRefImage()
  326. _fileList.reloadData()
  327. }
  328. func fileCellNeedShowReport(_ cell: TestFileCellView, fileName: String, sender:NSButton) {
  329. let files = _autoTestObj?.compareFiles(fileName) ?? []
  330. if files.count > 0 {
  331. if (_autoTestObj as? StringAutoTest) != nil {
  332. var compareVC = StringCompareViewController.shared()
  333. compareVC.setFiles(files)
  334. let point = sender.convert(CGPoint(x: 0, y: 0), to: self)
  335. compareVC.showIn(self, rect: NSRect.init(origin: point, size: sender.frame.size))
  336. }else {
  337. let compareVC = CompareViewController.shared()
  338. compareVC.setFiles(files)
  339. let point = sender.convert(CGPoint(x: 0, y: 0), to: self)
  340. compareVC.showIn(self, rect: NSRect.init(origin: point, size: sender.frame.size))
  341. }
  342. }
  343. }
  344. func fileCellNeedShowInFinder(_ cell:TestFileCellView, fileName:String, type:TestFileType) {
  345. var nFileName = NSString(string: fileName)
  346. var directory = _autoTestObj?.originFileDirectory()
  347. if (type == .Result) {
  348. nFileName = NSString(string: nFileName.deletingPathExtension).appendingPathExtension(_autoTestObj?.extention() ?? "")! as NSString
  349. directory = _autoTestObj?.resultFileDirectory()
  350. }else if (type == .Compare) {
  351. nFileName = NSString(string: nFileName.deletingPathExtension).appendingPathExtension(_autoTestObj?.extention() ?? "")! as NSString
  352. directory = _autoTestObj?.checkFileDirectory()
  353. }
  354. var path = NSString(string: directory!).appendingPathComponent(nFileName as String);
  355. if (FileManager.default.fileExists(atPath: path)) {
  356. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
  357. }else {
  358. path = NSString(string: path).deletingPathExtension.appending(".jpg")
  359. if (FileManager.default.fileExists(atPath: path)) {
  360. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
  361. }else {
  362. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: NSString(string: path).deletingLastPathComponent)])
  363. }
  364. }
  365. }
  366. func fileCellNeedDelete(_ cell:TestFileCellView, fileName:String) {
  367. let originDirectory = _autoTestObj?.originFileDirectory()
  368. let resultDirectory = _autoTestObj?.resultFileDirectory()
  369. let compareDirectory = _autoTestObj?.checkFileDirectory()
  370. let nFileName = NSString(string: NSString(string: fileName).deletingPathExtension).appendingPathExtension(_autoTestObj?.extention() ?? "")! as String
  371. let path = NSString(string: originDirectory!).appendingPathComponent(fileName);
  372. let resultPath = NSString(string: resultDirectory!).appendingPathComponent(nFileName);
  373. let comparePath = NSString(string: compareDirectory!).appendingPathComponent(nFileName);
  374. FileConverter.shared().cancelTaskForSrcPath(path);
  375. try? FileManager.default.removeItem(atPath: path);
  376. try? FileManager.default.removeItem(atPath: resultPath);
  377. try? FileManager.default.removeItem(atPath: comparePath);
  378. let index = _files.firstIndex(of: fileName)
  379. if (index != NSNotFound) {
  380. _files.remove(at: index!)
  381. _fileList.reloadData()
  382. }
  383. }
  384. func fileCellNeedDeleteAll(_ cell:TestFileCellView) {
  385. let originDirectory = _autoTestObj?.originFileDirectory()
  386. let resultDirectory = _autoTestObj?.resultFileDirectory()
  387. let compareDirectory = _autoTestObj?.checkFileDirectory()
  388. for fn in _files {
  389. let nFileName = NSString(string: NSString(string: fn).deletingPathExtension).appendingPathExtension(_autoTestObj?.extention() ?? "")! as String
  390. let path = NSString(string: originDirectory!).appendingPathComponent(fn);
  391. let resultPath = NSString(string: resultDirectory!).appendingPathComponent(nFileName);
  392. let comparePath = NSString(string: compareDirectory!).appendingPathComponent(nFileName);
  393. FileConverter.shared().cancelTaskForSrcPath(path);
  394. try? FileManager.default.removeItem(atPath: path);
  395. try? FileManager.default.removeItem(atPath: resultPath);
  396. try? FileManager.default.removeItem(atPath: comparePath);
  397. }
  398. _files = [];
  399. _fileList.reloadData()
  400. }
  401. func fileCellNeedCancelConvertTask(_ cell:TestFileCellView, fileName:String) {
  402. let originDirectory = _autoTestObj?.originFileDirectory()
  403. let path = NSString(string: originDirectory!).appendingPathComponent(fileName);
  404. FileConverter.shared().cancelTaskForSrcPath(path);
  405. }
  406. }