KMAnnotationSelectLinkViewController.swift 27 KB

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