KMRedactPageRangeContentView.swift 3.2 KB

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