KMOutlineEditViewController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //
  2. // KMOutlineEditViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lxy on 2022/11/9.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMOutlineEditViewController: KMNBaseViewController {
  10. @IBOutlet var outlineNameTextView: NSTextView!
  11. @IBOutlet weak var outlineNameLabel: NSTextField!
  12. @IBOutlet weak var outlineTargetPageIndexTextField: NSTextField!
  13. @IBOutlet weak var pageButton: NSButton!
  14. @IBOutlet weak var totalPageCountLabel: NSTextField!
  15. @IBOutlet weak var outlineURLTextField: NSTextField!
  16. @IBOutlet weak var urlButton: NSButton!
  17. @IBOutlet weak var mailAddressTextField: NSTextField!
  18. @IBOutlet weak var mailButton: NSButton!
  19. @IBOutlet weak var inputScrollView: NSScrollView!
  20. @IBOutlet weak var pageBox: NSBox!
  21. @IBOutlet weak var webBox: NSBox!
  22. @IBOutlet weak var emailBox: NSBox!
  23. @IBOutlet weak var bottomBox: NSBox!
  24. @IBOutlet weak var leftButtonBox: NSBox!
  25. @IBOutlet weak var rightButtonBox: NSBox!
  26. weak var outline : CPDFOutline?
  27. weak var pdfView : CPDFView?
  28. var originalURLString : String = ""
  29. weak var originalDestination : CPDFDestination?
  30. var originalPageIndex = -1
  31. var originalLabel : String = ""
  32. var currentPageIndex : Int = 0
  33. var pageCount = 0
  34. private lazy var inputView_: ComponentTextarea = {
  35. let view = ComponentTextarea()
  36. view.properties = ComponentTextareaProperty(size: .m, state: .pressed, isError: false, placeholderString: "", totalCount: -1, text: "", isDisabled: false)
  37. return view
  38. }()
  39. private lazy var pageRadio_: ComponentRadio = {
  40. let view = ComponentRadio()
  41. view.properties = ComponentCheckBoxProperty(size: .s, state: .normal, isDisabled: false, showhelp: false, text: KMLocalizedString("Page:", comment: ""), checkboxType: .normal)
  42. return view
  43. }()
  44. private lazy var pageInput_: ComponentInputAddon = {
  45. let view = ComponentInputAddon()
  46. view.properties = ComponentInputAddonProperty(size: .s, state: .normal, isDisabled: false, addOnBefore: false, onlyRead: true, addonType: .textWithColor, iconImage: nil, text: KMLocalizedString("11"))
  47. return view
  48. }()
  49. private lazy var pageLabel: NSTextField = {
  50. let view = NSTextField(labelWithString: "")
  51. return view
  52. }()
  53. private lazy var webRadio_: ComponentRadio = {
  54. let view = ComponentRadio()
  55. view.properties = ComponentCheckBoxProperty(size: .s, state: .normal, isDisabled: false, showhelp: false, text: KMLocalizedString("Webside:", comment: ""), checkboxType: .normal)
  56. return view
  57. }()
  58. private lazy var webInput_: ComponentInput = {
  59. let view = ComponentInput()
  60. view.properties = ComponentInputProperty(size: .s, placeholder: KMLocalizedString("http://www.pdfreaderpro.com"), text: "")
  61. return view
  62. }()
  63. private lazy var emailRadio_: ComponentRadio = {
  64. let view = ComponentRadio()
  65. view.properties = ComponentCheckBoxProperty(size: .s, state: .normal, isDisabled: false, showhelp: false, text: KMLocalizedString("Email:", comment: ""), checkboxType: .normal)
  66. return view
  67. }()
  68. private lazy var emailInput_: ComponentInput = {
  69. let view = ComponentInput()
  70. view.properties = ComponentInputProperty(size: .s, placeholder: KMLocalizedString("support@pdfreaderpro.com"), text: "")
  71. return view
  72. }()
  73. private lazy var cancelButton_: ComponentButton = {
  74. let view = ComponentButton()
  75. view.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, state: .normal, isDisable: false, onlyIcon: false, buttonText: KMLocalizedString("Cancel"), keepPressState: false)
  76. return view
  77. }()
  78. private lazy var okButton_: ComponentButton = {
  79. let view = ComponentButton()
  80. view.properties = ComponentButtonProperty(type: .primary, size: .s, state: .normal, isDisable: false, onlyIcon: false, buttonText: KMLocalizedString("Apply"), keepPressState: false)
  81. return view
  82. }()
  83. convenience init(outline: CPDFOutline?, document: CPDFView?) {
  84. self.init()
  85. self.outline = outline
  86. self.pdfView = document
  87. }
  88. override func loadView() {
  89. super.loadView()
  90. inputView_.frame = inputScrollView.contentView.bounds
  91. inputView_.autoresizingMask = [.width, .height]
  92. inputScrollView.documentView = inputView_
  93. pageButton.isHidden = true
  94. totalPageCountLabel.isHidden = true
  95. pageBox.borderWidth = 0
  96. pageBox.contentView?.addSubview(pageRadio_)
  97. pageBox.contentView?.addSubview(pageInput_)
  98. pageBox.contentView?.addSubview(pageLabel)
  99. pageRadio_.km_add_centerY_constraint()
  100. pageRadio_.km_add_leading_constraint()
  101. pageRadio_.km_add_width_constraint(constant: pageRadio_.properties.propertyInfo.viewWidth+8)
  102. pageRadio_.km_add_height_constraint(constant: 20)
  103. pageInput_.km_add_leading_constraint(equalTo: pageRadio_, attribute: .trailing, constant: 0)
  104. pageInput_.km_add_centerY_constraint()
  105. pageInput_.km_add_height_constraint(constant: 32)
  106. pageInput_.km_add_width_constraint(constant: 165)
  107. pageLabel.km_add_leading_constraint(equalTo: pageInput_, attribute: .trailing, constant: 8)
  108. pageLabel.km_add_centerY_constraint()
  109. pageLabel.stringValue = "/\(pageCount)"
  110. pageInput_.properties.text = "11"
  111. pageInput_.reloadData()
  112. urlButton.isHidden = true
  113. outlineURLTextField.isHidden = true
  114. webBox.borderWidth = 0
  115. webBox.contentView?.addSubview(webRadio_)
  116. webBox.contentView?.addSubview(webInput_)
  117. webRadio_.km_add_centerY_constraint()
  118. webRadio_.km_add_leading_constraint()
  119. webRadio_.km_add_width_constraint(constant: webRadio_.properties.propertyInfo.viewWidth+8)
  120. webRadio_.km_add_height_constraint(constant: 20)
  121. webInput_.km_add_leading_constraint(equalTo: webRadio_, attribute: .trailing, constant: 0)
  122. webInput_.km_add_centerY_constraint()
  123. webInput_.km_add_height_constraint(constant: 32)
  124. webInput_.km_add_trailing_constraint()
  125. mailButton.isHidden = true
  126. mailAddressTextField.isHidden = true
  127. emailBox.borderWidth = 0
  128. emailBox.contentView?.addSubview(emailRadio_)
  129. emailBox.contentView?.addSubview(emailInput_)
  130. emailRadio_.km_add_centerY_constraint()
  131. emailRadio_.km_add_leading_constraint()
  132. emailRadio_.km_add_width_constraint(constant: emailRadio_.properties.propertyInfo.viewWidth+8)
  133. emailRadio_.km_add_height_constraint(constant: 20)
  134. emailInput_.km_add_leading_constraint(equalTo: emailRadio_, attribute: .trailing, constant: 0)
  135. emailInput_.km_add_centerY_constraint()
  136. emailInput_.km_add_height_constraint(constant: 32)
  137. emailInput_.km_add_trailing_constraint()
  138. bottomBox.borderWidth = 0
  139. leftButtonBox.borderWidth = 0
  140. leftButtonBox.contentView = cancelButton_
  141. rightButtonBox.borderWidth = 0
  142. rightButtonBox.contentView = okButton_
  143. outlineNameLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  144. pageLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  145. self.localizedLanguage()
  146. self.setONButton(button: self.pageButton)
  147. self.enableTextField(textField: self.outlineTargetPageIndexTextField)
  148. self.setDelegateAndFormatter()
  149. if let action = self.outline?.action {
  150. self.adjustUIWithAction(action: action)
  151. }
  152. }
  153. override func updateUILanguage() {
  154. super.updateUILanguage()
  155. KMMainThreadExecute {
  156. self.outlineNameLabel.stringValue = KMLocalizedString("Rename")
  157. }
  158. }
  159. override func updateUIThemeColor() {
  160. super.updateUIThemeColor()
  161. view.wantsLayer = true
  162. KMMainThreadExecute {
  163. self.view.layer?.backgroundColor = KMNColorTools.colorBg_popup().cgColor
  164. self.outlineNameLabel.textColor = KMNColorTools.colorText_1()
  165. self.pageLabel.textColor = KMNColorTools.compField_colorTextFilledNor()
  166. }
  167. }
  168. //MARK: - Private Methods
  169. private func localizedLanguage () {
  170. self.pageButton.title = "\(NSLocalizedString("Page", comment: ""))"
  171. self.urlButton.title = "\(NSLocalizedString("URL:", comment: ""))"
  172. self.outlineURLTextField.placeholderString = "\(NSLocalizedString("https://www.pdfreaderpro.com", comment: ""))"
  173. self.mailButton.title = "Email:"
  174. self.mailAddressTextField.placeholderString = "\(NSLocalizedString("support@pdfreaderpro.com", comment: ""))"
  175. self.outlineNameTextView.string = self.outline?.label ?? ""
  176. }
  177. private func adjustUIWithAction(action:CPDFAction!) {
  178. guard let doc = self.pdfView?.document else {
  179. return
  180. }
  181. if action == nil {
  182. self.totalPageCountLabel.stringValue = " \(doc.pageCount)"
  183. self.outlineTargetPageIndexTextField.stringValue = " \(self.currentPageIndex + 1)"
  184. } else {
  185. guard let outL = self.outline else {
  186. return
  187. }
  188. if action.isKind(of: CPDFURLAction.self) {
  189. var newAction : CPDFURLAction = self.outline?.action as! CPDFURLAction
  190. var urlString = newAction.url() ?? ""
  191. self.originalURLString = urlString
  192. if urlString.hasPrefix("mailto:") {
  193. self.setONButton(button: self.mailButton)
  194. self.enableTextField(textField: self.mailAddressTextField)
  195. urlString = String(urlString.suffix(from: "mailto:".endIndex))
  196. self.mailAddressTextField.stringValue = urlString
  197. } else {
  198. self.setONButton(button: self.urlButton)
  199. self.enableTextField(textField: self.outlineURLTextField)
  200. self.outlineURLTextField.stringValue = urlString
  201. }
  202. self.currentPageIndex = Int(doc.index(for: doc.page(at: UInt(self.pdfView!.currentPageIndex))))
  203. } else {
  204. self.setONButton(button: self.pageButton)
  205. self.enableTextField(textField: self.outlineTargetPageIndexTextField)
  206. self.currentPageIndex = Int(doc.index(for: doc.page(at: UInt(outL.destination.pageIndex))))
  207. self.originalDestination = outL.destination
  208. self.originalPageIndex = self.originalDestination?.pageIndex ?? -1
  209. }
  210. self.originalLabel = outL.label ?? ""
  211. self.totalPageCountLabel.stringValue = " /\(doc.pageCount)"
  212. self.outlineTargetPageIndexTextField.stringValue = "\(self.currentPageIndex + 1)"
  213. }
  214. }
  215. private func setDelegateAndFormatter () {
  216. self.outlineTargetPageIndexTextField.delegate = self
  217. self.outlineNameTextView.delegate = self
  218. var formatter = NumberFormatter()
  219. formatter.numberStyle = .none
  220. formatter.maximum = NSNumber(value: self.pdfView?.document?.pageCount ?? 0)
  221. formatter.minimum = NSNumber(value: 1)
  222. self.outlineTargetPageIndexTextField.formatter = formatter
  223. }
  224. private func setONButton(button:NSButton) {
  225. self.pageButton.state = NSControl.StateValue.off
  226. self.urlButton.state = NSControl.StateValue.off
  227. self.mailButton.state = NSControl.StateValue.off
  228. button.state = NSControl.StateValue.on
  229. }
  230. private func enableTextField(textField:NSTextField) {
  231. self.outlineTargetPageIndexTextField.isEditable = false
  232. self.outlineTargetPageIndexTextField.isSelectable = false
  233. self.outlineURLTextField.isEditable = false
  234. self.outlineURLTextField.isSelectable = false
  235. self.mailAddressTextField.isEditable = false
  236. self.mailAddressTextField.isSelectable = false
  237. self.outlineTargetPageIndexTextField.textColor = KMAppearance.Layout.h0Color()
  238. self.outlineURLTextField.textColor = KMAppearance.Layout.h0Color()
  239. self.mailAddressTextField.textColor = KMAppearance.Layout.h0Color()
  240. textField.isEditable = true
  241. textField.isSelectable = true
  242. textField.textColor = KMAppearance.Layout.h0Color()
  243. }
  244. //MARK: Button Action
  245. @IBAction func buttonClicked_GotoAction(_ sender: Any) {
  246. self.setONButton(button: self.pageButton)
  247. self.enableTextField(textField: self.outlineTargetPageIndexTextField)
  248. self.view.window?.makeFirstResponder(self.outlineTargetPageIndexTextField)
  249. }
  250. @IBAction func buttonClicked_URLAction(_ sender: Any) {
  251. self.setONButton(button: self.urlButton)
  252. self.enableTextField(textField: self.outlineURLTextField)
  253. self.view.window?.makeFirstResponder(self.outlineURLTextField)
  254. if (self.outlineTargetPageIndexTextField.stringValue == "0" || self.outlineTargetPageIndexTextField.stringValue.lengthOfBytes(using: String.Encoding(rawValue: String.Encoding.utf16.rawValue)) < 1){
  255. self.outlineTargetPageIndexTextField.stringValue = "\(self.currentPageIndex + 1)"
  256. }
  257. }
  258. @IBAction func buttonClicked_MailAction(_ sender: Any) {
  259. self.setONButton(button: self.mailButton)
  260. self.enableTextField(textField: self.mailAddressTextField)
  261. self.view.window?.makeFirstResponder(self.mailAddressTextField)
  262. if (self.outlineTargetPageIndexTextField.stringValue == "0" || self.outlineTargetPageIndexTextField.stringValue.lengthOfBytes(using: String.Encoding(rawValue: String.Encoding.utf16.rawValue)) < 1){
  263. self.outlineTargetPageIndexTextField.stringValue = "\(self.currentPageIndex + 1)"
  264. }
  265. }
  266. }
  267. extension KMOutlineEditViewController : NSTextFieldDelegate,NSTextViewDelegate {
  268. func controlTextDidChange(_ obj: Notification) {
  269. if self.outlineTargetPageIndexTextField.isEqual(obj.object) {
  270. let number = NSNumber(value: Int(self.outlineTargetPageIndexTextField.stringValue) ?? 0)
  271. let string = self.outlineTargetPageIndexTextField.formatter?.string(for: number)
  272. let s = Int(string ?? "")
  273. if s != 0 {
  274. self.currentPageIndex = (s ?? 0) - 1
  275. }
  276. self.outlineTargetPageIndexTextField.stringValue = string ?? ""
  277. }
  278. }
  279. }