KMHeaderFooterTableCellView.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // KMHeaderFooterTableCellView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/11/7.
  6. //
  7. import Cocoa
  8. typealias HeaderFooterTableCellViewCallback = (_ type: KMBatchModifyTemplateType) -> ()
  9. class KMHeaderFooterTableCellView: NSTableCellView {
  10. var headerFooterTableCellViewCallback: HeaderFooterTableCellViewCallback?
  11. @IBOutlet var textFiled1: NSTextField!
  12. @IBOutlet var textFiled2: NSTextField!
  13. @IBOutlet var textFiled3: NSTextField!
  14. @IBOutlet var textFiled4: NSTextField!
  15. @IBOutlet var textFiled5: NSTextField!
  16. @IBOutlet var textFiled6: NSTextField!
  17. @IBOutlet var titleTextFiled: NSTextField!
  18. @IBOutlet var auxiliaryView: NSView!
  19. @IBOutlet var editButton: KMButton!
  20. @IBOutlet var deleteButtonl: KMButton!
  21. override func prepareForReuse() {
  22. super.prepareForReuse()
  23. self.textFiled1.stringValue = ""
  24. self.textFiled2.stringValue = ""
  25. self.textFiled3.stringValue = ""
  26. self.textFiled4.stringValue = ""
  27. self.textFiled5.stringValue = ""
  28. self.textFiled5.stringValue = ""
  29. self.titleTextFiled.stringValue = ""
  30. self.auxiliaryView.isHidden = true
  31. }
  32. override func awakeFromNib() {
  33. super.awakeFromNib()
  34. self.titleTextFiled.textColor = KMAppearance.Layout.h0Color()
  35. self.textFiled1.textColor = KMAppearance.Layout.h0Color()
  36. self.textFiled2.textColor = KMAppearance.Layout.h0Color()
  37. self.textFiled3.textColor = KMAppearance.Layout.h0Color()
  38. self.textFiled4.textColor = KMAppearance.Layout.h0Color()
  39. self.textFiled5.textColor = KMAppearance.Layout.h0Color()
  40. self.textFiled6.textColor = KMAppearance.Layout.h1Color()
  41. self.textFiled1.stringValue = ""
  42. self.textFiled2.stringValue = ""
  43. self.textFiled3.stringValue = ""
  44. self.textFiled4.stringValue = ""
  45. self.textFiled5.stringValue = ""
  46. self.textFiled6.stringValue = ""
  47. self.titleTextFiled.stringValue = ""
  48. self.auxiliaryView.isHidden = true
  49. self.wantsLayer = true
  50. self.editButton.mouseMoveCallback = {[weak self] mouseEntered in
  51. if mouseEntered {
  52. self?.editButton.image = NSImage(named: KMImageNameUXIconBtnEditHov)
  53. } else {
  54. self?.editButton.image = NSImage(named: KMImageNameUXIconBtnEditNor)
  55. }
  56. }
  57. self.deleteButtonl.mouseMoveCallback = {[weak self] mouseEntered in
  58. if mouseEntered {
  59. self?.deleteButtonl.image = NSImage(named: KMImageNameUXIconBtnCloseHov)
  60. } else {
  61. self?.deleteButtonl.image = NSImage(named: KMImageNameUXIconBtnCloseNor)
  62. }
  63. }
  64. self.newTrackingArea()
  65. }
  66. func newTrackingArea() {
  67. let trackingArea = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeAlways, .mouseMoved], owner: self, userInfo: nil)
  68. self.addTrackingArea(trackingArea)
  69. }
  70. func appendBaseString(_ string: String) -> String {
  71. var appendString = string
  72. return appendString
  73. }
  74. func updateInterface(_ obj: KMHeaderFooterObject) {
  75. var arr = NSMutableArray()
  76. if obj.topLeftString.count > 0 {
  77. arr.add(appendBaseString(NSLocalizedString("Left Header Text", comment: ""), withString: appendBaseString(obj.topLeftString)))
  78. }
  79. if obj.topCenterString.count > 0 {
  80. arr.add(appendBaseString(NSLocalizedString("Center Header Text", comment: ""), withString: appendBaseString(obj.topCenterString)))
  81. }
  82. if obj.topRightString.count > 0 {
  83. arr.add(appendBaseString(NSLocalizedString("Right Header Text", comment: ""), withString: appendBaseString(obj.topRightString)))
  84. }
  85. if obj.bottomLeftString.count > 0 {
  86. arr.add(appendBaseString(NSLocalizedString("Left Footer Text", comment: ""), withString: appendBaseString(obj.bottomLeftString)))
  87. }
  88. if obj.bottomCenterString.count > 0 {
  89. arr.add(appendBaseString(NSLocalizedString("Center Footer Text", comment: ""), withString: appendBaseString(obj.bottomCenterString)))
  90. }
  91. if obj.bottomRightString.count > 0 {
  92. arr.add(appendBaseString(NSLocalizedString("Right Footer Text", comment: ""), withString: appendBaseString(obj.bottomRightString)))
  93. }
  94. for i in 0..<arr.count {
  95. switch i {
  96. case 0:
  97. self.textFiled1.stringValue = arr[i] as! String
  98. case 1:
  99. self.textFiled2.stringValue = arr[i] as! String
  100. case 2:
  101. self.textFiled3.stringValue = arr[i] as! String
  102. case 3:
  103. self.textFiled4.stringValue = arr[i] as! String
  104. case 4:
  105. self.textFiled5.stringValue = arr[i] as! String
  106. case 5:
  107. self.textFiled6.stringValue = arr[i] as! String
  108. default:
  109. break
  110. }
  111. }
  112. self.titleTextFiled.stringValue = obj.id
  113. }
  114. func appendBaseString(_ prefixString: String, withString suffixString: String) -> String {
  115. return "\(prefixString): \(suffixString)"
  116. }
  117. override func mouseEntered(with event: NSEvent) {
  118. super.mouseEntered(with: event)
  119. self.auxiliaryView.isHidden = false
  120. self.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  121. }
  122. override func mouseExited(with event: NSEvent) {
  123. super.mouseExited(with: event)
  124. self.auxiliaryView.isHidden = true
  125. self.layer?.backgroundColor = NSColor.clear.cgColor
  126. }
  127. @IBAction func buttonClicked_Edit(_ sender: Any) {
  128. if let callback = self.headerFooterTableCellViewCallback {
  129. callback(.Edit)
  130. }
  131. }
  132. @IBAction func buttonClicked_Delete(_ sender: Any) {
  133. if let callback = self.headerFooterTableCellViewCallback {
  134. callback(.Delete)
  135. }
  136. }
  137. }