KMAnnotationSelectLinkViewController.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. if modifiedURLString.hasPrefix("smb://") {
  338. } else {
  339. modifiedURLString = "https://" + modifiedURLString
  340. }
  341. }
  342. if modifiedURLString == "https://" {
  343. modifiedURLString = ""
  344. }
  345. return modifiedURLString
  346. }
  347. func judgeEmailURL(_ urlString: String) -> String {
  348. var modifiedURLString = urlString
  349. if !modifiedURLString.hasPrefix("mailto:") {
  350. modifiedURLString = "mailto:" + modifiedURLString
  351. }
  352. if modifiedURLString == "mailto:" {
  353. modifiedURLString = ""
  354. }
  355. return modifiedURLString
  356. }
  357. func annotationLinkSelectBox(_ box: KMBox) {
  358. if box.isEqual(linkPageBox) {
  359. linkPageBox.fillColor = KMAppearance.Status.selColor()
  360. linkUrlBox.fillColor = NSColor.clear
  361. linkEmailBox.fillColor = NSColor.clear
  362. linkPageImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkPageSel")
  363. linkUrlImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkUrl")
  364. linkEmailImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkEmail")
  365. linkPageLabel.textColor = KMAppearance.Layout.h0Color()
  366. linkUrlLabel.textColor = KMAppearance.Layout.h1Color()
  367. linkEmailLabel.textColor = KMAppearance.Layout.h1Color()
  368. if let dexPage = Int(pageRecord!), pageRecord!.count >= 1 {
  369. if dexPage < 1 || dexPage > self.pdfDocument!.pageCount {
  370. errorLabel.isHidden = false
  371. } else {
  372. errorLabel.isHidden = true
  373. }
  374. }
  375. } else if box.isEqual(linkUrlBox) {
  376. linkUrlBox.fillColor = KMAppearance.Status.selColor()
  377. linkPageBox.fillColor = NSColor.clear
  378. linkEmailBox.fillColor = NSColor.clear
  379. linkPageImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkPage")
  380. linkUrlImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkUrlSel")
  381. linkEmailImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkEmail")
  382. linkUrlLabel.textColor = KMAppearance.Layout.h0Color()
  383. linkPageLabel.textColor = KMAppearance.Layout.h1Color()
  384. linkEmailLabel.textColor = KMAppearance.Layout.h1Color()
  385. errorLabel.isHidden = true
  386. } else if box.isEqual(linkEmailBox) {
  387. linkEmailBox.fillColor = KMAppearance.Status.selColor()
  388. linkPageBox.fillColor = NSColor.clear
  389. linkUrlBox.fillColor = NSColor.clear
  390. linkPageImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkPage")
  391. linkUrlImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkUrl")
  392. linkEmailImageView.image = NSImage(named: "KMImageNameUXIconPropertybarLinkEmailSel")
  393. linkEmailLabel.textColor = KMAppearance.Layout.h0Color()
  394. linkPageLabel.textColor = KMAppearance.Layout.h1Color()
  395. linkUrlLabel.textColor = KMAppearance.Layout.h1Color()
  396. errorLabel.isHidden = true
  397. }
  398. }
  399. // MARK: Public Method
  400. // MARK: Action
  401. @IBAction func goButtonAction(_ sender: Any) {
  402. if linkType == .page {
  403. guard let pageFloat = Float(inputPageTextField.stringValue) else {
  404. return
  405. }
  406. if pageFloat < 1 || UInt(pageFloat) > self.pdfDocument!.pageCount {
  407. return
  408. }
  409. } else if linkType == .url {
  410. if urlRecord!.count <= 0 {
  411. return
  412. }
  413. } else if linkType == .email {
  414. if emailRecord!.count <= 0 {
  415. return
  416. }
  417. }
  418. goButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  419. Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timered(_:)), userInfo: nil, repeats: false)
  420. switch linkType {
  421. case .page:
  422. guard let activeAnnotation = pdfview?.activeAnnotation as? CPDFLinkAnnotation else { return }
  423. activeAnnotation.setURL(nil)
  424. let dexPage: Int
  425. if isGo {
  426. isGo = false
  427. dexPage = Int(inputPageTextField.stringValue) ?? 0
  428. goButton.title = NSLocalizedString("Go Back", comment: "Tool tip message")
  429. } else {
  430. isGo = true
  431. dexPage = Int(startPage!) ?? 0
  432. goButton.title = NSLocalizedString("Go", comment: "")
  433. }
  434. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  435. guard dexPage >= 1 && dexPage <= pdfDocument!.pageCount else {
  436. let alert = NSAlert()
  437. alert.alertStyle = .critical
  438. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  439. alert.runModal()
  440. return
  441. }
  442. let goPage = Int(inputPageTextField.stringValue) ?? 0
  443. if let page = pdfDocument!.page(at: UInt(goPage - 1)) {
  444. let bounds = page.bounds(for: .cropBox)
  445. let destination = CPDFDestination(page: page, at: NSPoint(x: 0, y: bounds.size.height))
  446. (pdfview?.activeAnnotation as! CPDFLinkAnnotation as CPDFLinkAnnotation).setDestination(destination)
  447. pdfview!.needsDisplay = true
  448. }
  449. if let page = pdfDocument!.page(at: UInt(dexPage - 1)) {
  450. let bounds = page.bounds(for: .cropBox)
  451. let dest = CPDFDestination(page: page, at: NSPoint(x: 0, y: bounds.size.height))
  452. pdfview!.go(to: dest)
  453. }
  454. case .url:
  455. guard let activeAnnotation = pdfview?.activeAnnotation as? CPDFLinkAnnotation else { return }
  456. activeAnnotation.setDestination(nil)
  457. let linkUrlPath = judgeWebURL(inputUrlTextField.stringValue)
  458. activeAnnotation.setURL(linkUrlPath)
  459. pdfview!.needsDisplay = true
  460. if let url = (pdfview?.activeAnnotation as! CPDFLinkAnnotation).url() {
  461. if let data = URL(string: url) {
  462. NSWorkspace.shared.open(data)
  463. }
  464. }
  465. case .email:
  466. guard let activeAnnotation = pdfview?.activeAnnotation as? CPDFLinkAnnotation else { return }
  467. activeAnnotation.setDestination(nil)
  468. if !isValidateEmail(inputUrlTextField.stringValue) {
  469. let alert = NSAlert()
  470. alert.alertStyle = .critical
  471. alert.messageText = NSLocalizedString("Invalid Email. Please try again.", comment: "")
  472. alert.runModal()
  473. return
  474. }
  475. let linkUrlPath = judgeEmailURL(inputUrlTextField.stringValue)
  476. activeAnnotation.setURL(linkUrlPath)
  477. pdfview!.needsDisplay = true
  478. if let url = (pdfview?.activeAnnotation as! CPDFLinkAnnotation).url() {
  479. NSWorkspace.shared.open(URL(string: url)!)
  480. }
  481. default:
  482. break
  483. }
  484. }
  485. @objc func timered(_ timer: Timer) {
  486. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  487. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  488. }
  489. // MARK: NSTextFieldDelegate Methods
  490. @objc func controlTextDidChange(_ notification: Notification) {
  491. guard let textField = notification.object as? NSTextField else { return }
  492. var string = textField.stringValue.replacingOccurrences(of: "\n", with: "")
  493. string = string.replacingOccurrences(of: "\r", with: "")
  494. textField.stringValue = string
  495. if mouseDownBox!.isEqual(linkPageBox) {
  496. pageRecord = string
  497. } else if mouseDownBox!.isEqual(linkUrlBox) {
  498. urlRecord = string
  499. } else if mouseDownBox!.isEqual(linkEmailBox) {
  500. emailRecord = string
  501. }
  502. if linkType == KMAnnotationLinkType.page {
  503. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setURL(nil)
  504. let dexPage = Int(inputPageTextField.stringValue) ?? 0
  505. if (dexPage < 1 || dexPage > pdfDocument!.pageCount) && pageRecord!.count > 0 {
  506. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  507. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  508. errorLabel.isHidden = false
  509. return
  510. } else if pageRecord!.count > 0 {
  511. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  512. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  513. errorLabel.isHidden = true
  514. } else {
  515. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  516. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  517. }
  518. let pageIndex = UInt(max(dexPage-1, 0))
  519. let page = pdfDocument!.page(at: pageIndex)
  520. let bounds = page?.bounds(for: .cropBox) ?? .zero
  521. let destination = CPDFDestination(page: page, at: NSMakePoint(0, bounds.size.height))
  522. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setDestination(destination)
  523. pdfview?.setNeedsDisplay(pdfview!.visibleRect)
  524. } else if linkType == KMAnnotationLinkType.url {
  525. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setDestination(nil)
  526. let linkUrlPath: String = judgeWebURL(urlRecord!)
  527. if linkUrlPath == "" {
  528. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setURL(nil)
  529. } else {
  530. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setURL(linkUrlPath)
  531. }
  532. if urlRecord!.count > 0 {
  533. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  534. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  535. } else {
  536. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  537. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  538. }
  539. pdfview!.setNeedsDisplay(pdfview!.visibleRect)
  540. } else if linkType == KMAnnotationLinkType.email {
  541. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setDestination(nil)
  542. let linkUrlPath: String = judgeEmailURL(emailRecord!)
  543. if linkUrlPath == "" {
  544. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setURL(nil)
  545. } else {
  546. (pdfview?.activeAnnotation as? CPDFLinkAnnotation)?.setURL(linkUrlPath)
  547. }
  548. if emailRecord!.count > 0 {
  549. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  550. goButton.setTitleColor(KMAppearance.Layout.w0Color())
  551. } else {
  552. goButton.layer!.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  553. goButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.4))
  554. }
  555. pdfview?.setNeedsDisplay(pdfview!.visibleRect)
  556. }
  557. }
  558. }