KMRedactMutilPageFlagContentView.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // KMRedactMutilPageFlagContentView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/1/19.
  6. //
  7. import Cocoa
  8. class KMRedactMutilPageFlagContentView: KMRedactContentBaseView {
  9. @IBOutlet weak var allPageRadio: NSButton!
  10. @IBOutlet weak var oddPageRadio: NSButton!
  11. @IBOutlet weak var evenPageRadio: NSButton!
  12. @IBOutlet weak var customPageRadio: NSButton!
  13. @IBOutlet weak var inputView: NSView!
  14. @IBOutlet weak var inputTextField: NSTextField!
  15. @IBOutlet weak var totalPageNumberLabel: NSTextField!
  16. var pageCount: Int {
  17. get {
  18. -1
  19. }
  20. set {
  21. self.totalPageNumberLabel.stringValue = "/\(newValue)"
  22. }
  23. }
  24. override func awakeFromNib() {
  25. super.awakeFromNib()
  26. self.allPageRadio.title = NSLocalizedString("All Pages", comment: "")
  27. self.oddPageRadio.title = NSLocalizedString("Odd Pages", comment: "")
  28. self.evenPageRadio.title = NSLocalizedString("Even Pages", comment: "")
  29. self.customPageRadio.title = NSLocalizedString("Customized", comment: "")
  30. for radio in [self.allPageRadio, self.oddPageRadio, self.evenPageRadio, self.customPageRadio] {
  31. radio?.target = self
  32. radio?.action = #selector(radioAction)
  33. }
  34. selectRadio(self.allPageRadio)
  35. self.inputView.wantsLayer = true
  36. self.inputView.layer?.backgroundColor = NSColor(white: 238.0/255.0, alpha: 1.0).cgColor
  37. self.inputTextField.placeholderString = NSLocalizedString("e.g. 1,3-5,10", comment: "")
  38. self.inputTextField.focusRingType = .none
  39. self.inputTextField.delegate = self
  40. }
  41. @objc private func radioAction(sender: NSButton) {
  42. selectRadio(sender)
  43. guard let callback = self.itemClick else {
  44. return
  45. }
  46. var index: Int = 1
  47. for radio in [self.allPageRadio, self.oddPageRadio, self.evenPageRadio, self.customPageRadio] {
  48. if ((radio?.isEqual(to: sender))!) {
  49. break
  50. }
  51. index += 1
  52. }
  53. callback(index, nil)
  54. }
  55. private func selectRadio(_ selectedRadio: NSButton) {
  56. for radio in [self.allPageRadio, self.oddPageRadio, self.evenPageRadio, self.customPageRadio] {
  57. if ((radio?.isEqual(to: selectedRadio))!) {
  58. radio?.state = .on
  59. } else {
  60. radio?.state = .off
  61. }
  62. }
  63. if (customPageRadio.state == .on) {
  64. self.inputTextField.isEditable = true
  65. } else {
  66. self.inputTextField.isEditable = false
  67. }
  68. }
  69. }
  70. extension KMRedactMutilPageFlagContentView: NSTextFieldDelegate {
  71. func controlTextDidChange(_ obj: Notification) {
  72. if ((self.inputTextField.isEqual(to: obj.object)) == false) {
  73. return
  74. }
  75. guard let callback = self.itemClick else {
  76. return
  77. }
  78. callback(5, self.inputTextField.stringValue)
  79. }
  80. }