KMPrintAccessoryController.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // KMPrintAccessoryController.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/2/21.
  6. //
  7. import Cocoa
  8. class KMPrintAccessoryController: NSViewController, NSPrintPanelAccessorizing {
  9. let AUTOROTATE_KEY = "autoRotate"
  10. let AUTOROTATE_KEYPATH = "representedObject.dictionary.PDFPrintAutoRotate"
  11. let PRINTSCALINGMODE_KEY = "printScalingMode"
  12. let PRINTSCALINGMODE_KEYPATH = "representedObject.dictionary.PDFPrintScalingMode"
  13. let REPRESENTEDOBJECT_KEY = "representedObject"
  14. var printOperation: NSPrintOperation?
  15. @objc var printScalingMode: PDFPrintScalingMode
  16. {
  17. @objc set {
  18. let printInfo = self.representedObject as! NSPrintInfo
  19. printInfo.dictionary().setObject(newValue.rawValue, forKey: "PDFPrintScalingMode" as NSCopying)
  20. // self.setValue(NSNumber.init(value: newValue.rawValue), forKeyPath: PRINTSCALINGMODE_KEYPATH)
  21. }
  22. @objc get {
  23. return PDFPrintScalingMode(rawValue: (self.value(forKeyPath: PRINTSCALINGMODE_KEYPATH)) as! Int) ?? .pageScaleNone
  24. }
  25. }
  26. // didSet {
  27. // let printInfo = self.representedObject as! NSPrintInfo
  28. // printInfo.dictionary().setObject(self.printScalingMode.rawValue, forKey: "PDFPrintScalingMode" as NSCopying)
  29. // self.printOperation?.view?.updateLayer()
  30. // self.printOperation?.printInfo.updateFromPMPageFormat()
  31. // self.printOperation?.printInfo.updateFromPMPrintSettings()
  32. // self.printOperation!.runModal(for: NSApplication.shared.mainWindow!, delegate: nil, didRun: nil, contextInfo: nil)
  33. // self.printOperation?.view?.updateLayer()
  34. // print(printInfo)
  35. // printInfo.dictionary.PDFPrintScalingMode
  36. // 不改变
  37. // [[printInfo dictionary] setObject:[NSNumber numberWithBool:[[[printInfo dictionary] objectForKey:NSPrintHeaderAndFooter] boolValue]] forKey:NSPrintHeaderAndFooter];
  38. // }
  39. // }
  40. var autoRotate: Bool = false
  41. // {
  42. // set {
  43. // self.setValue(NSNumber.init(booleanLiteral: newValue), forKeyPath: AUTOROTATE_KEYPATH)
  44. // }
  45. // get {
  46. // return (self.value(forKeyPath: AUTOROTATE_KEYPATH)) as! Bool
  47. // }
  48. // }
  49. @IBOutlet weak var actualSizeButton: NSButton!
  50. @IBOutlet weak var scaleFachPageButton: NSButton!
  51. @IBOutlet weak var onlyScaleDownLargeButton: NSButton!
  52. // func keyPathsForValuesAffectingLocalizedSummaryItems() -> Set<String> {
  53. // return NSSet(objects: AUTOROTATE_KEY, PRINTSCALINGMODE_KEY) as! Set<String>
  54. // }
  55. //
  56. // func keyPathsForValuesAffectingAutoRotate() -> Set<String> {
  57. // return NSSet(objects: REPRESENTEDOBJECT_KEY) as! Set<String>
  58. // }
  59. //
  60. // func keyPathsForValuesAffectingPrintScalingMode() -> Set<String> {
  61. // return NSSet(objects: REPRESENTEDOBJECT_KEY) as! Set<String>
  62. // }
  63. //
  64. // func keyPathsForValuesAffectingPreview() -> Set<String> {
  65. // return NSSet(objects: AUTOROTATE_KEY, PRINTSCALINGMODE_KEY) as! Set<String>
  66. // }
  67. //
  68. // override func viewDidLoad() {
  69. // super.viewDidLoad()
  70. // // Do view setup here.
  71. // }
  72. //
  73. func keyPathsForValuesAffectingPreview() -> Set<String> {
  74. return NSSet(objects: "printScalingMode") as! Set<String> //scalingMode scaleMode
  75. }
  76. // func keyPathsForValuesAffectingPreview() -> Set<String> {
  77. // return NSSet(objects: "printScalingMode") as! Set<String> //scalingMode scaleMode
  78. // }
  79. //缩小后显示的内容
  80. func localizedSummaryItems() -> [[NSPrintPanel.AccessorySummaryKey : String]] {
  81. let autoRotation = self.autoRotate ? NSLocalizedString("On", comment: "") : NSLocalizedString("Off", comment: "")
  82. var autoScaling = ""
  83. // switch (printScalingMode) {
  84. // case .pageScaleNone:
  85. // autoScaling = NSLocalizedString("None", comment: "")
  86. // case .pageScaleToFit:
  87. // autoScaling = NSLocalizedString("Scale Each Page", comment: "")
  88. // case .pageScaleDownToFit:
  89. // autoScaling = NSLocalizedString("Only Scale Down Large Pages", comment: "")
  90. // @unknown default:
  91. // print("未找到")
  92. // }
  93. return [[NSPrintPanel.AccessorySummaryKey.itemName: NSLocalizedString("Page Auto Rotation", comment: ""),
  94. NSPrintPanel.AccessorySummaryKey.itemDescription : autoRotation],
  95. [NSPrintPanel.AccessorySummaryKey.itemName: NSLocalizedString("Page Auto Scaling", comment: ""),
  96. NSPrintPanel.AccessorySummaryKey.itemDescription : autoScaling]]
  97. }
  98. @IBAction func actualSizeButtonAction(_ sender: Any) {
  99. self.printScalingMode = .pageScaleNone
  100. }
  101. @IBAction func scaleFachPageButtonAction(_ sender: Any) {
  102. self.printScalingMode = .pageScaleToFit
  103. }
  104. @IBAction func onlyScaleDownLargeButtonAction(_ sender: Any) {
  105. self.printScalingMode = .pageScaleDownToFit
  106. }
  107. // - (void)setWxmailPreview:(BOOL)flag {
  108. // NSPrintInfo *printInfo = [self representedObject];
  109. // // 不改变
  110. // [[printInfo dictionary] setObject:[NSNumber numberWithBool:[[[printInfo dictionary] objectForKey:NSPrintHeaderAndFooter] boolValue]] forKey:NSPrintHeaderAndFooter];
  111. // }
  112. //
  113. // - (void) refreshPreview {
  114. // qInfo() << "refreshPreview";
  115. // // NOTE: 刷新
  116. // [self setWxmailPreview:NO];
  117. // }
  118. }