KMScreenShotEditViewController.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // KMScreenShotEditViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2024/1/24.
  6. //
  7. import Cocoa
  8. typealias editButtonClickCallBack = (_ captureOrNot: Bool) -> ()
  9. typealias editSizeCallBack = (_ size: CGSize) -> ()
  10. let KTextFieldFontSize = 12//输入框大小
  11. let KLabelFontSize = 14//提示框字体大小
  12. let kLeftRightGap = 10//左右边距
  13. let kTopBottomGap = 10//上下边距
  14. let kMaskViewBorderLineWidth = 8 //maskView的线边框
  15. let selectMinWidthAndHeight = 40
  16. class KMScreenShotEditViewController: NSViewController{
  17. @IBOutlet var widthIdentifyLabel: NSTextField!
  18. @IBOutlet var heightIdentifyLabel: NSTextField!
  19. @IBOutlet var widthLabel: NSTextField!
  20. @IBOutlet var heightLabel: NSTextField!
  21. @IBOutlet var confirmButton: NSButton!
  22. @IBOutlet var cancelButton: NSButton!
  23. @IBOutlet var widthStepper: NSStepper!
  24. @IBOutlet var heightStepper: NSStepper!
  25. var buttonCallBack: editButtonClickCallBack?
  26. var sizeCallBack: editSizeCallBack?
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. view.wantsLayer = true
  30. view.layer?.backgroundColor = KMAppearance.viewBackgroundColor().cgColor
  31. localizedLanguage()
  32. }
  33. func localizedLanguage() {
  34. widthIdentifyLabel.stringValue = NSLocalizedString("Width", comment: "") + ":"
  35. heightIdentifyLabel.stringValue = NSLocalizedString("Height", comment: "") + ":"
  36. confirmButton.title = NSLocalizedString("OK", comment: "")
  37. cancelButton.title = NSLocalizedString("Cancel", comment: "")
  38. }
  39. @IBAction func buttonClicked_ConfirmButton(_ sender: Any) {
  40. if let callback = buttonCallBack {
  41. callback(true)
  42. }
  43. }
  44. @IBAction func buttonClicked_CancelButton(_ sender: Any) {
  45. if let callback = buttonCallBack {
  46. callback(false)
  47. }
  48. }
  49. @IBAction func stepperClicked_WidthStepper(_ sender: NSStepper) {
  50. widthLabel.integerValue = widthStepper.integerValue
  51. heightLabel.integerValue = heightStepper.integerValue
  52. if widthStepper.integerValue >= selectMinWidthAndHeight && heightStepper.integerValue >= selectMinWidthAndHeight {
  53. sizeCallBack?(CGSize(width: widthStepper.integerValue, height: heightStepper.integerValue))
  54. } else {
  55. sender.integerValue = selectMinWidthAndHeight
  56. }
  57. }
  58. @IBAction func stepperClicked_HeightStepper(_ sender: NSStepper) {
  59. widthLabel.integerValue = widthStepper.integerValue
  60. heightLabel.integerValue = heightStepper.integerValue
  61. if widthStepper.integerValue >= selectMinWidthAndHeight && heightStepper.integerValue >= selectMinWidthAndHeight {
  62. sizeCallBack?(CGSize(width: widthStepper.integerValue, height: heightStepper.integerValue))
  63. } else {
  64. sender.integerValue = selectMinWidthAndHeight
  65. }
  66. }
  67. func updateSize(withRect rect: CGRect) {
  68. widthLabel.integerValue = Int(rect.size.width)
  69. heightLabel.integerValue = Int(rect.size.height)
  70. guard let screen = NSScreen.screens.first else { return }
  71. let xMax = screen.frame.size.width
  72. let yMax = screen.frame.size.height
  73. widthStepper.minValue = Double(selectMinWidthAndHeight)
  74. widthStepper.minValue = Double(selectMinWidthAndHeight)
  75. widthStepper.maxValue = Double(xMax - rect.origin.x)
  76. heightStepper.maxValue = Double(yMax - rect.origin.y)
  77. widthStepper.valueWraps = false
  78. widthStepper.valueWraps = false
  79. widthStepper.increment = 1.0
  80. heightStepper.increment = 1.0
  81. widthStepper.integerValue = Int(rect.size.width)
  82. heightStepper.integerValue = Int(rect.size.height)
  83. }
  84. }