KMNAlertTipViewController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. //
  2. // KMNAlertTipViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/12/18.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. typealias FormFieldHighlightCallback = () -> ()
  10. typealias EnterPasswordCallback = () -> ()
  11. typealias DigitalDetailsCallback = () -> ()
  12. typealias RedactApplyCallback = () -> ()
  13. typealias OCRApplyCallback = () -> ()
  14. typealias viewFrameHeightChange = (_ height:CGFloat) -> ()
  15. @objc public enum CPromptDignSignaturesState: NSInteger {
  16. case failure = 0
  17. case Unknown = 1
  18. case Success = 2
  19. }
  20. class KMNAlertTipViewController: KMNBaseViewController {
  21. //Form提示
  22. @IBOutlet var formComponentAlert: ComponentAlert!
  23. //Secure提示
  24. @IBOutlet var secureComponentAlert: ComponentAlert!
  25. //数字签名提示
  26. @IBOutlet var digitalComponentAlert: ComponentAlert!
  27. //密文提示
  28. @IBOutlet var redactComponentAlert: ComponentAlert!
  29. //OCR提示
  30. @IBOutlet var OCRComponentAlert: ComponentAlert!
  31. var type:CPromptDignSignaturesState = .failure
  32. var isShowFormAlert = true
  33. var isShowDigitalAlert = true
  34. var isShowSecureAlert = true
  35. var isShowRedactAlert = true
  36. var isShowOCRAlert = true
  37. weak var listView:CPDFListView?
  38. weak var subView:NSView?
  39. var formFieldHighlightCallback: FormFieldHighlightCallback?
  40. var enterPasswordCallback: EnterPasswordCallback?
  41. var digitalDetailsCallback: DigitalDetailsCallback?
  42. var redactApplyCallback: RedactApplyCallback?
  43. var OCRApplyCallback: OCRApplyCallback?
  44. var heightCallback: viewFrameHeightChange?
  45. override func viewDidLoad() {
  46. super.viewDidLoad()
  47. // Do view setup here.
  48. }
  49. override func initContentView() {
  50. super.initContentView()
  51. }
  52. override func updateUIThemeColor() {
  53. super.updateUIThemeColor()
  54. }
  55. override func updateUILanguage() {
  56. super.updateUILanguage()
  57. let highlightFormFiled = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  58. var detailButtonString = ""
  59. if highlightFormFiled {
  60. detailButtonString = KMLocalizedString("Disable Highlight Effect")
  61. } else {
  62. detailButtonString = KMLocalizedString("Highlight Form Fields")
  63. }
  64. formComponentAlert.properties = ComponentAlertProperty(subTitle: KMLocalizedString("This document contains interactive form fields."),detailButtonString: detailButtonString)
  65. formComponentAlert.delegate = self
  66. secureComponentAlert.properties = ComponentAlertProperty(subTitle: KMLocalizedString("This document has a permission password."),detailButtonString: KMLocalizedString("Enter Password"))
  67. secureComponentAlert.delegate = self
  68. digitalComponentAlert.properties = ComponentAlertProperty(subTitle: KMLocalizedString("Signature is valid."),detailButtonString:nil)
  69. digitalComponentAlert.delegate = self
  70. redactComponentAlert.properties = ComponentAlertProperty(subTitle: KMLocalizedString("There are unapplied redactions in this file"),detailButtonString:KMLocalizedString("Apply"))
  71. redactComponentAlert.delegate = self
  72. OCRComponentAlert.properties = ComponentAlertProperty(subTitle: KMLocalizedString("This document contains scanned pages and requires OCR recognition before editing."),detailButtonString:KMLocalizedString("OCR"))
  73. OCRComponentAlert.delegate = self
  74. }
  75. public func reloadFormAlertUI() {
  76. if isShowFormAlert == true {
  77. var isContainsForm = false
  78. for i in 0..<(listView?.document?.pageCount ?? 0) {
  79. guard i < listView?.document?.pageCount ?? 0 else { continue }
  80. let page = listView?.document.page(at: i)
  81. if let annotations = page?.annotations, !annotations.isEmpty {
  82. for annotation in annotations {
  83. if annotation is CPDFWidgetAnnotation {
  84. isContainsForm = true
  85. break
  86. }
  87. }
  88. }
  89. if isContainsForm {
  90. break
  91. }
  92. }
  93. if isContainsForm {
  94. if formComponentAlert.superview == nil {
  95. formComponentAlert.frame = CGRect(x: 0, y: 0, width: subView?.frame.width ?? 0, height: 32.0)
  96. formComponentAlert.autoresizingMask = .width
  97. self.view.addSubview(formComponentAlert)
  98. }
  99. formComponentAlert.reloadData()
  100. } else {
  101. if formComponentAlert.superview != nil {
  102. formComponentAlert.removeFromSuperview()
  103. }
  104. }
  105. } else {
  106. if formComponentAlert.superview != nil {
  107. formComponentAlert.removeFromSuperview()
  108. }
  109. }
  110. }
  111. public func reloadSecureAlertUI() {
  112. if isShowSecureAlert == true {
  113. if listView?.document.permissionsStatus == .user {
  114. if secureComponentAlert.superview == nil {
  115. secureComponentAlert.frame = CGRect(x: 0, y: 0, width: subView?.frame.width ?? 0, height: 32.0)
  116. secureComponentAlert.autoresizingMask = .width
  117. self.view.addSubview(secureComponentAlert)
  118. }
  119. secureComponentAlert.reloadData()
  120. } else {
  121. if secureComponentAlert.superview != nil {
  122. secureComponentAlert.removeFromSuperview()
  123. }
  124. }
  125. } else {
  126. if secureComponentAlert.superview != nil {
  127. secureComponentAlert.removeFromSuperview()
  128. }
  129. }
  130. }
  131. public func reloadDigitalAlertUI() {
  132. if isShowDigitalAlert == true {
  133. let signatures:[CPDFSignature] = listView?.signatures as? [CPDFSignature] ?? []
  134. if signatures.count > 0 {
  135. if digitalComponentAlert.superview == nil {
  136. digitalComponentAlert.frame = CGRect(x: 0, y: 0, width: subView?.frame.width ?? 0, height: 32.0)
  137. digitalComponentAlert.autoresizingMask = .width
  138. self.view.addSubview(digitalComponentAlert)
  139. }
  140. var isSignVerified = false
  141. var isCertTrusted = false
  142. for item in signatures {
  143. let signature: CPDFSignature = item
  144. if signature.signers != nil {
  145. let signer = signature.signers.first
  146. if signer?.isCertTrusted == false {
  147. isCertTrusted = false
  148. break
  149. } else {
  150. isCertTrusted = true
  151. }
  152. }
  153. }
  154. for item in signatures {
  155. let signature: CPDFSignature = item
  156. if signature.signers != nil {
  157. let signer = signature.signers.first
  158. if signer?.isSignVerified == false {
  159. isSignVerified = false
  160. break
  161. } else {
  162. isSignVerified = true
  163. }
  164. }
  165. }
  166. if (isSignVerified && isCertTrusted) {
  167. self.type = .Success;
  168. } else if(isSignVerified && !isCertTrusted) {
  169. self.type = .Unknown;
  170. } else {
  171. self.type = .failure;
  172. }
  173. if (.Success == self.type) {
  174. digitalComponentAlert.properties.messageType = .success
  175. digitalComponentAlert.properties.iconImage = NSImage(named: "KMNImageNameSigntureVerifySuccess")
  176. digitalComponentAlert.properties.subTitle = KMLocalizedString("Signature is valid.")
  177. } else if(.Unknown == self.type) {
  178. digitalComponentAlert.properties.messageType = .warning
  179. digitalComponentAlert.properties.iconImage = NSImage(named: "KMNImageNameSigntureTrustedFailure")
  180. if(signatures.count > 1) {
  181. digitalComponentAlert.properties.subTitle = KMLocalizedString("At least one signature is invalid.")
  182. } else {
  183. digitalComponentAlert.properties.subTitle = KMLocalizedString("Signature is invalid")
  184. }
  185. } else {
  186. digitalComponentAlert.properties.messageType = .error
  187. digitalComponentAlert.properties.iconImage = NSImage(named: "KMNImageNameSigntureVerifyFailures")
  188. if(signatures.count > 1) {
  189. digitalComponentAlert.properties.subTitle = KMLocalizedString("At least one signature is invalid.")
  190. } else {
  191. digitalComponentAlert.properties.subTitle = KMLocalizedString("Signature is invalid")
  192. }
  193. }
  194. digitalComponentAlert.reloadData()
  195. } else {
  196. if digitalComponentAlert.superview != nil {
  197. digitalComponentAlert.removeFromSuperview()
  198. }
  199. }
  200. } else {
  201. if digitalComponentAlert.superview != nil {
  202. digitalComponentAlert.removeFromSuperview()
  203. }
  204. }
  205. }
  206. public func reloadRedactAlertUI() {
  207. if isShowRedactAlert == true {
  208. var isContainsRedact = false
  209. for i in 0..<(listView?.document?.pageCount ?? 0) {
  210. guard i < listView?.document?.pageCount ?? 0 else { continue }
  211. let page = listView?.document.page(at: i)
  212. if let annotations = page?.annotations, !annotations.isEmpty {
  213. for annotation in annotations {
  214. if annotation is CPDFRedactAnnotation {
  215. isContainsRedact = true
  216. break
  217. }
  218. }
  219. }
  220. if isContainsRedact {
  221. break
  222. }
  223. }
  224. if isContainsRedact {
  225. if redactComponentAlert.superview == nil {
  226. redactComponentAlert.frame = CGRect(x: 0, y: 0, width: subView?.frame.width ?? 0, height: 32.0)
  227. redactComponentAlert.autoresizingMask = .width
  228. self.view.addSubview(redactComponentAlert)
  229. }
  230. redactComponentAlert.reloadData()
  231. } else {
  232. if redactComponentAlert.superview != nil {
  233. redactComponentAlert.removeFromSuperview()
  234. }
  235. }
  236. } else {
  237. if redactComponentAlert.superview != nil {
  238. redactComponentAlert.removeFromSuperview()
  239. }
  240. }
  241. }
  242. public func reloadOCRAlertUI() {
  243. if isShowOCRAlert == true && self.listView?.toolMode == .CEditPDFToolMode {
  244. var isContainsImagePage = false
  245. for i in 0..<(listView?.document?.pageCount ?? 0) {
  246. guard i < listView?.document?.pageCount ?? 0 else { continue }
  247. let page = listView?.document.page(at: i)
  248. if (page?.isImagePage() == true) {
  249. isContainsImagePage = true
  250. break
  251. }
  252. }
  253. if isContainsImagePage {
  254. if OCRComponentAlert.superview == nil {
  255. OCRComponentAlert.frame = CGRect(x: 0, y: 0, width: subView?.frame.width ?? 0, height: 40.0)
  256. OCRComponentAlert.autoresizingMask = .width
  257. self.view.addSubview(OCRComponentAlert)
  258. }
  259. OCRComponentAlert.reloadData()
  260. } else {
  261. if OCRComponentAlert.superview != nil {
  262. OCRComponentAlert.removeFromSuperview()
  263. }
  264. }
  265. } else {
  266. if OCRComponentAlert.superview != nil {
  267. OCRComponentAlert.removeFromSuperview()
  268. }
  269. }
  270. }
  271. public func reloadAlertUIFrame() {
  272. var alertHeight = 0.0
  273. if digitalComponentAlert.superview != nil {
  274. digitalComponentAlert.frame = CGRect(x: 0, y: alertHeight, width: subView?.frame.width ?? 0, height: 32.0)
  275. alertHeight += 32.0
  276. }
  277. if formComponentAlert.superview != nil {
  278. if alertHeight > 0 {
  279. alertHeight += 1
  280. }
  281. formComponentAlert.frame = CGRect(x: 0, y: alertHeight, width: subView?.frame.width ?? 0, height: 32.0)
  282. alertHeight += 32.0
  283. }
  284. if secureComponentAlert.superview != nil {
  285. if alertHeight > 0 {
  286. alertHeight += 1
  287. }
  288. secureComponentAlert.frame = CGRect(x: 0, y: alertHeight, width: subView?.frame.width ?? 0, height: 32.0)
  289. alertHeight += 32.0
  290. }
  291. if redactComponentAlert.superview != nil {
  292. if alertHeight > 0 {
  293. alertHeight += 1
  294. }
  295. redactComponentAlert.frame = CGRect(x: 0, y: alertHeight, width: subView?.frame.width ?? 0, height: 32.0)
  296. alertHeight += 32.0
  297. }
  298. if OCRComponentAlert.superview != nil {
  299. if alertHeight > 0 {
  300. alertHeight += 1
  301. }
  302. OCRComponentAlert.frame = CGRect(x: 0, y: alertHeight, width: subView?.frame.width ?? 0, height: 40.0)
  303. alertHeight += 40.0
  304. }
  305. if(alertHeight > 0) {
  306. if(listView?.toolMode == .CRedactToolMode) {
  307. if self.view.superview != nil {
  308. self.view.removeFromSuperview()
  309. heightCallback?(0.0)
  310. }
  311. } else {
  312. var rect = self.view.frame
  313. rect.size.height = alertHeight
  314. rect.size.width = subView?.frame.size.width ?? 0
  315. rect.origin.y = (subView?.frame.size.height ?? 0) - alertHeight
  316. self.view.frame = rect
  317. if self.view.superview == nil {
  318. subView?.addSubview(self.view)
  319. self.view.autoresizingMask = [.width, .minYMargin]
  320. }
  321. heightCallback?(alertHeight)
  322. }
  323. } else {
  324. if self.view.superview != nil {
  325. self.view.removeFromSuperview()
  326. heightCallback?(0)
  327. }
  328. }
  329. }
  330. func showInView(listView: CPDFListView?,subView:NSView) {
  331. self.listView = listView
  332. self.subView = subView
  333. self.view.frame = CGRect(x: 0, y: 0, width: subView.frame.size.width , height: 0)
  334. self.view.autoresizingMask = [.width, .minYMargin]
  335. subView.addSubview(self.view)
  336. reloadSecureAlertUI()
  337. reloadFormAlertUI()
  338. reloadDigitalAlertUI()
  339. reloadRedactAlertUI()
  340. reloadFormAlertUI()
  341. reloadOCRAlertUI()
  342. }
  343. }
  344. //MARK: - ComponentAlertDelegate
  345. extension KMNAlertTipViewController: ComponentAlertDelegate {
  346. func componentAlertDidCloseButtonClick(inputView: ComponentAlert) {
  347. if inputView == formComponentAlert {
  348. isShowFormAlert = false
  349. reloadFormAlertUI()
  350. } else if inputView == secureComponentAlert {
  351. isShowSecureAlert = false
  352. reloadSecureAlertUI()
  353. } else if inputView == digitalComponentAlert {
  354. isShowDigitalAlert = false
  355. reloadDigitalAlertUI()
  356. } else if inputView == redactComponentAlert {
  357. isShowRedactAlert = false
  358. reloadRedactAlertUI()
  359. } else if inputView == OCRComponentAlert {
  360. isShowOCRAlert = false
  361. reloadOCRAlertUI()
  362. }
  363. reloadAlertUIFrame()
  364. }
  365. func componentAlertDidButtonClick(inputView: ComponentAlert, buttonIndex: Int) {
  366. if buttonIndex == 1 {
  367. if inputView == formComponentAlert {
  368. formFieldHighlightCallback?()
  369. let highlightFormFiled = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  370. var detailButtonString = ""
  371. if highlightFormFiled {
  372. detailButtonString = KMLocalizedString("Disable Highlight Effect")
  373. } else {
  374. detailButtonString = KMLocalizedString("Highlight Form Fields")
  375. }
  376. formComponentAlert.properties.detailButtonString = detailButtonString
  377. formComponentAlert.reloadData()
  378. } else if inputView == secureComponentAlert {
  379. enterPasswordCallback?()
  380. } else if inputView == digitalComponentAlert {
  381. digitalDetailsCallback?()
  382. } else if inputView == redactComponentAlert {
  383. redactApplyCallback?()
  384. } else if inputView == OCRComponentAlert {
  385. OCRApplyCallback?()
  386. }
  387. }
  388. }
  389. }