KMAnnotationSelectLinkViewController.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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", 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. let cell = self.inputUrlTextField.cell as? NSTextFieldCell
  159. cell?.allowedInputSourceLocales = [NSAllRomanInputSourcesLocaleIdentifier]
  160. if let annotationURL = annotation?.url() {
  161. var urlString = annotationURL.fileURL.absoluteString
  162. if annotationURL.hasPrefix("mailto:") {
  163. linkType = .email
  164. mouseDownBox = linkEmailBox
  165. } else {
  166. linkType = .url
  167. mouseDownBox = linkUrlBox
  168. }
  169. } else {
  170. linkType = .page
  171. mouseDownBox = linkPageBox
  172. }
  173. }
  174. override func viewDidAppear() {
  175. super.viewDidAppear()
  176. inputPageTextField.delegate = self
  177. inputUrlTextField.delegate = self
  178. if let url = annotation?.url {
  179. inputUrlTextField.becomeFirstResponder()
  180. } else {
  181. inputPageTextField.becomeFirstResponder()
  182. }
  183. }
  184. // MARK: Get & Set
  185. var linkType: KMAnnotationLinkType {
  186. set {
  187. _linkType = newValue
  188. contentBoxLayoutConstraint.constant = 46.0
  189. switch _linkType {
  190. case .page:
  191. contentBoxLayoutConstraint.constant = 24.0
  192. createLinkPageView()
  193. contentBox.contentView = pageView
  194. inputPageTextField.becomeFirstResponder()
  195. case .url:
  196. createLinkURLView()
  197. contentBox.contentView = urlView
  198. inputUrlTextField.becomeFirstResponder()
  199. case .email:
  200. createLinkEmailView()
  201. contentBox.contentView = urlView
  202. inputUrlTextField.becomeFirstResponder()
  203. default:
  204. break
  205. }
  206. }
  207. get {
  208. return _linkType
  209. }
  210. }
  211. weak var pdfview: CPDFListView? {
  212. set {
  213. _pdfview = newValue
  214. // startPage = _pdfview?.currentPage().label
  215. self.startPage = "\((self._pdfview?.currentPage().pageIndex() ?? 1)+1)"
  216. }
  217. get {
  218. return _pdfview
  219. }
  220. }
  221. var annotations: [CPDFAnnotation]? {
  222. set {
  223. _annotations = newValue
  224. annotation = _annotations?.first as? CPDFLinkAnnotation
  225. }
  226. get {
  227. return _annotations
  228. }
  229. }
  230. // MARK: Private
  231. func getURLViewHeightLayoutConstraint() -> CGFloat {
  232. return urlLabel.frame.height + 4 + inputUrlTextField.frame.height
  233. }
  234. func getPageViewHeightLayoutConstraint() -> CGFloat {
  235. return inputPageTextField.frame.height
  236. }
  237. func createLinkPageView() {
  238. annotationLinkSelectBox(linkPageBox)
  239. inputPageTextField.formatter = NumberFormatter()
  240. if pdfDocument!.pageCount > 1 {
  241. inputPageTextField.placeholderString = "1-\(pdfDocument!.pageCount)"
  242. } else {
  243. inputPageTextField.placeholderString = "1"
  244. }
  245. allPageLabel.stringValue = "/\(pdfDocument!.pageCount)"
  246. if let destination = annotation?.destination() {
  247. if let page = destination.page() {
  248. let pageIndex = page.document?.index(for: page) ?? 0
  249. inputPageTextField.stringValue = "\(pageIndex + 1)"
  250. pageRecord = "\(pageIndex + 1)"
  251. }
  252. }
  253. if inputPageTextField.stringValue.isEmpty {
  254. inputPageTextField.stringValue = pageRecord!
  255. }
  256. let pageFloat = Float(inputPageTextField.stringValue) ?? 0
  257. if pageFloat < 1 || UInt(pageFloat) > self.pdfDocument!.pageCount {
  258. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  259. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  260. } else {
  261. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  262. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  263. }
  264. }
  265. func createLinkURLView() {
  266. annotationLinkSelectBox(linkUrlBox)
  267. urlLabel.stringValue = NSLocalizedString("URL:", comment: "")
  268. urlLabel.textColor = KMAppearance.Layout.h1Color()
  269. inputUrlTextField.placeholderString = "https://www.pdfreaderpro.com"
  270. if let annotationURL = annotation?.url() {
  271. var urlString = annotationURL.fileURL.absoluteString
  272. if !annotationURL.hasPrefix("mailto:") {
  273. if urlString.hasPrefix("http://") {
  274. urlString = String(urlString.suffix(from: urlString.index(urlString.startIndex, offsetBy: 7)))
  275. } else if urlString.hasPrefix("https://") {
  276. urlString = String(urlString.suffix(from: urlString.index(urlString.startIndex, offsetBy: 8)))
  277. }
  278. inputUrlTextField.stringValue = annotationURL//urlString
  279. urlRecord = urlString
  280. }else {
  281. inputUrlTextField.stringValue = ""
  282. }
  283. }else {
  284. inputUrlTextField.stringValue = ""
  285. }
  286. if urlRecord!.isEmpty {
  287. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  288. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  289. } else {
  290. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  291. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  292. }
  293. }
  294. func createLinkEmailView() {
  295. annotationLinkSelectBox(linkEmailBox)
  296. urlLabel.stringValue = NSLocalizedString("Email:", comment: "")
  297. urlLabel.textColor = KMAppearance.Layout.h1Color()
  298. inputUrlTextField.placeholderString = "support@pdfreaderpro.com"
  299. if let annotationURL = annotation?.url() {
  300. // if let urlString = annotation?.url().fileURL.absoluteString, urlString.hasPrefix("mailto:") {
  301. // let trimmedString = String(urlString.suffix(from: urlString.index(urlString.startIndex, offsetBy: 7)))
  302. // inputUrlTextField.stringValue = trimmedString
  303. // emailRecord = trimmedString
  304. // }
  305. var urlString = annotationURL.fileURL.absoluteString
  306. if annotationURL.hasPrefix("mailto:") {
  307. let trimmedString = String(urlString.suffix(from: urlString.index(urlString.startIndex, offsetBy: 7)))
  308. inputUrlTextField.stringValue = annotationURL
  309. emailRecord = trimmedString
  310. }
  311. }else {
  312. inputUrlTextField.stringValue = ""
  313. }
  314. if emailRecord!.count > 0 {
  315. goButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  316. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  317. } else {
  318. goButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  319. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  320. }
  321. }
  322. func isValidateEmail(_ email: String) -> Bool {
  323. let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
  324. let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegex)
  325. return emailTest.evaluate(with: email)
  326. }
  327. func judgeWebURL(_ urlString: String) -> String {
  328. var modifiedURLString = urlString
  329. if !modifiedURLString.hasPrefix("http://") && !modifiedURLString.hasPrefix("https://") {
  330. modifiedURLString = "https://" + modifiedURLString
  331. }
  332. if modifiedURLString == "https://" {
  333. modifiedURLString = ""
  334. }
  335. return modifiedURLString
  336. }
  337. func judgeEmailURL(_ urlString: String) -> String {
  338. var modifiedURLString = urlString
  339. if !modifiedURLString.hasPrefix("mailto:") {
  340. modifiedURLString = "mailto:" + modifiedURLString
  341. }
  342. if modifiedURLString == "mailto:" {
  343. modifiedURLString = ""
  344. }
  345. return modifiedURLString
  346. }
  347. func annotationLinkSelectBox(_ box: KMBox) {
  348. if box.isEqual(linkPageBox) {
  349. linkPageBox.fillColor = KMAppearance.Status.selColor()
  350. linkUrlBox.fillColor = NSColor.clear
  351. linkEmailBox.fillColor = NSColor.clear
  352. linkPageImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkPageSel")
  353. linkUrlImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkUrl")
  354. linkEmailImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkEmail")
  355. linkPageLabel.textColor = KMAppearance.Layout.h0Color()
  356. linkUrlLabel.textColor = KMAppearance.Layout.h1Color()
  357. linkEmailLabel.textColor = KMAppearance.Layout.h1Color()
  358. if let dexPage = Int(pageRecord!), pageRecord!.count >= 1 {
  359. if dexPage < 1 || dexPage > self.pdfDocument!.pageCount {
  360. errorLabel.isHidden = false
  361. } else {
  362. errorLabel.isHidden = true
  363. }
  364. }
  365. } else if box.isEqual(linkUrlBox) {
  366. linkUrlBox.fillColor = KMAppearance.Status.selColor()
  367. linkPageBox.fillColor = NSColor.clear
  368. linkEmailBox.fillColor = NSColor.clear
  369. linkPageImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkPage")
  370. linkUrlImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkUrlSel")
  371. linkEmailImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkEmail")
  372. linkUrlLabel.textColor = KMAppearance.Layout.h0Color()
  373. linkPageLabel.textColor = KMAppearance.Layout.h1Color()
  374. linkEmailLabel.textColor = KMAppearance.Layout.h1Color()
  375. errorLabel.isHidden = true
  376. } else if box.isEqual(linkEmailBox) {
  377. linkEmailBox.fillColor = KMAppearance.Status.selColor()
  378. linkPageBox.fillColor = NSColor.clear
  379. linkUrlBox.fillColor = NSColor.clear
  380. linkPageImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkPage")
  381. linkUrlImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkUrl")
  382. linkEmailImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkEmailSel")
  383. linkEmailLabel.textColor = KMAppearance.Layout.h0Color()
  384. linkPageLabel.textColor = KMAppearance.Layout.h1Color()
  385. linkUrlLabel.textColor = KMAppearance.Layout.h1Color()
  386. errorLabel.isHidden = true
  387. }
  388. }
  389. // MARK: Public Method
  390. // MARK: Action
  391. @IBAction func goButtonAction(_ sender: Any) {
  392. if linkType == .page {
  393. guard let pageFloat = Float(inputPageTextField.stringValue) else {
  394. return
  395. }
  396. if pageFloat < 1 || UInt(pageFloat) > self.pdfDocument!.pageCount {
  397. return
  398. }
  399. } else if linkType == .url {
  400. if urlRecord!.count <= 0 {
  401. return
  402. }
  403. } else if linkType == .email {
  404. if emailRecord!.count <= 0 {
  405. return
  406. }
  407. }
  408. goButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  409. Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timered(_:)), userInfo: nil, repeats: false)
  410. switch linkType {
  411. case .page:
  412. guard let activeAnnotation = pdfview?.activeAnnotation as? CPDFLinkAnnotation else { return }
  413. activeAnnotation.setURL(nil)
  414. let dexPage: Int
  415. if isGo {
  416. isGo = false
  417. dexPage = Int(inputPageTextField.stringValue) ?? 0
  418. goButton.title = NSLocalizedString("Go Back", comment: "Tool tip message")
  419. } else {
  420. isGo = true
  421. dexPage = Int(startPage!) ?? 0
  422. goButton.title = NSLocalizedString("Go", comment: "")
  423. }
  424. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  425. guard dexPage >= 1 && dexPage <= pdfDocument!.pageCount else {
  426. let alert = NSAlert()
  427. alert.alertStyle = .critical
  428. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  429. alert.runModal()
  430. return
  431. }
  432. let goPage = Int(inputPageTextField.stringValue) ?? 0
  433. if let page = pdfDocument!.page(at: UInt(goPage - 1)) {
  434. let bounds = page.bounds(for: .cropBox)
  435. let destination = CPDFDestination(page: page, at: NSPoint(x: 0, y: bounds.size.height))
  436. (pdfview?.activeAnnotation as! CPDFLinkAnnotation as CPDFLinkAnnotation).setDestination(destination)
  437. pdfview!.needsDisplay = true
  438. }
  439. if let page = pdfDocument!.page(at: UInt(dexPage - 1)) {
  440. let bounds = page.bounds(for: .cropBox)
  441. let dest = CPDFDestination(page: page, at: NSPoint(x: 0, y: bounds.size.height))
  442. pdfview!.go(to: dest)
  443. }
  444. case .url:
  445. guard let activeAnnotation = pdfview?.activeAnnotation as? CPDFLinkAnnotation else { return }
  446. activeAnnotation.setDestination(nil)
  447. let linkUrlPath = judgeWebURL(inputUrlTextField.stringValue)
  448. activeAnnotation.setURL(linkUrlPath)
  449. pdfview!.needsDisplay = true
  450. if let url = (pdfview?.activeAnnotation as! CPDFLinkAnnotation).url() {
  451. if let data = URL(string: url) {
  452. NSWorkspace.shared.open(data)
  453. }
  454. }
  455. case .email:
  456. guard let activeAnnotation = pdfview?.activeAnnotation as? CPDFLinkAnnotation else { return }
  457. activeAnnotation.setDestination(nil)
  458. if !isValidateEmail(inputUrlTextField.stringValue) {
  459. let alert = NSAlert()
  460. alert.alertStyle = .critical
  461. alert.messageText = NSLocalizedString("Invalid Email. Please try again.", comment: "")
  462. alert.runModal()
  463. return
  464. }
  465. let linkUrlPath = judgeEmailURL(inputUrlTextField.stringValue)
  466. activeAnnotation.setURL(linkUrlPath)
  467. pdfview!.needsDisplay = true
  468. if let url = (pdfview?.activeAnnotation as! CPDFLinkAnnotation).url() {
  469. NSWorkspace.shared.open(URL(string: url)!)
  470. }
  471. default:
  472. break
  473. }
  474. }
  475. @objc func timered(_ timer: Timer) {
  476. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  477. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  478. }
  479. // MARK: NSTextFieldDelegate Methods
  480. @objc func controlTextDidChange(_ notification: Notification) {
  481. guard let textField = notification.object as? NSTextField else { return }
  482. if mouseDownBox!.isEqual(linkPageBox) {
  483. pageRecord = textField.stringValue
  484. } else if mouseDownBox!.isEqual(linkUrlBox) {
  485. urlRecord = textField.stringValue
  486. } else if mouseDownBox!.isEqual(linkEmailBox) {
  487. emailRecord = textField.stringValue
  488. }
  489. if linkType == KMAnnotationLinkType.page {
  490. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setURL(nil)
  491. let dexPage = Int(inputPageTextField.stringValue) ?? 0
  492. if (dexPage < 1 || dexPage > pdfDocument!.pageCount) && pageRecord!.count > 0 {
  493. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  494. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  495. errorLabel.isHidden = false
  496. return
  497. } else if pageRecord!.count > 0 {
  498. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  499. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  500. errorLabel.isHidden = true
  501. } else {
  502. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  503. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  504. }
  505. let pageIndex = UInt(max(dexPage-1, 0))
  506. let page = pdfDocument!.page(at: pageIndex)
  507. let bounds = page?.bounds(for: .cropBox) ?? .zero
  508. let destination = CPDFDestination(page: page, at: NSMakePoint(0, bounds.size.height))
  509. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setDestination(destination)
  510. pdfview?.setNeedsDisplay(pdfview!.visibleRect)
  511. } else if linkType == KMAnnotationLinkType.url {
  512. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setDestination(nil)
  513. let linkUrlPath: String = judgeWebURL(urlRecord!)
  514. if linkUrlPath == "" {
  515. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setURL(nil)
  516. } else {
  517. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setURL(linkUrlPath)
  518. }
  519. if urlRecord!.count > 0 {
  520. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  521. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  522. } else {
  523. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  524. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  525. }
  526. pdfview!.setNeedsDisplay(pdfview!.visibleRect)
  527. } else if linkType == KMAnnotationLinkType.email {
  528. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setDestination(nil)
  529. let linkUrlPath: String = judgeEmailURL(emailRecord!)
  530. if linkUrlPath == "" {
  531. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setURL(nil)
  532. } else {
  533. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setURL(linkUrlPath)
  534. }
  535. if emailRecord!.count > 0 {
  536. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  537. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  538. } else {
  539. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  540. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  541. }
  542. pdfview?.setNeedsDisplay(pdfview!.visibleRect)
  543. }
  544. }
  545. }