TestFileCellView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // TeseCaseCell.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/21.
  6. //
  7. import Foundation
  8. import AppKit
  9. protocol TestFileCellViewDelegate : NSObjectProtocol {
  10. func fileCellNeedReplace(_ cell:TestFileCellView, fileName:String)
  11. func fileCellNeedShowReport(_ cell:TestFileCellView, fileName:String, sender:NSButton)
  12. func fileCellNeedShowInFinder(_ cell:TestFileCellView, fileName:String)
  13. func fileCellNeedDelete(_ cell:TestFileCellView, fileName:String)
  14. }
  15. class TestFileCellView : NSTableCellView, NSMenuDelegate {
  16. @IBOutlet var _titleLbl : NSTextField!
  17. @IBOutlet var _sepLine : NSView!
  18. @IBOutlet var _progressView : NSProgressIndicator!
  19. @IBOutlet var _replaceBtn : NSButton!
  20. @IBOutlet var _testReportBtn : NSButton!
  21. var _title : String!
  22. var _isExpad: Bool!
  23. var _typeInfo : NSDictionary!
  24. var _isActivting : Bool = false;
  25. var _degree : Double = 0
  26. weak public var _delegate : (TestFileCellViewDelegate)?
  27. class func shared() -> TestFileCellView? {
  28. var objects : NSArray!
  29. Bundle.main.loadNibNamed("TestFileCellView", owner: nil, topLevelObjects: &objects)
  30. for tView in objects {
  31. if let tv = tView as? TestFileCellView {
  32. return tv
  33. }
  34. }
  35. return nil
  36. }
  37. override func awakeFromNib() {
  38. _sepLine.wantsLayer = true;
  39. _sepLine.layer?.backgroundColor = NSColor.lightGray.cgColor
  40. let _menu = NSMenu(title: "")
  41. let item = NSMenuItem.init(title: "在访达中显示", action:#selector(self.showInFinder(_:)), keyEquivalent: "")
  42. item.target = self;
  43. _menu.addItem(item)
  44. let item1 = NSMenuItem.init(title: "删除", action: #selector(self.deleteActions(_:)), keyEquivalent: "")
  45. _menu.addItem(item1)
  46. item1.target = self;
  47. self.menu = _menu
  48. self.menu?.delegate = self;
  49. NSEvent.addLocalMonitorForEvents(matching: NSEvent.EventTypeMask.rightMouseUp) { event in
  50. var point = event.locationInWindow;
  51. if (nil != self.window?.contentView) {
  52. point = self.window!.contentView!.convert(point, to: self)
  53. if (event.type == .rightMouseUp && CGRectContainsPoint(self.bounds, point)) {
  54. NSMenu.popUpContextMenu(self.menu!, with: event, for: self)
  55. return nil;
  56. }
  57. }
  58. return event
  59. }
  60. }
  61. // Setter & Getter
  62. public func setTitle(_ title:String) {
  63. _title = title;
  64. _titleLbl.stringValue = _title ?? ""
  65. }
  66. public func title() -> String? {
  67. return _title
  68. }
  69. public func set(_ activiting:Bool) {
  70. _isActivting = activiting;
  71. if (_isActivting) {
  72. _progressView.startAnimation(nil);
  73. }else {
  74. _progressView.stopAnimation(nil);
  75. }
  76. _progressView.isHidden = !_isActivting
  77. }
  78. public func fileType() -> String? {
  79. return _title
  80. }
  81. public func setNeedReplaceBtn(_ needReplace:Bool) {
  82. _replaceBtn.isHidden = !needReplace
  83. }
  84. public func setTypeInfo(_ typeInfo:NSDictionary) {
  85. _typeInfo = typeInfo
  86. self.setTitle(_typeInfo["Type"] as! String)
  87. }
  88. public func typeInfo() -> NSDictionary? {
  89. return _typeInfo
  90. }
  91. public func setNeedDegreeBtn(_ needDegree:Bool) {
  92. _testReportBtn.isHidden = !needDegree
  93. if (needDegree) {
  94. self.set(!needDegree)
  95. }
  96. }
  97. public func setDegree(_ degree:Double) {
  98. _degree = degree
  99. if fabs(_degree - 100) > 0.01 {
  100. _testReportBtn.contentTintColor = NSColor.red
  101. }else {
  102. _testReportBtn.contentTintColor = NSColor.gray
  103. }
  104. _testReportBtn.title = NSString(format: "%.0f%%", _degree) as String
  105. }
  106. // IBActionn
  107. @IBAction func replaceAction(_ sender:NSButton) {
  108. if _delegate != nil {
  109. _delegate?.fileCellNeedReplace(self, fileName: _title)
  110. }
  111. }
  112. @IBAction func showReportAction(_ sender:NSButton) {
  113. if _delegate != nil {
  114. _delegate?.fileCellNeedShowReport(self, fileName: _title, sender: sender)
  115. }
  116. }
  117. @IBAction func showInFinder(_ sender:NSMenuItem) {
  118. if _delegate != nil {
  119. _delegate?.fileCellNeedShowInFinder(self, fileName: _title)
  120. }
  121. }
  122. @IBAction func deleteActions(_ sender:NSMenuItem) {
  123. if _delegate != nil {
  124. _delegate?.fileCellNeedDelete(self, fileName: _title)
  125. }
  126. }
  127. }