KMAnnotationSelectLinkViewController.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. //
  2. // KMAnnotationSelectLinkViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/11/23.
  6. //
  7. import Cocoa
  8. let URLPlaceholder = "https://www.pdfreaderpro.com"
  9. let EmailPlaceholder = "support@pdfreaderpro.com"
  10. enum KMAnnotationLinkType: UInt {
  11. case page = 0
  12. case url
  13. case email
  14. }
  15. @objcMembers class KMAnnotationSelectLinkViewController: NSViewController, NSTextFieldDelegate {
  16. var _annotations: [CPDFAnnotation]?
  17. var pdfDocument: CPDFDocument?
  18. weak var _pdfview: CPDFListView?
  19. @IBOutlet var linkPageBox: KMBox!
  20. @IBOutlet var linkPageImageView: NSImageView!
  21. @IBOutlet var linkPageLabel: NSTextField!
  22. @IBOutlet var linkUrlBox: KMBox!
  23. @IBOutlet var linkUrlImageView: NSImageView!
  24. @IBOutlet var linkUrlLabel: NSTextField!
  25. @IBOutlet var linkEmailBox: KMBox!
  26. @IBOutlet var linkEmailImageView: NSImageView!
  27. @IBOutlet var linkEmailLabel: NSTextField!
  28. @IBOutlet var contentBox: NSBox!
  29. @IBOutlet var contentBoxLayoutConstraint: NSLayoutConstraint!
  30. @IBOutlet var pageView: NSView!
  31. @IBOutlet var pageLabel: NSTextField!
  32. @IBOutlet var inputPageTextField: NSTextField!
  33. @IBOutlet var allPageLabel: NSTextField!
  34. @IBOutlet var urlView: NSView!
  35. @IBOutlet var urlLabel: NSTextField!
  36. @IBOutlet var inputUrlTextField: NSTextField!
  37. @IBOutlet var goButton: NSButton!
  38. @IBOutlet var errorLabel: NSTextField!
  39. var annotation: CPDFLinkAnnotation?
  40. var _linkType: KMAnnotationLinkType = .page
  41. var mouseDownBox: KMBox?
  42. var pageRecord: String?
  43. var urlRecord: String?
  44. var emailRecord: String?
  45. var startPage: String?
  46. var isGo: Bool = false
  47. // MARK: Init Methods
  48. deinit {
  49. NotificationCenter.default.removeObserver(self)
  50. inputUrlTextField.delegate = nil
  51. inputPageTextField.delegate = nil
  52. }
  53. override func viewDidLoad() {
  54. super.viewDidLoad()
  55. // Do view setup here.
  56. pageRecord = ""
  57. urlRecord = ""
  58. emailRecord = ""
  59. linkPageLabel.stringValue = NSLocalizedString("Go To Page", comment: "")
  60. linkPageLabel.toolTip = NSLocalizedString("Go To Page", comment: "")
  61. linkPageLabel.textColor = KMAppearance.Layout.h1Color()
  62. linkUrlLabel.stringValue = NSLocalizedString("Hyperlink", comment: "")
  63. linkUrlLabel.toolTip = NSLocalizedString("Hyperlink", comment: "")
  64. linkUrlLabel.textColor = KMAppearance.Layout.h1Color()
  65. linkEmailLabel.stringValue = NSLocalizedString("Email", comment: "")
  66. linkEmailLabel.toolTip = NSLocalizedString("Email", comment: "")
  67. linkEmailLabel.textColor = KMAppearance.Layout.h1Color()
  68. errorLabel.stringValue = NSLocalizedString("Page number out of range", comment: "")
  69. errorLabel.textColor = KMAppearance.Status.errColor()
  70. let boxArr: [KMBox] = [linkPageBox, linkUrlBox, linkEmailBox]
  71. for box in boxArr {
  72. box.moveCallback = { [weak self] mouseEntered, mouseBox in
  73. guard let self = self else { return }
  74. if mouseEntered {
  75. if self.mouseDownBox != mouseBox {
  76. if box == self.linkPageBox {
  77. self.linkPageBox.fillColor = KMAppearance.Status.selColor()
  78. self.linkPageImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkPageSel")
  79. self.linkPageLabel.textColor = KMAppearance.Layout.h0Color()
  80. } else if box == self.linkUrlBox {
  81. self.linkUrlBox.fillColor = KMAppearance.Status.selColor()
  82. self.linkUrlImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkUrlSel")
  83. self.linkUrlLabel.textColor = KMAppearance.Layout.h0Color()
  84. } else if box == self.linkEmailBox {
  85. self.linkEmailBox.fillColor = KMAppearance.Status.selColor()
  86. self.linkEmailImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkEmailSel")
  87. self.linkEmailLabel.textColor = KMAppearance.Layout.h0Color()
  88. }
  89. }
  90. } else {
  91. if self.mouseDownBox != mouseBox {
  92. box.fillColor = NSColor.clear
  93. if box == self.linkPageBox {
  94. self.linkPageImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkPage")
  95. self.linkPageLabel.textColor = KMAppearance.Layout.h1Color()
  96. } else if box == self.linkUrlBox {
  97. self.linkUrlImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkUrl")
  98. self.linkUrlLabel.textColor = KMAppearance.Layout.h1Color()
  99. } else if box == self.linkEmailBox {
  100. self.linkEmailImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkEmail")
  101. self.linkEmailLabel.textColor = KMAppearance.Layout.h1Color()
  102. }
  103. }
  104. }
  105. }
  106. box.downCallback = { [weak self] downEntered, mouseBox, event in
  107. guard let self = self else { return }
  108. if downEntered {
  109. if !IAPProductsManager.default().isAvailableAllFunction() {
  110. if mouseBox == self.linkUrlBox || mouseBox == self.linkEmailBox {
  111. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  112. return
  113. }
  114. }
  115. if self.mouseDownBox == mouseBox {
  116. return
  117. }
  118. var mouseDownInt = 0
  119. if mouseBox == self.linkPageBox {
  120. self.inputPageTextField.stringValue = ""
  121. self.linkType = .page
  122. mouseDownInt = 0
  123. } else if mouseBox == self.linkUrlBox {
  124. self.inputUrlTextField.stringValue = ""
  125. self.linkType = .url
  126. mouseDownInt = 1
  127. } else if mouseBox == self.linkEmailBox {
  128. self.inputUrlTextField.stringValue = ""
  129. self.linkType = .email
  130. mouseDownInt = 2
  131. }
  132. UserDefaults.standard.set(mouseDownInt, forKey: "kmLinkSelectIndex")
  133. UserDefaults.standard.synchronize()
  134. self.mouseDownBox = mouseBox
  135. }
  136. }
  137. }
  138. isGo = true
  139. pageLabel.stringValue = NSLocalizedString("Page Number:", comment: "")
  140. pageLabel.textColor = KMAppearance.Layout.h1Color()
  141. allPageLabel.textColor = KMAppearance.Layout.h2Color()
  142. goButton.title = NSLocalizedString("Go", tableName: "MainMenu", comment: "")
  143. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  144. goButton.wantsLayer = true
  145. goButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  146. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  147. inputPageTextField.backgroundColor = KMAppearance.Layout.l1Color()
  148. inputUrlTextField.backgroundColor = KMAppearance.Layout.l1Color()
  149. inputPageTextField.wantsLayer = true
  150. inputUrlTextField.wantsLayer = true
  151. inputPageTextField.layer?.borderWidth = 1.0
  152. inputUrlTextField.layer?.borderWidth = 1.0
  153. inputPageTextField.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  154. inputUrlTextField.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  155. inputPageTextField.layer?.cornerRadius = 1.0
  156. inputUrlTextField.layer?.cornerRadius = 1.0
  157. inputUrlTextField.inputContext?.allowedInputSourceLocales = [NSAllRomanInputSourcesLocaleIdentifier]
  158. if let annotationURL = annotation?.url() {
  159. var urlString = annotationURL.fileURL.absoluteString
  160. if annotationURL.hasPrefix("mailto:") {
  161. linkType = .email
  162. mouseDownBox = linkEmailBox
  163. } else {
  164. linkType = .url
  165. mouseDownBox = linkUrlBox
  166. }
  167. } else {
  168. linkType = .page
  169. mouseDownBox = linkPageBox
  170. }
  171. }
  172. override func viewDidAppear() {
  173. super.viewDidAppear()
  174. inputPageTextField.delegate = self
  175. inputUrlTextField.delegate = self
  176. if let url = annotation?.url {
  177. inputUrlTextField.becomeFirstResponder()
  178. } else {
  179. inputPageTextField.becomeFirstResponder()
  180. }
  181. }
  182. // MARK: Get & Set
  183. var linkType: KMAnnotationLinkType {
  184. set {
  185. _linkType = newValue
  186. contentBoxLayoutConstraint.constant = 46.0
  187. switch _linkType {
  188. case .page:
  189. contentBoxLayoutConstraint.constant = 24.0
  190. createLinkPageView()
  191. contentBox.contentView = pageView
  192. inputPageTextField.becomeFirstResponder()
  193. case .url:
  194. createLinkURLView()
  195. contentBox.contentView = urlView
  196. inputUrlTextField.becomeFirstResponder()
  197. case .email:
  198. createLinkEmailView()
  199. contentBox.contentView = urlView
  200. inputUrlTextField.becomeFirstResponder()
  201. default:
  202. break
  203. }
  204. }
  205. get {
  206. return _linkType
  207. }
  208. }
  209. weak var pdfview: CPDFListView? {
  210. set {
  211. _pdfview = newValue
  212. // startPage = _pdfview?.currentPage().label
  213. self.startPage = "\((self._pdfview?.currentPage().pageIndex() ?? 1)+1)"
  214. }
  215. get {
  216. return _pdfview
  217. }
  218. }
  219. var annotations: [CPDFAnnotation]? {
  220. set {
  221. _annotations = newValue
  222. annotation = _annotations?.first as? CPDFLinkAnnotation
  223. }
  224. get {
  225. return _annotations
  226. }
  227. }
  228. // MARK: Private
  229. func getURLViewHeightLayoutConstraint() -> CGFloat {
  230. return urlLabel.frame.height + 4 + inputUrlTextField.frame.height
  231. }
  232. func getPageViewHeightLayoutConstraint() -> CGFloat {
  233. return inputPageTextField.frame.height
  234. }
  235. func createLinkPageView() {
  236. annotationLinkSelectBox(linkPageBox)
  237. inputPageTextField.formatter = TextFieldFormatter()
  238. if pdfDocument!.pageCount > 1 {
  239. inputPageTextField.placeholderString = "1-\(pdfDocument!.pageCount)"
  240. } else {
  241. inputPageTextField.placeholderString = "1"
  242. }
  243. allPageLabel.stringValue = "/\(pdfDocument!.pageCount)"
  244. if let destination = annotation?.destination() {
  245. if let page = destination.page() {
  246. let pageIndex = page.document?.index(for: page) ?? 0
  247. inputPageTextField.stringValue = "\(pageIndex + 1)"
  248. pageRecord = "\(pageIndex + 1)"
  249. }
  250. }
  251. if inputPageTextField.stringValue.isEmpty {
  252. inputPageTextField.stringValue = pageRecord!
  253. }
  254. let pageFloat = Float(inputPageTextField.stringValue) ?? 0
  255. if pageFloat < 1 || UInt(pageFloat) > self.pdfDocument!.pageCount {
  256. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  257. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  258. } else {
  259. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  260. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  261. }
  262. }
  263. func createLinkURLView() {
  264. annotationLinkSelectBox(linkUrlBox)
  265. urlLabel.stringValue = NSLocalizedString("URL:", comment: "")
  266. urlLabel.textColor = KMAppearance.Layout.h1Color()
  267. inputUrlTextField.placeholderString = "https://www.pdfreaderpro.com"
  268. if let annotationURL = annotation?.url() {
  269. var urlString = annotationURL.fileURL.absoluteString
  270. if !annotationURL.hasPrefix("mailto:") {
  271. if urlString.hasPrefix("http://") {
  272. urlString = String(urlString.suffix(from: urlString.index(urlString.startIndex, offsetBy: 7)))
  273. } else if urlString.hasPrefix("https://") {
  274. urlString = String(urlString.suffix(from: urlString.index(urlString.startIndex, offsetBy: 8)))
  275. }
  276. inputUrlTextField.stringValue = annotationURL//urlString
  277. urlRecord = urlString
  278. }else {
  279. inputUrlTextField.stringValue = ""
  280. }
  281. }else {
  282. inputUrlTextField.stringValue = ""
  283. }
  284. if urlRecord!.isEmpty {
  285. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  286. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  287. } else {
  288. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  289. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  290. }
  291. }
  292. func createLinkEmailView() {
  293. annotationLinkSelectBox(linkEmailBox)
  294. urlLabel.stringValue = NSLocalizedString("Email:", comment: "")
  295. urlLabel.textColor = KMAppearance.Layout.h1Color()
  296. inputUrlTextField.placeholderString = "support@pdfreaderpro.com"
  297. if let annotationURL = annotation?.url() {
  298. // if let urlString = annotation?.url().fileURL.absoluteString, urlString.hasPrefix("mailto:") {
  299. // let trimmedString = String(urlString.suffix(from: urlString.index(urlString.startIndex, offsetBy: 7)))
  300. // inputUrlTextField.stringValue = trimmedString
  301. // emailRecord = trimmedString
  302. // }
  303. var urlString = annotationURL.fileURL.absoluteString
  304. if annotationURL.hasPrefix("mailto:") {
  305. let trimmedString = String(urlString.suffix(from: urlString.index(urlString.startIndex, offsetBy: 7)))
  306. inputUrlTextField.stringValue = annotationURL
  307. emailRecord = trimmedString
  308. }
  309. }else {
  310. inputUrlTextField.stringValue = ""
  311. }
  312. if emailRecord!.count > 0 {
  313. goButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  314. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  315. } else {
  316. goButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  317. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  318. }
  319. }
  320. func isValidateEmail(_ email: String) -> Bool {
  321. let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
  322. let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegex)
  323. return emailTest.evaluate(with: email)
  324. }
  325. func judgeWebURL(_ urlString: String) -> String {
  326. var modifiedURLString = urlString
  327. if !modifiedURLString.hasPrefix("http://") && !modifiedURLString.hasPrefix("https://") {
  328. modifiedURLString = "https://" + modifiedURLString
  329. }
  330. if modifiedURLString == "https://" {
  331. modifiedURLString = ""
  332. }
  333. return modifiedURLString
  334. }
  335. func judgeEmailURL(_ urlString: String) -> String {
  336. var modifiedURLString = urlString
  337. if !modifiedURLString.hasPrefix("mailto:") {
  338. modifiedURLString = "mailto:" + modifiedURLString
  339. }
  340. if modifiedURLString == "mailto:" {
  341. modifiedURLString = ""
  342. }
  343. return modifiedURLString
  344. }
  345. func annotationLinkSelectBox(_ box: KMBox) {
  346. if box.isEqual(linkPageBox) {
  347. linkPageBox.fillColor = KMAppearance.Status.selColor()
  348. linkUrlBox.fillColor = NSColor.clear
  349. linkEmailBox.fillColor = NSColor.clear
  350. linkPageImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkPageSel")
  351. linkUrlImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkUrl")
  352. linkEmailImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkEmail")
  353. linkPageLabel.textColor = KMAppearance.Layout.h0Color()
  354. linkUrlLabel.textColor = KMAppearance.Layout.h1Color()
  355. linkEmailLabel.textColor = KMAppearance.Layout.h1Color()
  356. if let dexPage = Int(pageRecord!), pageRecord!.count >= 1 {
  357. if dexPage < 1 || dexPage > self.pdfDocument!.pageCount {
  358. errorLabel.isHidden = false
  359. } else {
  360. errorLabel.isHidden = true
  361. }
  362. }
  363. } else if box.isEqual(linkUrlBox) {
  364. linkUrlBox.fillColor = KMAppearance.Status.selColor()
  365. linkPageBox.fillColor = NSColor.clear
  366. linkEmailBox.fillColor = NSColor.clear
  367. linkPageImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkPage")
  368. linkUrlImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkUrlSel")
  369. linkEmailImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkEmail")
  370. linkUrlLabel.textColor = KMAppearance.Layout.h0Color()
  371. linkPageLabel.textColor = KMAppearance.Layout.h1Color()
  372. linkEmailLabel.textColor = KMAppearance.Layout.h1Color()
  373. errorLabel.isHidden = true
  374. } else if box.isEqual(linkEmailBox) {
  375. linkEmailBox.fillColor = KMAppearance.Status.selColor()
  376. linkPageBox.fillColor = NSColor.clear
  377. linkUrlBox.fillColor = NSColor.clear
  378. linkPageImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkPage")
  379. linkUrlImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkUrl")
  380. linkEmailImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkEmailSel")
  381. linkEmailLabel.textColor = KMAppearance.Layout.h0Color()
  382. linkPageLabel.textColor = KMAppearance.Layout.h1Color()
  383. linkUrlLabel.textColor = KMAppearance.Layout.h1Color()
  384. errorLabel.isHidden = true
  385. }
  386. }
  387. // MARK: Public Method
  388. // MARK: Action
  389. @IBAction func goButtonAction(_ sender: Any) {
  390. if linkType == .page {
  391. let pageFloat = Float(inputPageTextField.stringValue)
  392. if pageFloat! < 1 || UInt(pageFloat!) > self.pdfDocument!.pageCount {
  393. return
  394. }
  395. } else if linkType == .url {
  396. if urlRecord!.count <= 0 {
  397. return
  398. }
  399. } else if linkType == .email {
  400. if emailRecord!.count <= 0 {
  401. return
  402. }
  403. }
  404. goButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  405. Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timered(_:)), userInfo: nil, repeats: false)
  406. switch linkType {
  407. case .page:
  408. guard let activeAnnotation = pdfview?.activeAnnotation as? CPDFLinkAnnotation else { return }
  409. activeAnnotation.setURL(nil)
  410. let dexPage: Int
  411. if isGo {
  412. isGo = false
  413. dexPage = Int(inputPageTextField.stringValue) ?? 0
  414. goButton.title = NSLocalizedString("Go Back", comment: "Tool tip message")
  415. } else {
  416. isGo = true
  417. dexPage = Int(startPage!) ?? 0
  418. goButton.title = NSLocalizedString("Go", tableName: "MainMenu", comment: "")
  419. }
  420. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  421. guard dexPage >= 1 && dexPage <= pdfDocument!.pageCount else {
  422. let alert = NSAlert()
  423. alert.alertStyle = .critical
  424. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  425. alert.runModal()
  426. return
  427. }
  428. let goPage = Int(inputPageTextField.stringValue) ?? 0
  429. if let page = pdfDocument!.page(at: UInt(goPage - 1)) {
  430. let bounds = page.bounds(for: .cropBox)
  431. let destination = CPDFDestination(page: page, at: NSPoint(x: 0, y: bounds.size.height))
  432. (pdfview?.activeAnnotation as! CPDFLinkAnnotation as CPDFLinkAnnotation).setDestination(destination)
  433. pdfview!.needsDisplay = true
  434. }
  435. if let page = pdfDocument!.page(at: UInt(dexPage - 1)) {
  436. let bounds = page.bounds(for: .cropBox)
  437. let dest = CPDFDestination(page: page, at: NSPoint(x: 0, y: bounds.size.height))
  438. pdfview!.go(to: dest)
  439. }
  440. case .url:
  441. guard let activeAnnotation = pdfview?.activeAnnotation as? CPDFLinkAnnotation else { return }
  442. activeAnnotation.setDestination(nil)
  443. let linkUrlPath = judgeWebURL(inputUrlTextField.stringValue)
  444. activeAnnotation.setURL(linkUrlPath)
  445. pdfview!.needsDisplay = true
  446. if let url = (pdfview?.activeAnnotation as! CPDFLinkAnnotation).url() {
  447. NSWorkspace.shared.open(URL(string: url)!)
  448. }
  449. case .email:
  450. guard let activeAnnotation = pdfview?.activeAnnotation as? CPDFLinkAnnotation else { return }
  451. activeAnnotation.setDestination(nil)
  452. if !isValidateEmail(inputUrlTextField.stringValue) {
  453. let alert = NSAlert()
  454. alert.alertStyle = .critical
  455. alert.messageText = NSLocalizedString("Invalid Email. Please try again.", comment: "")
  456. alert.runModal()
  457. return
  458. }
  459. let linkUrlPath = judgeEmailURL(inputUrlTextField.stringValue)
  460. activeAnnotation.setURL(linkUrlPath)
  461. pdfview!.needsDisplay = true
  462. if let url = (pdfview?.activeAnnotation as! CPDFLinkAnnotation).url() {
  463. NSWorkspace.shared.open(URL(string: url)!)
  464. }
  465. default:
  466. break
  467. }
  468. }
  469. @objc func timered(_ timer: Timer) {
  470. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  471. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  472. }
  473. // MARK: NSTextFieldDelegate Methods
  474. @objc func controlTextDidChange(_ notification: Notification) {
  475. guard let textField = notification.object as? NSTextField else { return }
  476. if mouseDownBox!.isEqual(linkPageBox) {
  477. pageRecord = textField.stringValue
  478. } else if mouseDownBox!.isEqual(linkUrlBox) {
  479. urlRecord = textField.stringValue
  480. } else if mouseDownBox!.isEqual(linkEmailBox) {
  481. emailRecord = textField.stringValue
  482. }
  483. if linkType == KMAnnotationLinkType.page {
  484. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setURL(nil)
  485. let dexPage = Int(inputPageTextField.stringValue) ?? 0
  486. if (dexPage < 1 || dexPage > pdfDocument!.pageCount) && pageRecord!.count > 0 {
  487. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  488. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  489. errorLabel.isHidden = false
  490. return
  491. } else if pageRecord!.count > 0 {
  492. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  493. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  494. errorLabel.isHidden = true
  495. } else {
  496. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  497. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  498. }
  499. let pageIndex = UInt(max(dexPage-1, 0))
  500. let page = pdfDocument!.page(at: pageIndex)
  501. let bounds = page?.bounds(for: .cropBox) ?? .zero
  502. let destination = CPDFDestination(page: page, at: NSMakePoint(0, bounds.size.height))
  503. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setDestination(destination)
  504. pdfview?.setNeedsDisplay(pdfview!.visibleRect)
  505. } else if linkType == KMAnnotationLinkType.url {
  506. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setDestination(nil)
  507. let linkUrlPath: String = judgeWebURL(urlRecord!)
  508. if linkUrlPath == "" {
  509. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setURL(nil)
  510. } else {
  511. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setURL(linkUrlPath)
  512. }
  513. if urlRecord!.count > 0 {
  514. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  515. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  516. } else {
  517. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  518. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  519. }
  520. pdfview!.setNeedsDisplay(pdfview!.visibleRect)
  521. } else if linkType == KMAnnotationLinkType.email {
  522. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setDestination(nil)
  523. let linkUrlPath: String = judgeEmailURL(emailRecord!)
  524. if linkUrlPath == "" {
  525. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setURL(nil)
  526. } else {
  527. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setURL(linkUrlPath)
  528. }
  529. if emailRecord!.count > 0 {
  530. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  531. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  532. } else {
  533. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  534. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  535. }
  536. pdfview?.setNeedsDisplay(pdfview!.visibleRect)
  537. }
  538. }
  539. }