TestFileCellView.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // TeseCaseCell.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/21.
  6. //
  7. import Foundation
  8. import AppKit
  9. enum TestFileType {
  10. case Origin
  11. case Result
  12. case Compare
  13. }
  14. protocol TestFileCellViewDelegate : NSObjectProtocol {
  15. func fileCellNeedReplace(_ cell:TestFileCellView, fileName:String)
  16. func fileCellNeedShowReport(_ cell:TestFileCellView, fileName:String, sender:NSButton)
  17. func fileCellNeedShowInFinder(_ cell:TestFileCellView, fileName:String, type:TestFileType)
  18. func fileCellNeedDelete(_ cell:TestFileCellView, fileName:String)
  19. func fileCellNeedCancelConvertTask(_ cell:TestFileCellView, fileName:String)
  20. }
  21. class TestFileCellView : NSTableCellView, NSMenuDelegate {
  22. @IBOutlet var _titleLbl : NSTextField!
  23. @IBOutlet var _sepLine : NSView!
  24. @IBOutlet var _progressView : NSProgressIndicator!
  25. @IBOutlet var _replaceBtn : NSButton!
  26. @IBOutlet var _testReportBtn : NSButton!
  27. var _title : String!
  28. var _isExpad: Bool!
  29. var _typeInfo : NSDictionary!
  30. var _showOriginMenuItem : NSMenuItem!
  31. var _showResultMenuItem : NSMenuItem!
  32. var _showCompareMenuItem : NSMenuItem!
  33. var _cancelMenuItem : NSMenuItem!
  34. var _isActivting : Bool = false;
  35. var _degree : Double = 0
  36. weak public var _delegate : (TestFileCellViewDelegate)?
  37. class func shared() -> TestFileCellView? {
  38. var objects : NSArray!
  39. Bundle.main.loadNibNamed("TestFileCellView", owner: nil, topLevelObjects: &objects)
  40. for tView in objects {
  41. if let tv = tView as? TestFileCellView {
  42. return tv
  43. }
  44. }
  45. return nil
  46. }
  47. override func awakeFromNib() {
  48. _sepLine.wantsLayer = true;
  49. _sepLine.layer?.backgroundColor = NSColor.lightGray.cgColor
  50. let _menu = NSMenu(title: "")
  51. // Show In Finder
  52. _showOriginMenuItem = NSMenuItem.init(title: "在访达中显示[原文件]", action:#selector(self.showInFinder(_:)), keyEquivalent: "")
  53. _showOriginMenuItem.target = self;
  54. _menu.addItem(_showOriginMenuItem)
  55. _showResultMenuItem = NSMenuItem.init(title: "在访达中显示[结果文件]", action:#selector(self.showInFinder(_:)), keyEquivalent: "")
  56. _showResultMenuItem.target = self;
  57. _menu.addItem(_showResultMenuItem)
  58. _showCompareMenuItem = NSMenuItem.init(title: "在访达中显示[对照文件]", action:#selector(self.showInFinder(_:)), keyEquivalent: "")
  59. _showCompareMenuItem.target = self;
  60. _menu.addItem(_showCompareMenuItem)
  61. _menu.addItem(NSMenuItem.separator());
  62. // Cancel Convert Task
  63. _cancelMenuItem = NSMenuItem.init(title: "取消转档任务", action: #selector(self.cancelAction(_:)), keyEquivalent: "")
  64. _cancelMenuItem.isEnabled = false;
  65. _menu.addItem(_cancelMenuItem)
  66. _cancelMenuItem.target = self;
  67. _menu.addItem(NSMenuItem.separator());
  68. // Delete Origin Files
  69. let item1 = NSMenuItem.init(title: "删除", action: #selector(self.deleteActions(_:)), keyEquivalent: "")
  70. _menu.addItem(item1)
  71. item1.target = self;
  72. self.menu = _menu
  73. self.menu?.delegate = self;
  74. NSEvent.addLocalMonitorForEvents(matching: NSEvent.EventTypeMask.rightMouseUp) { event in
  75. var point = event.locationInWindow;
  76. if (nil != self.window?.contentView) {
  77. point = self.window!.contentView!.convert(point, to: self)
  78. if (event.type == .rightMouseUp && CGRectContainsPoint(self.bounds, point)) {
  79. NSMenu.popUpContextMenu(self.menu!, with: event, for: self)
  80. return nil;
  81. }
  82. }
  83. return event
  84. }
  85. }
  86. // Setter & Getter
  87. public func setTitle(_ title:String) {
  88. _title = title;
  89. _titleLbl.stringValue = _title ?? ""
  90. }
  91. public func title() -> String? {
  92. return _title
  93. }
  94. public func set(_ activiting:Bool) {
  95. _isActivting = activiting;
  96. if (_isActivting) {
  97. _progressView.startAnimation(nil);
  98. _cancelMenuItem.isEnabled = true;
  99. }else {
  100. _progressView.stopAnimation(nil);
  101. _cancelMenuItem.isEnabled = false;
  102. }
  103. _progressView.isHidden = !_isActivting
  104. }
  105. public func fileType() -> String? {
  106. return _title
  107. }
  108. public func setNeedReplaceBtn(_ needReplace:Bool) {
  109. _replaceBtn.isHidden = !needReplace
  110. }
  111. public func setTypeInfo(_ typeInfo:NSDictionary) {
  112. _typeInfo = typeInfo
  113. self.setTitle(_typeInfo["Type"] as! String)
  114. }
  115. public func typeInfo() -> NSDictionary? {
  116. return _typeInfo
  117. }
  118. public func setNeedDegreeBtn(_ needDegree:Bool) {
  119. _testReportBtn.isHidden = !needDegree
  120. if (needDegree) {
  121. self.set(!needDegree)
  122. }
  123. }
  124. public func setDegree(_ degree:Double) {
  125. _degree = degree
  126. if fabs(_degree - 100) > 0.01 {
  127. _testReportBtn.contentTintColor = NSColor.red
  128. }else {
  129. _testReportBtn.contentTintColor = NSColor.gray
  130. }
  131. _testReportBtn.title = NSString(format: "%.0f%%", floor(_degree)) as String
  132. }
  133. // IBActionn
  134. @IBAction func replaceAction(_ sender:NSButton) {
  135. if _delegate != nil {
  136. _delegate?.fileCellNeedReplace(self, fileName: _title)
  137. }
  138. }
  139. @IBAction func showReportAction(_ sender:NSButton) {
  140. if _delegate != nil {
  141. _delegate?.fileCellNeedShowReport(self, fileName: _title, sender: sender)
  142. }
  143. }
  144. @IBAction func showInFinder(_ sender:NSMenuItem) {
  145. if _delegate != nil {
  146. var type = TestFileType.Origin;
  147. if (sender == _showResultMenuItem) {
  148. type = TestFileType.Result
  149. }else if (sender == _showCompareMenuItem) {
  150. type = TestFileType.Compare
  151. }
  152. _delegate?.fileCellNeedShowInFinder(self, fileName: _title, type: type)
  153. }
  154. }
  155. @IBAction func deleteActions(_ sender:NSMenuItem) {
  156. if _delegate != nil {
  157. _delegate?.fileCellNeedDelete(self, fileName: _title)
  158. }
  159. }
  160. @IBAction func cancelAction(_ sender:NSMenuItem) {
  161. if (_delegate != nil) {
  162. _cancelMenuItem.isEnabled = false;
  163. _delegate?.fileCellNeedCancelConvertTask(self, fileName: _title);
  164. }
  165. }
  166. }