KMScreenShotMaskViewController.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // KMScreenShotMaskViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2024/1/24.
  6. //
  7. import Cocoa
  8. let keditViewGap = 5
  9. class KMScreenShotMaskViewController: NSViewController{
  10. @IBOutlet var screenShotMaskView: KMScreenShotMaskView!
  11. var editViewController: KMScreenShotEditViewController!
  12. var holeDragImageView: NSImageView!
  13. var callBack: captureScreenCallBack?
  14. convenience init(callB: @escaping captureScreenCallBack) {
  15. self.init(nibName: "KMScreenShotMaskViewController", bundle: nil)
  16. self.callBack = callB
  17. }
  18. override func loadView() {
  19. super.loadView()
  20. initViews()
  21. handlerCallBack()
  22. }
  23. func initViews() {
  24. view.wantsLayer = true
  25. view.layer?.backgroundColor = NSColor.clear.cgColor
  26. screenShotMaskView.frame = view.frame
  27. editViewController = KMScreenShotEditViewController(nibName: "KMScreenShotEditViewController", bundle: nil)
  28. view.addSubview(editViewController.view)
  29. editViewController.view.isHidden = true
  30. holeDragImageView = NSImageView(frame: NSRect(x: 0, y: 0, width: 40, height: 40))
  31. view.addSubview(holeDragImageView)
  32. holeDragImageView.isHidden = true
  33. holeDragImageView.image = NSImage(named: "drag.png")
  34. }
  35. func handlerCallBack() {
  36. self.screenShotMaskView.callBack = self.callBack
  37. self.screenShotMaskView.frameCallBack = {[weak self] rect in
  38. if self?.editViewController.view.isHidden == true {
  39. self?.editViewController.view.isHidden = false
  40. }
  41. if self?.holeDragImageView.isHidden == true {
  42. self?.holeDragImageView.isHidden = false
  43. }
  44. self?.editViewController.updateSize(withRect: rect)
  45. self?.updateEditViewFrame(with: rect)
  46. self?.updateHoleButtonPosition(rect)
  47. }
  48. self.editViewController.buttonCallBack = { [weak self] captureOrNot in
  49. self?.holeDragImageView.isHidden = true
  50. if captureOrNot {
  51. self?.screenShotMaskView.captureImage()
  52. } else {
  53. self?.screenShotMaskView.endImageCapture()
  54. }
  55. }
  56. self.editViewController.sizeCallBack = { [weak self] size in
  57. self?.screenShotMaskView.updateholeSize(newSize: size)
  58. self?.updateHoleButtonPositionWithHoleSize(holeSize: size)
  59. }
  60. self.screenShotMaskView.detachCallBack = { [weak self] event in
  61. let cursorPoint = KMScreenShotHandler.screenPosition(for: event)
  62. self?.screenShotMaskView.holeDragMode = NSPointInRect(cursorPoint, NSMakeRect((self?.screenShotMaskView.borderFrame.origin.x ?? 0) + 4, (self?.screenShotMaskView.borderFrame.origin.y ?? 0) + 4, (self?.screenShotMaskView.borderFrame.size.width ?? 0) - 8, (self?.screenShotMaskView.borderFrame.size.height ?? 0) - 8))
  63. }
  64. }
  65. func beginImageCapture(_ isCaptureWindow: Bool) {
  66. self.holeDragImageView.isHidden = true
  67. if isCaptureWindow {
  68. self.editViewController.view.isHidden = true
  69. }
  70. self.screenShotMaskView.beginImageCapture(isCaptureWindow: isCaptureWindow)
  71. }
  72. func updateHoleButtonPositionWithHoleSize(holeSize: CGSize) {
  73. self.holeDragImageView.frame = NSMakeRect(self.screenShotMaskView.borderFrame.origin.x + holeSize.width/2 - self.holeDragImageView.frame.size.width/2, self.screenShotMaskView.borderFrame.origin.y + holeSize.height/2 - self.holeDragImageView.frame.size.height/2, self.holeDragImageView.frame.size.width, self.holeDragImageView.frame.size.height)
  74. }
  75. func updateHoleButtonPosition(_ rect: NSRect) {
  76. self.holeDragImageView.frame = NSMakeRect(NSMidX(rect) - self.holeDragImageView.frame.size.width/2, NSMidY(rect) - self.holeDragImageView.frame.size.height/2, self.holeDragImageView.frame.size.width, self.holeDragImageView.frame.size.height)
  77. }
  78. func updateEditViewFrame(with aRect: NSRect) {
  79. let viewWidth = self.editViewController.view.frame.size.width
  80. let viewHeight = self.editViewController.view.frame.size.height
  81. var newRect = NSMakeRect(NSMinX(aRect), NSMinY(aRect) - viewHeight - CGFloat(keditViewGap), viewWidth, viewHeight)
  82. if NSMinY(newRect) < 0 {
  83. newRect.origin.x = NSMinX(newRect) - viewWidth - CGFloat(keditViewGap)
  84. newRect.origin.y = NSMinY(aRect) + CGFloat(keditViewGap)
  85. if newRect.origin.x < 0 {
  86. newRect.origin.x = NSMinX(aRect) + CGFloat(keditViewGap)
  87. }
  88. }
  89. let screenFrame = NSScreen.screens.first!.frame
  90. if NSMaxX(screenFrame) < NSMaxX(newRect) {
  91. newRect.origin.x = screenFrame.size.width - viewWidth
  92. }
  93. if NSMaxY(screenFrame) < NSMaxY(newRect) {
  94. newRect.origin.y = screenFrame.size.height - viewHeight
  95. }
  96. self.editViewController.view.frame = newRect
  97. }
  98. }