AutoTestAdvanceSettingView.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. continue
  214. }
  215. let nPath = NSString(string: path).appendingPathComponent(subpath)
  216. var isDirectory = ObjCBool(false)
  217. if FileManager.default.fileExists(atPath: nPath, isDirectory: &isDirectory) && isDirectory.boolValue {
  218. continue
  219. }
  220. let resultPath = NSString(string: toDirectory).appendingPathComponent(NSString(string: path).lastPathComponent+"/"+subpath)
  221. createSymbolicLink(nPath, toDirectory: NSString(string: resultPath).deletingLastPathComponent)
  222. }
  223. }else {
  224. let resultPath = NSString(string: toDirectory).appendingPathComponent(NSString(string: path).lastPathComponent)
  225. let directory = NSString(string: resultPath).deletingLastPathComponent
  226. // Create directory for
  227. var isDirectory = ObjCBool(false)
  228. if !FileManager.default.fileExists(atPath: directory, isDirectory: &isDirectory) || !isDirectory.boolValue {
  229. try? FileManager.default.createDirectory(atPath: directory, withIntermediateDirectories: true)
  230. }
  231. if FileManager.default.fileExists(atPath: resultPath) {
  232. try? FileManager.default.removeItem(atPath: resultPath)
  233. }
  234. try? FileManager.default.createSymbolicLink(atPath: resultPath, withDestinationPath: path)
  235. }
  236. }
  237. }
  238. func copyFile(_ path:String, toDirectory:String) {
  239. let resultPath = NSString(string: toDirectory).appendingPathComponent(NSString(string: path).lastPathComponent)
  240. if FileManager.default.fileExists(atPath: path) {
  241. let directory = NSString(string: resultPath).deletingLastPathComponent
  242. // Create directory for
  243. var isDirectory = ObjCBool(false)
  244. if !FileManager.default.fileExists(atPath: directory, isDirectory: &isDirectory) || !isDirectory.boolValue {
  245. try? FileManager.default.createDirectory(atPath: directory, withIntermediateDirectories: true)
  246. }
  247. if FileManager.default.fileExists(atPath: resultPath) {
  248. try? FileManager.default.removeItem(atPath: resultPath)
  249. }
  250. try? FileManager.default.copyItem(atPath: path, toPath: resultPath)
  251. }
  252. }
  253. // TableView Delegate
  254. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  255. let cellView = TestFileCellView.shared()
  256. if (_files.count >= row) {
  257. let title = _files[row]
  258. cellView?.setTitle(title)
  259. cellView?._delegate = self
  260. if _autoTestObj != nil {
  261. cellView?.setNeedReplaceBtn(_autoTestObj!.canUpdateRefImage(title))
  262. }else {
  263. cellView?.setNeedReplaceBtn(false)
  264. }
  265. if (_autoTestObj != nil) {
  266. cellView?.set((_autoTestObj!.status() == .Process &&
  267. _autoTestObj!.convertFiles.contains(title) &&
  268. !_autoTestObj!.compareFinishedFiles.contains(title)))
  269. }else {
  270. cellView?.set(false)
  271. }
  272. if _autoTestObj != nil &&
  273. (_autoTestObj?.status() == .Finished ||
  274. _autoTestObj!.compareFinishedFiles.contains(title)) {
  275. cellView?.setNeedDegreeBtn(true)
  276. cellView?.setDegree((_autoTestObj?.degreeOfFile(title))!)
  277. }else {
  278. cellView?.setNeedDegreeBtn(false)
  279. }
  280. }
  281. return cellView
  282. }
  283. func selectionShouldChange(in tableView: NSTableView) -> Bool {
  284. return true
  285. }
  286. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  287. return false
  288. }
  289. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  290. return false
  291. }
  292. func tableView(_ tableView: NSTableView, mouseDownInHeaderOf tableColumn: NSTableColumn) {
  293. }
  294. func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) {
  295. }
  296. func tableView(_ tableView: NSTableView, didDrag tableColumn: NSTableColumn) {
  297. }
  298. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  299. return 30
  300. }
  301. func tableView(_ tableView: NSTableView, isGroupRow row: Int) -> Bool {
  302. return false
  303. }
  304. func tableView(_ tableView: NSTableView, sizeToFitWidthOfColumn column: Int) -> CGFloat {
  305. return tableView.frame.width
  306. }
  307. func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
  308. return []
  309. }
  310. func tableViewSelectionDidChange(_ notification: Notification) {
  311. }
  312. // TableView Data Source
  313. func numberOfRows(in tableView: NSTableView) -> Int {
  314. return _files.count
  315. }
  316. // TestFileCellViewDelegate
  317. func fileCellNeedReplace(_ cell: TestFileCellView, fileName: String) {
  318. if (_autoTestObj != nil) {
  319. _autoTestObj?.updateRefImage(fileName)
  320. }
  321. _replaceAllBtn.isEnabled = _autoTestObj!.canUpdateRefImage()
  322. _fileList.reloadData()
  323. }
  324. func fileCellNeedShowReport(_ cell: TestFileCellView, fileName: String, sender:NSButton) {
  325. let files = _autoTestObj?.compareFiles(fileName) ?? []
  326. if files.count > 0 {
  327. if (_autoTestObj as? StringAutoTest) != nil {
  328. var compareVC = StringCompareViewController.shared()
  329. compareVC.setFiles(files)
  330. let point = sender.convert(CGPoint(x: 0, y: 0), to: self)
  331. compareVC.showIn(self, rect: NSRect.init(origin: point, size: sender.frame.size))
  332. }else {
  333. let compareVC = CompareViewController.shared()
  334. compareVC.setFiles(files)
  335. let point = sender.convert(CGPoint(x: 0, y: 0), to: self)
  336. compareVC.showIn(self, rect: NSRect.init(origin: point, size: sender.frame.size))
  337. }
  338. }
  339. }
  340. func fileCellNeedShowInFinder(_ cell:TestFileCellView, fileName:String, type:TestFileType) {
  341. var nFileName = NSString(string: fileName)
  342. var directory = _autoTestObj?.originFileDirectory()
  343. if (type == .Result) {
  344. nFileName = NSString(string: nFileName.deletingPathExtension).appendingPathExtension(_autoTestObj?.extention() ?? "")! as NSString
  345. directory = _autoTestObj?.resultFileDirectory()
  346. }else if (type == .Compare) {
  347. nFileName = NSString(string: nFileName.deletingPathExtension).appendingPathExtension(_autoTestObj?.extention() ?? "")! as NSString
  348. directory = _autoTestObj?.checkFileDirectory()
  349. }
  350. var path = NSString(string: directory!).appendingPathComponent(nFileName as String);
  351. if (FileManager.default.fileExists(atPath: path)) {
  352. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
  353. }else {
  354. path = NSString(string: path).deletingPathExtension.appending(".jpg")
  355. if (FileManager.default.fileExists(atPath: path)) {
  356. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
  357. }else {
  358. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: NSString(string: path).deletingLastPathComponent)])
  359. }
  360. }
  361. }
  362. func fileCellNeedDelete(_ cell:TestFileCellView, fileName:String) {
  363. let originDirectory = _autoTestObj?.originFileDirectory()
  364. let path = NSString(string: originDirectory!).appendingPathComponent(fileName);
  365. FileConverter.shared().cancelTaskForSrcPath(path);
  366. try? FileManager.default.removeItem(atPath: path);
  367. let index = _files.firstIndex(of: fileName)
  368. if (index != NSNotFound) {
  369. _files.remove(at: index!)
  370. _fileList.reloadData()
  371. }
  372. }
  373. func fileCellNeedCancelConvertTask(_ cell:TestFileCellView, fileName:String) {
  374. let originDirectory = _autoTestObj?.originFileDirectory()
  375. let path = NSString(string: originDirectory!).appendingPathComponent(fileName);
  376. FileConverter.shared().cancelTaskForSrcPath(path);
  377. }
  378. }