KMLongerButton.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // KMLongerButton.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by kdanmobile on 2023/10/26.
  6. //
  7. import Cocoa
  8. class KMLongerButton: NSButton,KMCustomButtonPopMenuViewControllerDelegate,KMCustomButtonPopMenuViewControllerDataSources,NSPopoverDelegate{
  9. weak var containerViewController: KMBatchOperateLeftViewController?
  10. var isImageToPDF = false
  11. var popOver: NSPopover?
  12. var buttonLayer: CALayer?
  13. override func draw(_ dirtyRect: NSRect) {
  14. super.draw(dirtyRect)
  15. }
  16. override var intrinsicContentSize: NSSize {
  17. var size = super.intrinsicContentSize
  18. size.width += 20
  19. return size
  20. }
  21. override func awakeFromNib() {
  22. super.awakeFromNib()
  23. self.newTrackingArea()
  24. self.wantsLayer = true
  25. self.buttonLayer = CALayer()
  26. self.layer?.addSublayer(self.buttonLayer!)
  27. self.buttonLayer?.frame = CGRect(x: 0, y: 0, width: CGRectGetWidth(self.bounds), height: CGRectGetHeight(self.bounds))
  28. self.buttonLayer?.backgroundColor = KMAppearance.KM_000000_Color5DarkChange01().cgColor
  29. self.buttonLayer?.cornerRadius = 1
  30. self.buttonLayer?.isHidden = true
  31. }
  32. func newTrackingArea() {
  33. let trackingArea = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeAlways], owner: self, userInfo: nil)
  34. self.addTrackingArea(trackingArea)
  35. }
  36. func showPop(sender: NSView) {
  37. let menuViewController = KMCustomButtonPopMenuViewController()
  38. menuViewController.delegate = self
  39. menuViewController.dataSources = self
  40. self.popOver = NSPopover()
  41. self.popOver?.delegate = self
  42. self.popOver?.contentViewController = menuViewController
  43. self.popOver?.animates = false
  44. self.popOver?.behavior = .semitransient
  45. self.popOver?.contentSize = menuViewController.view.frame.size
  46. var sourcesRect = sender.bounds
  47. sourcesRect = sender.convert(sourcesRect, to: nil)
  48. sourcesRect.origin.y -= 20
  49. sourcesRect.size.height += 20
  50. self.window?.popover = self.popOver
  51. self.window?.sourcesRect = sourcesRect
  52. self.popOver?.show(relativeTo: self.bounds.insetBy(dx: 0, dy: 5), of: self, preferredEdge: .maxY)
  53. }
  54. func closePop() {
  55. popOver?.close()
  56. popOver = nil
  57. window?.popover = nil
  58. }
  59. override func mouseEntered(with event: NSEvent) {
  60. super.mouseEntered(with: event)
  61. self.showPop(sender: self)
  62. self.buttonLayer?.isHidden = false
  63. }
  64. override func mouseExited(with event: NSEvent) {
  65. super.mouseExited(with: event)
  66. self.buttonLayer?.isHidden = true
  67. }
  68. override func mouseDown(with event: NSEvent) {
  69. super.mouseDown(with: event)
  70. }
  71. override func mouseUp(with event: NSEvent) {
  72. super.mouseUp(with: event)
  73. }
  74. func customViewButtonPopDidSelectIndex(_ index: Int) {
  75. self.closePop()
  76. if isImageToPDF {
  77. if index == 0 {
  78. containerViewController?.chooseFile()
  79. } else if index == 1 {
  80. containerViewController?.chooseFileFromCamera()
  81. } else {
  82. containerViewController?.chooseFileFromScanner()
  83. }
  84. } else {
  85. if index == 0 {
  86. containerViewController?.chooseFile()
  87. } else {
  88. containerViewController?.addOpendFile()
  89. }
  90. }
  91. }
  92. func numberOfLine() -> Int {
  93. if (self.isImageToPDF) {
  94. return 3;
  95. } else {
  96. return 2;
  97. }
  98. }
  99. func stringForLine(at index: Int) -> String? {
  100. if self.isImageToPDF {
  101. let arr = [String.localizedStringWithFormat(NSLocalizedString("Import From %@", comment: ""), NSLocalizedString("My Mac", comment: "")), NSLocalizedString("Import From Camera", comment: ""), NSLocalizedString("Import From Scanner", comment: "")]
  102. return arr[index]
  103. } else {
  104. let arr = [NSLocalizedString("Select Files", comment: ""), NSLocalizedString("Add Recent Files", comment: "")]
  105. return arr[index]
  106. }
  107. }
  108. func needInsertSeperateLine(at index: Int) -> Bool {
  109. return false
  110. }
  111. func needHightLightLine(at index: Int) -> Bool {
  112. return false
  113. }
  114. func itemEnable(at index: Int) -> Bool {
  115. if self.isImageToPDF {
  116. return true
  117. } else {
  118. if index == 0 {
  119. return true
  120. } else {
  121. var hasOpenPDF = false
  122. let documentArray = NSDocumentController.shared.documents
  123. for i in 0..<documentArray.count {
  124. let document = documentArray[i]
  125. let path = document.fileURL?.path
  126. if path == nil {
  127. return false
  128. }
  129. let UTTypeString = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, path!.customPathExtension as CFString, nil)?.takeRetainedValue() as String?
  130. let TypeCFString = UTTypeString as CFString?
  131. if UTTypeConformsTo(TypeCFString!, kUTTypePDF) {
  132. hasOpenPDF = true
  133. break
  134. }
  135. }
  136. return hasOpenPDF
  137. }
  138. }
  139. }
  140. }