KMPDFRedactViewController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. //
  2. // KMPDFRedactViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/18.
  6. //
  7. import Cocoa
  8. class KMPDFRedactViewController: NSViewController {
  9. var scaleFactor: CGFloat = 0 {
  10. didSet {
  11. self.redactPdfView.scaleFactor = scaleFactor
  12. }
  13. }
  14. var callback: ((Bool, Int, Bool, URL?)->Void)?
  15. var titleBack: ((String)->Void)?
  16. @IBOutlet var redactPdfView: KMRedactPDFView!
  17. @IBOutlet weak var redactTipLabel: NSTextField!
  18. @IBOutlet weak var propertiesLabel: NSTextField!
  19. @IBOutlet weak var redactTextLabel: NSTextField!
  20. @IBOutlet weak var exitBox: KMBox!
  21. @IBOutlet weak var applyBox: KMBox!
  22. @IBOutlet weak var exitButton: NSButton!
  23. @IBOutlet weak var applyButton: NSButton!
  24. @IBOutlet weak var toolView: NSView!
  25. @IBOutlet weak var redactTextBox: NSBox!
  26. @IBOutlet weak var propertiesBox: NSBox!
  27. @IBOutlet weak var redactTextButton: KMCustomButton!
  28. @IBOutlet weak var propertiesButton: KMCustomButton!
  29. @IBOutlet weak var whiteOutBox: NSBox!
  30. @IBOutlet weak var whiteOutLabel: NSTextField!
  31. @IBOutlet weak var whiteOutButton: KMCustomButton!
  32. private var _url: URL?
  33. private var url: URL? {
  34. get {
  35. return self._url
  36. }
  37. }
  38. private var _password: String?
  39. private var _indicator: NSProgressIndicator?
  40. private var isWhiteOut_ = false
  41. deinit {
  42. KMPrint("KMPDFRedactViewController deinit.")
  43. NotificationCenter.default.removeObserver(self)
  44. self.redactPdfView.delegate = nil
  45. }
  46. convenience init(url: URL, password: String?) {
  47. self.init()
  48. self._url = url
  49. self._password = password
  50. }
  51. override func loadView() {
  52. super.loadView()
  53. self.isWhiteOut_ = false
  54. self.propertiesBox.wantsLayer = true
  55. self.redactTextBox.wantsLayer = true
  56. self.redactTextBox.fillColor = KMAppearance.Layout.w30Color()
  57. self.redactTextBox.layer?.cornerRadius = 6
  58. self.redactTextLabel.textColor = KMAppearance.Layout.w0Color()
  59. self.propertiesLabel.textColor = KMAppearance.Layout.w0Color()
  60. self.redactTextLabel.stringValue = KMLocalizedString("Redact Text", nil)
  61. self.propertiesLabel.stringValue = KMLocalizedString("Properties", nil)
  62. self.propertiesButton.mouseMoveCallback = { [weak self] mouseEntered in
  63. self?.propertiesBox.layer?.cornerRadius = 6
  64. if mouseEntered {
  65. self?.propertiesBox.fillColor = KMAppearance.Layout.w30Color()
  66. } else {
  67. self?.propertiesBox.fillColor = .clear
  68. }
  69. }
  70. self.whiteOutBox.wantsLayer = true
  71. // self.whiteOutBox.fillColor = KMAppearance.Layout.w30Color()
  72. self.whiteOutBox.layer?.cornerRadius = 6
  73. self.whiteOutLabel.textColor = KMAppearance.Layout.w0Color()
  74. self.whiteOutLabel.stringValue = NSLocalizedString("White Out PDF", comment: "")
  75. self.redactTextButton.target = self
  76. self.redactTextButton.action = #selector(redactButtonAction)
  77. self.whiteOutButton.target = self
  78. self.whiteOutButton.action = #selector(whiteOutButtonAction)
  79. // Do view setup here.
  80. self.redactTipLabel.stringValue = KMLocalizedString("You are under redact mode", nil)
  81. self.applyButton.title = KMLocalizedString("Apply", nil)
  82. self.exitButton.toolTip = KMLocalizedString("Exit", nil)
  83. self.exitBox.fillColor = .clear
  84. self.exitBox.borderColor = KMAppearance.Layout.w70Color()
  85. self.exitBox.borderWidth = 1.0
  86. self.exitButton.title = KMLocalizedString("Exit", nil)
  87. self.exitButton.setTitleColor(KMAppearance.Layout.w0Color())
  88. self.applyButton.setTitleColor(KMAppearance.Interactive.a0Color())
  89. self.applyButton.wantsLayer = true
  90. self.applyBox.downCallback = { [weak self] downEntered, mouseBox, event in
  91. if downEntered {
  92. self?.apply_button(nil)
  93. }
  94. }
  95. self.exitBox.downCallback = { [weak self] downEntered, mouseBox, event in
  96. if downEntered {
  97. self?.exit_button(nil)
  98. }
  99. }
  100. self.toolView.wantsLayer = true
  101. self.toolView.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  102. let document = CPDFDocument(url: self.url!)
  103. if let pwd = self._password, pwd.isEmpty == false {
  104. document?.unlock(withPassword: pwd)
  105. }
  106. self.redactPdfView.document = document
  107. // self.redactPdfView.scaleFactor = self.scaleFactor;
  108. self.redactPdfView.autoScales = true
  109. self.redactPdfView.delegate = self
  110. // self.redactPdfView.toolMode = .redactToolMode
  111. self.redactPdfView.operationType = .redact
  112. // [self.redactPdfView layoutDocumentView];
  113. let frame = NSMakeRect((self.redactPdfView.frame.size.width-32)/2.0, (self.redactPdfView.frame.size.height-32)/2.0, 32, 32)
  114. self._indicator = NSProgressIndicator(frame: frame)
  115. self._indicator?.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  116. self._indicator?.style = .spinning
  117. self._indicator?.controlSize = .regular
  118. self._indicator?.isIndeterminate = true
  119. self._indicator?.startAnimation(nil)
  120. self.redactPdfView.addSubview(self._indicator!)
  121. self._indicator?.isHidden = true
  122. }
  123. override func viewDidLoad() {
  124. super.viewDidLoad()
  125. NotificationCenter.default.addObserver(self, selector: #selector(showCurrentRedactAnnotationProperties), name: KMRedactPDFView.showCurrentRedactAnnotationNotificationName, object: nil)
  126. NotificationCenter.default.addObserver(self, selector: #selector(apply), name: KMRedactPDFView.redactAnnotationApplyNotificationName, object: nil)
  127. NotificationCenter.default.addObserver(self, selector: #selector(across), name: KMRedactPDFView.redactAnnotationAcrossNotificationName, object: nil)
  128. }
  129. func setCurrentPageIndex(_ currentPageIndex: Int) {
  130. self.redactPdfView.go(toPageIndex: currentPageIndex, animated: false)
  131. }
  132. // MARK: - Button Actions
  133. @IBAction func showProperties_button(_ sender: AnyObject?) {
  134. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_RedactText", withProperties: ["SubTbr_Btn": "Btn_SubTbr_RedactText_Properties"])
  135. let properties = KMRedactPropertiesWindowController()
  136. self.km_beginSheet(windowC: properties)
  137. properties.callback = { annotation in
  138. }
  139. }
  140. @IBAction func apply_button(_ sender: AnyObject?) {
  141. var isApply = false
  142. for i in 0 ..< self.redactPdfView.document.pageCount {
  143. let page = self.redactPdfView.document.page(at: i)
  144. for anno in page?.annotations ?? [] {
  145. if anno.isKind(of: CPDFRedactAnnotation.self) {
  146. isApply = true
  147. }
  148. }
  149. }
  150. if(self.redactPdfView.newAddAnnotation.count == 0 && !isApply) {
  151. return
  152. }
  153. let returnCode = KMAlertTool.runModelForMainThread_r(message: "", informative: KMLocalizedString("This will permanently remove the redacted information from this document. Once you save this document, you won’t be able to retrieve the redacted information.", nil), buttons: [KMLocalizedString("Apply", nil), KMLocalizedString("Cancel", nil)])
  154. if returnCode == .alertFirstButtonReturn {
  155. DispatchQueue.main.async {
  156. self.saveAsPath()
  157. }
  158. }
  159. }
  160. @IBAction func exit_button(_ sender: AnyObject?) {
  161. guard let block = self.callback else {
  162. return
  163. }
  164. block(false, self.redactPdfView.currentPageIndex, false, nil)
  165. }
  166. func saveAsPath() {
  167. let saveAccessCtr = KMSavePanelAccessoryController()
  168. var fileName = self.redactPdfView.document.documentURL.deletingPathExtension().lastPathComponent
  169. fileName = String(format: "%@_%@.pdf", fileName, "Redact")
  170. let outputSavePanel = NSSavePanel()
  171. outputSavePanel.allowedFileTypes = ["pdf"]
  172. outputSavePanel.nameFieldStringValue = fileName
  173. outputSavePanel.accessoryView = saveAccessCtr.view
  174. outputSavePanel.beginSheetModal(for: NSApp.mainWindow!) { result in
  175. if result == .OK {
  176. self.redactPdfView.document.applyRedactions()
  177. self._indicator?.isHidden = false
  178. self._indicator?.startAnimation(nil)
  179. let savePDFPath = outputSavePanel.url!.path
  180. let isSuccess = self.redactPdfView.document.write(toFile: savePDFPath)
  181. if (isSuccess) {
  182. // [self.redactPdfView.newAddAnnotation removeAllObjects];
  183. if (saveAccessCtr.openAutomaticButton.state == .on) {
  184. NSDocumentController.shared.km_safe_openDocument(withContentsOf: outputSavePanel.url!, display: true) { _, _, _ in
  185. }
  186. } else {
  187. KMTools.viewFile(at: savePDFPath)
  188. }
  189. }
  190. self._indicator?.stopAnimation(nil)
  191. self._indicator?.isHidden = true
  192. guard let block = self.callback else {
  193. return
  194. }
  195. block(true, self.redactPdfView.currentPageIndex, false, outputSavePanel.url)
  196. }
  197. }
  198. }
  199. // MARK: - Button notice
  200. @objc func showCurrentRedactAnnotationProperties(_ notice: NSNotification) {
  201. guard let pdfview = notice.object as? KMRedactPDFView else {
  202. return
  203. }
  204. if(pdfview == self.redactPdfView) {
  205. let properties = KMRedactPropertiesWindowController()
  206. properties.readactAnnotation = self.redactPdfView.currentAnnotation
  207. // __block KMPDFRedactViewController *weak_self = self;
  208. self.km_beginSheet(windowC: properties)
  209. properties.callback = { [weak self] annotation in
  210. if let page = self?.redactPdfView.currentAnnotation?.page {
  211. if let anno = annotation {
  212. self?.redactPdfView.newAddAnnotation.append(anno)
  213. }
  214. self?.redactPdfView.setNeedsDisplayAnnotationViewFor(page)
  215. }
  216. }
  217. }
  218. }
  219. @objc func apply(_ notice: NSNotification) {
  220. guard let pdfview = notice.object as? KMRedactPDFView else {
  221. return
  222. }
  223. if(pdfview == self.redactPdfView) {
  224. self.apply_button(nil)
  225. }
  226. }
  227. @objc func across(_ notice: NSNotification) {
  228. guard let pdfview = notice.object as? KMRedactPDFView else {
  229. return
  230. }
  231. if(pdfview == self.redactPdfView) {
  232. let pagesWindowController = KMRedactSelectPagesWindowController(document: self.redactPdfView.document)
  233. self.km_beginSheet(windowC: pagesWindowController)
  234. pagesWindowController.callback = { [unowned self] pages in
  235. if pages.count > 0 {
  236. self.redactPdfView.acrossAddAnnotations(pages)
  237. }
  238. }
  239. }
  240. }
  241. @objc func redactButtonAction(sender : NSButton) {
  242. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_RedactText", withProperties: ["SubTbr_Btn": "Btn_SubTbr_RedactText_RedactPDF"])
  243. self.isWhiteOut_ = false
  244. self.redactPdfView.isWhiteOut = false
  245. self.updateButtonsState()
  246. }
  247. @objc func whiteOutButtonAction(sender: NSButton) {
  248. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_RedactText", withProperties: ["SubTbr_Btn": "Btn_SubTbr_RedactText_WhiteOutPDF"])
  249. self.isWhiteOut_ = true
  250. self.redactPdfView.isWhiteOut = true
  251. self.updateButtonsState()
  252. }
  253. // MARK: - Private Methods
  254. private func updateButtonsState() {
  255. if self.isWhiteOut_ {
  256. self.whiteOutBox.fillColor = KMAppearance.Layout.w30Color()
  257. self.redactTextBox.fillColor = .clear
  258. } else {
  259. self.redactTextBox.fillColor = KMAppearance.Layout.w30Color()
  260. self.whiteOutBox.fillColor = .clear
  261. }
  262. }
  263. }
  264. extension KMPDFRedactViewController: CPDFViewDelegate {
  265. func pdfViewCurrentPageDidChanged(_ pdfView: CPDFView!) {
  266. let fileName = pdfView.document.documentURL.deletingLastPathComponent().lastPathComponent
  267. let title = String(format: KMLocalizedString("%@ (page %ld / %ld)", "Window title format"), fileName, pdfView.currentPageIndex+1, pdfView.document.pageCount)
  268. if(self.titleBack != nil) {
  269. self.titleBack!(title)
  270. }
  271. }
  272. }