KMAnnotationSelectLinkViewController.swift 27 KB

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