KMBatesPrefixInfoView.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // KMBatesPrefixInfoView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/28.
  6. //
  7. import Cocoa
  8. class KMBatesPrefixInfoView: KMHeaderFooterAdjectiveInfoBaseView {
  9. var preFixBox = NSView()
  10. var preFixTextField = NSTextField()
  11. var sufFixLabel = NSTextField(labelWithString: "")
  12. var sufFixBox = NSView()
  13. var sufFixTextField = NSTextField()
  14. override func initSubviews() {
  15. super.initSubviews()
  16. self.addSubview(self.preFixBox)
  17. self.addSubview(self.preFixTextField)
  18. self.addSubview(self.sufFixLabel)
  19. self.addSubview(self.sufFixBox)
  20. self.addSubview(self.sufFixTextField)
  21. self.titleLabel.stringValue = NSLocalizedString("Prefix:", comment: "")
  22. self.sufFixLabel.stringValue = NSLocalizedString("Suffix:", comment: "")
  23. for box in [self.preFixBox, self.sufFixBox] {
  24. box.wantsLayer = true
  25. box.layer?.borderWidth = 1
  26. box.layer?.borderColor = NSColor.black.cgColor
  27. }
  28. for textField in [self.preFixTextField, self.sufFixTextField] {
  29. textField.focusRingType = .none
  30. textField.backgroundColor = NSColor.clear
  31. textField.cell?.isBordered = false
  32. textField.delegate = self
  33. }
  34. }
  35. override func layout() {
  36. super.layout()
  37. let leftMargin: CGFloat = self.contentInset.left
  38. let contentWidth: CGFloat = NSWidth(self.bounds)-leftMargin-self.contentInset.right
  39. let boxH: CGFloat = 30
  40. let textFieldH: CGFloat = 20
  41. self.preFixBox.frame = NSMakeRect(leftMargin, self.titleLabel.frame.maxY+2, contentWidth, boxH)
  42. self.preFixTextField.frame = NSMakeRect(leftMargin+4, self.preFixBox.frame.minY+5, contentWidth, textFieldH)
  43. self.sufFixLabel.frame = NSMakeRect(leftMargin, self.preFixBox.frame.maxY+12, contentWidth, 18)
  44. self.sufFixBox.frame = NSMakeRect(leftMargin, self.sufFixLabel.frame.maxY+2, contentWidth, boxH)
  45. self.sufFixTextField.frame = NSMakeRect(leftMargin+4, self.sufFixBox.frame.minY+5, contentWidth, textFieldH)
  46. }
  47. override var model: KMHeaderFooterAdjectiveModel {
  48. get {
  49. return super.model
  50. }
  51. set {
  52. super.model = newValue
  53. let myModel: KMHeaderFooterObject = newValue as! KMHeaderFooterObject
  54. self.preFixTextField.stringValue = myModel.batesPrefixString
  55. self.sufFixTextField.stringValue = myModel.batesSuffixString
  56. }
  57. }
  58. }
  59. extension KMBatesPrefixInfoView: NSTextFieldDelegate {
  60. func controlTextDidChange(_ obj: Notification) {
  61. if (self.preFixTextField.isEqual(to:obj.object ) || self.sufFixTextField.isEqual(to: obj.object)) {
  62. guard let callback = self.itemClick else {
  63. return
  64. }
  65. var index: Int = 1
  66. var sender = self.preFixTextField
  67. if (self.sufFixTextField.isEqual(to: obj.object)) {
  68. index = 2
  69. sender = self.sufFixTextField
  70. }
  71. callback(index, sender.stringValue)
  72. }
  73. }
  74. }